65
|
1 |
"""
|
|
2 |
Command implementations
|
|
3 |
"""
|
|
4 |
|
76
|
5 |
import inspect, logging, traceback
|
65
|
6 |
|
|
7 |
class CommandList (object) :
|
|
8 |
"""
|
|
9 |
A list of available Commands
|
|
10 |
|
|
11 |
XXX: not yet used
|
|
12 |
"""
|
|
13 |
|
|
14 |
def __init__ (self, commands) :
|
|
15 |
"""
|
|
16 |
Store with given initial commands
|
|
17 |
"""
|
|
18 |
|
|
19 |
self.list = commands
|
|
20 |
self.dict = dict((cmd.name, cmd) for cmd in commands)
|
|
21 |
|
|
22 |
def lookup (self, name) :
|
|
23 |
"""
|
|
24 |
Lookup a command by name
|
|
25 |
"""
|
|
26 |
|
|
27 |
return self.dict[name]
|
|
28 |
|
|
29 |
class Command (object) :
|
|
30 |
"""
|
|
31 |
A Command is simply a function that can be executed from the command line with some options/arguments
|
|
32 |
"""
|
|
33 |
|
|
34 |
def __init__ (self, name, func, doc=None) :
|
|
35 |
"""
|
|
36 |
Create a new Command
|
|
37 |
|
|
38 |
name - the name of the command
|
|
39 |
func - the callable python function
|
|
40 |
doc - descriptive help text
|
|
41 |
"""
|
|
42 |
|
|
43 |
self.name = name
|
|
44 |
self.func = func
|
|
45 |
self.doc = doc
|
|
46 |
|
76
|
47 |
def setup (self, config, gallery) :
|
65
|
48 |
"""
|
|
49 |
Run the command with the given context
|
|
50 |
"""
|
|
51 |
|
76
|
52 |
return CommandContext(self, config, gallery)
|
65
|
53 |
|
|
54 |
class CommandContext (object) :
|
|
55 |
"""
|
|
56 |
A CommandContext is the context that a Command executes in
|
|
57 |
|
|
58 |
It is bound to a Configuration and a Gallery.
|
|
59 |
"""
|
|
60 |
|
|
61 |
def __init__ (self, command, config, gallery) :
|
|
62 |
"""
|
|
63 |
Create the execution environment
|
|
64 |
"""
|
|
65 |
|
|
66 |
self.command = command
|
|
67 |
self.config = config
|
|
68 |
self.gallery = gallery
|
|
69 |
|
|
70 |
def __call__ (self, *args, **kwargs) :
|
|
71 |
"""
|
|
72 |
Run the command in this context
|
|
73 |
"""
|
|
74 |
|
|
75 |
return self.command.func(self, *args, **kwargs)
|
|
76 |
|
|
77 |
def log_msg (self, level, msg, *args, **kwargs) :
|
|
78 |
"""
|
|
79 |
Output a log message with the given level
|
|
80 |
|
|
81 |
XXX: unicode
|
|
82 |
"""
|
|
83 |
|
|
84 |
# control level of output
|
|
85 |
if level < self.config.log_level :
|
|
86 |
return
|
|
87 |
|
|
88 |
# format?
|
|
89 |
if args or kwargs :
|
|
90 |
if args and not kwargs :
|
|
91 |
msg = msg % args
|
|
92 |
|
|
93 |
elif kwargs and not args :
|
|
94 |
msg = msg % kwargs
|
|
95 |
|
|
96 |
else :
|
|
97 |
raise Exception("log_msg called with both args and kwargs")
|
|
98 |
|
|
99 |
# output
|
|
100 |
# XXX: stdout/err?
|
|
101 |
print msg
|
|
102 |
|
|
103 |
def log_debug (self, msg, *args, **kwargs) :
|
76
|
104 |
self.log_msg(logging.DEBUG, msg, *args, **kwargs)
|
65
|
105 |
|
|
106 |
def log_info (self, msg, *args, **kwargs) :
|
76
|
107 |
self.log_msg(logging.INFO, msg, *args, **kwargs)
|
65
|
108 |
|
|
109 |
def log_warning (self, msg, *args, **kwargs) :
|
76
|
110 |
self.log_msg(logging.WARNING, msg, *args, **kwargs)
|
65
|
111 |
|
|
112 |
def log_error (self, msg, *args, **kwargs) :
|
76
|
113 |
self.log_msg(logging.ERROR, msg, *args, **kwargs)
|
|
114 |
|
|
115 |
def handle_error (self, exc_info=None) :
|
|
116 |
"""
|
|
117 |
Do something to handle an error that occured
|
|
118 |
"""
|
|
119 |
|
|
120 |
if exc_info :
|
|
121 |
traceback.print_execption(*exc_info)
|
|
122 |
|
|
123 |
else :
|
|
124 |
traceback.print_exc()
|
65
|
125 |
|
|
126 |
def command (func) :
|
|
127 |
"""
|
|
128 |
A function decorator used to define Commands automatically
|
|
129 |
"""
|
|
130 |
|
|
131 |
return Command(func.__name__, func, inspect.getdoc(func))
|
|
132 |
|