예제 #1
0
파일: blog.py 프로젝트: Peiiii/wk
 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)
예제 #2
0
파일: piu.py 프로젝트: Peiiii/wpkit
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
예제 #3
0
 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]
예제 #4
0
파일: piu.py 프로젝트: Peiiii/wpkit
 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'))
예제 #5
0
파일: piu.py 프로젝트: Peiiii/wpkit
 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.')
예제 #6
0
파일: rar.py 프로젝트: Peiiii/wpkit
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.')
예제 #7
0
 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,
     }
예제 #8
0
파일: piu.py 프로젝트: Peiiii/wpkit
 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)
예제 #9
0
파일: fsitem.py 프로젝트: Peiiii/wpkit
 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)
예제 #10
0
파일: blog.py 프로젝트: Peiiii/wk
 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.'
예제 #11
0
def catchfiles(dir, s):
    return PowerDirPath(dir).deepglob(s)
예제 #12
0
파일: node.py 프로젝트: Peiiii/wpkit
 def tofile(self, fp):
     from wpkit.basic import PowerDirPath
     PowerDirPath(fp).tofile()(self.to_string())
     return self
예제 #13
0
 def clear(cls, path):
     PowerDirPath(path).rmself().todir()
예제 #14
0
 def saveFile(self, filename, location, content):
     location = self.true_path(location)
     f = PowerDirPath(location) / filename
     return f(content)
예제 #15
0
 def destroy(self):
     try:
         PowerDirPath(self.lpath).rmself()
     except:
         pass
예제 #16
0
 def dir(self, name):
     path = self._truepath(name)
     p = PowerDirPath(path)
     p.todir()
     return p
예제 #17
0
 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)
예제 #18
0
 def newDir(self, dirname, location):
     location = self.true_path(location)
     loc = PowerDirPath(location)
     return loc(dirname)
예제 #19
0
 def getFile(self, filename, location):
     location = self.true_path(location)
     loc = PowerDirPath(location)
     return (loc / filename)()
예제 #20
0
 def delete(self, name, location):
     location = self.true_path(location)
     loc = PowerDirPath(location)
     return (loc / name).rmself()
예제 #21
0
 def info(self, path, depth=2, format=True):
     path = self._truepath(path)
     return PowerDirPath(path).tranverse_info(depth=depth, format=format)
예제 #22
0
파일: piu.py 프로젝트: Peiiii/wpkit
 def __call__(self, *args, **kwargs):
     helper = self.geta('helper')
     fp = helper.get_filepath(self.fid)
     # print(fp)
     if fp: return PowerDirPath(fp)(*args, **kwargs)
예제 #23
0
 def file(self, name):
     path = self._truepath(name)
     p = PowerDirPath(path)
     p.tofile()
     return p
예제 #24
0
파일: pkg_info.py 프로젝트: Peiiii/wpkit
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