| 1 |  |  | # -*- coding: utf-8 -*- | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | import sys | 
            
                                                        
            
                                    
            
            
                | 3 |  |  | import argparse | 
            
                                                        
            
                                    
            
            
                | 4 |  |  | import transaction | 
            
                                                        
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 6 |  |  | from cliff.app import App | 
            
                                                        
            
                                    
            
            
                | 7 |  |  | from cliff.command import Command | 
            
                                                        
            
                                    
            
            
                | 8 |  |  | from cliff.commandmanager import CommandManager | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 10 |  |  | from pyramid.paster import bootstrap | 
            
                                                        
            
                                    
            
            
                | 11 |  |  | from pyramid.scripting import AppEnvironment | 
            
                                                        
            
                                    
            
            
                | 12 |  |  | from tracim.exceptions import CommandAbortedError | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 15 |  |  | class TracimCLI(App): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |     def __init__(self) -> None: | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |         super(TracimCLI, self).__init__( | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |             description='TracimCli', | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |             version='0.1', | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |             command_manager=CommandManager('tracimcli'), | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |             deferred_help=True, | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |             ) | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |     def initialize_app(self, argv) -> None: | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |         self.LOG.debug('initialize_app') | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |     def prepare_to_run_command(self, cmd) -> None: | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |         self.LOG.debug('prepare_to_run_command %s', cmd.__class__.__name__) | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |     def clean_up(self, cmd, result, err) -> None: | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         self.LOG.debug('clean_up %s', cmd.__class__.__name__) | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         if err: | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |             self.LOG.debug('got an error: %s', err) | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 36 |  |  | def main(argv=sys.argv[1:]): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |     myapp = TracimCLI() | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |     return myapp.run(argv) | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 41 |  |  | if __name__ == "__main__": | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |     main() | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 45 |  |  | class AppContextCommand(Command): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |     Command who initialize app context at beginning of take_action method. | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |     auto_setup_context = True | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |     def take_action(self, parsed_args: argparse.Namespace) -> None: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         super(AppContextCommand, self).take_action(parsed_args) | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         if self.auto_setup_context: | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |             with bootstrap(parsed_args.config_file) as app_context: | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |                 with app_context['request'].tm: | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |                     self.take_app_action(parsed_args, app_context) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     def get_parser(self, prog_name: str) -> argparse.ArgumentParser: | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         parser = super(AppContextCommand, self).get_parser(prog_name) | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         parser.add_argument( | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             "-c", | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |             "--config", | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             help='application config file to read (default: development.ini)', | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |             dest='config_file', | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |             default="development.ini" | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         ) | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         return parser | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     # def run(self, parsed_args): | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |     #     super().run(parsed_args) | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     #     transaction.commit() | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  | # TODO - G.M - 10-04-2018 - [Cleanup][tempExample] - Drop this | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 76 |  |  | class TestTracimCommand(AppContextCommand): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |     def take_app_action( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |             self, | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |             parser: argparse.ArgumentParser, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |             app_context: AppEnvironment | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |     ) -> None: | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         print('test') | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 |  |  | class Extender(argparse.Action): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     Copied class from http://stackoverflow.com/a/12461237/801924 | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |     def __call__(self, parser, namespace, values, option_strings=None): | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         # Need None here incase `argparse.SUPPRESS` was supplied for `dest` | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         dest = getattr(namespace, self.dest, None) | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         # print dest,self.default,values,option_strings | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         if not hasattr(dest, 'extend') or dest == self.default: | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |             dest = [] | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |             setattr(namespace, self.dest, dest) | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |             # if default isn't set to None, this method might be called | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |             # with the default as `values` for other arguements which | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |             # share this destination. | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |             parser.set_defaults(**{self.dest: None}) | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         try: | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |             dest.extend(values) | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         except ValueError: | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |             dest.append(values) | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                        
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.