annotate getan.py @ 107:a23e8191c6bc

Update project list when a new project was created
author Björn Ricks <bjoern.ricks@intevation.de>
date Mon, 12 Sep 2011 14:54:00 +0200
parents 4912568f9048
children 1165422b5db7
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
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
19 from getan.backend import *
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
20 from getan.view import *
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
21 from getan.utils import format_time
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
22
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 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
24
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 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
26 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
27 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
28 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
29
107
a23e8191c6bc Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents: 99
diff changeset
30 self.backend = backend
a23e8191c6bc Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents: 99
diff changeset
31 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
32 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
33 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
34
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 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
36 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
37
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 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
39 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
40
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 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
42 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
43
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 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
45 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
46
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 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
48 if 'window resize' in input:
5e4d2810a739 Added support for resizing the application window.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents: 25
diff changeset
49 self.view.loop.screen_size = None
5e4d2810a739 Added support for resizing the application window.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents: 25
diff changeset
50 self.view.loop.draw_screen()
5e4d2810a739 Added support for resizing the application window.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents: 25
diff changeset
51 else:
5e4d2810a739 Added support for resizing the application window.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents: 25
diff changeset
52 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
53
107
a23e8191c6bc Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents: 99
diff changeset
54 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
55 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
56 if projects:
a23e8191c6bc Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents: 99
diff changeset
57 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
58 else:
a23e8191c6bc Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents: 99
diff changeset
59 entries = []
a23e8191c6bc Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents: 99
diff changeset
60 return (projects, entries)
a23e8191c6bc Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents: 99
diff changeset
61
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
62 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
63 logger.debug("GetanController: update entries.")
42
a5439795ef09 More checks againts empty projects.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 41
diff changeset
64 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
65 else: entries = []
a5439795ef09 More checks againts empty projects.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 41
diff changeset
66 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
67 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
68
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 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
70 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
71 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
72 try:
33
f96a18c10836 Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 31
diff changeset
73 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
74 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
75 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
76 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
77 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
78 "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
79 % (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
80 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
81
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 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91
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 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101
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 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
103 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
104 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
105
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 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
107 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
108
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 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
110 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
111 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
112 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
113 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
114
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 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
116 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
117 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
118 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
119 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
120
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 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
122 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
123 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
124 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
125 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
126 % (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
127 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
128 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
129
90
04dbf4c9f297 Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents: 69
diff changeset
130 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
131 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
132 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
133 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
134 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
135 % (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
136 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
137 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
138 if display:
04dbf4c9f297 Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents: 69
diff changeset
139 self.update_entries(project)
04dbf4c9f297 Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents: 69
diff changeset
140 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
141 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
142
53
1d63ab21b8af add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents: 42
diff changeset
143 def add_project(self, key, description):
1d63ab21b8af add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents: 42
diff changeset
144 if not key or not description:
1d63ab21b8af add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents: 42
diff changeset
145 return
1d63ab21b8af add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents: 42
diff changeset
146 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
147 self.update_projects()
53
1d63ab21b8af add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents: 42
diff changeset
148 self.update_project_list()
1d63ab21b8af add add_project method
Björn Ricks <bjoern.ricks@intevation.de>
parents: 42
diff changeset
149
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
150 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
151 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
152
31
fa5b3b1db867 Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 30
diff changeset
153 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
154 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
155 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
156
107
a23e8191c6bc Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents: 99
diff changeset
157 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
158 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
159 self.projects = projects
a23e8191c6bc Update project list when a new project was created
Björn Ricks <bjoern.ricks@intevation.de>
parents: 99
diff changeset
160 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
161
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
162
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 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
164
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 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
166 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
167 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
168 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
169 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
170 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
171 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
172 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
173 (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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181
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
182 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
183 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
184 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
185 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
186 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
187 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
188
31
fa5b3b1db867 Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 30
diff changeset
189 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
190
fa5b3b1db867 Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 30
diff changeset
191 try:
30
fea63a224065 Stop still running projects before getan quits - even if getan crashes.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents: 26
diff changeset
192 controller.main()
90
04dbf4c9f297 Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents: 69
diff changeset
193 except KeyboardInterrupt:
04dbf4c9f297 Handle KeyboardInterrupt and shutdown getan carefully afterwards
Björn Ricks <bjoern.ricks@intevation.de>
parents: 69
diff changeset
194 pass
31
fa5b3b1db867 Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 30
diff changeset
195 finally:
fa5b3b1db867 Bugfix: removed 'with' statement - replaced by try/finally (ISSUE1566).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 30
diff changeset
196 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
197
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 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
200 main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)