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