cmd.py (2204B)
1 # standard imports 2 import io 3 import os 4 import sys 5 import argparse 6 import logging 7 import tempfile 8 from base64 import b64decode 9 from email.utils import parsedate_to_datetime 10 import importlib 11 import copy 12 13 # local imports 14 from piknik.cli import Context 15 from piknik.cli.show import subparser as subparser_show 16 from piknik.cli.mod import subparser as subparser_mod 17 from piknik.cli.add import subparser as subparser_add 18 from piknik.cli.comment import subparser as subparser_comment 19 20 logging.basicConfig(level=logging.WARNING) 21 logg = logging.getLogger() 22 23 argp = argparse.ArgumentParser() 24 argp.add_argument('-d', type=str, help='Data directory') 25 argp.add_argument('-f', '--files', dest='f', action='store_true', help='Save attachments to filesystem') 26 argp.add_argument('-o', '--files-dir', dest='files_dir', type=str, help='Directory to output saved files to') 27 argp.add_argument('-v', action='store_true', help='Turn on debug logs') 28 argp.add_argument('-i','--issue-id', type=str, help='Issue id to show') 29 argp.add_argument('--alias', type=str, help='alias string to refer to issue to with cli commands') 30 argp.add_argument('cmd', type=str, nargs='?', choices=['show', 'add', 'mod', 'comment'], help='subcommand to execute') 31 strargs = copy.copy(sys.argv[1:]) 32 33 have_help = False 34 try: 35 strargs.remove('-h') 36 have_help = True 37 except ValueError: 38 pass 39 try: 40 strargs.remove('--help') 41 have_help = True 42 except ValueError: 43 pass 44 arg, unknown = argp.parse_known_args(strargs) 45 46 m = None 47 create = False 48 if arg.cmd == 'show': 49 m = importlib.import_module('piknik.cli.show') 50 elif arg.cmd == 'add': 51 m = importlib.import_module('piknik.cli.add') 52 create = True 53 elif arg.cmd == 'mod': 54 m = importlib.import_module('piknik.cli.mod') 55 elif arg.cmd == 'comment': 56 m = importlib.import_module('piknik.cli.comment') 57 else: 58 if not have_help: 59 #raise ValueError('unknown subcommand') 60 m = importlib.import_module('piknik.cli.show') 61 62 if m != None: 63 argp = m.subparser(argp) 64 65 arg = argp.parse_args(sys.argv[1:]) 66 if arg.v: 67 logg.setLevel(logging.DEBUG) 68 69 m.ctx = Context(arg, m.assembler, create=create) 70 71 72 def main(): 73 global m 74 m.main() 75 76 77 if __name__ == '__main__': 78 main()