def write_zim_versions_idx(zim_versions, kiwix_library_xml, zim_version_idx_dir, zim_menu_defs): zims_installed, path_to_id_map = iiab.read_library_xml(kiwix_library_xml) # drives off of zim_versions which is what is in file system for perma_ref in zim_versions: zim_versions[perma_ref]['menu_item'] = zim_menu_defs.get( perma_ref, {}).get('name') articlecount, mediacount, size, tags, lang, date = \ get_substitution_data(perma_ref, zim_versions, zims_installed, path_to_id_map) zim_versions[perma_ref]['article_count'] = articlecount zim_versions[perma_ref]['media_count'] = mediacount size = iiab.human_readable(float(size) * 1024) # kiwix reports in K zim_versions[perma_ref]['size'] = size zim_versions[perma_ref]['tags'] = tags zim_versions[perma_ref]['language'] = lang zim_versions[perma_ref]['zim_date'] = date # Write Version Map if os.path.isdir(zim_version_idx_dir): with open(zim_version_idx_dir + CONST.zim_version_idx_file, 'w') as fp: fp.write(json.dumps(zim_versions, indent=2)) fp.close() else: print(zim_version_idx_dir + " not found.")
def main(): global download_source global zim_path args = parse_args() zim_fname = args.zim_file if args.path: # allow override of path zim_path = args.path if args.source: # allow override of download source download_source = args.source zim_full_path = zim_path + '/' + zim_fname if not os.path.isfile(zim_full_path): print('Zim file ' + zim_full_path + ' not found.') sys.exit(1) # create temp library.xml and read back json kiwix_library_xml = "/tmp/library.xml" try: os.remove(kiwix_library_xml) except OSError: pass iiab.add_libr_xml(kiwix_library_xml, zim_path, zim_fname, '') zims_installed, _ = iiab.read_library_xml(kiwix_library_xml, kiwix_exclude_attr=['']) zim_id = list(zims_installed)[0] name_parts = zim_fname.split('_') name = '_' if name_parts[-2] in ['mini', 'nopic', 'maxi']: flavour = '_' + name_parts[-2] name = name.join(name_parts[:-2]) else: flavour = '' name = name.join(name_parts[:-1]) url = download_source + zim_fname zims_installed[zim_id]['url'] = url zims_installed[zim_id]['name'] = name zims_installed[zim_id]['flavour'] = flavour zim_json = zim_fname.replace('.zim', '.json') adm.write_json_file(zims_installed, zim_json) try: os.remove(kiwix_library_xml) except OSError: pass sys.exit()
def main(): zim_path = iiab.CONST.zim_path zim_version_idx_dir = "" if adm_cons_installed: zim_version_idx_dir = adm.CONST.zim_version_idx_dir args = parse_args() # args.device is either value or None if args.device: # allow override of path zim_path = args.device + zim_path zim_version_idx_dir = args.device + zim_version_idx_dir kiwix_library_xml = zim_path + "/library.xml" if not args.no_tmp: # don't append .tmp kiwix_library_xml += ".tmp" # remove existing file if force if args.force: try: os.remove(kiwix_library_xml) except OSError: pass zims_installed = {} path_to_id_map = {} else: zims_installed, path_to_id_map = iiab.read_library_xml( kiwix_library_xml) zim_files, zim_versions = iiab.get_zim_list(zim_path) # Remove zims not in file system from library.xml remove_list_str = "" for item in path_to_id_map: if item not in zim_files: iiab.rem_libr_xml(path_to_id_map[item], kiwix_library_xml) # Add zims from file system that are not in library.xml for item in zim_files: if item not in path_to_id_map: iiab.add_libr_xml(kiwix_library_xml, zim_path, item, zim_files[item]) # Create zim_versions_idx if Admin Console installed if adm_cons_installed: print("Writing zim_versions_idx") iiab.read_lang_codes() # needed by following zim_menu_defs = adm.get_zim_menu_defs() # read all menu defs adm.write_zim_versions_idx(zim_versions, kiwix_library_xml, zim_version_idx_dir, zim_menu_defs) sys.exit()
def put_kiwix_enabled_into_menu_json(): # steps: # 1. Make sure all downloaded zims are in zim_verion_idx # 2. Look for a back link to perma_ref in menu_defs_dir # 3. If back link exist update, otherwise create new menuDef # check for un-indexed zims in zims/content/,write to zim_versions_idx.json iiab.read_lang_codes() # initialize zim_menu_defs = get_zim_menu_defs() # read all menu defs zim_files, zim_versions = iiab.get_zim_list(iiab.CONST.zim_path) write_zim_versions_idx(zim_versions, iiab.CONST.kiwix_library_xml, CONST.zim_version_idx_dir, zim_menu_defs) zims_installed, path_to_id_map = iiab.read_library_xml( iiab.CONST.kiwix_library_xml) # use that data zim_idx = CONST.zim_version_idx_dir + CONST.zim_version_idx_file if os.path.isfile(zim_idx): with open(zim_idx, "r") as zim_fp: zim_versions_info = json.loads(zim_fp.read()) for perma_ref in zim_versions_info: #print(perma_ref) # if other zims exist, do not add test zim if len(zim_versions_info) > 1 and perma_ref == 'tes': continue # check if menu def exists for this perma_ref menu_item_name = zim_menu_defs.get(perma_ref, {}).get('name') if menu_item_name == None: # no menu def points to this perma_ref # create the canonical menu_item name lang = zim_versions_info[perma_ref].get('language', 'en') new_def_name = lang + '-' + perma_ref new_def_name = new_def_name.replace( '.', '_') # handle embedded '.' if new_def_name in zim_menu_defs: # name already taken new_def_name += '1' # OK this could exist too, but really path = zim_versions[perma_ref].get('file_name') + '.zim' zim_id = path_to_id_map['content/' + path] zim_info = zims_installed[zim_id] new_menu_def = generate_zim_menu_def( perma_ref, new_def_name, zim_info) new_menu_def = format_menu_item_def( new_def_name, new_menu_def) print("Creating %s" % new_def_name) write_menu_item_def(new_def_name, new_menu_def, change_ref='generated') menu_item_name = new_def_name update_menu_json(menu_item_name) # add to menu