-
Notifications
You must be signed in to change notification settings - Fork 2
/
ccal.py
executable file
·727 lines (673 loc) · 26.8 KB
/
ccal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/usr/bin/env python3
"""ccal.py 0.5
Python implementation processing and extending ccal style ~/.cal.dat files"""
# License (MIT)
#
# Copyright © 2014 Michael Zeltner <m@niij.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
## TODO
# -------------------------------------------------
# extend notation for date ranges:
# "YYYY MM DD WD +D" - the "+D" means "plus D days",
# starting on YYYY-MM-DD and continuing to YYYY-MM-(DD+D).
# example: "2012 12 27 00 +3 29C3" then expands to this:
# 2012 12 27 00 29C3
# 2012 12 28 00 29C3
# 2012 12 29 00 29C3
# 2012 12 30 00 29C3
#
# "the congress" is taking place on the same dates every year,
# so even the yearly entries should take a "+D" argument:
# -999 12 27 00 +3 Chaos Communication Congress
#
# allow relative dates, some arithmetic:
# 'tomorrow' and 'yesterday',
# (in) [+-] N [days/months/years] (ago)
# (in) N [days/months/years]
# N [days/months/years] ago
# examples:
# in 4d, in 3m, in 2y -- 4d ago, 3m ago, 2y ago
# "in 3mon 1 year 2d"
#
# WARNING! watch for spaces and abbreviated time names!
# allow multiple occurrences of times:
# example: "1y 3m 2d 2y 1m 4d" -> "3y 4m 6d"
# (probably wont be used very often ;)
#
# setup file: ~/.ccalpy.rc ?
#
# arguments/actions:
# general: Implement @actions properly (XXX OH MY GOD IT'S SO UGLY RIGHT NOW)
#
# add "ia" (interactive mode) which automatically updates the screen,
# possibly with some paging.
#
# add "add" to add new entries and comments.
# example: ccal add 3d -- Three days from when this entry was created
#
# add "del" to delete entries (with auto completion)
# del should behave like ['foo'].pop() i.e. return object before removal
# example: TODO
#
# add "search" to search for entries including foo within date ranges
# example: search party vienna 2012-12-21 2013-01-05
#
# input+output:
# add output for ical (ICS files)
# allow short names and numbers for month
#
# color:
# add colors for each weekday
# add color for time ranges
# add color for places (@placename)
# add color for URLs
#
# -------------------------------------------------
# Poor persons ./ccal.py add
#
# #!/bin/bash
# DATE=$(date -d "$1" +"%Y %m %d")
# if [[ $? -eq 0 ]]; then
# DATE=$(echo $DATE 00 $(sed -e "s,$1 ,,"<<<"$@"))
# echo $DATE >> ~/.cal.dat
# ccal.py ls -p -C
# echo \"$DATE\" added
# fi
#
import datetime as dt
import calendar as cal
import re
import sys
import os
import argparse
from io import StringIO
today = dt.date.today
class fmt(dict):
"""Easy ANSI formatting
>>> fmt = fmt()
>>> print fmt.f('yellow'), "Yellow foreground"
>>> print fmt.b('red'), "Yellow fg, red background"
>>> print fmt.bfs('blue', 'magenta', 'bright')
>>> print "Blue bg, magenta fg, bright style"
>>> print fmt.s('normal')
>>> print "Blue bg, magenta fg, normal style"
>>> print fmt.r, "Everything reset to normal"
>>> foo = "%sHello!%s" % (fmt.s('bright'), fmt.r)
>>> print fmt.c(foo)
Any permutation of f/b/s as well as c is a valid method to call, r is a
static attribute to reset colouring.
"""
def __init__(self):
"""Initialise standard colours, styles and colouring support"""
self['f'] = { 'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37,
'reset': 39 }
self['b'] = { 'black': 40, 'red': 41, 'green': 42, 'yellow': 43,
'blue': 44, 'magenta': 45, 'cyan': 46, 'white': 47,
'reset': 49 }
self['s'] = { 'normal': '22;23;24;25', 'bright': 1, 'nobright': '22',
'dim': 2, 'reset': 0, 'transparent': 8, 'underline': 4 }
self.colors = self.has_colors(sys.stdout)
def has_colors(self, stream):
"""Determine if our output stream supports ANSI colouring"""
if not hasattr(stream, "isatty"):
return False
if not stream.isatty():
return False # auto color only on TTYs
try:
if "COLORTERM" in os.environ or "color" in os.environ["TERM"]:
return True
import curses
curses.setupterm()
return curses.tigetnum("colors") > 2
except:
return False # guess false in case of error
def __getattr__(self, attr):
"""Shortcuts for easier use"""
valid = re.compile(r'^[fbs]+$')
if not valid.match(attr) and not attr in ('r', 'c') and \
not attr.startswith('__'):
raise UnboundLocalError("Method can only be a permutation of " \
+ "f/b/s or c, attribute can only be r " \
+ "('%s' given)" % attr)
elif attr == 'r':
return self.reset
elif attr == 'c':
return lambda string: self.clear(string)
# >>> help(fmt)
elif attr.startswith('__'):
return dict.__getattr__(self, attr)
return lambda *values: self.format(self.lookup(attr, *values))
def lookup(self, keys, *names):
"""Returns values for color names"""
if len(keys) != len(names):
raise TypeError("Method name length and values passed need to" \
+ "be of the same length (%s and %s given)" % \
(len(method), len(names)))
values = []
for char in keys:
if char in self and names[keys.index(char)] in self[char]:
values.append(self[char][names[keys.index(char)]])
else:
raise KeyError("Colour/style '%s' not found in '%s'" % \
(names[keys.index(char)] ,char))
return values
@property
def reset(self):
"""ANSI escape sequence clearing all colouring"""
return self.fbs('reset', 'reset', 'reset')
def clear(self, string):
"""Clear ANSI coloring from a string"""
exp = re.compile(r'\x1B\[[0-9;]*[mK]')
return exp.sub('', string)
def format(self, colors):
"""Format ANSI escape sequence"""
return '\033[%sm' % ";".join([str(color) for color in colors])
fmt = fmt() # We really don't need more than one instance here.
def ordinal(value):
try:
value = int(value)
except ValueError:
return value
if value % 100//10 != 1:
if value % 10 == 1:
ordval = "%d%s" % (value, "st")
elif value % 10 == 2:
ordval = "%d%s" % (value, "nd")
elif value % 10 == 3:
ordval = "%d%s" % (value, "rd")
else:
ordval = "%d%s" % (value, "th")
else:
ordval = "%d%s" % (value, "th")
return ordval
class Entry(object):
"""Calender Entry
In some cases when we instantiate an Entry, it turns out it's not an
entry but a comment of a previous one. In this case, we're just going
to return the line it was instanciated with instead.
If an entry is dynamically generated and would apply to more than one
day, it might be expanded, in which case a list with multiple entries
is returned.
>>> Entry("2012 12 11 00 Foo")
Tue 11: Foo
>>> Entry("-999 -9 11 05 Bar")
[Sun 16: Bar, Fri 21: Bar, Wed 26: Bar, Mon 31: Bar]
>>> Entry("Hung out with Sven Guckes in Berlin")
'Hung out with Sven Guckes in Berlin'
"""
def __new__(cls, line='', bdt=today(), edt=None, exp=True):
self = super(Entry, cls).__new__(cls)
self.comm = ''
line = line.strip()
if edt:
self.dt = edt
self.desc = line
return self
if len(line) < 14 or line.count(' ') < 4:
return line
yyyy, mm, dd, wd = line.split(' ')[:4]
self.desc = line[14:]
try:
yyyy = int(yyyy)
mm = int(mm)
dd = int(dd)
# week/day
if dd < 1:
w = int(wd[0])
d = int(wd[1])
# periodic
else:
w = -1
d = int(wd)
# Must be a comment then
except:
return line
if not exp and not wd.startswith("00"):
return None
if yyyy < 1970: yyyy = bdt.year
if mm < 1: mm = bdt.month
if "{" in self.desc and "}" in self.desc:
try:
oc = int((" %s " % self.desc).split(" {")[1].split("} ")[0])
self.desc = self.desc.replace("{%s}" % oc, ordinal(yyyy - oc))
except:
pass
self.desc = self.desc.replace("\{", "{").replace("\}", "}")
# week/day
try:
fw = dt.date(yyyy, mm, 1)
except:
return line
# every week
if exp and w == 0 and d != 0:
entries = []
for i in range(5):
n = dt.timedelta(days=d-fw.isoweekday()+7*i)
self.dt = fw + n
if self.dt.month == bdt.month:
entries.append(Entry(self.desc, bdt, self.dt))
return entries
# last week of the month
if exp and w == 9 and d != 0:
fw = dt.datetime(yyyy, mm, cal.monthrange(yyyy, mm)[1])
n = dt.timedelta(days=d-fw.isoweekday())
dd = (fw + n).day
# If this date doesn't exist within this month then there is probably
# no week starting with that day
if exp and dd < 1 and w != 0 and \
(w*7)-6 > cal.monthrange(yyyy, mm)[1]:
return
# every day, but show only once
if exp and w == 0 and d == 0:
dd = today().day
# specific day/week
elif exp and dd < 1 and w != -1 and d != 0 and fw.isoweekday() > d:
n = dt.timedelta(days=7*w)
w = fw + n
elif exp and dd < 1 and w != 0:
w = dt.date(yyyy, mm, (w*7)-6 if w > 0 else bdt.day)
if exp and w != 9 and dd < 1 and d != 0:
d = dt.timedelta(days=d-w.isoweekday())
dd = (w + d).day
# periodic
if exp and w == -1 and isinstance(d, int) and dd > 0 and d > 1:
entries = []
self.dt = dt.date(yyyy, mm, dd)
while self.dt.month <= bdt.month and self.dt.year <= bdt.year:
self.dt = self.dt + dt.timedelta(days=d)
if self.dt.month == bdt.month and self.dt.year == bdt.year:
entries.append(Entry(self.desc, bdt, self.dt))
return entries
# daily, but show only once
if exp and w == -1 and isinstance(d, int) and dd > 0 and d == 1 and \
bdt > dt.date(yyyy, mm, dd):
dd = bdt.day
# XXX dirty stuff, should probably stop earlier?
try:
self.dt = dt.date(yyyy, mm, dd)
return self
except:
return
def __getitem__(self, item):
return self.dt.__getattribute__(item)
def __repr__(self):
return nextTo(self.dt.strftime("%a %e:"), self.desc)
def full(self):
"""Show entry with comment"""
both = "%s\n %s" % \
(self.desc,
' # '.join(self.comm.split("\n")).strip().replace(" #","\n #"))
return nextTo(self.dt.strftime("%a %e:"), both)
def __lt__(self, other):
return self.dt < other.dt
class Entries(list):
def __init__(self, fp=os.path.expanduser('~/.cal.dat'), bdt=(today(),),
exp=True, comm=False, every=False):
list.__init__(self)
self.caldat = open(fp) if isinstance(fp, str) else fp
self.bdt = bdt
self.years = [d.year for d in bdt[0 if hasattr(bdt[0],
'year') else 1:]]
self.months = [d.month for d in bdt[0 if hasattr(bdt[0],
'month') else 1:]]
self.days = []
# If our first date is None, skip highlighting
if bdt[0] == None:
pass
elif (bdt[0] and hasattr(bdt[0], 'day') and bdt[0].day != 1) or \
(today().day == 1 or '1' in sys.argv or '01' in sys.argv):
for d in bdt:
if hasattr(d, 'day'):
self.days.append(d.day)
#self.days = [d.day if hasattr(d, 'day') else None for d in bdt]
self.comm = comm
# It's necessary to first create a whole list of entries because
# otherwise comments would end up on wrong entries ...
entries = []
for line in self.caldat:
entry = Entry(line, bdt[0 if hasattr(bdt[0], 'day') else 1],
exp=exp)
if isinstance(entry, Entry):
entries.append(entry) # self.append sorts, quicker that way
elif isinstance(entry, list):
entries.extend(entry)
continue
elif self.comm and isinstance(entry, str) and len(entries) > 0:
entries[-1].comm += ('\n' + entry)
continue
# ... hence we filter out entries later
for entry in entries:
if every:
list.append(self, entry)
elif entry['year'] in self.years and entry['month'] in self.months:
list.append(self, entry)
self.sort()
def limit(self, limit="24"):
"""Limit range of entries from today to a specific count"""
days = False
try:
days = True if "d" in limit else False
limit = int(limit[:-1])
except:
limit = int(limit)
entries = Entries()
for i in range(len(self)):
if self[0].dt < today():
del self[0]
if days:
limit = today() + dt.timedelta(days=limit)
for i in range(len(self)):
if self[-1].dt > limit:
del self[-1]
else:
while len(self) > limit:
self.pop()
def __repr__(self):
out = ""
tmw = today() + dt.timedelta(days=1)
for entry in self:
e = ''
if self.comm and entry.comm:
e = entry.full().replace('\n', '%s\n' % fmt.r)
if entry['day'] in self.days and entry['month'] in self.months:
if e:
e = e.replace(' #',
' %s#' % fmt.bf('red', 'reset'))
out += "%s*%s%s%s%s" % (fmt.s('transparent'), fmt.r,
fmt.bf('red', 'reset'), e or entry,
fmt.r)
# If we're highlighting today, highlight tomorrow as well
# XXX How do we know we are we highlighting day? Needs better check
elif entry['day'] == tmw.day and entry['month'] == tmw.month:
if e:
e = e.replace(' #',
' %s#' % fmt.bf('yellow', 'reset'))
out += "%s*%s%s%s%s" % (fmt.s('transparent'), fmt.r,
fmt.bf('yellow', 'reset'), e or entry,
fmt.r)
else:
if e:
e = e.replace(' #',
' %s#' % fmt.bf('white', 'black'))
out += " %s%s%s" % (fmt.bf('white', 'black'), e or entry,
fmt.r)
out += '\n'
return out.strip('\n')
def append(self, obj):
list.append(self, obj)
self.sort()
class Calendar(dict):
def __init__(self, bdt=today(), hl=(today(),), entries=[]):
dict.__init__(self)
self.appointments = []
for entry in entries:
self.appointments.append(entry.dt.day)
self._keys = []
self.bdt = bdt
self.hl = ()
self.bd = [str(entry.dt.day) for entry in entries]
if hl[0].day != 1 or (today().day == 1 or '1' in sys.argv or '01' in \
sys.argv):
self.hl = hl
wd, dy = cal.monthrange(self.bdt.year, self.bdt.month)
for day in range(1, dy+1):
self[day] = []
if cal.weekday(self.bdt.year, self.bdt.month, day) == 6:
hl = dt.date(self.bdt.year, self.bdt.month, day) in self.hl
self.mark(day, fmt.fs('white' if hl else 'magenta', 'bright'),
fmt.fs('white', 'nobright' if hl else 'normal'))
def __setitem__(self, key, item):
dict.__setitem__(self, key, item)
self._keys.append(key)
def keys(self):
return self._keys
def values(self):
return map(self.get, self._keys)
def mark(self, day, pre, post):
if not pre in self[day] and not post in self[day]:
self[day].insert(0, pre)
self[day].append(post)
def dayr(self, day):
half = int(len(self[day])/2)
ds = str(day) if len(str(day)) > 1 else " %s" % day
if half:
return "%s%s%s" % ("".join(self[day][:half]), ds,
"".join(self[day][half:]))
else:
return ds
def __repr__(self):
for date in self.hl:
self.mark(date.day, "%s<" % fmt.bf("red", "reset"),
">%s" % fmt.bf('blue', 'white'))
for day in self.appointments:
self.mark(day, fmt.s('underline'), fmt.s('normal'))
wd, dy = cal.monthrange(self.bdt.year, self.bdt.month)
my = self.bdt.strftime("%B %Y")
out = " "*int((22-len(my))/2) + my
while len(out) < 22: out += " "
out = "%s%s%s\n" % (fmt.fb('black', 'green'), out, fmt.r)
# XXX How 'bout letting the week start on Sunday, right 'merica?
ds = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]
out += "%s %s %s\n" % (fmt.fb('blue', 'cyan'), " ".join(ds), fmt.r)
m = []
for i in range(wd): m.append(" ")
for day in self.keys():
m.append(self.dayr(day))
ws = len(m)/7
ws = int(ws) + 1 if int(ws) < ws else int(ws)
for spf in range(7-len(m[(ws-1)*7:7+(ws-1)*7])):
m.append(" ")
for week in range(ws):
out += "%s " % fmt.bf('blue', 'white')
for day in m[week*7:7+week*7]:
if "<" in day: out += day
else: out += "-%s-" % day
if fmt.c(out)[-1] != ">":
out += " "
out += "%s\n" % fmt.r
out = out.replace("--", " ")
out = out.replace("-", "")
out = out.replace(">%s%s<" % (fmt.bf('blue', 'white'),
fmt.bf('red', 'reset')), " ")
out = out.replace("%s %s<" % (fmt.bf('blue', 'white'),
fmt.bf('red', 'reset')),
"%s<" % fmt.bf('red', 'reset'))
out = out.replace("%s %s%s" % (fmt.bf('blue', 'white'),
fmt.s('underline'),
fmt.bf('red', 'reset')),
"%s%s" % (fmt.s('underline'),
fmt.bf('red', 'reset')))
# out = out.replace(">%s%s%s<"
return out.rstrip()
def split(self, char):
return repr(self).split(char)
def nextTo(one, two):
one = one.split('\n')
two = two.split('\n')
merge = ""
padding = len(fmt.c(one[0]))
[one.append(' '*padding) for i in range(len(two)-len(one))]
[two.append('') for i in range(len(one)-len(two))]
if len(one) == len(two):
for i in range(len(one)):
merge += "%s %s\n" % (one[i], two[i])
return merge.strip()
def icsin(inp):
from icalendar import cal as ical
if sys.stdin.isatty() and inp:
data = open(inp).read()
else:
data = sys.stdin.read()
cal = ical.Calendar.from_ical(data)
for comp in cal.walk():
if comp.name == "VEVENT":
codt = comp.decoded("DTSTART")
print(codt.strftime("%Y %m %d"), "00", comp.get("summary"))
sys.exit(0)
def icsout(inp):
from icalendar.cal import Calendar, Event
from hashlib import sha1
inp = os.path.expanduser(inp)
cal = Calendar()
cal.add('prodid', '-//ccal.py 0.5//niij.org//')
cal.add('version', '2.0')
entries = Entries(every=True, comm=True)
for entry in entries:
event = Event()
event.add('summary', entry.desc)
event.add('dtstart', entry.dt)
event.add('dtend', entry.dt+dt.timedelta(days=1))
event.add('dtstamp', dt.datetime.now())
uid = "%s%s%s %s" % (entry.dt.year, entry.dt.month, entry.dt.day, entry.desc)
event.add('uid', sha1(uid.encode('utf-8')).hexdigest())
if (entry.comm):
event.add('description', entry.comm.strip())
cal.add_component(event)
print(cal.to_ical().decode('utf-8'))
sys.exit(0)
def ls(bdt, pve=7, cmt=False, fp=os.path.expanduser('~/.cal.dat'), comm=False,
exp=True, eli=0, evo=False):
entries = Entries(bdt=bdt, fp=fp, comm=comm, exp=exp)
pvs = ""
if pve > 0:
if isinstance(fp, StringIO): fp.seek(0)
pvd = bdt[0] + dt.timedelta(days=-(bdt[0].day-2)+30)
pvs = repr(Entries(bdt=(None,pvd), fp=fp, exp=False))
if pvs:
pvs = "%s%s" % (pvd.strftime("\n %B --\n"),
'\n'.join(pvs.split('\n')[:pve]))
else:
pvs = ""
if evo:
return repr(entries)+pvs
cal = Calendar(bdt[0], bdt if len(bdt) > 1 else (bdt[0],), entries)
if eli:
entries.limit(eli)
return nextTo(cal, repr(entries)+pvs)
if __name__ == '__main__':
# If nothing is passed, assume ls --preview
if len(sys.argv) > 1:
parser = argparse.ArgumentParser('parent', add_help=False)
parser.add_argument('--version', action='version',
version='%(prog)s 0.1')
parser.add_argument('-c', action='store_true',
help='force colored output')
parser.add_argument('-d', '--data-file', metavar="FILE", nargs="?",
default="~/.cal.dat",
help="file to load appointments from " + \
"(default: ~/.cal.dat)")
parser.add_argument('-e', '--entries-only', action='store_true',
help="Suppress calendar month view")
parser = argparse.ArgumentParser(parents=[parser])
sub_p = parser.add_subparsers(help='actions')
p_ls = sub_p.add_parser('ls', help='list calendar entries')
p_ls.add_argument("-l", "--limit", nargs="?", default="24",
metavar="N",
help="limit event output to N events from today " + \
"or Nd days from today (default, if set: 24)")
p_ls.add_argument("-p", "--preview", type=int, nargs="?", default=7,
metavar="N",
help="preview next month's non-periodic entries " + \
"(default, if set: 7)")
p_ls.add_argument("-C", "--comments",
help='include comments in listing',
action='store_true')
p_ls.add_argument("-n", "--noperiodic",
help='do not expand periodic dates',
action='store_false')
p_ls.add_argument("date", nargs='*')
p_ics = sub_p.add_parser('ics',
help="icalendar to ccal conversion tools")
p_ics.add_argument('-i', metavar="FILE", nargs='?',
help="Read from stdin or file")
p_ics.add_argument('-o', action='store_true',
help="Output entire file as ics to stdout")
#p_add = sub_p.add_parser('add', help='add calendar entry')
#p_add.add_argument("date")
#p_add.add_argument("description")
#p_add.add_argument("comment", nargs="?")
#p_ia = sub_p.add_parser('ia', help='Interactive mode')
args = parser.parse_args()
if sys.argv[1] == 'ics' and sys.argv[2] == '-i':
icsin(args.i)
if sys.argv[1] == 'ics' and 'o' in args and args.o:
icsout(args.data_file)
if not 'date' in args or not args.date:
args.date = (today(),)
elif len(args.date) == 1:
args.date = (dt.date(int(args.date[-1]), today().month, 1),)
elif len(args.date) == 2:
try:
args.date = (dt.datetime.strptime("1 %s" % " ".join(args.date),
"%d %B %Y").date(),)
except:
args.date = (dt.datetime.strptime("1 %s" % " ".join(args.date),
"%d %b %Y").date(),)
elif len(args.date) > 2:
month = args.date[-2:][0]
year = args.date[-2:][1]
args.date = tuple([dt.datetime.strptime("%s %s %s" % \
(day, month, year),
"%d %B %Y").date() \
for day in args.date[:-2]])
evo = args.entries_only
if args.c:
fmt.colors = args.c
dates = args.date
if '-l' in sys.argv or '--limit' in sys.argv:
eli = args.limit or 24
else:
eli = 0
if '-p' in sys.argv or '--preview' in sys.argv:
pve = args.preview or 7
else:
pve = 0
data = args.data_file
if 'comments' in args:
comm = args.comments
else:
comm = False
if 'noperiodic' in args:
exp = args.noperiodic
else:
exp = True
else:
dates = (today(),)
pve = 7
data = "~/.cal.dat"
comm = False
exp = True
eli = 0
evo = False
# Override file data with stuff from stdin
if sys.stdin.isatty():
fp = os.path.expanduser(data)
else:
fp = sys.stdin
if not sys.stdin.isatty():
fp = StringIO(sys.stdin.read())
out = ls(bdt=dates, pve=pve, fp=fp, comm=comm, exp=exp, eli=eli, evo=evo)
print('')
if fmt.colors:
print(out)
else:
print(fmt.c(out))
print('')