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