__init__.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.exceptions import CommandAbortedError
  11. from tracim.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. # TODO - G.M - 10-04-2018 - [Cleanup][tempExample] - Drop this
  60. class TestTracimCommand(AppContextCommand):
  61. def take_app_action(
  62. self,
  63. parser: argparse.ArgumentParser,
  64. app_context: AppEnvironment
  65. ) -> None:
  66. print('test')
  67. class Extender(argparse.Action):
  68. """
  69. Copied class from http://stackoverflow.com/a/12461237/801924
  70. """
  71. def __call__(self, parser, namespace, values, option_strings=None):
  72. # Need None here incase `argparse.SUPPRESS` was supplied for `dest`
  73. dest = getattr(namespace, self.dest, None)
  74. # print dest,self.default,values,option_strings
  75. if not hasattr(dest, 'extend') or dest == self.default:
  76. dest = []
  77. setattr(namespace, self.dest, dest)
  78. # if default isn't set to None, this method might be called
  79. # with the default as `values` for other arguements which
  80. # share this destination.
  81. parser.set_defaults(**{self.dest: None})
  82. try:
  83. dest.extend(values)
  84. except ValueError:
  85. dest.append(values)