comparison frontends/src/jp/cmd_event.py @ 2244:9d49e66bdbf2

jp (event): creation/modification of main event + invitee commands are now in a "invitee" subcommand
author Goffi <goffi@goffi.org>
date Fri, 19 May 2017 12:45:26 +0200
parents 4b1873ce6b61
children 0f3bfe89999e
comparison
equal deleted inserted replaced
2243:5e12fc5ae52a 2244:9d49e66bdbf2
20 20
21 import base 21 import base
22 from sat.core.i18n import _ 22 from sat.core.i18n import _
23 from sat_frontends.jp.constants import Const as C 23 from sat_frontends.jp.constants import Const as C
24 from functools import partial 24 from functools import partial
25 from dateutil import parser as du_parser
26 import calendar
27 import time
25 28
26 __commands__ = ["Event"] 29 __commands__ = ["Event"]
27 30
31 # TODO: move date parsing to base, it may be useful for other commands
32
28 33
29 class Get(base.CommandBase): 34 class Get(base.CommandBase):
35
36 def __init__(self, host):
37 base.CommandBase.__init__(self,
38 host,
39 'get',
40 use_output=C.OUTPUT_DICT,
41 use_pubsub_node_req=True,
42 use_verbose=True,
43 help=_(u'get event data'))
44 self.need_loop=True
45
46 def add_parser_options(self):
47 self.parser.add_argument("-i", "--id", type=base.unicode_decoder, default=u'', help=_(u"ID of the PubSub Item"))
48
49 def eventInviteeGetCb(self, result):
50 event_date, event_data = result
51 event_data['date'] = event_date
52 self.output(event_data)
53 self.host.quit()
54
55 def start(self):
56 self.host.bridge.eventGet(
57 self.args.service,
58 self.args.node,
59 self.args.id,
60 self.profile,
61 callback=self.eventInviteeGetCb,
62 errback=partial(self.errback,
63 msg=_(u"can't get event data: {}"),
64 exit_code=C.EXIT_BRIDGE_ERRBACK))
65
66
67 class EventBase(object):
68
69 def add_parser_options(self):
70 self.parser.add_argument("-i", "--id", type=base.unicode_decoder, default=u'', help=_(u"ID of the PubSub Item"))
71 self.parser.add_argument("-d", "--date", type=unicode, help=_(u"date of the event"))
72 self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields',
73 metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set"))
74
75 def parseFields(self):
76 return dict(self.args.fields) if self.args.fields else {}
77
78 def parseDate(self):
79 if self.args.date:
80 try:
81 date = int(self.args.date)
82 except ValueError:
83 try:
84 date_time = du_parser.parse(self.args.date, dayfirst=True)
85 except ValueError as e:
86 self.parser.error(_(u"Can't parse date: {msg}").format(msg=e))
87 if date_time.tzinfo is None:
88 date = calendar.timegm(date_time.timetuple())
89 else:
90 date = time.mktime(date_time.timetuple())
91 else:
92 date = -1
93 return date
94
95
96 class Create(EventBase, base.CommandBase):
97 def __init__(self, host):
98 super(Create, self).__init__(host, 'create', use_pubsub_node_req=True, help=_('create or replace event'))
99 EventBase.__init__(self)
100 self.need_loop=True
101
102 def eventCreateCb(self, node):
103 self.disp(_(u'Event created successfuly on node {node}').format(node=node))
104 self.host.quit()
105
106 def start(self):
107 fields = self.parseFields()
108 date = self.parseDate()
109 self.host.bridge.eventCreate(
110 date,
111 fields,
112 self.args.service,
113 self.args.node,
114 self.args.id,
115 self.profile,
116 callback=self.eventCreateCb,
117 errback=partial(self.errback,
118 msg=_(u"can't create event: {}"),
119 exit_code=C.EXIT_BRIDGE_ERRBACK))
120
121
122 class Modify(EventBase, base.CommandBase):
123 def __init__(self, host):
124 super(Modify, self).__init__(host, 'modify', use_pubsub_node_req=True, help=_('modify an existing event'))
125 EventBase.__init__(self)
126 self.need_loop=True
127
128 def start(self):
129 fields = self.parseFields()
130 date = 0 if not self.args.date else self.parseDate()
131 self.host.bridge.eventModify(
132 self.args.service,
133 self.args.node,
134 self.args.id,
135 date,
136 fields,
137 self.profile,
138 callback=self.host.quit,
139 errback=partial(self.errback,
140 msg=_(u"can't update event data: {}"),
141 exit_code=C.EXIT_BRIDGE_ERRBACK))
142
143
144 class InviteeGet(base.CommandBase):
30 145
31 def __init__(self, host): 146 def __init__(self, host):
32 base.CommandBase.__init__(self, 147 base.CommandBase.__init__(self,
33 host, 148 host,
34 'get', 149 'get',
39 self.need_loop=True 154 self.need_loop=True
40 155
41 def add_parser_options(self): 156 def add_parser_options(self):
42 pass 157 pass
43 158
44 def eventGetCb(self, event_data): 159 def eventInviteeGetCb(self, event_data):
45 self.output(event_data) 160 self.output(event_data)
46 self.host.quit() 161 self.host.quit()
47 162
48 def start(self): 163 def start(self):
49 self.host.bridge.eventGet( 164 self.host.bridge.eventInviteeGet(
50 self.args.service, 165 self.args.service,
51 self.args.node, 166 self.args.node,
52 self.profile, 167 self.profile,
53 callback=self.eventGetCb, 168 callback=self.eventInviteeGetCb,
54 errback=partial(self.errback, 169 errback=partial(self.errback,
55 msg=_(u"can't get event data: {}"), 170 msg=_(u"can't get event data: {}"),
56 exit_code=C.EXIT_BRIDGE_ERRBACK)) 171 exit_code=C.EXIT_BRIDGE_ERRBACK))
57 172
58 173
59 class Set(base.CommandBase): 174 class InviteeSet(base.CommandBase):
60 def __init__(self, host): 175 def __init__(self, host):
61 super(Set, self).__init__(host, 'set', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, help=_('set event attendance')) 176 super(InviteeSet, self).__init__(host, 'set', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, help=_('set event attendance'))
62 self.need_loop=True 177 self.need_loop=True
63 178
64 def add_parser_options(self): 179 def add_parser_options(self):
65 self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields', 180 self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields',
66 metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set")) 181 metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set"))
67 182
68 def start(self): 183 def start(self):
69 fields = dict(self.args.fields) if self.args.fields else {} 184 fields = dict(self.args.fields) if self.args.fields else {}
70 self.host.bridge.eventSet( 185 self.host.bridge.eventInviteeSet(
71 self.args.service, 186 self.args.service,
72 self.args.node, 187 self.args.node,
73 fields, 188 fields,
74 self.profile, 189 self.profile,
75 callback=self.host.quit, 190 callback=self.host.quit,
76 errback=partial(self.errback, 191 errback=partial(self.errback,
77 msg=_(u"can't set event data: {}"), 192 msg=_(u"can't set event data: {}"),
78 exit_code=C.EXIT_BRIDGE_ERRBACK)) 193 exit_code=C.EXIT_BRIDGE_ERRBACK))
79 194
80 195
196 class Invitee(base.CommandBase):
197 subcommands = (InviteeGet, InviteeSet)
198
199 def __init__(self, host):
200 super(Invitee, self).__init__(host, 'invitee', use_profile=False, help=_(u'manage invities'))
201
202
81 class Event(base.CommandBase): 203 class Event(base.CommandBase):
82 subcommands = (Get, Set) 204 subcommands = (Get, Create, Modify, Invitee)
83 205
84 def __init__(self, host): 206 def __init__(self, host):
85 super(Event, self).__init__(host, 'event', use_profile=False, help=_('event management')) 207 super(Event, self).__init__(host, 'event', use_profile=False, help=_('event management'))