cctool

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

commit
f533c476ba3c80e749aef890fc54b7963d1cdd8a
parent
a45726a5b6b4a738dba703197128510e3ffe8353
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-04-21 00:49
allow to merge entries by key

Diffstat

M cctool.py 25 ++++++++++++++++++++++++-

1 files changed, 24 insertions, 1 deletions


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

@@ -24,7 +24,6 @@
   24    24 # -	type conversion (especially dates)
   25    25 # -	filter/convert for valid fields
   26    26 # -	tests
   27    -1 # -	merge
   28    27 # -	filter
   29    28 
   30    29 import os
@@ -93,6 +92,25 @@ class MultiDict(OrderedDict):
   93    92 		else:
   94    93 			return sep.join(self[key])
   95    94 
   -1    95 	def update(self, other):
   -1    96 		for key in other:
   -1    97 			self[key] = list(set(self[key] + other[key]))
   -1    98 
   -1    99 
   -1   100 def merged(data, key):
   -1   101 	tmp = dict()
   -1   102 	missing = []
   -1   103 	for entry in data:
   -1   104 		if key in entry:
   -1   105 			tmp_key = str(entry[key])
   -1   106 			if tmp_key in tmp:
   -1   107 				tmp[tmp_key].update(entry)
   -1   108 			else:
   -1   109 				tmp[tmp_key] = entry
   -1   110 		else:
   -1   111 			missing.append(entry)
   -1   112 	return tmp.values() + missing
   -1   113 
   96   114 
   97   115 class Format(object):
   98   116 	"""Baseclass with an API similar to the marshal, pickle and json modules."""
@@ -328,6 +346,8 @@ if __name__ == '__main__':
  328   346 	parser.add_argument('--output', '-o', metavar='FILENAME')
  329   347 	parser.add_argument('--sort', '-s', metavar='SORTKEY',
  330   348 		help="sort entries by this field")
   -1   349 	parser.add_argument('--merge', '-m', metavar='MERGEKEY',
   -1   350 		help="merge entries by this field")
  331   351 	args = parser.parse_args()
  332   352 
  333   353 	if args.outformat is None and args.output is not None:
@@ -361,6 +381,9 @@ if __name__ == '__main__':
  361   381 			log.error(e)
  362   382 			sys.exit(1)
  363   383 
   -1   384 	if args.merge is not None:
   -1   385 		data = merged(data, key=args.merge)
   -1   386 
  364   387 	if args.sort is not None:
  365   388 		data = sorted(data, key=lambda x: x[args.sort])
  366   389