Formatted python code using flake8 w/ default configuration

This commit is contained in:
Manish 2020-04-26 00:11:36 +10:00
parent 5e9ae7f2f0
commit 89f91410e9
5 changed files with 155 additions and 125 deletions

View File

@ -8,6 +8,7 @@ else:
PathSlash = '/' PathSlash = '/'
FileProtocol = "file:" + 2*PathSlash FileProtocol = "file:" + 2*PathSlash
def ConvertToStandardPathFormat(Path): def ConvertToStandardPathFormat(Path):
""" Example, """ Example,
Input: '"file:///some/path/somefile.extension" Input: '"file:///some/path/somefile.extension"
@ -18,6 +19,7 @@ def ConvertToStandardPathFormat(Path):
Path = Path[len(FileProtocol):] Path = Path[len(FileProtocol):]
return Path return Path
def GetTextAfter(Text, ReadlinesTextFile): def GetTextAfter(Text, ReadlinesTextFile):
for Lines in range(len(ReadlinesTextFile)): for Lines in range(len(ReadlinesTextFile)):
Line = ReadlinesTextFile[Lines].strip('\n') Line = ReadlinesTextFile[Lines].strip('\n')
@ -25,18 +27,21 @@ def GetTextAfter(Text, ReadlinesTextFile):
return Line[len(Text):] return Line[len(Text):]
return '' return ''
def SingleQuoteString(String): def SingleQuoteString(String):
if len(String) > 0: if len(String) > 0:
if String[0] != '\'' or String[-1] != '\'': if String[0] != '\'' or String[-1] != '\'':
String = '\'' + String + '\'' String = '\'' + String + '\''
return String return String
def DoubleQuoteString(String): def DoubleQuoteString(String):
if len(String) > 0: if len(String) > 0:
if String[0] != '"' or String[-1] != '"': if String[0] != '"' or String[-1] != '"':
String = '"' + String + '"' String = '"' + String + '"'
return String return String
def ListIntoString(List, QuoteItems=0, Seprator=' '): def ListIntoString(List, QuoteItems=0, Seprator=' '):
if QuoteItems == 2: if QuoteItems == 2:
for i in range(len(List)): for i in range(len(List)):
@ -49,36 +54,42 @@ def ListIntoString(List, QuoteItems=0, Seprator=' '):
Stringoflist = (Seprator).join(List) Stringoflist = (Seprator).join(List)
return Stringoflist return Stringoflist
# strip=0 => remove both ' & ", 1 => remove ', 2 => remove " # strip=0 => remove both ' & ", 1 => remove ', 2 => remove "
def UnquoteString(String, strip=0): def UnquoteString(String, strip=0):
while True: while True:
if ( if (
strip != 2 strip != 2 and
and String.startswith('"') String.startswith('"') and
and String.endswith('"')): String.endswith('"')
):
String = String.strip('"') String = String.strip('"')
elif ( elif (
strip != 1 strip != 1
and String.startswith("'") and String.startswith("'")
and String.endswith("'")): and String.endswith("'")
):
String = String.strip("'") String = String.strip("'")
else: else:
break break
return String return String
def StandardVariableName(Variable): def StandardVariableName(Variable):
Variable = Variable.casefold() Variable = Variable.casefold()
Variable = Variable.replace('_', '').replace(' ', '') Variable = Variable.replace('_', '').replace(' ', '')
return Variable return Variable
#def DictionaryToJsonStr(Dict, BaseIndentation=0): """
#BI = '\t'*BaseIndentation def DictionaryToJsonStr(Dict, BaseIndentation=0):
#JsonStr = BI+'{\n' BI = '\t'*BaseIndentation
#for k, v in Dict.items(): JsonStr = BI+'{\n'
#JsonStr += BI+'\t"'+k+'" : "'+v+'",\n' for k, v in Dict.items():
#JsonStr = JsonStr[:-2] JsonStr += BI+'\t"'+k+'" : "'+v+'",\n'
#JsonStr += '\n'+BI+'}' JsonStr = JsonStr[:-2]
#return JsonStr JsonStr += '\n'+BI+'}'
return JsonStr
"""
def StringToKeyValuePair(String, Seprator): def StringToKeyValuePair(String, Seprator):
SepratorAt = String.find(Seprator) SepratorAt = String.find(Seprator)
@ -89,14 +100,17 @@ def StringToKeyValuePair(String, Seprator):
else: else:
return "", String return "", String
def FormatStrForDictinary(String): def FormatStrForDictinary(String):
String = String.strip(" \n\r") String = String.strip(" \n\r")
return UnquoteString(String) return UnquoteString(String)
def StrListToDictionary( def StrListToDictionary(
List, List,
Seprator = '=', Seprator='=',
FormatFunction = FormatStrForDictinary): FormatFunction=FormatStrForDictinary
):
Dictionary = {} Dictionary = {}
for i in List: for i in List:
k, v = StringToKeyValuePair(i, Seprator) k, v = StringToKeyValuePair(i, Seprator)

View File

@ -2,6 +2,7 @@ from os.path import isfile as DoesFileExist
from .DataStructureManupulations import ConvertToStandardPathFormat from .DataStructureManupulations import ConvertToStandardPathFormat
def ReadTextFile(FilePath): def ReadTextFile(FilePath):
FilePath = ConvertToStandardPathFormat(FilePath) FilePath = ConvertToStandardPathFormat(FilePath)
if DoesFileExist(FilePath) is True: if DoesFileExist(FilePath) is True:
@ -11,11 +12,13 @@ def ReadTextFile(FilePath):
return ReadFile return ReadFile
else: else:
return '' return ''
def ReadlinesTextFile(FilePath): def ReadlinesTextFile(FilePath):
String = ReadTextFile(FilePath) String = ReadTextFile(FilePath)
return String.split('\n') return String.split('\n')
def WriteTextFiles(FilePath, Text): def WriteTextFiles(FilePath, Text):
if type(Text) != str: if type(Text) != str:
Text = '\n'.join(Text) Text = '\n'.join(Text)
@ -23,7 +26,8 @@ def WriteTextFiles(FilePath, Text):
File.write(Text) File.write(Text)
File.close() File.close()
def WriteBinaryToFile(Filepath, Data): def WriteBinaryToFile(Filepath, Data):
File=open(Filepath, 'wb') File = open(Filepath, 'wb')
File.write(Data) File.write(Data)
File.close() File.close()

View File

@ -3,6 +3,6 @@ from urllib.request import urlopen
HttpsContext = ssl.create_default_context() HttpsContext = ssl.create_default_context()
def Download(Url): def Download(Url):
return urlopen(Url, context=HttpsContext).read() return urlopen(Url, context=HttpsContext).read()

View File

@ -1,11 +1,13 @@
import os import os
def CallFuncInDir(Directory, Function, *args, **kwArgs): def CallFuncInDir(Directory, Function, *args, **kwArgs):
CurrentDir = os.getcwd() CurrentDir = os.getcwd()
os.chdir(Directory) os.chdir(Directory)
Function(*args, **kwArgs) Function(*args, **kwArgs)
os.chdir(CurrentDir) os.chdir(CurrentDir)
# return True if operation succesful and False if failed # return True if operation succesful and False if failed
def CreateDir(Directory): def CreateDir(Directory):
if not os.path.isfile(Directory): if not os.path.isfile(Directory):

View File

@ -6,10 +6,6 @@ All sub-directories of bundles directory will be scanned for activity
bundles i.e. .xo files. bundles i.e. .xo files.
""" """
""" FIXME: paths hard coded unix style & most likely will not work on winodws.
Use code written for IMM to handle it.
"""
import glob import glob
import json import json
import os import os
@ -19,8 +15,6 @@ from urllib.parse import quote as strToHtmlFmt
import zipfile import zipfile
from GeneralFunctions.DataStructureManupulations import ( from GeneralFunctions.DataStructureManupulations import (
GetTextAfter,
UnquoteString,
StrListToDictionary StrListToDictionary
) )
from GeneralFunctions.InputOutput import ( from GeneralFunctions.InputOutput import (
@ -32,15 +26,21 @@ from GeneralFunctions.OS import (
CreateDir CreateDir
) )
""" FIXME: paths hard coded unix style & most likely will not work on winodws.
Use code written for IMM to handle it.
"""
class extractData: class extractData:
def findInfoFiles(self, bundle): def findInfoFiles(self, bundle):
infoFiles = [] infoFiles = []
for File in bundle.namelist(): for File in bundle.namelist():
if File.endswith("activity/activity.info"): if File.endswith("activity/activity.info"):
infoFiles.append(File) infoFiles.append(File)
return infoFiles return infoFiles
def copyBundle(self, bundleSrc, activityName): def copyBundle(self, bundleSrc, activityName):
CallFuncInDir( CallFuncInDir(
self.websiteDir, self.websiteDir,
@ -48,31 +48,31 @@ class extractData:
self.bundlesDir+bundleSrc, self.bundlesDir+bundleSrc,
"bundles/"+activityName+".xo" "bundles/"+activityName+".xo"
) )
def createDirectories(self): def createDirectories(self):
assert(CreateDir("app")) assert(CreateDir("app"))
assert(CreateDir("icons")) assert(CreateDir("icons"))
assert(CreateDir("bundles")) assert(CreateDir("bundles"))
assert(CreateDir("js")) assert(CreateDir("js"))
def extractActivityInfo(self, infoFilePath, zipFile): def extractActivityInfo(self, infoFilePath, zipFile):
infoList, infoDict = [], {} infoList = []
infoList = zipFile.read( infoList = zipFile.read(
infoFilePath).decode("utf-8").split('\n') infoFilePath).decode("utf-8").split('\n')
return StrListToDictionary(infoList) return StrListToDictionary(infoList)
def extractInfoAndIconFromBundles(self): def extractInfoAndIconFromBundles(self):
for bundlePath in self.activityBundles: for bundlePath in self.activityBundles:
bundle = zipfile.ZipFile(bundlePath, "r") bundle = zipfile.ZipFile(bundlePath, "r")
infoFiles = self.findInfoFiles(bundle) infoFiles = self.findInfoFiles(bundle)
if len(infoFiles) != 1: if len(infoFiles) != 1:
self.bundlesNotExactlyOneInfoFile.append(bundlePath) self.bundlesNotExactlyOneInfoFile.append(bundlePath)
else: else:
infoDict = self.extractActivityInfo(infoFiles[0], bundle) infoDict = self.extractActivityInfo(infoFiles[0], bundle)
self.bundlesInfoList.append(infoDict) self.bundlesInfoList.append(infoDict)
# FIXME: create seprate function for it # FIXME: create seprate function for it
# extract and copy icon # extract and copy icon
activityName = infoDict.get("name") activityName = infoDict.get("name")
@ -85,9 +85,9 @@ class extractData:
if iconAbsolutePath in bundle.namelist(): if iconAbsolutePath in bundle.namelist():
icon = bundle.read(iconAbsolutePath) icon = bundle.read(iconAbsolutePath)
iconPath = ( iconPath = (
"icons/"+ "icons/" +
activityName activityName
+".svg" + ".svg"
) )
CallFuncInDir( CallFuncInDir(
self.websiteDir, self.websiteDir,
@ -98,115 +98,122 @@ class extractData:
else: else:
# Conitnue without icon since non-fatal error # Conitnue without icon since non-fatal error
self.iconErroredBundles.append(bundlePath) self.iconErroredBundles.append(bundlePath)
bundle.close() bundle.close()
# FIXME: uncomment below function. # FIXME: uncomment below function.
# Disabled sometime during devlopment as time consuming # Disabled sometime during devlopment as time consuming
self.copyBundle(bundlePath, activityName) self.copyBundle(bundlePath, activityName)
bundle.close() bundle.close()
def generateAppsHtmlPages(self): def generateAppsHtmlPages(self):
iconsDir = "../icons/" iconsDir = "../icons/"
bundlesDir = "../bundles/" bundlesDir = "../bundles/"
for appInfo in self.indexDictList: for appInfo in self.indexDictList:
pathName = strToHtmlFmt(appInfo["name"], safe='') pathName = strToHtmlFmt(appInfo["name"], safe='')
html = ( html = (
'<!DOCTYPE html>\n<html>\n<head>\n<title>'+appInfo["name"]+ '<!DOCTYPE html>\n<html>\n<head>\n<title>' + appInfo["name"] +
'</title>\n<meta charset="utf-8"/>\n<link rel="stylesheet" ' '</title>\n<meta charset="utf-8"/>\n<link rel="stylesheet" '
'type="text/css" href="../css/main.css"/>\n</head>\n<body>\n' 'type="text/css" href="../css/main.css"/>\n</head>\n<body>\n'
'</body>\n<h1>'+appInfo["name"]+'</h1>\n<p><img src="'+ '</body>\n<h1>' + appInfo["name"] + '</h1>\n<p><img src="' +
str(iconsDir+pathName+'.svg')+'"></img></p>\n<div id=summary>' str(iconsDir + pathName + '.svg') + '"></img></p>\n'
'<h2>Summary</h2>\n<p>'+appInfo["summary"]+ '<div id=summary><h2>Summary</h2>\n<p>' + appInfo["summary"] +
'</p>\n</div>\n<div id=description><h2>Description</h2>\n<p>'+ '</p>\n</div>\n<div id=description><h2>Description</h2>\n<p>' +
appInfo["description"]+'</p>\n</div>\n<div id=tags><h2>Tags' appInfo["description"] + '</p>\n</div>\n<div id=tags><h2>Tags'
'</h2>\n<ul>\n' '</h2>\n<ul>\n'
) )
for tag in appInfo["tags"]: for tag in appInfo["tags"]:
html += '<li>'+tag+'</li>\n' html += '<li>' + tag + '</li>\n'
html += ( html += (
'</ul>\n</div>\n<a href="'+str(bundlesDir+pathName+'.xo')+ '</ul>\n</div>\n<a href="' +
'"><h2>Download<h2></a>\n</body>\n</html>' str(bundlesDir + pathName + '.xo') +
'"><h2>Download<h2></a>\n</body>\n</html>'
) )
WriteTextFiles("./app/"+appInfo["name"]+".html", html) WriteTextFiles("./app/" + appInfo["name"] + ".html", html)
""" Only those which are specified in map will be added to index. """ Only those which are specified in map will be added to index.
If an entry or value does not exist in infoJSON than emprty entry will If an entry or value does not exist in infoJSON than emprty entry will
be created for it. be created for it.
appends keys rather than replacing where mutiple map to same appends keys rather than replacing where mutiple map to same
""" """
def generateIndex(self, def generateIndex(
infoToIndexMap = { self,
"name" : ("name", str), infoToIndexMap={
"summary" : ("summary", str), "name": ("name", str),
"description" : ("description", str), "summary": ("summary", str),
"tag" : ("tags", list), "description": ("description", str),
"tags" : ("tags", list), "tag": ("tags", list),
"categories" : ("tags", list), "tags": ("tags", list),
"category" : ("tags", list) "categories": ("tags", list),
}): "category": ("tags", list)
unexpectedInputError = ( }
"main.py generateIndex() : expect only str, list or tuple as " ):
"kwargs -> value[1] but found " unexpectedInputError = (
) "main.py generateIndex() : expect only str, list or tuple as "
"kwargs -> value[1] but found "
i2IMap = infoToIndexMap )
self.indexDictList = []
i2IMap = infoToIndexMap
for obj in json.loads(self.infoJson): self.indexDictList = []
indexDict = {}
for k, v in obj.items(): for obj in json.loads(self.infoJson):
if k in i2IMap: indexDict = {}
for k, v in obj.items():
# add new entry/key to app index if k in i2IMap:
if k not in indexDict:
if i2IMap[k][1] == str: # add new entry/key to app index
indexDict[i2IMap[k][0]] = v if k not in indexDict:
elif (i2IMap[k][1] == list if i2IMap[k][1] == str:
or i2IMap[k][1] == tuple): indexDict[i2IMap[k][0]] = v
if v.find(';') >= 0: elif (i2IMap[k][1] == list
indexDict[i2IMap[k][0]] = v.split(';') or i2IMap[k][1] == tuple):
else: if v.find(';') >= 0:
indexDict[i2IMap[k][0]] = v.split() indexDict[i2IMap[k][0]] = v.split(';')
else:
# Append to existing entry/key to app index indexDict[i2IMap[k][0]] = v.split()
else:
if i2IMap[k][1] == str: # Append to existing entry/key to app index
indexDict[i2IMap[k][0]] += ' '+v
elif (i2IMap[k][1] == list
or i2IMap[k][1] == tuple):
if v.find(';') >= 0:
indexDict[i2IMap[k][0]] += v.split(';')
else:
indexDict[i2IMap[k][0]] += v.split()
else: else:
print(unexpectedInputError, i2IMap[k][1]) if i2IMap[k][1] == str:
indexDict[i2IMap[k][0]] += ' '+v
elif (i2IMap[k][1] == list
or i2IMap[k][1] == tuple):
if v.find(';') >= 0:
indexDict[i2IMap[k][0]] += v.split(';')
else:
indexDict[i2IMap[k][0]] += v.split()
else:
print(unexpectedInputError, i2IMap[k][1])
sys.exit(1)
# Create entry/key with empty value for keys not present
# in activty.info
for k, v in i2IMap.items():
if v[0] not in indexDict:
if v[1] == str:
indexDict[v[0]] = ""
elif (v[1] == list or v[1] == tuple):
indexDict[v[0]] = ()
else:
print(unexpectedInputError, v[1])
sys.exit(1) sys.exit(1)
# Create entry/key with empty value for keys not present self.indexDictList.append(indexDict)
# in activty.info self.indexJs = (
for k ,v in i2IMap.items(): "search.assignIndex(" +
if v[0] not in indexDict: json.dumps(self.indexDictList, indent=4) +
if v[1] == str: ")"
indexDict[v[0]] = "" )
elif (v[1] == list or v[1] == tuple):
indexDict[v[0]] = ()
else:
print(unexpectedInputError, v[1])
sys.exit(1)
self.indexDictList.append(indexDict)
self.indexJs = "search.assignIndex("+json.dumps(self.indexDictList, indent=4)+")"
def generateInfoJson(self): def generateInfoJson(self):
self.infoJson = json.dumps(self.bundlesInfoList, indent=4) self.infoJson = json.dumps(self.bundlesInfoList, indent=4)
def __init__(self, bundlesDir, websiteDir): def __init__(self, bundlesDir, websiteDir):
""" FIXME: WARNING:: some files may be missing such as some app """ FIXME: WARNING:: some files may be missing such as some app
may not have icon (use a placeholder icon for them) may not have icon (use a placeholder icon for them)
Some bundles are not succesfully processed (html page + bundle copy) Some bundles are not succesfully processed (html page + bundle copy)
but are not in error logs as well. There are 495 bundles in but are not in error logs as well. There are 495 bundles in
Tony's repo, 479 sucessfully processed but showing fatal error of Tony's repo, 479 sucessfully processed but showing fatal error of
12 only, i.e. 4 missing. 12 only, i.e. 4 missing.
""" """
@ -220,27 +227,27 @@ class extractData:
self.indexDictList = [] self.indexDictList = []
self.erroredBundles = [] self.erroredBundles = []
self.iconErroredBundles = [] self.iconErroredBundles = []
CallFuncInDir(self.websiteDir, self.createDirectories) CallFuncInDir(self.websiteDir, self.createDirectories)
os.chdir(bundlesDir) os.chdir(bundlesDir)
self.activityBundles = glob.glob("**/*.xo", recursive=True) self.activityBundles = glob.glob("**/*.xo", recursive=True)
self.purgeBundlesNotZipFile() self.purgeBundlesNotZipFile()
self.extractInfoAndIconFromBundles() self.extractInfoAndIconFromBundles()
self.generateInfoJson() self.generateInfoJson()
self.generateIndex() self.generateIndex()
os.chdir(websiteDir) os.chdir(websiteDir)
self.generateAppsHtmlPages() self.generateAppsHtmlPages()
self.writeFiles() self.writeFiles()
#self.copyBundles() #self.copyBundles()
def purgeBundlesNotZipFile(self): def purgeBundlesNotZipFile(self):
activityBundles = [] activityBundles = []
@ -250,13 +257,13 @@ class extractData:
else: else:
self.bundlesNotZipFiles.append(bundle) self.bundlesNotZipFiles.append(bundle)
self.activityBundles = activityBundles self.activityBundles = activityBundles
def writeFiles(self): def writeFiles(self):
""" Files which are not continously written during the process """ Files which are not continously written during the process
Eg. Html, icon and bundles are written while processing each bundle Eg. Html, icon and bundles are written while processing each bundle
""" """
WriteTextFiles("info.json", self.infoJson) WriteTextFiles("info.json", self.infoJson)
WriteTextFiles("./js/index.js",self.indexJs) WriteTextFiles("./js/index.js", self.indexJs)
WriteTextFiles( WriteTextFiles(
"bundlesNotExactlyOneInfoFile.txt", "bundlesNotExactlyOneInfoFile.txt",
self.bundlesNotExactlyOneInfoFile self.bundlesNotExactlyOneInfoFile
@ -265,6 +272,7 @@ class extractData:
WriteTextFiles("erroredBundles.txt", self.erroredBundles) WriteTextFiles("erroredBundles.txt", self.erroredBundles)
WriteTextFiles("iconErroredBundles.txt", self.iconErroredBundles) WriteTextFiles("iconErroredBundles.txt", self.iconErroredBundles)
def processArguments(): def processArguments():
variables = {} variables = {}
if len(sys.argv) == 3: if len(sys.argv) == 3:
@ -282,9 +290,11 @@ def processArguments():
sys.exit(1) sys.exit(1)
return variables return variables
def main(): def main():
variables = processArguments() variables = processArguments()
extractData(variables["bundlesDir"], variables["websiteDir"]) extractData(variables["bundlesDir"], variables["websiteDir"])
if __name__ == "__main__": if __name__ == "__main__":
main(); main()