Changeset 37:abe2ef05ce80 in stamper
- Timestamp:
- Aug 12, 2014, 6:15:59 PM (10 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
bin/stamps
r32 r37 23 23 parser.add_argument('-d', '--delete', action="store", type=int, 24 24 help='Delete up to n recorded stamps') 25 parser.add_argument('-i', '--import', action="store", dest="import_file", 26 help='Import stamps from the given file') 25 27 26 28 args = parser.parse_args() … … 42 44 elif args.graph: 43 45 s.graph_stamps(args.customer, args.filter) 46 elif args.import_file: 47 s.import_stamps(args.import_file) 44 48 else: 45 49 s.show_stamps(args.customer, args.filter, args.verbose, args.sum) -
stamper/stamper.py
r36 r37 4 4 import pygal 5 5 from datetime import datetime, date, timedelta 6 from os.path import expanduser, exists, islink 6 from os.path import expanduser, exists, islink, isdir 7 7 from os import symlink, remove 8 8 from collections import OrderedDict 9 from operator import itemgetter 9 10 10 11 … … 24 25 self.stamps = [] 25 26 27 def __json_load(self, filename): 28 """ 29 Load the stamps from a file in json format, returning 30 the parsed list. 31 """ 32 with open(filename, 'r') as stamps_file: 33 try: 34 stamps = json.load(stamps_file) 35 except ValueError: 36 stamps = [] 37 return stamps 38 39 def remove_duplicates(self): 40 """ 41 Remove duplicated stamps from the stamps list 42 """ 43 stamps = [dict(t) for t in set( 44 [tuple(d.items()) for d in self.stamps])] 45 self.stamps = stamps 46 26 47 def ensure_stamps_file(self): 27 48 if not exists(self.stamps_file): … … 30 51 31 52 def load_stamps(self): 32 with open(self.stamps_file, 'r') as stamps_file: 33 try: 34 self.stamps = json.load(stamps_file) 35 except ValueError: 36 self.stamps = [] 53 self.stamps = self.__json_load(self.stamps_file) 54 55 def sort_stamps(self): 56 """ 57 Sort all the stamps by start and end dates 58 """ 59 self.stamps = sorted(self.stamps, key=itemgetter('start', 'end')) 37 60 38 61 def save_stamps(self): … … 346 369 break 347 370 self.save_stamps() 371 372 def import_stamps(self, filename): 373 """ 374 Import the stamps from the given file into the main stamps list, 375 merging them into the list (removing duplicated entries) 376 """ 377 if not exists(filename): 378 print('[error] ' + filename + 'does not exist') 379 return 380 if isdir(filename): 381 print('[error] ' + filename + 'is a directory') 382 return 383 stamps = self.__json_load(filename) 384 if not stamps: 385 print('[warning] no stamps can be imported from ' + filename) 386 return 387 self.stamps.extend(stamps) 388 self.remove_duplicates() 389 self.sort_stamps() 390 self.save_stamps() 391 print('[warning] ' + str(len(stamps)) + ' stamps merged') 392 print('[warning] remember to review the resulting stamps file')
Note:
See TracChangeset
for help on using the changeset viewer.