author | Tero Marttila <terom@fixme.fi> |
Sat, 13 Jun 2009 19:21:12 +0300 | |
branch | new-exif |
changeset 103 | 63e89dc2d6f1 |
parent 92 | eb50b4f7812d |
child 114 | 4096f8a7e63c |
permissions | -rw-r--r-- |
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 |
||
92
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
70 |
def execute (self, *args, **kwargs) : |
65 | 71 |
""" |
72 |
Run the command in this context |
|
73 |
""" |
|
74 |
||
75 |
return self.command.func(self, *args, **kwargs) |
|
92
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
76 |
|
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
77 |
def run (self, *args, **kwargs) : |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
78 |
""" |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
79 |
Run the command with error handling |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
80 |
""" |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
81 |
|
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
82 |
try : |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
83 |
# run it |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
84 |
return self.execute(*args, **kwargs) |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
85 |
|
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
86 |
except KeyboardInterrupt : |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
87 |
self.log_error("Interrupted") |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
88 |
|
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
89 |
except : |
eb50b4f7812d
move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
90 |
command_ctx.handle_error() |
65 | 91 |
|
92 |
def log_msg (self, level, msg, *args, **kwargs) : |
|
93 |
""" |
|
94 |
Output a log message with the given level |
|
95 |
||
96 |
XXX: unicode |
|
97 |
""" |
|
98 |
||
99 |
# control level of output |
|
100 |
if level < self.config.log_level : |
|
101 |
return |
|
102 |
||
103 |
# format? |
|
104 |
if args or kwargs : |
|
105 |
if args and not kwargs : |
|
106 |
msg = msg % args |
|
107 |
||
108 |
elif kwargs and not args : |
|
109 |
msg = msg % kwargs |
|
110 |
||
111 |
else : |
|
112 |
raise Exception("log_msg called with both args and kwargs") |
|
113 |
||
114 |
# output |
|
115 |
# XXX: stdout/err? |
|
116 |
print msg |
|
117 |
||
118 |
def log_debug (self, msg, *args, **kwargs) : |
|
76 | 119 |
self.log_msg(logging.DEBUG, msg, *args, **kwargs) |
65 | 120 |
|
121 |
def log_info (self, msg, *args, **kwargs) : |
|
76 | 122 |
self.log_msg(logging.INFO, msg, *args, **kwargs) |
65 | 123 |
|
124 |
def log_warning (self, msg, *args, **kwargs) : |
|
76 | 125 |
self.log_msg(logging.WARNING, msg, *args, **kwargs) |
65 | 126 |
|
127 |
def log_error (self, msg, *args, **kwargs) : |
|
76 | 128 |
self.log_msg(logging.ERROR, msg, *args, **kwargs) |
129 |
||
130 |
def handle_error (self, exc_info=None) : |
|
131 |
""" |
|
132 |
Do something to handle an error that occured |
|
133 |
""" |
|
134 |
||
135 |
if exc_info : |
|
136 |
traceback.print_execption(*exc_info) |
|
137 |
||
138 |
else : |
|
139 |
traceback.print_exc() |
|
65 | 140 |
|
141 |
def command (func) : |
|
142 |
""" |
|
143 |
A function decorator used to define Commands automatically |
|
144 |
""" |
|
145 |
||
146 |
return Command(func.__name__, func, inspect.getdoc(func)) |
|
147 |