Mercurial > getan
annotate getan.py @ 111:dcccc2059b05
Add version info to getan module
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Mon, 12 Dec 2011 09:18:41 +0100 |
parents | 1165422b5db7 |
children | d85b2a25797c |
rev | line source |
---|---|
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
3 # |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
4 # (c) 2010 by Ingo Weinzierl <ingo.weinzierl@intevation.de> |
97
99639833968d
Add me as author in files that I did touch
Björn Ricks <bjoern.ricks@intevation.de>
parents:
90
diff
changeset
|
5 # (c) 2011 by Björn Ricks <bjoern.ricks@intevation.de> |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
6 # |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
7 # A python worklog-alike to log what you have 'getan' (done). |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
8 # |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
9 # This is Free Software licensed under the terms of GPLv3 or later. |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
10 # For details see LICENSE coming with the source of 'getan'. |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
11 # |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
12 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
13 import logging |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
14 import sys |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
15 from datetime import datetime |
67
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
16 from optparse import OptionParser |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
17 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
18 import getan.config as config |
109
1165422b5db7
Only import necessary classes and functions
Björn Ricks <bjoern.ricks@intevation.de>
parents:
107
diff
changeset
|
19 from getan.backend import DEFAULT_DATABASE, Backend |
1165422b5db7
Only import necessary classes and functions
Björn Ricks <bjoern.ricks@intevation.de>
parents:
107
diff
changeset
|
20 from getan.view import GetanView, ProjectList, EntryList |
1165422b5db7
Only import necessary classes and functions
Björn Ricks <bjoern.ricks@intevation.de>
parents:
107
diff
changeset
|
21 from getan.utils import format_time |
1165422b5db7
Only import necessary classes and functions
Björn Ricks <bjoern.ricks@intevation.de>
parents:
107
diff
changeset
|
22 from getan.states import PausedProjectsState |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
23 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
24 logger = logging.getLogger() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
25 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
26 class GetanController: |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
27 def __init__(self, backend, pv_class, ev_class): |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
28 self.ev_class = ev_class |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
29 self.pv_class = pv_class |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
30 |
107
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
31 self.backend = backend |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
32 projects, entries = self.load_projects() |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
33 self.projects = projects |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
34 self.running = [] |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
35 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
36 self.project_view = pv_class(self, self.projects) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
37 self.entries_view = ev_class(entries) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
38 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
39 self.view = GetanView(self, self.project_view, self.entries_view) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
40 self.state = PausedProjectsState(self, self.project_view) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
41 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
42 def main(self): |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
43 self.view.run() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
44 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
45 def unhandled_keypress(self, key): |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
46 self.state = self.state.keypress(key) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
47 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
48 def input_filter(self, input, raw_input): |
26
5e4d2810a739
Added support for resizing the application window.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
25
diff
changeset
|
49 if 'window resize' in input: |
5e4d2810a739
Added support for resizing the application window.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
25
diff
changeset
|
50 self.view.loop.screen_size = None |
5e4d2810a739
Added support for resizing the application window.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
25
diff
changeset
|
51 self.view.loop.draw_screen() |
5e4d2810a739
Added support for resizing the application window.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
25
diff
changeset
|
52 else: |
5e4d2810a739
Added support for resizing the application window.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
25
diff
changeset
|
53 self.state = self.state.keypress(input) |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
54 |
107
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
55 def load_projects(self): |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
56 projects = self.backend.load_projects() |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
57 if projects: |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
58 entries = self.backend.load_entries(projects[0].id) |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
59 else: |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
60 entries = [] |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
61 return (projects, entries) |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
62 |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
63 def update_entries(self, project): |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
64 logger.debug("GetanController: update entries.") |
42
a5439795ef09
More checks againts empty projects.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
41
diff
changeset
|
65 if project: entries = self.backend.load_entries(project.id) |
a5439795ef09
More checks againts empty projects.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
41
diff
changeset
|
66 else: entries = [] |
a5439795ef09
More checks againts empty projects.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
41
diff
changeset
|
67 self.entries_view.set_rows(entries) |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
68 self.view.update_view() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
69 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
70 def move_selected_entries(self, project): |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
71 old_project = None |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
72 entries = [] |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
73 try: |
33
f96a18c10836
Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
31
diff
changeset
|
74 while self.entries_view.selection: |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
75 node = self.entries_view.selection.pop() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
76 if node.selected: node.select() |
25
155b23da504b
Bugfix and improvements in the process to move/delete entries.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
23
diff
changeset
|
77 entries.append(node.item) |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
78 logger.info("GetanController: move entry '%s' (id = %d, "\ |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
79 "project id = %d) to project '%s'" |
25
155b23da504b
Bugfix and improvements in the process to move/delete entries.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
23
diff
changeset
|
80 % (node.item.desc, node.item.id, |
155b23da504b
Bugfix and improvements in the process to move/delete entries.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
23
diff
changeset
|
81 node.item.project_id, project.desc)) |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
82 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
83 if not old_project: |
25
155b23da504b
Bugfix and improvements in the process to move/delete entries.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
23
diff
changeset
|
84 old_project = self.project_by_id(node.item.project_id) |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
85 finally: |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
86 self.backend.move_entries(entries, project.id) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
87 if not old_project: return |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
88 project.entries = self.backend.load_entries(project.id) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
89 old_project.entries = self.backend.load_entries(old_project.id) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
90 self.update_entries(old_project) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
91 self.project_view.update_all() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
92 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
93 def delete_entries(self, entry_nodes): |
99
4912568f9048
Refactor delete_entries to do what it says: delete the entries passed as argument
Björn Ricks <bjoern.ricks@intevation.de>
parents:
97
diff
changeset
|
94 if not entry_nodes: |
4912568f9048
Refactor delete_entries to do what it says: delete the entries passed as argument
Björn Ricks <bjoern.ricks@intevation.de>
parents:
97
diff
changeset
|
95 return |
4912568f9048
Refactor delete_entries to do what it says: delete the entries passed as argument
Björn Ricks <bjoern.ricks@intevation.de>
parents:
97
diff
changeset
|
96 proj = self.project_by_id(entry_nodes[0].project_id) |
4912568f9048
Refactor delete_entries to do what it says: delete the entries passed as argument
Björn Ricks <bjoern.ricks@intevation.de>
parents:
97
diff
changeset
|
97 entries = entry_nodes |
4912568f9048
Refactor delete_entries to do what it says: delete the entries passed as argument
Björn Ricks <bjoern.ricks@intevation.de>
parents:
97
diff
changeset
|
98 self.backend.delete_entries(entries) |
4912568f9048
Refactor delete_entries to do what it says: delete the entries passed as argument
Björn Ricks <bjoern.ricks@intevation.de>
parents:
97
diff
changeset
|
99 proj.entries = self.backend.load_entries(proj.id) |
4912568f9048
Refactor delete_entries to do what it says: delete the entries passed as argument
Björn Ricks <bjoern.ricks@intevation.de>
parents:
97
diff
changeset
|
100 self.update_entries(proj) |
4912568f9048
Refactor delete_entries to do what it says: delete the entries passed as argument
Björn Ricks <bjoern.ricks@intevation.de>
parents:
97
diff
changeset
|
101 self.project_view.update() |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
102 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
103 def update_project_list(self): |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
104 self.project_view.update() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
105 self.view.update_view() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
106 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
107 def exit(self): |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
108 self.view.exit() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
109 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
110 def project_by_key(self, key): |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
111 for proj in self.projects: |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
112 if proj.key == key: |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
113 return proj |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
114 return None |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
115 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
116 def project_by_id(self, id): |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
117 for proj in self.projects: |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
118 if proj.id == id: |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
119 return proj |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
120 return None |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
121 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
122 def start_project(self, project): |
41
f44f808e7d47
Make getan runnable if there is no project in database.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
33
diff
changeset
|
123 if not project: return |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
124 self.running.append(project) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
125 project.start = datetime.now() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
126 logger.info("Start project '%s' at %s." |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
127 % (project.desc, format_time(datetime.now()))) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
128 self.view.set_footer_text(" Running on '%s'" % project.desc, 'running') |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
129 logger.debug('All running projects: %r' % self.running) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
130 |
90
04dbf4c9f297
Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents:
69
diff
changeset
|
131 def stop_project(self, desc='-no description-', display=True): |
42
a5439795ef09
More checks againts empty projects.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
41
diff
changeset
|
132 if not self.running: return |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
133 project = self.running.pop() |
41
f44f808e7d47
Make getan runnable if there is no project in database.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
33
diff
changeset
|
134 if not project: return |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
135 logger.info("Stop project '%s' at %s." |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
136 % (project.desc, format_time(datetime.now()))) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
137 project.stop = datetime.now() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
138 self.backend.insert_project_entry(project, datetime.now(), desc) |
90
04dbf4c9f297
Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents:
69
diff
changeset
|
139 if display: |
04dbf4c9f297
Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents:
69
diff
changeset
|
140 self.update_entries(project) |
04dbf4c9f297
Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents:
69
diff
changeset
|
141 self.update_project_list() |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
142 logger.debug('Still running projects: %r' % self.running) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
143 |
53
1d63ab21b8af
add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents:
42
diff
changeset
|
144 def add_project(self, key, description): |
1d63ab21b8af
add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents:
42
diff
changeset
|
145 if not key or not description: |
1d63ab21b8af
add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents:
42
diff
changeset
|
146 return |
1d63ab21b8af
add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents:
42
diff
changeset
|
147 self.backend.insert_project(key, description) |
107
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
148 self.update_projects() |
53
1d63ab21b8af
add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents:
42
diff
changeset
|
149 self.update_project_list() |
1d63ab21b8af
add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents:
42
diff
changeset
|
150 |
69
f0e4637ad4e4
Add a update_entry method in getan controller that stores a changed entry in the backened
Björn Ricks <bjoern.ricks@intevation.de>
parents:
67
diff
changeset
|
151 def update_entry(self, entry): |
f0e4637ad4e4
Add a update_entry method in getan controller that stores a changed entry in the backened
Björn Ricks <bjoern.ricks@intevation.de>
parents:
67
diff
changeset
|
152 self.backend.update_entry(entry) |
f0e4637ad4e4
Add a update_entry method in getan controller that stores a changed entry in the backened
Björn Ricks <bjoern.ricks@intevation.de>
parents:
67
diff
changeset
|
153 |
31
fa5b3b1db867
Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
30
diff
changeset
|
154 def shutdown(self): |
30
fea63a224065
Stop still running projects before getan quits - even if getan crashes.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
26
diff
changeset
|
155 for project in self.running: |
90
04dbf4c9f297
Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents:
69
diff
changeset
|
156 self.stop_project(display=False) |
30
fea63a224065
Stop still running projects before getan quits - even if getan crashes.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
26
diff
changeset
|
157 |
107
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
158 def update_projects(self): |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
159 projects, entries = self.load_projects() |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
160 self.projects = projects |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
161 self.project_view.load_rows(projects) |
a23e8191c6bc
Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents:
99
diff
changeset
|
162 |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
163 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
164 def main(): |
67
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
165 |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
166 usage = "usage: %prog [options] [databasefile (default: " + \ |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
167 DEFAULT_DATABASE + ")]" |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
168 parser = OptionParser(usage=usage) |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
169 parser.add_option("-d", "--debug", action="store_true", dest="debug", |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
170 help="Set verbosity to debug") |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
171 parser.add_option("-l", "--logfile", dest="logfile", metavar="FILE", |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
172 help="Write log information to FILE [default: %default]", |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
173 default="getan.log") |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
174 (options, args) = parser.parse_args() |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
175 logargs = dict() |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
176 if options.debug: |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
177 logargs["level"] = logging.DEBUG |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
178 if options.logfile: |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
179 logargs["filename"] = options.logfile |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
180 config.initialize(**logargs) |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
181 global logger |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
182 |
67
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
183 if len(args) > 0: |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
184 backend = Backend(args[0]) |
34a0f5c533bd
Use an OptionParser in getan to add options for debug level and logfile
Björn Ricks <bjoern.ricks@intevation.de>
parents:
53
diff
changeset
|
185 logging.info("Use database '%s'." % args[0]) |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
186 else: |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
187 backend = Backend() |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
188 logging.info("Use database '%s'." % DEFAULT_DATABASE) |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
189 |
31
fa5b3b1db867
Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
30
diff
changeset
|
190 controller = GetanController(backend, ProjectList, EntryList) |
fa5b3b1db867
Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
30
diff
changeset
|
191 |
fa5b3b1db867
Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
30
diff
changeset
|
192 try: |
30
fea63a224065
Stop still running projects before getan quits - even if getan crashes.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
26
diff
changeset
|
193 controller.main() |
90
04dbf4c9f297
Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents:
69
diff
changeset
|
194 except KeyboardInterrupt: |
04dbf4c9f297
Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents:
69
diff
changeset
|
195 pass |
31
fa5b3b1db867
Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
30
diff
changeset
|
196 finally: |
fa5b3b1db867
Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
30
diff
changeset
|
197 controller.shutdown() |
23
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
198 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
199 |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
200 if __name__ == '__main__': |
9c4e8ba3c4fa
Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff
changeset
|
201 main() |