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
|
||||
|
||||
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):
|
||||
"""Load a module
|
||||
|
||||
|
26
importer.py
26
importer.py
@ -39,16 +39,16 @@ class ModuleFinder(Finder):
|
||||
self.prompt = prompt
|
||||
|
||||
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)
|
||||
if path is None:
|
||||
for mpath in self.context.modules_paths:
|
||||
#print ("looking for", fullname, "in", mpath)
|
||||
if (os.path.isfile(mpath + fullname + ".py") or
|
||||
os.path.isfile(mpath + fullname + "/__init__.py")):
|
||||
# print ("looking for", fullname, "in", mpath)
|
||||
if (os.path.isfile(os.path.join(mpath, fullname + ".py")) or
|
||||
os.path.isfile(os.path.join(os.path.join(mpath, fullname), "__init__.py"))):
|
||||
return ModuleLoader(self.context, self.prompt,
|
||||
fullname, mpath)
|
||||
#print ("not found")
|
||||
# print ("not found")
|
||||
return None
|
||||
|
||||
|
||||
@ -63,14 +63,14 @@ class ModuleLoader(SourceLoader):
|
||||
else:
|
||||
self.config = None
|
||||
|
||||
if os.path.isfile(path + fullname + ".py"):
|
||||
self.source_path = path + self.name + ".py"
|
||||
if os.path.isfile(os.path.join(path, fullname + ".py")):
|
||||
self.source_path = os.path.join(path, self.name + ".py")
|
||||
self.package = False
|
||||
self.mpath = path
|
||||
elif os.path.isfile(path + fullname + "/__init__.py"):
|
||||
self.source_path = path + self.name + "/__init__.py"
|
||||
elif os.path.isfile(os.path.join(os.path.join(path, fullname), "__init__.py")):
|
||||
self.source_path = os.path.join(os.path.join(path, self.name), "__init__.py")
|
||||
self.package = True
|
||||
self.mpath = path + self.name + "/"
|
||||
self.mpath = path + self.name + os.sep
|
||||
else:
|
||||
raise ImportError
|
||||
|
||||
@ -146,7 +146,7 @@ class ModuleLoader(SourceLoader):
|
||||
module.logger.debug(*args)
|
||||
|
||||
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.DATAS.save(fpath)
|
||||
|
||||
@ -191,8 +191,8 @@ class ModuleLoader(SourceLoader):
|
||||
module.del_event = del_event
|
||||
|
||||
if not hasattr(module, "NODATA"):
|
||||
module.DATAS = parse_file(self.context.data_path
|
||||
+ module.__name__ + ".xml")
|
||||
module.DATAS = parse_file(os.path.join(self.context.data_path,
|
||||
module.__name__ + ".xml"))
|
||||
module.save = mod_save
|
||||
else:
|
||||
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
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import argparse
|
||||
import imp
|
||||
import logging
|
||||
import os
|
||||
@ -44,8 +45,33 @@ if __name__ == "__main__":
|
||||
fh.setLevel(logging.DEBUG)
|
||||
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
|
||||
context = bot.Bot()
|
||||
context = bot.Bot(modules_paths=modules_paths, data_path=args.data_path)
|
||||
|
||||
# Load the prompt
|
||||
prmpt = prompt.Prompt()
|
||||
@ -53,18 +79,12 @@ if __name__ == "__main__":
|
||||
# Register the hook for futur import
|
||||
sys.meta_path.append(importer.ModuleFinder(context, prmpt))
|
||||
|
||||
# Add modules dir path
|
||||
if os.path.isdir("./modules/"):
|
||||
context.add_modules_path(
|
||||
os.path.realpath(os.path.abspath("./modules/")))
|
||||
|
||||
# Parse command line arguments
|
||||
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)
|
||||
# Load requested configuration files
|
||||
for path in args.files:
|
||||
if os.path.isfile(path):
|
||||
load_file(path, context)
|
||||
else:
|
||||
logger.error("%s is not a readable file", path)
|
||||
|
||||
print ("Nemubot v%s ready, my PID is %i!" % (bot.__version__,
|
||||
os.getpid()))
|
||||
|
Loading…
Reference in New Issue
Block a user