Mercurial > getan
comparison getan.py @ 112:d85b2a25797c
Move GetanController to it's own module
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Mon, 12 Dec 2011 09:21:04 +0100 |
parents | 1165422b5db7 |
children | 9d6df74058d7 |
comparison
equal
deleted
inserted
replaced
111:dcccc2059b05 | 112:d85b2a25797c |
---|---|
18 import getan.config as config | 18 import getan.config as config |
19 from getan.backend import DEFAULT_DATABASE, Backend | 19 from getan.backend import DEFAULT_DATABASE, Backend |
20 from getan.view import GetanView, ProjectList, EntryList | 20 from getan.view import GetanView, ProjectList, EntryList |
21 from getan.utils import format_time | 21 from getan.utils import format_time |
22 from getan.states import PausedProjectsState | 22 from getan.states import PausedProjectsState |
23 from getan.controller import GetanController | |
23 | 24 |
24 logger = logging.getLogger() | 25 logger = logging.getLogger() |
25 | |
26 class GetanController: | |
27 def __init__(self, backend, pv_class, ev_class): | |
28 self.ev_class = ev_class | |
29 self.pv_class = pv_class | |
30 | |
31 self.backend = backend | |
32 projects, entries = self.load_projects() | |
33 self.projects = projects | |
34 self.running = [] | |
35 | |
36 self.project_view = pv_class(self, self.projects) | |
37 self.entries_view = ev_class(entries) | |
38 | |
39 self.view = GetanView(self, self.project_view, self.entries_view) | |
40 self.state = PausedProjectsState(self, self.project_view) | |
41 | |
42 def main(self): | |
43 self.view.run() | |
44 | |
45 def unhandled_keypress(self, key): | |
46 self.state = self.state.keypress(key) | |
47 | |
48 def input_filter(self, input, raw_input): | |
49 if 'window resize' in input: | |
50 self.view.loop.screen_size = None | |
51 self.view.loop.draw_screen() | |
52 else: | |
53 self.state = self.state.keypress(input) | |
54 | |
55 def load_projects(self): | |
56 projects = self.backend.load_projects() | |
57 if projects: | |
58 entries = self.backend.load_entries(projects[0].id) | |
59 else: | |
60 entries = [] | |
61 return (projects, entries) | |
62 | |
63 def update_entries(self, project): | |
64 logger.debug("GetanController: update entries.") | |
65 if project: entries = self.backend.load_entries(project.id) | |
66 else: entries = [] | |
67 self.entries_view.set_rows(entries) | |
68 self.view.update_view() | |
69 | |
70 def move_selected_entries(self, project): | |
71 old_project = None | |
72 entries = [] | |
73 try: | |
74 while self.entries_view.selection: | |
75 node = self.entries_view.selection.pop() | |
76 if node.selected: node.select() | |
77 entries.append(node.item) | |
78 logger.info("GetanController: move entry '%s' (id = %d, "\ | |
79 "project id = %d) to project '%s'" | |
80 % (node.item.desc, node.item.id, | |
81 node.item.project_id, project.desc)) | |
82 | |
83 if not old_project: | |
84 old_project = self.project_by_id(node.item.project_id) | |
85 finally: | |
86 self.backend.move_entries(entries, project.id) | |
87 if not old_project: return | |
88 project.entries = self.backend.load_entries(project.id) | |
89 old_project.entries = self.backend.load_entries(old_project.id) | |
90 self.update_entries(old_project) | |
91 self.project_view.update_all() | |
92 | |
93 def delete_entries(self, entry_nodes): | |
94 if not entry_nodes: | |
95 return | |
96 proj = self.project_by_id(entry_nodes[0].project_id) | |
97 entries = entry_nodes | |
98 self.backend.delete_entries(entries) | |
99 proj.entries = self.backend.load_entries(proj.id) | |
100 self.update_entries(proj) | |
101 self.project_view.update() | |
102 | |
103 def update_project_list(self): | |
104 self.project_view.update() | |
105 self.view.update_view() | |
106 | |
107 def exit(self): | |
108 self.view.exit() | |
109 | |
110 def project_by_key(self, key): | |
111 for proj in self.projects: | |
112 if proj.key == key: | |
113 return proj | |
114 return None | |
115 | |
116 def project_by_id(self, id): | |
117 for proj in self.projects: | |
118 if proj.id == id: | |
119 return proj | |
120 return None | |
121 | |
122 def start_project(self, project): | |
123 if not project: return | |
124 self.running.append(project) | |
125 project.start = datetime.now() | |
126 logger.info("Start project '%s' at %s." | |
127 % (project.desc, format_time(datetime.now()))) | |
128 self.view.set_footer_text(" Running on '%s'" % project.desc, 'running') | |
129 logger.debug('All running projects: %r' % self.running) | |
130 | |
131 def stop_project(self, desc='-no description-', display=True): | |
132 if not self.running: return | |
133 project = self.running.pop() | |
134 if not project: return | |
135 logger.info("Stop project '%s' at %s." | |
136 % (project.desc, format_time(datetime.now()))) | |
137 project.stop = datetime.now() | |
138 self.backend.insert_project_entry(project, datetime.now(), desc) | |
139 if display: | |
140 self.update_entries(project) | |
141 self.update_project_list() | |
142 logger.debug('Still running projects: %r' % self.running) | |
143 | |
144 def add_project(self, key, description): | |
145 if not key or not description: | |
146 return | |
147 self.backend.insert_project(key, description) | |
148 self.update_projects() | |
149 self.update_project_list() | |
150 | |
151 def update_entry(self, entry): | |
152 self.backend.update_entry(entry) | |
153 | |
154 def shutdown(self): | |
155 for project in self.running: | |
156 self.stop_project(display=False) | |
157 | |
158 def update_projects(self): | |
159 projects, entries = self.load_projects() | |
160 self.projects = projects | |
161 self.project_view.load_rows(projects) | |
162 | |
163 | 26 |
164 def main(): | 27 def main(): |
165 | 28 |
166 usage = "usage: %prog [options] [databasefile (default: " + \ | 29 usage = "usage: %prog [options] [databasefile (default: " + \ |
167 DEFAULT_DATABASE + ")]" | 30 DEFAULT_DATABASE + ")]" |