__init__.py 3.0KB

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