Add files from the old svn, r101.
[matthijs/upstream/mobilegtd.git] / src / persistence / action_file.py
1 import re,os
2 from model.action import *
3 from model.model import WriteableItem
4 from model import action
5
6 class ActiveActionStatus(ActionStatus):
7         def __init__(self):
8                 super(ActiveActionStatus,self).__init__('active',1,u'-')
9         def update(self,a):
10                 for o in a.observers:
11                         if type(o) == ActionFile:
12                                 if not o.exists():
13                                         return action.done
14                 return self
15
16 active_status = ActiveActionStatus()
17 action.active = active_status
18
19
20
21 class ActionFile(WriteableItem):
22         def __init__(self,action):
23                 self.action = action
24                 self.update_methods = {'status':self.update_status,'description':self.update_description,'context':self.set_context}
25                 self.action.observers.append(self)
26                 
27         def notify(self,action,attribute,new=None,old=None):
28                 super(ActionFile,self).notify(action,attribute,new=new,old=old)
29                 if attribute in self.update_methods:
30                         self.update_methods[attribute](new=new,old=old)
31                         
32         def write(self):
33                 if self.action.status == active:
34                         super(ActionFile,self).write()
35         
36         def update_description(self,new,old=None):
37                 if self.action.status == active and old:
38                         self.remove(self.path(description=old))
39
40         def update_status(self,new,old=None):
41                 if new == inactive or new == done:
42                         self.remove()
43
44         def set_context(self,new,old=None):
45                 if self.action.status == active and old:
46                         self.remove(self.path(context=old))
47
48         def update_done_status(self):
49                 if self.action.status == active and not self.exists():
50                         self.action.status = done
51                         
52         def path(self,context=None,description=None):
53                 if not context:
54                         context = self.action.context
55                 if not description:
56                         description = self.action.description
57                 return os.path.join(context,description+'.act')
58         
59         
60         def file_string(self):
61                 string = self.action.project_file_string()
62                 if self.action.project:
63                         string = string+u'\nProject: %s'%self.action.project
64                 return string
65