Use argparse to parse CLI argument
This commit is contained in:
parent
d6ea5736a5
commit
65aa371fdc
13
bot.py
13
bot.py
@ -334,19 +334,6 @@ class Bot(threading.Thread):
|
|||||||
|
|
||||||
# Modules methods
|
# Modules methods
|
||||||
|
|
||||||
def add_modules_path(self, path):
|
|
||||||
"""Add a path to the modules_path array, used by module loader"""
|
|
||||||
# The path must end by / char
|
|
||||||
if path[-1] != "/":
|
|
||||||
path += "/"
|
|
||||||
|
|
||||||
if path not in self.modules_paths:
|
|
||||||
self.modules_paths.append(path)
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def import_module(self, name):
|
def import_module(self, name):
|
||||||
"""Load a module
|
"""Load a module
|
||||||
|
|
||||||
|
26
importer.py
26
importer.py
@ -39,16 +39,16 @@ class ModuleFinder(Finder):
|
|||||||
self.prompt = prompt
|
self.prompt = prompt
|
||||||
|
|
||||||
def find_module(self, fullname, path=None):
|
def find_module(self, fullname, path=None):
|
||||||
#print ("looking for", fullname, "in", path)
|
# print ("looking for", fullname, "in", path)
|
||||||
# Search only for new nemubot modules (packages init)
|
# Search only for new nemubot modules (packages init)
|
||||||
if path is None:
|
if path is None:
|
||||||
for mpath in self.context.modules_paths:
|
for mpath in self.context.modules_paths:
|
||||||
#print ("looking for", fullname, "in", mpath)
|
# print ("looking for", fullname, "in", mpath)
|
||||||
if (os.path.isfile(mpath + fullname + ".py") or
|
if (os.path.isfile(os.path.join(mpath, fullname + ".py")) or
|
||||||
os.path.isfile(mpath + fullname + "/__init__.py")):
|
os.path.isfile(os.path.join(os.path.join(mpath, fullname), "__init__.py"))):
|
||||||
return ModuleLoader(self.context, self.prompt,
|
return ModuleLoader(self.context, self.prompt,
|
||||||
fullname, mpath)
|
fullname, mpath)
|
||||||
#print ("not found")
|
# print ("not found")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -63,14 +63,14 @@ class ModuleLoader(SourceLoader):
|
|||||||
else:
|
else:
|
||||||
self.config = None
|
self.config = None
|
||||||
|
|
||||||
if os.path.isfile(path + fullname + ".py"):
|
if os.path.isfile(os.path.join(path, fullname + ".py")):
|
||||||
self.source_path = path + self.name + ".py"
|
self.source_path = os.path.join(path, self.name + ".py")
|
||||||
self.package = False
|
self.package = False
|
||||||
self.mpath = path
|
self.mpath = path
|
||||||
elif os.path.isfile(path + fullname + "/__init__.py"):
|
elif os.path.isfile(os.path.join(os.path.join(path, fullname), "__init__.py")):
|
||||||
self.source_path = path + self.name + "/__init__.py"
|
self.source_path = os.path.join(os.path.join(path, self.name), "__init__.py")
|
||||||
self.package = True
|
self.package = True
|
||||||
self.mpath = path + self.name + "/"
|
self.mpath = path + self.name + os.sep
|
||||||
else:
|
else:
|
||||||
raise ImportError
|
raise ImportError
|
||||||
|
|
||||||
@ -146,7 +146,7 @@ class ModuleLoader(SourceLoader):
|
|||||||
module.logger.debug(*args)
|
module.logger.debug(*args)
|
||||||
|
|
||||||
def mod_save():
|
def mod_save():
|
||||||
fpath = self.context.data_path + "/" + module.__name__ + ".xml"
|
fpath = os.path.join(self.context.data_path, module.__name__ + ".xml")
|
||||||
module.print_debug("Saving DATAS to " + fpath)
|
module.print_debug("Saving DATAS to " + fpath)
|
||||||
module.DATAS.save(fpath)
|
module.DATAS.save(fpath)
|
||||||
|
|
||||||
@ -191,8 +191,8 @@ class ModuleLoader(SourceLoader):
|
|||||||
module.del_event = del_event
|
module.del_event = del_event
|
||||||
|
|
||||||
if not hasattr(module, "NODATA"):
|
if not hasattr(module, "NODATA"):
|
||||||
module.DATAS = parse_file(self.context.data_path
|
module.DATAS = parse_file(os.path.join(self.context.data_path,
|
||||||
+ module.__name__ + ".xml")
|
module.__name__ + ".xml"))
|
||||||
module.save = mod_save
|
module.save = mod_save
|
||||||
else:
|
else:
|
||||||
module.DATAS = None
|
module.DATAS = None
|
||||||
|
46
nemubot.py
46
nemubot.py
@ -17,6 +17,7 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import argparse
|
||||||
import imp
|
import imp
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@ -44,8 +45,33 @@ if __name__ == "__main__":
|
|||||||
fh.setLevel(logging.DEBUG)
|
fh.setLevel(logging.DEBUG)
|
||||||
logger.addHandler(fh)
|
logger.addHandler(fh)
|
||||||
|
|
||||||
|
# Parse command line arguments
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
|
parser.add_argument("-M", "--modules-path", nargs='*',
|
||||||
|
default=["./modules/"],
|
||||||
|
help="Directory to use as modules store")
|
||||||
|
|
||||||
|
parser.add_argument("-D", "--data-path", default="./datas/",
|
||||||
|
help="Path to use to save bot data")
|
||||||
|
|
||||||
|
parser.add_argument('files', metavar='FILE', nargs='*',
|
||||||
|
help="Configuration files to load")
|
||||||
|
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Add modules dir paths
|
||||||
|
modules_paths = list()
|
||||||
|
for path in args.modules_path:
|
||||||
|
if os.path.isdir(path):
|
||||||
|
modules_paths.append(
|
||||||
|
os.path.realpath(os.path.abspath(path)))
|
||||||
|
else:
|
||||||
|
logger.error("%s is not a directory", path)
|
||||||
|
|
||||||
# Create bot context
|
# Create bot context
|
||||||
context = bot.Bot()
|
context = bot.Bot(modules_paths=modules_paths, data_path=args.data_path)
|
||||||
|
|
||||||
# Load the prompt
|
# Load the prompt
|
||||||
prmpt = prompt.Prompt()
|
prmpt = prompt.Prompt()
|
||||||
@ -53,18 +79,12 @@ if __name__ == "__main__":
|
|||||||
# Register the hook for futur import
|
# Register the hook for futur import
|
||||||
sys.meta_path.append(importer.ModuleFinder(context, prmpt))
|
sys.meta_path.append(importer.ModuleFinder(context, prmpt))
|
||||||
|
|
||||||
# Add modules dir path
|
# Load requested configuration files
|
||||||
if os.path.isdir("./modules/"):
|
for path in args.files:
|
||||||
context.add_modules_path(
|
if os.path.isfile(path):
|
||||||
os.path.realpath(os.path.abspath("./modules/")))
|
load_file(path, context)
|
||||||
|
else:
|
||||||
# Parse command line arguments
|
logger.error("%s is not a readable file", path)
|
||||||
if len(sys.argv) >= 2:
|
|
||||||
for arg in sys.argv[1:]:
|
|
||||||
if os.path.isdir(arg):
|
|
||||||
context.add_modules_path(arg)
|
|
||||||
else:
|
|
||||||
load_file(arg, context)
|
|
||||||
|
|
||||||
print ("Nemubot v%s ready, my PID is %i!" % (bot.__version__,
|
print ("Nemubot v%s ready, my PID is %i!" % (bot.__version__,
|
||||||
os.getpid()))
|
os.getpid()))
|
||||||
|
Loading…
Reference in New Issue
Block a user