__init__.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import argparse
  4. import transaction
  5. from cliff.app import App
  6. from cliff.command import Command
  7. from cliff.commandmanager import CommandManager
  8. from pyramid.paster import bootstrap
  9. from tracim.exceptions import CommandAbortedError
  10. class TracimCLI(App):
  11. def __init__(self):
  12. super(TracimCLI, self).__init__(
  13. description='TracimCli',
  14. version='0.1',
  15. command_manager=CommandManager('tracimcli'),
  16. deferred_help=True,
  17. )
  18. def initialize_app(self, argv):
  19. self.LOG.debug('initialize_app')
  20. def prepare_to_run_command(self, cmd):
  21. self.LOG.debug('prepare_to_run_command %s', cmd.__class__.__name__)
  22. def clean_up(self, cmd, result, err):
  23. self.LOG.debug('clean_up %s', cmd.__class__.__name__)
  24. if err:
  25. self.LOG.debug('got an error: %s', err)
  26. def main(argv=sys.argv[1:]):
  27. myapp = TracimCLI()
  28. return myapp.run(argv)
  29. if __name__ == "__main__":
  30. main()
  31. class AppContextCommand(Command):
  32. """
  33. Command who initialize app context at beginning of take_action method.
  34. """
  35. auto_setup_context = True
  36. def take_action(self, parsed_args):
  37. super(AppContextCommand, self).take_action(parsed_args)
  38. if self.auto_setup_context:
  39. with bootstrap(parsed_args.config_file) as app_context:
  40. with app_context['request'].tm:
  41. self.take_app_action(parsed_args, app_context)
  42. def get_parser(self, prog_name):
  43. parser = super(AppContextCommand, self).get_parser(prog_name)
  44. parser.add_argument(
  45. "-c",
  46. "--config",
  47. help='application config file to read (default: development.ini)',
  48. dest='config_file',
  49. default="development.ini"
  50. )
  51. return parser
  52. # def run(self, parsed_args):
  53. # super().run(parsed_args)
  54. # transaction.commit()
  55. class TestTracimCommand(AppContextCommand):
  56. def take_app_action(self, parser, app_context):
  57. print('test')
  58. class Extender(argparse.Action):
  59. """
  60. Copied class from http://stackoverflow.com/a/12461237/801924
  61. """
  62. def __call__(self, parser, namespace, values, option_strings=None):
  63. # Need None here incase `argparse.SUPPRESS` was supplied for `dest`
  64. dest = getattr(namespace, self.dest, None)
  65. # print dest,self.default,values,option_strings
  66. if not hasattr(dest, 'extend') or dest == self.default:
  67. dest = []
  68. setattr(namespace, self.dest, dest)
  69. # if default isn't set to None, this method might be called
  70. # with the default as `values` for other arguements which
  71. # share this destination.
  72. parser.set_defaults(**{self.dest: None})
  73. try:
  74. dest.extend(values)
  75. except ValueError:
  76. dest.append(values)