def to_bool(value):
if not value: return None
- if value == 'Ja': return True
- if value == 'Nein': return False
- raise Exception('%s is not a valid boolean value, use one of "Ja" or "Nein"' % value)
+ if value == u'Ja': return True
+ if value == u'Nein': return False
+ raise Exception(u"'%s' is not a valid boolean value, use one of 'Ja' or 'Nein'" % value)
def to_geo(value):
"""Formats to coordinates '47.076207 N 11.453553 E' to the (latitude, longitude) tuplet."""
if not value: return (None, None)
- r = re.match('(\d+\.\d+) N (\d+\.\d+) E', value)
- if r is None: raise Exception("Coordinates '%s' have not a format like '47.076207 N 11.453553 E'")
+ r = re.match(u'(\d+\.\d+) N (\d+\.\d+) E', value)
+ if r is None: raise Exception(u"Coordinates '%s' have not a format like '47.076207 N 11.453553 E'" % value)
return (float(r.groups()[0]), float(r.groups()[1]))
def to_title(value):
- # Line 2237 of includes/Title.php says: $this->mTextform = str_replace( '_', ' ', $dbkey );
- # No not check for None because a missing title is an error
- return value.replace('_', ' ')
+ """Line 2237 of includes/Title.php says: $this->mTextform = str_replace( '_', ' ', $dbkey );
+ No not check for None because a missing title is an error"""
+ return value.replace(u'_', u' ')
+
+def unicode_e(exception):
+ """Does "unicode(exception)" as it should be. This is a workaround for bug http://bugs.python.org/issue2517
+ that is not fixed in python 2.5.2.
+ Details of bug: "unicode(Exception(u'\xe4'))" raises an UnicodeEncodeError exception."""
+ if exception.message: return unicode(exception.message)
+ return unicode(exception)
# Sledding run
"Like 'to_bool' but adds the field name to the exception description"
try: return to_bool(value)
except Exception, e:
- raise Exception("Error converting to bool in field %s: %s" % (fieldname, e.message))
+ raise Exception(u"Error converting to bool in field '%s': %s" % (fieldname, unicode_e(e)))
def to_geo_f(value, fieldname):
"Like 'to_geo' but adds the field name to the exception description"
try: return to_geo(value)
except Exception, e:
- raise Exception("Error converting to geo-coordinates in field %s: %s" % (fieldname, e.message))
+ raise Exception(u"Error converting to geo-coordinates in field '%s': %s" % (fieldname, unicode_e(e)))
def process_row(row):
"It converts a database row to a dictionary and performs checks."
old_text = unicode(old_text, 'UTF-8')
# Initialize property dict
property_keys = [
- 'page_id',
- 'page_title',
- 'length',
- 'walktime',
- 'height_top',
- 'height_bottom',
- 'walkup_separate',
- 'lift',
- 'night_light',
- 'sledge_rental',
- 'public_transport',
- 'image',
- 'position_latitude',
- 'position_longitude',
- 'information',
- 'show_in_overview',
- 'creation_date']
+ u'page_id',
+ u'page_title',
+ u'length',
+ u'walktime',
+ u'height_top',
+ u'height_bottom',
+ u'walkup_separate',
+ u'lift',
+ u'night_light',
+ u'sledge_rental',
+ u'public_transport',
+ u'image',
+ u'position_latitude',
+ u'position_longitude',
+ u'information',
+ u'show_in_overview',
+ u'creation_date']
properties = dict()
for property in property_keys:
properties[property] = None
# Match Rodelbahnbox
match = regexp.search(old_text)
if not match:
- raise Exception("No 'Rodelbahnbox' found")
+ raise Exception(u"No 'Rodelbahnbox' found")
box = match.group(1)
# Process Rodelbahnbox
for property in box.split('|'):
property = property.strip()
- if property == 'Rodelbahnbox': continue
+ if property == u'Rodelbahnbox': continue
key_value = property.split('=')
if len(key_value) != 2:
- raise Exception("Property '%s' has unexpected format" % key_value)
+ raise Exception(u"Property '%s' has unexpected format" % key_value)
key = key_value[0].strip()
value = key_value[1].strip()
- if key == 'Rodelbahnnummer': pass
- elif key == u'Länge' and value: properties['length'] = int(value)
- elif key == u'Gehzeit' and value: properties['walktime'] = int(value)
- elif key == u'Höhe oben' and value: properties['height_top'] = int(value)
- elif key == u'Höhe unten' and value: properties['height_bottom'] = int(value)
- elif key == u'Aufstieg getrennt': properties['walkup_separate'] = to_bool_f(value, u'Aufstieg getrennt')
+ if key == u'Rodelbahnnummer': pass
+ elif key == u'Länge' and value: properties[u'length'] = int(value)
+ elif key == u'Gehzeit' and value: properties[u'walktime'] = int(value)
+ elif key == u'Höhe oben' and value: properties[u'height_top'] = int(value)
+ elif key == u'Höhe unten' and value: properties[u'height_bottom'] = int(value)
+ elif key == u'Aufstieg getrennt': properties[u'walkup_separate'] = to_bool_f(value, u'Aufstieg getrennt')
elif key == u'Lift': properties['lift'] = to_bool_f(value, u'Lift')
- elif key == u'Beleuchtung': properties['night_light'] = to_bool_f(value, u'Beleuchtung')
- elif key == u'Rodelverleih': properties['sledge_rental'] = to_bool_f(value, u'Rodelverleih')
- elif key == u'Öffentliche Anreise': properties['public_transport'] = to_bool_f(value, u'Öffentliche Anreise')
- elif key == u'Bild': properties['image'] = value
- elif key == u'Position': (properties['position_latitude'], properties['position_longitude']) = to_geo_f(value, u'Position') # '47.583333 N 15.75 E'
- elif key == u'Auskunft': properties['information'] = value
- elif key == u'In Übersichtskarte': properties['show_in_overview'] = to_bool_f(value, u'In Übersichtskarte')
- elif key == u'Aufnahmedatum': properties['creation_date'] = value # '2006-03-15'
- properties['page_id'] = page_id
- properties['page_title'] = to_title(page_title)
- properties['under_construction'] = under_construction
- del properties['creation_date'] # this is not saved in the database yet
+ elif key == u'Beleuchtung': properties[u'night_light'] = to_bool_f(value, u'Beleuchtung')
+ elif key == u'Rodelverleih': properties[u'sledge_rental'] = to_bool_f(value, u'Rodelverleih')
+ elif key == u'Öffentliche Anreise': properties[u'public_transport'] = to_bool_f(value, u'Öffentliche Anreise')
+ elif key == u'Bild': properties[u'image'] = value
+ elif key == u'Position': (properties[u'position_latitude'], properties[u'position_longitude']) = to_geo_f(value, u'Position') # '47.583333 N 15.75 E'
+ elif key == u'Auskunft': properties[u'information'] = value
+ elif key == u'In Übersichtskarte': properties[u'show_in_overview'] = to_bool_f(value, u'In Übersichtskarte')
+ elif key == u'Aufnahmedatum': properties[u'creation_date'] = value # '2006-03-15'
+ properties[u'page_id'] = page_id
+ properties[u'page_title'] = to_title(page_title)
+ properties[u'under_construction'] = under_construction
+ del properties[u'creation_date'] # this is not saved in the database yet
return properties
cuo = conn.cursor() # cursor for output (out of the database)
cui = conn.cursor() # cursor for input (into the database)
- sql = "select page_id, rev_id, old_id, page_title, old_text, 'In_Arbeit' in (select cl_to from categorylinks where cl_from=page_id) as under_construction from page, revision, %(pagecontent)s, categorylinks where page_latest=rev_id and old_id=rev_text_id and cl_from=page_id and cl_to='Rodelbahn' order by page_title" % {'pagecontent': "text"}
+ sql = u"select page_id, rev_id, old_id, page_title, old_text, 'In_Arbeit' in (select cl_to from categorylinks where cl_from=page_id) as under_construction from page, revision, text, categorylinks where page_latest=rev_id and old_id=rev_text_id and cl_from=page_id and cl_to='Rodelbahn' order by page_title"
cuo.execute(sql)
- regexp = re.compile("\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL)
+ regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL)
try:
sledding_list = [];
for row in cuo:
try:
+ page_id = row[0]
+ page_title = unicode(row[3], 'UTF-8')
properties = process_row(row)
except Exception, e:
- raise Exception(e.message + '. Seite "%s" (page_id %d).' % (row[3], row[0]))
+ raise Exception(unicode_e(e) + u". Seite '%s' (page_id %d)." % (page_title, page_id))
sledding_list.append(properties)
- sql = 'delete from wrsleddingcache'
+ sql = u'delete from wrsleddingcache'
cui.execute(sql)
if len(sledding_list) > 0:
columns = sledding_list[0].keys()
- sql = 'insert into wrsleddingcache (' + ', '.join(columns) + ') values '
+ sql = u'insert into wrsleddingcache (' + ', '.join(columns) + ') values '
sql = sql + '(' + ', '.join(['%s' for c in columns]) + ')'
for sledding in sledding_list:
cui.execute(sql, sledding.values())