diff options
Diffstat (limited to 'chimere/utils.py')
| -rw-r--r-- | chimere/utils.py | 80 | 
1 files changed, 65 insertions, 15 deletions
| diff --git a/chimere/utils.py b/chimere/utils.py index ba5e558..55fc45c 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -1,6 +1,6 @@  #!/usr/bin/env python  # -*- coding: utf-8 -*- -# Copyright (C) 2012-2013  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2012-2015  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>  # This program is free software: you can redistribute it and/or modify  # it under the terms of the GNU General Public License as @@ -879,7 +879,35 @@ DATE_PARSINGS = {'fr_FR':[             re.compile(r'(?P<day1>\d{1,2}) '\                        r'(?P<month1>'+ '|'.join(UNI_MONTH_NAMES['fr_FR']) +') *'\                        r'(?P<year1>\d{4})?') -                     ] +                     ], +                 'en':[ +           re.compile(r'(?P<year1>\d{4})-'\ +                      r'(?P<month1>\d{2})-'\ +                      r'(?P<day1>\d{2})'\ +                      r'(?:T'\ +                          r'(?P<hour1>\d{2})?:'\ +                          r'(?P<minut1>\d{2})?:'\ +                          r'(?P<second1>\d{2})'\ +                      r')?.*'\ +                      r'(?P<year2>\d{4})-'\ +                      r'(?P<month2>\d{2})-'\ +                      r'(?P<day2>\d{2})'\ +                      r'(?:T'\ +                          r'(?P<hour2>\d{2})?:'\ +                          r'(?P<minut2>\d{2})?:'\ +                          r'(?P<second2>\d{2})'\ +                      r')?.*' +                      ), +           re.compile(r'(?P<year1>\d{4})-'\ +                      r'(?P<month1>\d{2})-'\ +                      r'(?P<day1>\d{2})'\ +                      r'(?:T'\ +                          r'(?P<hour1>\d{2})?:'\ +                          r'(?P<minut1>\d{2})?:'\ +                          r'(?P<second1>\d{2})'\ +                      r')?' +                      ) +                     ],              }  def clean_field(value): @@ -979,6 +1007,29 @@ class HtmlXsltManager(ImportManager):                      u'", "'.join(self.missing_cats))          return (self.new_item, self.updated_item, msg) +    @classmethod +    def _internal_parse_date(cls, locale, year, month, day): +        try: +            year = datetime.date.today().year if not year else int(year) +        except ValueError: +            return +        month = month.encode('utf-8') +        if locale in MONTH_NAMES and month in MONTH_NAMES[locale]: +            month = MONTH_NAMES[locale].index(month) + 1 +        else: +            try: +                month = int(month) +            except ValueError: +                return +        try: +            day = int(day) +        except ValueError: +            return +        try: +            return datetime.date(year, month, day) +        except ValueError: +            return +      def parse_date(self, date):          dct = {}          has_dates = False @@ -989,22 +1040,21 @@ class HtmlXsltManager(ImportManager):                  m = r.search(date)                  if not m:                      continue -                has_dates = True                  values = m.groupdict() -                year1 = datetime.date.today().year if 'year1' not in values \ -                        else int(values['year1']) -                dct['start_date'] = datetime.date(year1, -                  MONTH_NAMES[locale].index(values['month1'].encode('utf-8') -                                                                        ) + 1, -                  int(values['day1'])) +                date = self._internal_parse_date(locale, +                              'year1' in values and values['year1'], +                              values['month1'], values['day1']) +                if not date: +                    continue +                dct['start_date'] = date +                has_dates = True                  if 'day2' not in values:                      break -                year2 = datetime.date.today().year if 'year2' not in values \ -                        else int(values['year2']) -                dct['end_date'] = datetime.date(year2, -                  MONTH_NAMES[locale].index(values['month2'].encode('utf-8') -                                                                        ) + 1, -                  int(values['day2'])) +                date = self._internal_parse_date(locale, +                              'year2' in values and values['year2'], +                              values['month2'], values['day2']) +                if date: +                    dct['end_date'] = date                  break          return dct | 
