def saveModules(where): with modulesLock: for i in ActiveModules: #Iterate over all of the resources in a module and save them as json files #under the URL urld module name for the filename. for resource in ActiveModules[i]: #Make sure there is a directory at where/module/ util.ensure_dir(os.path.join(where, url(i), url(resource))) #Open a file at /where/module/resource with open(os.path.join(where, url(i), url(resource)), "w") as f: #Make a json file there and prettyprint it json.dump(ActiveModules[i][resource], f, sort_keys=True, indent=4, separators=(',', ': ')) #Now we iterate over the existing resource files in the filesystem and delete those that correspond to #modules that have been deleted in the ActiveModules workspace thing. for i in util.get_immediate_subdirectories( os.path.join(where, url(i))): if unurl(i) not in ActiveModules: os.remove(os.path.join(where, url(i), i)) for i in util.get_immediate_subdirectories(where): #Look in the modules directory, and if the module folder is not in ActiveModules\ #We assume the user deleted the module so we should delete the save file for it. #Note that we URL url file names for the module filenames and foldernames. if unurl(i) not in ActiveModules: shutil.rmtree(os.path.join(where, i)) with open(os.path.join(where, '__COMPLETE__'), 'w') as f: f.write( "By this string of contents quite arbitrary, I hereby mark this dump as consistant!!!" )
def saveModules(where): with modulesLock: for i in ActiveModules: #Iterate over all of the resources in a module and save them as json files #under the URL urld module name for the filename. for resource in ActiveModules[i]: #Make sure there is a directory at where/module/ util.ensure_dir(os.path.join(where,url(i),url(resource)) ) #Open a file at /where/module/resource with open(os.path.join(where,url(i),url(resource)),"w") as f: #Make a json file there and prettyprint it json.dump(ActiveModules[i][resource],f,sort_keys=True,indent=4, separators=(',', ': ')) #Now we iterate over the existing resource files in the filesystem and delete those that correspond to #modules that have been deleted in the ActiveModules workspace thing. for i in util.get_immediate_subdirectories(os.path.join(where,url(i))): if unurl(i) not in ActiveModules: os.remove(os.path.join(where,url(i),i)) for i in util.get_immediate_subdirectories(where): #Look in the modules directory, and if the module folder is not in ActiveModules\ #We assume the user deleted the module so we should delete the save file for it. #Note that we URL url file names for the module filenames and foldernames. if unurl(i) not in ActiveModules: shutil.rmtree(os.path.join(where,i)) with open(os.path.join(where,'__COMPLETE__'),'w') as f: f.write("By this string of contents quite arbitrary, I hereby mark this dump as consistant!!!")
def load_modules_from_zip(f): "Given a zip file, import all modules found therin." new_modules = {} z = zipfile.ZipFile(f) for i in z.namelist(): #get just the folder, ie the module p = unurl(i.split('/')[0]) #Remove the.json by getting rid of last 5 chars n = unurl((i.split('/'))[1][:-5]) if p not in new_modules: new_modules[p] = {} f = z.open(i) new_modules[p][n] = json.loads(f.read().decode()) f.close() with modulesLock: for i in new_modules: if i in ActiveModules: raise cherrypy.HTTPRedirect("/errors/alreadyexists") for i in new_modules: ActiveModules[i] = new_modules[i] messagebus.postMessage("/system/notifications","User "+ pages.getAcessingUser() + " uploaded module" + i + " from a zip file") bookkeeponemodule(i) z.close()
def load_modules_from_zip(f): "Given a zip file, import all modules found therin." new_modules = {} z = zipfile.ZipFile(f) for i in z.namelist(): #get just the folder, ie the module p = unurl(i.split('/')[0]) #Remove the.json by getting rid of last 5 chars n = unurl((i.split('/'))[1][:-5]) if p not in new_modules: new_modules[p] = {} f = z.open(i) new_modules[p][n] = json.loads(f.read().decode()) f.close() with modulesLock: for i in new_modules: if i in ActiveModules: raise cherrypy.HTTPRedirect("/errors/alreadyexists") for i in new_modules: ActiveModules[i] = new_modules[i] messagebus.postMessage( "/system/notifications", "User " + pages.getAcessingUser() + " uploaded module" + i + " from a zip file") bookkeeponemodule(i) z.close()
def loadModule(moduledir,path_to_module_folder): with modulesLock: #Make an empty dict to hold the module resources module = {} #Iterate over all resource files and load them for i in util.get_files(os.path.join(path_to_module_folder,moduledir)): try: f = open(os.path.join(path_to_module_folder,moduledir,i)) #Load the resource and add it to the dict. Resouce names are urlencodes in filenames. module[unurl(i)] = json.load(f) finally: f.close() name = unurl(moduledir) ActiveModules[name] = module bookkeeponemodule(name)
def loadModule(moduledir, path_to_module_folder): with modulesLock: #Make an empty dict to hold the module resources module = {} #Iterate over all resource files and load them for i in util.get_files(os.path.join(path_to_module_folder, moduledir)): try: f = open(os.path.join(path_to_module_folder, moduledir, i)) #Load the resource and add it to the dict. Resouce names are urlencodes in filenames. module[unurl(i)] = json.load(f) finally: f.close() name = unurl(moduledir) ActiveModules[name] = module bookkeeponemodule(name)
def loadAll(): for i in range(0,15): #Gets the highest numbered of all directories that are named after floating point values(i.e. most recent timestamp) name = util.getHighestNumberedTimeDirectory(directories.persistdir) possibledir = os.path.join(directories.persistdir,name) #__COMPLETE__ is a special file we write to the dump directory to show it as valid if '''__COMPLETE__''' in util.get_files(possibledir): with persistance_dict_connection_lock: for i in util.get_files(possibledir): with open(os.path.join(directories.persistdir,i)) as f: persistancedict[util.unurl(i)] = _PersistanceFile(json.load(f)) break #We sucessfully found the latest good ActiveModules dump! so we break the loop else: #If there was no flag indicating that this was an actual complete dump as opposed #To an interruption, rename it and try again shutil.copytree(possibledir,os.path.join(directories.persistdir,name+"INCOMPLETE")) shutil.rmtree(possibledir)
def unurl(s): return util.unurl(s)