def do_view_file(self,path): # print("path:",path) if path.endswith('.md'): tem=self.get_template('view_md.tem',os.path.dirname(path)) # print(tem) return tem.render(markdown_data=PowerDirPath(path)()) elif path.endswith('.page'): tem=self.get_template(os.path.basename(path),os.path.dirname(path)) dir = path if os.path.isdir(path) else os.path.dirname(path) fs = os.listdir(dir) map = dict({f: f for f in fs}) return tem.render(map=map) elif path.endswith('.book'): return get_book(path) elif path.endswith('.txt') or \ path.endswith('.json') or \ path.endswith('.bat') or \ path.endswith('.sh') or \ path.endswith('.js') or \ path.endswith('.css') or \ path.endswith('.py'): tem=self.get_template('view_file.tem',os.path.dirname(path)) return tem.render(text_data=PowerDirPath(path)()) else: return self.do_send_file(path)
class FileStorageHelper(Piu): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.filesdir = PowerDirPath(os.path.join(self.dbpath, 'files')) self.filesdir.todir() # print('piu build up...:\nfids:',self.get('fids:')) if not self.get('fids', None): self.add(fids=[]) if self.get('first_fid', T.NOT_EXISTS) == T.NOT_EXISTS: self.add(first_id='0') if not self.get('last_fid', None): self.add(last_fid=self.get('first_fid')) def add_fid(self, fid): fids = self.get('fids') fids.append(fid) self.add(last_fid=fid) self.add(fids=fids) def gen_fid(self): last_fid = int(self.get('last_fid')) last_fid = str(last_fid + 1) self.add(last_fid=last_fid) self.add_fid(last_fid) # print('new fids:',self.get('fids')) return last_fid def get_filepath(self, fid): # print(self.get('last_fid')) return self.filesdir / fid if fid in self.get('fids') else None def gen_filepath(self, fid): self.add_fid(fid) if not fid in self.get('fids') else None return self.filesdir / fid
def getDir(self, dirname, location): location = self.true_path(location) loc = PowerDirPath(location) li = (loc / dirname)() return [{ 'name': i, 'type': PowerDirPath(loc / dirname / i).type() } for i in li]
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.filesdir = PowerDirPath(os.path.join(self.dbpath, 'files')) self.filesdir.todir() # print('piu build up...:\nfids:',self.get('fids:')) if not self.get('fids', None): self.add(fids=[]) if self.get('first_fid', T.NOT_EXISTS) == T.NOT_EXISTS: self.add(first_id='0') if not self.get('last_fid', None): self.add(last_fid=self.get('first_fid'))
def init(self, key): self.seta(helper=key) content = self.geta('content', None) fid = self.fid if fid and content: # write to file fp = key.gen_filepath(fid) PowerDirPath(fp).tofile()(content, encoding=self.encoding) elif fid and not content: # read file assert key.get_filepath(fid) elif not fid and content: # write to file self.fid = key.gen_fid() fp = key.gen_filepath(self.fid) PowerDirPath(fp).tofile()(content, encoding=self.encoding) else: # no file, no content raise Exception( 'Error:File record needs fid or content to be given.')
def rarx(src=None, dst=None): from wpkit.basic import PowerDirPath import os src = src or './' src = PowerDirPath(src) if src.isdir(): dst = dst or src dst = PowerDirPath(dst) fs = src.glob('*.rar') for i, f in enumerate(fs): dn = f.basename()[:-4] dp = dst.join_path(dn) unrar(f, dp) print(i, f, dp) elif src.isfile(): assert src.endswith('.rar') dst = dst or src[:-4] unrar(src, dst) print(src, dst) print('finished.')
def __init__(self, path): os.makedirs(path) if not os.path.exists(path) else None assert os.path.exists(path) # print("path:",path) path = os.path.abspath(path) self.lpath = path self.curser = PowerDirPath(path) self.cmd_dict = { 'newFile': self.newFile, 'newDir': self.newDir, 'getFile': self.getFile, 'getDir': self.getDir, 'saveFile': self.saveFile, 'delete': self.delete, }
def __init__(self, path): self.seta(path=path) path = PowerDirPath(self.geta('path')) if path.exists(): assert path.isfile() dic = json_load(path) assert isinstance(dic, dict) else: dic = {} path.tofile() json_dump(dic, path, indent=4) super().__init__(dic)
def __setitem__(self, path, value): tp=self._truepath(path) if os.path.exists(tp): if self.writable: PowerDirPath(tp).rmself() else: raise Exception('%s already exists.'%(tp)) if value is None: os.makedirs(tp) return if isinstance(value,str): assert os.path.exists(value) if os.path.isfile(value): value=File(value) else: assert os.path.isdir(value) value=Folder(value) if isinstance(value,Folder): value.moveto(tp) elif isinstance(value,File): shutil.copy(value.path,tp)
def do_route(req_path): print('reqpath:',req_path) root_path=self.db.get('root_path') print("root_path:",root_path) real_path=root_path+'/'+req_path if req_path!='' else root_path real_path=PowerDirPath(real_path) print("real_path:",real_path) basename=real_path.basename() if basename.startswith('view='): real_path=real_path.dirname()/basename[len('view='):] print("real_path:",real_path) if real_path.isdir(): return self.do_view_dir(real_path) else: return self.do_view_file(real_path) else: if real_path.isdir(): return self.do_send_dir(real_path) elif real_path.isfile(): return self.do_send_file(real_path) return 'not finished.'
def catchfiles(dir, s): return PowerDirPath(dir).deepglob(s)
def tofile(self, fp): from wpkit.basic import PowerDirPath PowerDirPath(fp).tofile()(self.to_string()) return self
def clear(cls, path): PowerDirPath(path).rmself().todir()
def saveFile(self, filename, location, content): location = self.true_path(location) f = PowerDirPath(location) / filename return f(content)
def destroy(self): try: PowerDirPath(self.lpath).rmself() except: pass
def dir(self, name): path = self._truepath(name) p = PowerDirPath(path) p.todir() return p
def newFile(self, filename, location, content=None): location = self.true_path(location) loc = PowerDirPath(location) return loc.file(filename)( content) if content is not None else loc.file(filename)
def newDir(self, dirname, location): location = self.true_path(location) loc = PowerDirPath(location) return loc(dirname)
def getFile(self, filename, location): location = self.true_path(location) loc = PowerDirPath(location) return (loc / filename)()
def delete(self, name, location): location = self.true_path(location) loc = PowerDirPath(location) return (loc / name).rmself()
def info(self, path, depth=2, format=True): path = self._truepath(path) return PowerDirPath(path).tranverse_info(depth=depth, format=format)
def __call__(self, *args, **kwargs): helper = self.geta('helper') fp = helper.get_filepath(self.fid) # print(fp) if fp: return PowerDirPath(fp)(*args, **kwargs)
def file(self, name): path = self._truepath(name) p = PowerDirPath(path) p.tofile() return p
import os from wpkit.basic import PowerDirPath pkg_dir = PowerDirPath(os.path.dirname(__file__)) pkg_data_dir = pkg_dir + '/data' pkg_scripts_dir = pkg_data_dir + '/shell_scripts' pkg_documents_dir = pkg_data_dir + '/documents' def is_linux(): import sys pf = sys.platform if pf == 'linux': return True return False def is_windows(): import sys pf = sys.platform if pf == 'win32': return True return False