Beispiel #1
0
Datei: db.py Projekt: Peiiii/wk
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
Beispiel #2
0
Datei: pan.py Projekt: Peiiii/wk
 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]
Beispiel #3
0
Datei: db.py Projekt: Peiiii/wk
 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'))
Beispiel #4
0
Datei: db.py Projekt: Peiiii/wk
 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.')
Beispiel #5
0
Datei: pan.py Projekt: Peiiii/wk
 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,
     }
Beispiel #6
0
Datei: db.py Projekt: Peiiii/wk
 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)
Beispiel #7
0
 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)
Beispiel #8
0
Datei: db.py Projekt: Peiiii/wk
 def __call__(self, *args, **kwargs):
     helper = self.geta('helper')
     fp = helper.get_filepath(self.fid)
     # print(fp)
     if fp: return PowerDirPath(fp)(*args, **kwargs)
Beispiel #9
0
 def file(self, name):
     path = self._truepath(name)
     p = PowerDirPath(path)
     p.tofile()
     return p
Beispiel #10
0
 def dir(self, name):
     path = self._truepath(name)
     p = PowerDirPath(path)
     p.todir()
     return p
Beispiel #11
0
Datei: pan.py Projekt: Peiiii/wk
 def clear(cls, path):
     PowerDirPath(path).rmself().todir()
Beispiel #12
0
Datei: pan.py Projekt: Peiiii/wk
 def getFile(self, filename, location):
     location = self.true_path(location)
     loc = PowerDirPath(location)
     return (loc / filename)()
Beispiel #13
0
Datei: pan.py Projekt: Peiiii/wk
 def delete(self, name, location):
     location = self.true_path(location)
     loc = PowerDirPath(location)
     return (loc / name).rmself()
Beispiel #14
0
Datei: pan.py Projekt: Peiiii/wk
 def newDir(self, dirname, location):
     location = self.true_path(location)
     loc = PowerDirPath(location)
     return loc(dirname)
Beispiel #15
0
Datei: pan.py Projekt: Peiiii/wk
 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)
Beispiel #16
0
Datei: pan.py Projekt: Peiiii/wk
 def saveFile(self, filename, location, content):
     location = self.true_path(location)
     f = PowerDirPath(location) / filename
     return f(content)
Beispiel #17
0
 def info(self, path, depth=2, format=True):
     path = self._truepath(path)
     return PowerDirPath(path).tranverse_info(depth=depth, format=format)
Beispiel #18
0
Datei: pan.py Projekt: Peiiii/wk
 def destroy(self):
     try:
         PowerDirPath(self.lpath).rmself()
     except:
         pass