cctool

A tool for managing contacts and calendars.
git clone https://git.ce9e.org/cctool.git

commit
992cf7d7cb24a60616f5e3c0045ee06f2cb39146
parent
92302879fbdd641f96824fe272e2fbd40238fc70
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-04-20 23:53
allow to sort entries

Diffstat

M cctool.py 26 ++++++++++++++++++--------

1 files changed, 18 insertions, 8 deletions


diff --git a/cctool.py b/cctool.py

@@ -26,7 +26,6 @@
   26    26 # -	doc
   27    27 # -	tests
   28    28 # -	merge
   29    -1 # -	sort
   30    29 # -	filter
   31    30 
   32    31 import os
@@ -57,6 +56,8 @@ class MultiDict(OrderedDict):
   57    56 	"""Dict subclass with multiple values for each key.
   58    57 
   59    58 	>>> d = MultiDict()
   -1    59 	>>> d['foo']
   -1    60 	[]
   60    61 	>>> d['foo'] = []
   61    62 	>>> 'foo' in d
   62    63 	False
@@ -70,7 +71,14 @@ class MultiDict(OrderedDict):
   70    71 	"""
   71    72 
   72    73 	def __contains__(self, key):
   73    -1 		return super(MultiDict, self).__contains__(key) and self[key] != []
   -1    74 		return (super(MultiDict, self).__contains__(key)
   -1    75 			and super(MultiDict, self).__getitem__(key) != [])
   -1    76 
   -1    77 	def __getitem__(self, key):
   -1    78 		if key in self:
   -1    79 			return super(MultiDict, self).__getitem__(key)
   -1    80 		else:
   -1    81 			return []
   74    82 
   75    83 	def first(self, key, default=NOTSET):
   76    84 		if key in self:
@@ -80,15 +88,11 @@ class MultiDict(OrderedDict):
   80    88 		else:
   81    89 			raise KeyError
   82    90 
   83    -1 	def join(self, key, default=NOTSET, sep=u','):
   -1    91 	def join(self, key, default='', sep=u','):
   84    92 		if key in self and len(self[key]) == 1:
   85    93 			return self[key][0]
   86    -1 		elif key in self:
   87    -1 			return sep.join(self[key])
   88    -1 		elif default is not NOTSET:
   89    -1 			return default
   90    94 		else:
   91    -1 			raise KeyError
   -1    95 			return sep.join(self[key])
   92    96 
   93    97 
   94    98 class Format(object):
@@ -321,6 +325,8 @@ if __name__ == '__main__':
  321   325 	parser.add_argument('--to', '-t', choices=outformats.keys(), dest='outformat')
  322   326 	parser.add_argument('input', nargs='?')
  323   327 	parser.add_argument('--output', '-o')
   -1   328 	parser.add_argument('--sort', '-s', metavar='SORTKEY',
   -1   329 		help="sort entries by this field")
  324   330 	args = parser.parse_args()
  325   331 
  326   332 	if args.informat is None and args.input is not None:
@@ -350,6 +356,10 @@ if __name__ == '__main__':
  350   356 	except Exception as e:
  351   357 		log.error(e)
  352   358 		sys.exit(1)
   -1   359 
   -1   360 	if args.sort is not None:
   -1   361 		data = sorted(data, key=lambda x: x[args.sort])
   -1   362 
  353   363 	try:
  354   364 		outformats[args.outformat]().dump(data, outfile)
  355   365 	except Exception as e: