def __init__(self, tid=None, desc='', workdir='.', worker=None, ttype=None, state=None, time_stamp=None, dependency=None, is_root=True, data=None): self.id = tid self.desc = desc self.workdir = Path(workdir).abs if worker is None: worker = Worker.NoAction self.worker = worker if time_stamp is None: time_stamp = TaskStamp.create_now() self.time_stamp = time_stamp if dependency is None: dependency = [] self.dependency = dependency if ttype is None: ttype = Type.Regular self.type = ttype if state is None: state = State.BeforeSubmit self.state = state self.is_root = is_root if data is None: data = {} self.data = data
class FileAbstract(metaclass=ABCMeta): """ File class focus on existance. """ def __init__(self, path, file_system): self.path = Path(path) self.fs = file_system if not self.check(self.path, self.fs): raise FileNotFoundOrWrongTypeError(self.path, self.ftype) @staticmethod def check(path, file_system): if isinstance(path, str): path = Path(path) return file_system.exists(path) def copy_to(self, path): result = self.fs.copy(path, self.path) return result def move_to(self, path): result = self.fs.move(path, self.path) return result @abstractmethod def update(self, content): return NotImplementedError @abstractmethod def delete(self): raise NotImplementedError @property def ftype(self): return {'FileAbstract'} @property def brief(self): """ A JSON serilizable info dict. """ types = [str(x) for x in self.ftype] types.sort() return {'path': self.path.abs, 'name': self.path.name, 'type': types} @property def detail(self): result = self.brief result.update({ 'parent': self.path.parent, 'parts': self.path.parts(), }) return result
def __init__(self, path, file_system): self.path = Path(path) self.fs = file_system if not self.check(self.path, self.fs): raise FileNotFoundOrWrongTypeError(self.path, self.ftype)
def test_glob(self): result = fs.Directory.glob(Path(self.root + '/sub6')) assert set(result) == { self.root + '/sub6/sub7', self.root + '/sub6/test.txt' }
def test_delete_directory(self): path = Path(self.root + '/sub2') fs.Directory.delete(path) assert not fs.exists(path)
def test_is_dir(self): assert fs.Directory.check(Path(self.root + '/sub1')) assert not fs.Directory.check(Path(self.root + '/subx')) assert not fs.Directory.check(Path(self.root + '/tmp.txt'))
def test_create_directory(self): path = Path(self.root + '/sub3') fs.Directory.create(path) assert fs.exists(path)
def test_create_file(self): path = Path(self.root + '/tmp3.txt') fs.File.create(path) assert fs.exists(path)
def neg(): file1 = str((pathlib.Path(self.root) / 'tmpx.txt').absolute()) assert not fs.exists(Path(file1))
def test_is_file(self): assert fs.File.check(Path(self.root + '/tmp.txt')) assert not fs.File.check(Path(self.root + '/tmpx.txt')) assert not fs.File.check(Path(self.root + '/sub1'))
def pos(): file1 = str((pathlib.Path(self.root) / 'tmp.txt').absolute()) assert fs.exists(Path(file1))
def exists(path): if isinstance(path, str): path = Path(path) return pathlib.Path(path.abs).exists()
def create(path, parents=False): if isinstance(path, str): path = Path(path) return pathlib.Path(path.abs).mkdir(parents=parents)
def check(path, file_system): if isinstance(path, str): path = Path(path) return file_system.exists(path)
def test_delete_file(self): path = Path(self.root + '/tmp2.txt') fs.File.delete(path) assert not fs.exists(path)
def check(path, file_system): if isinstance(path, str): path = Path(path) return file_system.Directory.check(path)
def __init__(self, workdir=None, duration=5): self.workdir = Path(workdir) self.duration = duration