-#!/usr/bin/python2.7
-# -*- coding: iso-8859-15 -*-
+#!/usr/bin/python3.4
# $Id$
# $HeadURL$
"""Contains functions that maintain/update the cache tables."""
from sqlalchemy.sql import select
from sqlalchemy.sql.expression import func as sqlfunc
from osgeo import ogr
-import formencode
-from wrpylib import mwdb, wrmwdb, mwmarkup, wrmwmarkup
+from wrpylib import mwdb, wrmwdb, wrmwmarkup, wrvalidators
class UpdateCacheError(RuntimeError):
categorylinks = mwdb.categorylinks_table(metadata)
revision = mwdb.revision_table(metadata)
text = mwdb.text_table(metadata)
+ class Sledrun:
+ pass
transaction = connection.begin()
# Refill wrsledruncache table
for sledrun_page in sledrun_pages:
- try:
- start, end, sledrun = wrmwmarkup.rodelbahnbox_to_sledrun(sledrun_page.old_text)
+ try:
+ rodelbahnbox = wrvalidators.rodelbahnbox_from_str(sledrun_page.old_text)
+ sledrun = wrmwmarkup.sledrun_from_rodelbahnbox(rodelbahnbox, Sledrun())
sledrun.page_id = sledrun_page.page_id
sledrun.page_title = sledrun_page.page_title
- sledrun.under_construction = connection.execute(select([categorylinks], (categorylinks.c.cl_from==sledrun_page.page_id) & (categorylinks.c.cl_to == 'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above
+ sledrun.name_url = wrvalidators.sledrun_page_title_to_pretty_url(sledrun_page.page_title)
+ sledrun.under_construction = connection.execute(select([sqlfunc.count()], (categorylinks.c.cl_from==sledrun_page.page_id) & (categorylinks.c.cl_to == 'In_Arbeit')).alias('x')).fetchone()[0] > 0
connection.execute(wrsledruncache.insert(sledrun.__dict__))
- except (RuntimeError, formencode.Invalid) as e:
+ except ValueError as e:
transaction.rollback()
error_msg = "Error at sled run '{0}': {1}".format(sledrun_page.page_title, str(e))
raise UpdateCacheError(error_msg, sledrun_page.page_title, e)
categorylinks = mwdb.categorylinks_table(metadata)
revision = mwdb.revision_table(metadata)
text = mwdb.text_table(metadata)
+ class Inn:
+ pass
transaction = connection.begin()
# Refill wrinncache table
for inn_page in inn_pages:
- try:
- start, end, inn = wrmwmarkup.gasthausbox_to_inn(inn_page.old_text)
+ try:
+ gasthausbox = wrvalidators.gasthausbox_from_str(inn_page.old_text)
+ inn = wrmwmarkup.inn_from_gasthausbox(gasthausbox, Inn())
inn.page_id = inn_page.page_id
inn.page_title = inn_page.page_title
- inn.under_construction = connection.execute(select([categorylinks], (categorylinks.c.cl_from==inn_page.page_id) & (categorylinks.c.cl_to == 'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above
+ inn.under_construction = connection.execute(select([sqlfunc.count()], (categorylinks.c.cl_from==inn_page.page_id) & (categorylinks.c.cl_to == 'In_Arbeit')).alias('x')).fetchone()[0] > 0 # It would be better to do this in the query above
connection.execute(wrinncache.insert(inn.__dict__))
- except (RuntimeError, formencode.Invalid) as e:
+ except ValueError as e:
transaction.rollback()
error_msg = "Error as inn '{0}': {1}".format(inn_page.page_title, str(e))
raise UpdateCacheError(error_msg, inn_page.page_title, e)
>>> update_wrreportcache(engine.connect())
"""
metadata = schema.MetaData()
- wrreport = wrmwdb.wrreport_table(metadata)
wrreportcache = wrmwdb.wrreportcache_table(metadata)
transaction = connection.begin()
# Refill wrmappointcache and wrmappathcache tables
for sledrun_page in sledrun_pages:
- try:
- start, content, endtag, end = mwmarkup.find_tag(sledrun_page.old_text, 'wrmap')
- if content is None:
- continue
- geojson = wrmwmarkup.parse_wrmap(sledrun_page.old_text[start:end])
+ try:
+ import mwparserfromhell
+ wikicode = mwparserfromhell.parse(sledrun_page.old_text)
+ wrmap_list = wikicode.filter_tags(recursive=False, matches=lambda tag: tag.tag == 'wrmap')
+ if len(wrmap_list) == 0:
+ continue # not wrmap in page
+ if len(wrmap_list) > 1:
+ raise UpdateCacheError('{} <wrmap ...> entries found in article "{}"'.format(len(wrmap_list), sledrun_page.page_title))
+ wrmap = wrmap_list[0]
+ geojson = wrmwmarkup.parse_wrmap(str(wrmap))
for feature in geojson['features']:
properties = feature['properties']
# commit
transaction.commit()
-