Changeset 37:abe2ef05ce80 in stamper for stamper


Ignore:
Timestamp:
Aug 12, 2014, 6:15:59 PM (10 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Added feature to import stamps from a file:

stamps -i /path/to/stamps.json

The stamps file is read, stamps are loaded and then merged into the
main stamps stream, keeping proper order (by start, end dates)
and removing duplicated entries.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stamper/stamper.py

    r36 r37  
    44import pygal
    55from datetime import datetime, date, timedelta
    6 from os.path import expanduser, exists, islink
     6from os.path import expanduser, exists, islink, isdir
    77from os import symlink, remove
    88from collections import OrderedDict
     9from operator import itemgetter
    910
    1011
     
    2425        self.stamps = []
    2526
     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
    2647    def ensure_stamps_file(self):
    2748        if not exists(self.stamps_file):
     
    3051
    3152    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'))
    3760
    3861    def save_stamps(self):
     
    346369                break
    347370        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.