summaryrefslogtreecommitdiff
path: root/chimere/utils.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@peacefrogs.net>2013-11-01 01:41:34 +0100
committerÉtienne Loks <etienne.loks@peacefrogs.net>2013-11-01 01:41:34 +0100
commitc023e15be620717339a780b6f4960649a9952afe (patch)
tree23553ee85b79acb6c1b4d9c6ae29ad467a22800f /chimere/utils.py
parent834673fa592f86bdf4d2a62b19d1f8081df4ae17 (diff)
downloadChimère-c023e15be620717339a780b6f4960649a9952afe.tar.bz2
Chimère-c023e15be620717339a780b6f4960649a9952afe.zip
HTML-XSLT import: parse dates of events
Diffstat (limited to 'chimere/utils.py')
-rw-r--r--chimere/utils.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/chimere/utils.py b/chimere/utils.py
index 8c8624e..1985f89 100644
--- a/chimere/utils.py
+++ b/chimere/utils.py
@@ -845,6 +845,35 @@ RE_CLEANS = ((re.compile('(\n)*|^( )*(\n)*( )*|( )*(\n)*( )*$'), ''),
'<a href="%(base_url)s\\1"'),
)
+from calendar import TimeEncoding, month_name
+
+def get_month_name(month_no, locale):
+ with TimeEncoding(locale) as encoding:
+ s = month_name[month_no]
+ if encoding is not None:
+ s = s.decode(encoding)
+ return s
+
+MONTH_NAMES = {locale:[get_month_name(no_month, locale+'.UTF-8')
+ for no_month in xrange(1, 13)] for locale in ['fr_FR']}
+
+UNI_MONTH_NAMES = {locale:[m.decode('utf-8') for m in MONTH_NAMES[locale]]
+ for locale in MONTH_NAMES}
+
+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})?[^\d]*'\
+ r'(?P<day2>\d{1,2}) '\
+ r'(?P<month2>'+ '|'.join(UNI_MONTH_NAMES['fr_FR']) +') *'\
+ r'(?P<year2>\d{4})?.*'),
+ re.compile(r'(?P<day1>\d{1,2}) '\
+ r'(?P<month1>'+ '|'.join(UNI_MONTH_NAMES['fr_FR']) +') *'\
+ r'(?P<year1>\d{4})?')
+ ]
+ }
+
+
class HtmlXsltManager(ImportManager):
def get(self):
u"""
@@ -942,7 +971,32 @@ class HtmlXsltManager(ImportManager):
else:
dct['point'] = self.importer_instance.default_localisation
dct['description'] = item['description']
+ if 'date' in item:
+ has_dates = False
+ for locale in DATE_PARSINGS:
+ if has_dates:
+ break
+ for r in DATE_PARSINGS[locale]:
+ m = r.search(item['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']))
+ 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']))
+ break
key = item['key']
+ print dct
it, updated, created = self.create_or_update_item(cls, dct, key)
if updated:
updated_item += 1