#!/usr/bin/python3 """ Run as: python3 thisscript.py "/bundles/directoty" "/website/template/root/directory/ All sub-directories of bundles directory will be scanned for activity bundles i.e. .xo files. """ import glob import json import os import shutil import sys from urllib.parse import quote as strToHtmlFmt import zipfile from GeneralFunctions.DataStructureManupulations import ( StrListToDictionary ) from GeneralFunctions.InputOutput import ( WriteTextFiles, WriteBinaryToFile ) from GeneralFunctions.OS import ( CallFuncInDir, CreateDir ) """ FIXME: paths hard coded unix style & most likely will not work on winodws. Use code written for IMM to handle it. """ class extractData: def findInfoFiles(self, bundle): infoFiles = [] for File in bundle.namelist(): if File.endswith("activity/activity.info"): infoFiles.append(File) return infoFiles def copyBundle(self, bundleSrc, activityName): CallFuncInDir( self.websiteDir, shutil.copy2, self.bundlesDir+bundleSrc, "bundles/"+activityName+".xo" ) def createDirectories(self): assert(CreateDir("app")) assert(CreateDir("icons")) assert(CreateDir("bundles")) assert(CreateDir("js")) def extractActivityInfo(self, infoFilePath, zipFile): infoList = [] infoList = zipFile.read( infoFilePath).decode("utf-8").split('\n') return StrListToDictionary(infoList) def extractInfoAndIconFromBundles(self): for bundlePath in self.activityBundles: bundle = zipfile.ZipFile(bundlePath, "r") infoFiles = self.findInfoFiles(bundle) if len(infoFiles) != 1: self.bundlesNotExactlyOneInfoFile.append(bundlePath) else: infoDict = self.extractActivityInfo(infoFiles[0], bundle) self.bundlesInfoList.append(infoDict) # FIXME: create seprate function for it # extract and copy icon activityName = infoDict.get("name") if type(activityName) == str: iconRelativePath = infoDict.get("icon") if type(iconRelativePath) == str: iconFolder = infoFiles[0][:infoFiles[0].rfind("/")+1] iconAbsolutePath = ( iconFolder+iconRelativePath+".svg") if iconAbsolutePath in bundle.namelist(): icon = bundle.read(iconAbsolutePath) iconPath = ( "icons/" + activityName + ".svg" ) CallFuncInDir( self.websiteDir, WriteBinaryToFile, iconPath, icon ) else: # Conitnue without icon since non-fatal error self.iconErroredBundles.append(bundlePath) bundle.close() # FIXME: uncomment below function. # Disabled sometime during devlopment as time consuming self.copyBundle(bundlePath, activityName) bundle.close() def generateAppsHtmlPages(self): iconsDir = "../icons/" bundlesDir = "../bundles/" for appInfo in self.indexDictList: pathName = strToHtmlFmt(appInfo["name"], safe='') html = ( '\n\n
\n' + appInfo["summary"] + '
\n' + appInfo["description"] + '
\n