2019-03-18 04:45:33 +00:00
|
|
|
#!/usr/bin/env python
|
2019-03-19 04:36:23 +00:00
|
|
|
# Scan the osm-vector directory, update the osm-vector-idx.json, add menu-defs
|
2019-03-18 04:45:33 +00:00
|
|
|
|
|
|
|
from geojson import Feature, Point, FeatureCollection, Polygon
|
|
|
|
import geojson
|
|
|
|
import json
|
|
|
|
import os
|
2019-03-19 04:36:23 +00:00
|
|
|
import sys
|
|
|
|
import fnmatch
|
2019-03-25 18:09:13 +00:00
|
|
|
import re
|
2019-03-18 04:45:33 +00:00
|
|
|
|
2019-03-25 18:09:13 +00:00
|
|
|
#IIAB_PATH='{{ iiab_dir }}'
|
2019-03-19 04:36:23 +00:00
|
|
|
IIAB_PATH='/etc/iiab'
|
|
|
|
if not IIAB_PATH in sys.path:
|
|
|
|
sys.path.append(IIAB_PATH)
|
|
|
|
from iiab_env import get_iiab_env
|
2019-03-18 04:45:33 +00:00
|
|
|
|
2019-03-25 18:09:13 +00:00
|
|
|
SCRIPT_DIR = '/opt/admin/cmdsrv/scripts'
|
|
|
|
if not SCRIPT_DIR in sys.path:
|
|
|
|
sys.path.append(SCRIPT_DIR)
|
|
|
|
import iiab_update_menus as update_menus
|
|
|
|
|
2019-03-19 04:36:23 +00:00
|
|
|
doc_root = get_iiab_env('WWWROOT')
|
|
|
|
menuDefs = doc_root + "/js-menu/menu-files/menu-defs/"
|
2019-03-25 18:09:13 +00:00
|
|
|
osm_vector_idx_dir = doc_root + "/common/assets/"
|
|
|
|
map_doc_root = '/library/osm-vector'
|
|
|
|
# map_catalog will be global, assumed always available
|
2019-03-19 04:36:23 +00:00
|
|
|
map_catalog = {}
|
|
|
|
map_menu_defs ={}
|
2019-03-25 18:09:13 +00:00
|
|
|
|
2019-03-18 04:45:33 +00:00
|
|
|
def main():
|
2019-03-19 04:36:23 +00:00
|
|
|
global map_menu_defs
|
|
|
|
get_map_catalog()
|
2019-03-25 18:09:13 +00:00
|
|
|
#print(json.dumps(map_catalog,indent=2))
|
2019-03-19 04:36:23 +00:00
|
|
|
map_menu_defs = get_menu_def_names()
|
2019-03-25 18:09:13 +00:00
|
|
|
#print(json.dumps(map_menu_defs,indent=2))
|
|
|
|
installed_maps = get_installed_maps()
|
|
|
|
print(installed_maps)
|
|
|
|
write_osm_vector_idx(installed_maps)
|
2019-03-19 04:36:23 +00:00
|
|
|
|
|
|
|
def get_map_catalog():
|
|
|
|
global map_catalog
|
|
|
|
#input_json = "{{ iiab_dir }}" + '/regions.json'
|
|
|
|
input_json = "/etc/iiab" + '/regions.json'
|
2019-03-18 04:45:33 +00:00
|
|
|
with open(input_json,'r') as regions:
|
|
|
|
reg_str = regions.read()
|
2019-03-19 04:36:23 +00:00
|
|
|
map_catalog = json.loads(reg_str)
|
|
|
|
#print(json.dumps(map_catalog,indent=2))
|
|
|
|
|
|
|
|
def get_menu_def_names(intended_use='map'):
|
|
|
|
menu_def_dict = {}
|
|
|
|
os.chdir(menuDefs)
|
|
|
|
for filename in os.listdir('.'):
|
|
|
|
if fnmatch.fnmatch(filename, '*.json'):
|
|
|
|
try:
|
|
|
|
with open(filename,'r') as json_file:
|
|
|
|
readstr = json_file.read()
|
|
|
|
data = json.loads(readstr)
|
|
|
|
except:
|
|
|
|
print("failed to parse %s"%filename)
|
|
|
|
print(readstr)
|
|
|
|
if data.get('intended_use','') != intended_use:
|
|
|
|
continue
|
2019-03-25 18:09:13 +00:00
|
|
|
map_name = data.get('name','')
|
|
|
|
if map_name != '':
|
|
|
|
menu_def_dict[data['name']] = menuDefs + filename
|
2019-03-19 04:36:23 +00:00
|
|
|
return menu_def_dict
|
2019-03-18 04:45:33 +00:00
|
|
|
|
2019-03-25 18:09:13 +00:00
|
|
|
def get_installed_maps():
|
|
|
|
installed = []
|
|
|
|
os.chdir(map_doc_root)
|
|
|
|
for filename in os.listdir('.'):
|
|
|
|
if fnmatch.fnmatch(filename, '??-osm-omt*'):
|
|
|
|
print filename
|
|
|
|
region = re.sub(r'^..-osm-omt_(.*)',r'\1',filename)
|
|
|
|
print(region)
|
|
|
|
installed.append(region)
|
|
|
|
return installed
|
|
|
|
|
|
|
|
def write_osm_vector_idx(installed_maps):
|
|
|
|
map_list = []
|
|
|
|
for map in installed_maps:
|
|
|
|
data_dict = map_catalog['regions'].get(map,'')
|
|
|
|
map_list.append(data_dict)
|
|
|
|
print(map,data_dict)
|
|
|
|
|
|
|
|
def create_menu_def(perma_ref,default_name,intended_use='map'):
|
|
|
|
if default_name.find('.') > -1:
|
|
|
|
default_name = default_name[:-5].replace('.','_') + '.json'
|
|
|
|
item = map_catalog[perma_ref]
|
|
|
|
if len(item.get('language','')) > 2:
|
|
|
|
lang = item['language'][:2]
|
|
|
|
else: # default to english
|
|
|
|
lang = 'en'
|
|
|
|
filename = lang + '-' + perma_ref + '.json'
|
|
|
|
# create a stub for this zim
|
|
|
|
menuDef = {}
|
|
|
|
default_logo = get_default_logo(perma_ref,lang)
|
|
|
|
menuDef["intended_use"] = "map"
|
|
|
|
menuDef["lang"] = lang
|
|
|
|
menuDef["logo_url"] = default_logo
|
|
|
|
menuitem = lang + '-' + perma_ref
|
|
|
|
menuDef["menu_item_name"] = default_name[:-5]
|
|
|
|
menuDef["title"] = item.get('title','')
|
|
|
|
menuDef["zim_name"] = perma_ref
|
|
|
|
menuDef["start_url"] = ''
|
|
|
|
menuDef["description"] = '<p>' + item.get('description','') + '</p>'
|
|
|
|
menuDef["description"] += '<p>Size: ##SIZE##, Articles: ##ARTICLE_COUNT##, Media: ##MEDIA_COUNT##, Tags; [##tags##], Language: ##language##, Date: ##zim_date##</p>'
|
|
|
|
menuDef["extra_html"] = ""
|
|
|
|
menuDef["automatically_generated"] = "true"
|
|
|
|
if not os.path.isfile(menuDefs + default_name): # logic to here can still overwrite existing menu def
|
|
|
|
print("creating %s"%menuDefs + default_name)
|
|
|
|
with open(menuDefs + default_name,'w') as menufile:
|
|
|
|
menufile.write(json.dumps(menuDef,indent=2))
|
|
|
|
return default_name[:-5]
|
|
|
|
|
2019-03-19 04:36:23 +00:00
|
|
|
def human_readable(num):
|
|
|
|
# return 3 significant digits and unit specifier
|
|
|
|
num = float(num)
|
|
|
|
units = [ '','K','M','G']
|
|
|
|
for i in range(4):
|
|
|
|
if num<10.0:
|
|
|
|
return "%.2f%s"%(num,units[i])
|
|
|
|
if num<100.0:
|
|
|
|
return "%.1f%s"%(num,units[i])
|
|
|
|
if num < 1000.0:
|
|
|
|
return "%.0f%s"%(num,units[i])
|
|
|
|
num /= 1000.0
|
2019-03-18 04:45:33 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|