[boobcoming] code review corrections
- split attends command in two (attends / unattends) - use formatter in export command
This commit is contained in:
parent
d2c9bab406
commit
23c228280d
1 changed files with 30 additions and 97 deletions
|
|
@ -40,9 +40,10 @@ class ICalFormatter(IFormatter):
|
||||||
MANDATORY_FIELDS = ('id', 'start_date', 'end_date', 'summary', 'status')
|
MANDATORY_FIELDS = ('id', 'start_date', 'end_date', 'summary', 'status')
|
||||||
|
|
||||||
def start_format(self, **kwargs):
|
def start_format(self, **kwargs):
|
||||||
self.output(u'BEGIN:VCALENDAR')
|
result = u'BEGIN:VCALENDAR\n'
|
||||||
self.output(u'VERSION:2.0')
|
result += u'VERSION:2.0\n'
|
||||||
self.output(u'PRODID:-//hacksw/handcal//NONSGML v1.0//EN')
|
result += u'PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n'
|
||||||
|
self.output(result)
|
||||||
|
|
||||||
def format_obj(self, obj, alias):
|
def format_obj(self, obj, alias):
|
||||||
result = u'BEGIN:VEVENT\n'
|
result = u'BEGIN:VEVENT\n'
|
||||||
|
|
@ -78,7 +79,7 @@ class ICalFormatter(IFormatter):
|
||||||
if hasattr(obj, 'url') and not empty(obj.url):
|
if hasattr(obj, 'url') and not empty(obj.url):
|
||||||
result += u'URL:%s\n' % obj.url
|
result += u'URL:%s\n' % obj.url
|
||||||
|
|
||||||
result += u'END:VEVENT'
|
result += u'END:VEVENT\n'
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def flush(self, **kwargs):
|
def flush(self, **kwargs):
|
||||||
|
|
@ -150,6 +151,7 @@ class Boobcoming(ReplApplication):
|
||||||
COMMANDS_FORMATTERS = {'list': 'upcoming_list',
|
COMMANDS_FORMATTERS = {'list': 'upcoming_list',
|
||||||
'search': 'upcoming_list',
|
'search': 'upcoming_list',
|
||||||
'info': 'upcoming',
|
'info': 'upcoming',
|
||||||
|
'export': 'ical_formatter'
|
||||||
}
|
}
|
||||||
|
|
||||||
WEEK = {'MONDAY': 0,
|
WEEK = {'MONDAY': 0,
|
||||||
|
|
@ -269,52 +271,6 @@ class Boobcoming(ReplApplication):
|
||||||
self.start_format()
|
self.start_format()
|
||||||
self.format(event)
|
self.format(event)
|
||||||
|
|
||||||
def start_format(self, **kwargs):
|
|
||||||
result = u'BEGIN:VCALENDAR\n'
|
|
||||||
result += u'VERSION:2.0\n'
|
|
||||||
result += u'PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n'
|
|
||||||
return result
|
|
||||||
|
|
||||||
def format_event(self, obj):
|
|
||||||
result = u'BEGIN:VEVENT\n'
|
|
||||||
result += u'UID:%s\n' % obj.id
|
|
||||||
result += u'DTSTART:%s\n' % obj.start_date.strftime("%Y%m%dT%H%M%SZ")
|
|
||||||
result += u'DTEND:%s\n' % obj.end_date.strftime("%Y%m%dT%H%M%SZ")
|
|
||||||
result += u'SUMMARY:%s\n' % obj.summary
|
|
||||||
result += u'STATUS:%s\n' % obj.status
|
|
||||||
|
|
||||||
location = ''
|
|
||||||
if hasattr(obj, 'location') and not empty(obj.location):
|
|
||||||
location += obj.location + ' '
|
|
||||||
|
|
||||||
if hasattr(obj, 'city') and not empty(obj.city):
|
|
||||||
location += obj.city + ' '
|
|
||||||
|
|
||||||
if not empty(location):
|
|
||||||
result += u'LOCATION:%s\n' % location
|
|
||||||
|
|
||||||
if hasattr(obj, 'categories') and not empty(obj.categories):
|
|
||||||
result += u'CATEGORIES:%s\n' % obj.categories
|
|
||||||
|
|
||||||
if hasattr(obj, 'description') and not empty(obj.description):
|
|
||||||
result += u'DESCRIPTION:%s\n' % obj.description.replace('\r\n', '\\n') \
|
|
||||||
.replace(',', '\,')
|
|
||||||
|
|
||||||
if hasattr(obj, 'transp') and not empty(obj.transp):
|
|
||||||
result += u'TRANSP:%s\n' % obj.transp
|
|
||||||
|
|
||||||
if hasattr(obj, 'sequence') and not empty(obj.sequence):
|
|
||||||
result += u'SEQUENCE:%s\n' % obj.sequence
|
|
||||||
|
|
||||||
if hasattr(obj, 'url') and not empty(obj.url):
|
|
||||||
result += u'URL:%s\n' % obj.url
|
|
||||||
|
|
||||||
result += u'END:VEVENT\n'
|
|
||||||
return result
|
|
||||||
|
|
||||||
def end_format(self):
|
|
||||||
return u'END:VCALENDAR'
|
|
||||||
|
|
||||||
def do_export(self, line):
|
def do_export(self, line):
|
||||||
"""
|
"""
|
||||||
export FILENAME [ID1 ID2 ID3 ...]
|
export FILENAME [ID1 ID2 ID3 ...]
|
||||||
|
|
@ -331,29 +287,14 @@ class Boobcoming(ReplApplication):
|
||||||
|
|
||||||
_file, args = self.parse_command_args(line, 2, req_n=1)
|
_file, args = self.parse_command_args(line, 2, req_n=1)
|
||||||
|
|
||||||
|
if not _file == "-":
|
||||||
|
dest = self.check_file_ext(_file)
|
||||||
|
self.formatter.outfile = dest
|
||||||
|
|
||||||
l = self.retrieve_events(args)
|
l = self.retrieve_events(args)
|
||||||
buff = self.create_buffer(l)
|
self.formatter.start_format()
|
||||||
|
|
||||||
if _file == "-":
|
|
||||||
print buff
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
dest = self.check_file_ext(_file)
|
|
||||||
with open(dest, 'w') as f:
|
|
||||||
f.write(buff.encode('ascii', 'ignore'))
|
|
||||||
except IOError as e:
|
|
||||||
print >>sys.stderr, 'Unable to write bill in "%s": %s' % (dest, e)
|
|
||||||
return 1
|
|
||||||
|
|
||||||
def create_buffer(self, l):
|
|
||||||
buff = self.start_format()
|
|
||||||
|
|
||||||
for item in l:
|
for item in l:
|
||||||
buff += self.format_event(item)
|
self.format(item)
|
||||||
|
|
||||||
buff += self.end_format()
|
|
||||||
|
|
||||||
return buff
|
|
||||||
|
|
||||||
def retrieve_events(self, args):
|
def retrieve_events(self, args):
|
||||||
l = []
|
l = []
|
||||||
|
|
@ -415,41 +356,33 @@ class Boobcoming(ReplApplication):
|
||||||
|
|
||||||
def do_attends(self, line):
|
def do_attends(self, line):
|
||||||
"""
|
"""
|
||||||
attend IS_ATTENDING [ID1 ID2 ID3 ...]
|
attends ID1 [ID2 ID3 ...]
|
||||||
|
|
||||||
ID is the identifier of the event. If no ID every events are exported
|
ID is the identifier of the event.
|
||||||
|
|
||||||
IS_ATTENDING is a booleanizable value that indicate if attending or not
|
|
||||||
|
|
||||||
Export event in ICALENDAR format
|
|
||||||
"""
|
"""
|
||||||
if not line:
|
if not line:
|
||||||
print >>sys.stderr, 'This command takes at leat one argument: %s' % self.get_command_help('export')
|
print >>sys.stderr, 'This command takes at leat one argument: %s' % self.get_command_help('attends')
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
attending, args = self.parse_command_args(line, 2, req_n=1)
|
args = self.parse_command_args(line, 1, req_n=1)
|
||||||
|
|
||||||
l = self.retrieve_events(args)
|
|
||||||
is_attending = self.booleanize(attending)
|
|
||||||
|
|
||||||
if not is_attending:
|
|
||||||
print >> sys.stderr, "Cannot booleanize ambiguous value '%s'" % attending
|
|
||||||
return 2
|
|
||||||
|
|
||||||
|
l = self.retrieve_events(args[0])
|
||||||
for event in l:
|
for event in l:
|
||||||
self.do('attends_event', event, is_attending)
|
self.do('attends_event', event, True)
|
||||||
|
|
||||||
def booleanize(self, value):
|
def do_unattends(self, line):
|
||||||
"""Return value as a boolean."""
|
"""
|
||||||
|
unattends ID1 [ID2 ID3 ...]
|
||||||
|
|
||||||
true_values = ("yes", "true")
|
ID is the identifier of the event.
|
||||||
false_values = ("no", "false")
|
"""
|
||||||
|
|
||||||
if isinstance(value, bool):
|
if not line:
|
||||||
return value
|
print >>sys.stderr, 'This command takes at leat one argument: %s' % self.get_command_help('unattends')
|
||||||
|
return 2
|
||||||
|
|
||||||
if value.lower() in true_values:
|
args = self.parse_command_args(line, 1, req_n=1)
|
||||||
return True
|
|
||||||
|
|
||||||
elif value.lower() in false_values:
|
l = self.retrieve_events(args[0])
|
||||||
return False
|
for event in l:
|
||||||
|
self.do('attends_event', event, False)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue