def _compress_compact(self, _spath_zip: str, _targets: List[str], _spath_base: str, _level: int = zipfile.ZIP_DEFLATED): path_base = path_.Path(_spath=_spath_base) path_zip = path_.Path(_spath=_spath_zip) self._check_validation(_spath_base=_spath_base, _targets=_targets) self._cd_target(_spath_base=_spath_base) if isinstance(_targets, list): type_target = list_.check_element_type_list(_list=_targets, _type=str) if type_target is True: with zipfile.ZipFile(path_zip.str, 'w', _level) as f: if self.pwd is not None: f.setpassword(pwd=self.pwd) for _idx, _target in enumerate(_targets): path_target = path_.Path( _spath=os.path.join(path_base.str, _target)) f.write(_target) for _idy, _path_target in enumerate( path_target.path.rglob(pattern='*')): f.write( str(_path_target).replace(os.getcwd(), '.')) self._cd_cwd() else: raise ValueError( 'The type for elements of the _targets may be incorrect.') else: raise ValueError('The type of _targets should be list.')
def generate_pems(cls, _spath_pem_private: str, _spath_pem_public: str, _seed: int = 1024) -> None: path_pem_private = path_.Path(_spath=_spath_pem_private) path_pem_public = path_.Path(_spath=_spath_pem_public) path_pem_private.unlink(_missing_ok=True) path_pem_public.unlink(_missing_ok=True) key_private, key_public = cls.__generate_keys(_seed=_seed) with open(_spath_pem_public, mode='wb') as f: f.write(key_private.exportKey('PEM')) with open(_spath_pem_private, mode='wb') as f: f.write(key_public.exportKey('PEM'))
def _cd_target(self, _spath_base: str) -> None: path_temp = path_.Path(_spath=_spath_base) if path_temp.path.is_dir() is True: path_target = path_temp else: path_target = path_temp.parent path_target.cd()
def decompress(self, _spath_zip: str, _d: Optional[str] = None) -> None: path_zip = path_.Path(_spath=_spath_zip) if _d is not None: path_base = path_.Path( _spath=os.path.join(path_zip.parent.str, _d)) path_zip_dst = path_.Path( _spath=os.path.join(path_base.str, path_zip.filename)) path_base.path.mkdir(mode=0o777, parents=True, exist_ok=True) path_zip.copy(_spath_dst=path_zip_dst.str) self._cd_target(_spath_base=path_base.str) self._decompress(_path_zip=path_zip_dst.str) path_zip_dst.unlink() self._cd_cwd() else: self._decompress(_path_zip=path_zip.str)
def __init__(self, _pwd: Optional[bytes] = None) -> None: super(Zip, self).__init__() if (_pwd is None) or (isinstance(_pwd, bytes) is True): self.path_cwd = path_.Path(_spath=os.getcwd()) self.pwd = _pwd else: raise ValueError('The type of _pwd, {} may be incorrect.'.format( type(_pwd)))
def __init__(self, _spath_filename: str, _is_remove: bool = False, _header: Optional[list] = None, _index: bool = False, _mode_write: str = 'a'): super(CSV, self).__init__() self.path_filename = path_.Path(_spath=_spath_filename) self.header = _header self.index = _index self.mode_write = _mode_write self.isFirstWrite = True if _is_remove is True and self.path_filename.path.is_file() is True: self.path_filename.path.unlink()
def __read_pem(cls, _spath_pem: str) -> RSA._RSAobj: path_pem = path_.Path(_spath=_spath_pem) if path_pem.path.is_file() is True: with open(path_pem.str, 'rb') as f: key = RSA.importKey(f.read()) else: raise FileNotFoundError( 'The _spath_key, {} is not existed.'.format(_spath_pem)) return key
def _check_validation(self, _spath_base: Optional[str], _targets: List[str]) -> None: if _spath_base is None: spath_base = '' else: spath_base = _spath_base for _idx, _target, in enumerate(_targets): path_target = path_.Path(_spath=os.path.join(spath_base, _target)) if path_target.path.exists() is False: raise FileNotFoundError('{}'.format(path_target.str))