Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/python # -*- coding: utf-8 -*- Create the monuments tables SQL from monuments_config.json files
Author: Platonides """
""" Process the country configs to create sql files.
@param countryconfig: country configuration """ # @todo: standardise these as 'monuments_{country}_({lang})' table = country_config.get('table')
try: if country_config.get('type') == 'sparql': sql = process_wikidata_config(country_config) else: sql = process_classic_config(country_config) except Exception as e: raise Exception( '{exception} for countrycode: {country}, lang: {lang}'.format( exception=e, country=country_config.get('country'), lang=country_config.get('lang')))
f = open(os.path.join(get_sql_dir(), 'create_table_{}.sql'.format(table)), 'w', encoding='utf-8') f.write(sql) f.close()
""" Process a country configuration for wikitext lists.
@param country_config: country configuration @return sql """
# primkey in config refers to the destination primkey = column
'`{}` double DEFAULT NULL,'.format(column)) else: else: field.get('default')) else:
except Exception: raise
",\n" "KEY `latitude` (`lat`),\n" "KEY `longitude` (`lon`)")
table=country_config['table'], rows='\n '.join(fields_sql), primkey=primkey, extra_keys=extra_keys)
""" Validate that the primkey was found or construct it from list.
@param source_primkey: the config data on the primkey @param primkey: the primkey, if matched @return primkey @raises Exception """ if not primkey: if source_primkey and not isinstance(source_primkey, str): primkey = "`,`".join(source_primkey) else: raise Exception('Primary key not found') return primkey
"""Fetch the SQL template for a wikidata config.""" filename = 'classic_table.sql.template' with open(os.path.join(get_template_dir(), filename), 'r') as f: sql = f.read() return sql
""" Process a country configuration for wikidata sparql queries.
@param country_config: country configuration @return sql """ sql = load_wikidata_template_sql().format( table=country_config['table']) return sql
"""Fetch the SQL template for a wikidata config.""" filename = 'wikidata_table.sql.template' with open(os.path.join(get_template_dir(), filename), 'r') as f: sql = f.read() return sql
"""Fetch the SQL template for a wikidata config.""" return os.path.join( os.path.dirname(os.path.abspath(__file__)), 'sql')
"""Fetch the SQL template for a wikidata config.""" return os.path.join( os.path.dirname(os.path.abspath(__file__)), 'template')
for (countrycode, lang), countryconfig in mconfig.countries.items(): process_country(countryconfig)
try: main() finally: # pywikibot.stopme() pass |