#!/usr/bin/python # -*- coding: utf-8 -*- from __future__ import unicode_literals import codecs def parse(entry): text = '' lines = entry.strip().split('\n') country, _, lang = lines.pop(0).strip('/* ').partition(' in ') lines.pop(0) # REPLACE INTO lines.pop(0) # SELECT text += ',\n "sql_lang": "{}"'.format(lang) text += ',\n "sql_country": "{}"'.format(country) text += ',\n "sql_data": {' template = '\n "%s": {\n "value": "%s",\n "type": "%s"\n },' country_code = lang_code = where = None for l in lines: l = l.strip() if not l: continue if l.startswith('WHERE'): where = l[len('WHERE '):].rstrip(';') continue l = l.split(' /* ')[0] val, _, label = l.rstrip(',').partition(' AS ') label = label.strip('`') typ = None if val.startswith("'"): val = val.strip("'") typ = 'Text' elif val.startswith("`"): val = val.strip("`") typ = 'Field' else: typ = 'Raw' text += template % (label, val, typ) if label == 'country': country_code = val elif label == 'lang': lang_code = val text = text.rstrip(',') + '\n }' if where: text += ',\n "sql_where": "{}"'.format(where) filename = '{}_{}.json'.format(country_code, lang_code) return text, filename def load_sql(): f = codecs.open('sql/temp.sql', 'r', 'utf-8') return f.read().split('ยค') def mod_json(text, filename): f = codecs.open('monuments_config/{}'.format(filename), 'r', 'utf-8') j = f.read().rstrip() f.close() j = j[:-2] + text + j[-2:] f = codecs.open('monuments_config/{}'.format(filename), 'w', 'utf-8') f.write(j) f.close() def run_all(): silly_map = { 'dk-bygninger_da.json': 'dk-bygning_da.json', 'dk-fortidsminder_da.json': 'dk-fortids_da.json', 'es_gl.json': 'es-gl_gl.json' } entries = load_sql() for entry in entries: if not entry.strip(): continue text, filename = parse(entry) if filename in silly_map: filename = silly_map[filename] mod_json(text, filename) if __name__ == "__main__": run_all()