Changeset 7:7f7b4ef7c0c1 in stamper


Ignore:
Timestamp:
Jul 8, 2014, 10:38:01 AM (10 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Added method to parse a string and tell if it is a valid date-based
filter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stamper/stamper.py

    r0 r7  
    11
    22import json
     3import re
    34from datetime import datetime, timedelta
    45from os.path import expanduser, exists
     6
    57
    68STAMPS_FILE = expanduser('~/.workstamps.json')
     
    4951        return worktime.seconds
    5052
     53    def validate_filter(self, stamp_filter):
     54        """
     55        Validate a given filter. Filters can have the following notation:
     56
     57        - %Y-%m-%d--%Y-%m-%d: Times recorded between two dates
     58
     59        - -%Y-%m-%d: Times recorded before a given date
     60
     61        - +%Y-%m-%d: Times recorded after a given date
     62
     63        - N...N[d|w|m|y]: Times recorded N...N days/weeks/months/years ago
     64        """
     65        # First try the date filters, one by one
     66        matches = ['%Y-%m-%d', '-%Y-%m-%d', '+%Y-%m-%d']
     67        for match in matches:
     68            try:
     69                if '--' in stamp_filter:
     70                    filter_from, filter_to = stamp_filter.split('--')
     71                    filter_from = datetime.strptime(stamp_filter, match)
     72                    filter_to = datetime.strptime(stamp_filter, match)
     73                else:
     74                    valid_filter = datetime.strptime(stamp_filter, match)
     75            except ValueError:
     76                pass
     77            else:
     78                return stamp_filter
     79
     80        valid_filter = re.search(r'(\d+[dwmyDWMY]{1})', stamp_filter)
     81        if valid_filter:
     82            return stamp_filter
     83
     84        # Not a valid filter
     85        return None
     86
    5187    def customers(self):
    5288        customers = []
Note: See TracChangeset for help on using the changeset viewer.