argparse: add verbosity level

This commit is contained in:
nemunaire 2014-12-15 21:43:40 +01:00
parent 65aa371fdc
commit 3265006adb
3 changed files with 29 additions and 20 deletions

4
bot.py
View File

@ -43,7 +43,7 @@ class Bot(threading.Thread):
"""Class containing the bot context and ensuring key goals"""
def __init__(self, ip="127.0.0.1", modules_paths=list(),
data_path="./datas/"):
data_path="./datas/", verbosity=0):
"""Initialize the bot context
Keyword arguments:
@ -56,6 +56,8 @@ class Bot(threading.Thread):
logger.info("Initiate nemubot v%s", __version__)
self.verbosity = verbosity
# External IP for accessing this bot
self.ip = ip

View File

@ -179,7 +179,7 @@ class ModuleLoader(SourceLoader):
# Set module common functions and datas
module.REGISTERED_HOOKS = list()
module.REGISTERED_EVENTS = list()
module.DEBUG = False
module.DEBUG = self.context.verbosity > 0
module.DIR = self.mpath
module.print = prnt
module.print_debug = prnt_dbg

View File

@ -29,25 +29,13 @@ from prompt.builtins import load_file
import importer
if __name__ == "__main__":
# Setup loggin interface
logger = logging.getLogger("nemubot")
formatter = logging.Formatter(
'%(asctime)s %(name)s %(levelname)s %(message)s')
ch = logging.StreamHandler()
ch.setFormatter(formatter)
ch.setLevel(logging.INFO)
logger.addHandler(ch)
fh = logging.FileHandler('./nemubot.log')
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action="count",
default=0,
help="Verbosity level")
parser.add_argument("-M", "--modules-path", nargs='*',
default=["./modules/"],
help="Directory to use as modules store")
@ -58,9 +46,27 @@ if __name__ == "__main__":
parser.add_argument('files', metavar='FILE', nargs='*',
help="Configuration files to load")
args = parser.parse_args()
# Setup loggin interface
logger = logging.getLogger("nemubot")
formatter = logging.Formatter(
'%(asctime)s %(name)s %(levelname)s %(message)s')
ch = logging.StreamHandler()
ch.setFormatter(formatter)
if args.verbose > 1:
ch.setLevel(logging.DEBUG)
else:
ch.setLevel(logging.INFO)
logger.addHandler(ch)
fh = logging.FileHandler('./nemubot.log')
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
# Add modules dir paths
modules_paths = list()
for path in args.modules_path:
@ -71,7 +77,8 @@ if __name__ == "__main__":
logger.error("%s is not a directory", path)
# Create bot context
context = bot.Bot(modules_paths=modules_paths, data_path=args.data_path)
context = bot.Bot(modules_paths=modules_paths, data_path=args.data_path,
verbosity=args.verbose)
# Load the prompt
prmpt = prompt.Prompt()