diff options
-rw-r--r-- | lib/myconfig.py | 15 | ||||
-rw-r--r-- | reduce.py | 50 |
2 files changed, 65 insertions, 0 deletions
diff --git a/lib/myconfig.py b/lib/myconfig.py index b35400f..29d7ae3 100644 --- a/lib/myconfig.py +++ b/lib/myconfig.py @@ -58,3 +58,18 @@ class MyConfig: def getStatusPostfix(self): return m_status_postfix + + m_index_postfix = '.index' + + def getIndexPostfix(self): + return m_index_postfix + + m_text_postfix = '.text' + + def getTextPostfix(self): + return m_text_postfix + + m_final_model_file_name = 'interpolation.text' + + def getFinalModelFileName(self): + return m_final_model_file_name diff --git a/reduce.py b/reduce.py new file mode 100644 index 0000000..1aa0f39 --- /dev/null +++ b/reduce.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 +import os +import os.path +import sys +from myconfig import MyConfig + + +config = MyConfig() + +def read_file(infile): + with open(infile, 'r') as f: + data = ''.join(f.readlines()) + f.close() + return data + +def handleError(error): + sys.exit(error) + +def mergeSubIndex(output, path): + for root, dirs, files in os.walk(path, topdown=True, onerror=handleError): + for onefile in files: + filepath = os.path.join(root, onefile) + if onefile.endswith(config.getIndexPostfix()): + data = read_file(filepath) + output.writelines([data, os.linesep]) + else: + print('Unexpected file:' + filepath) + + +def iterateSubDirectory(oldroot, newroot, level): + #Merge the index in oldroot + if level <= 0 : + newindex = newroot + config.getIndexPostfix() + os.makedirs(os.path.dirname(newindex), exist_ok=True) + newindexfile = open(newindex, 'a') + mergeSubIndex(newindexfile, oldroot) + newindexfile.close() + #Recursive into the sub directories + for onedir in os.listdir(oldroot): + olddir = os.path.join(oldroot, onedir) + newdir = os.path.join(newroot, onedir) + if not os.path.isdir(olddir): + sys.exit('Un-expected file:' + olddir) + else: + iterateSubDirectory(olddir, newdir, level - 1) + + +#test case +if __name__ == '__main__': + pass |