Skip to content

Commit

Permalink
added stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Al-Saffar committed Dec 11, 2023
1 parent 8368386 commit cf36531
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 12 additions & 5 deletions myresources/crocodile/file_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ def modify_text(self, txt_search: str, txt_alt: str, replace_line: bool = False,
def download_to_memory(self, allow_redirects: bool = True, timeout: Optional[int] = None, params: Any = None) -> 'Any':
import requests
return requests.get(self.as_url_str(), allow_redirects=allow_redirects, timeout=timeout, params=params) # Alternative: from urllib import request; request.urlopen(url).read().decode('utf-8').
def download(self, folder: OPLike = None, name: OPLike = None, memory: bool = False, allow_redirects: bool = True, timeout: Optional[int] = None, params: Any = None) -> Union['P', 'Any']:
def download(self, folder: OPLike = None, name: OPLike = None, allow_redirects: bool = True, timeout: Optional[int] = None, params: Any = None) -> 'P':
import requests
response = requests.get(self.as_url_str(), allow_redirects=allow_redirects, timeout=timeout, params=params) # Alternative: from urllib import request; request.urlopen(url).read().decode('utf-8').
if memory: return response # r.contents is bytes encoded as per docs of requests.
# if memory: return response # r.contents is bytes encoded as per docs of requests.
if name is not None: f_name = name
else: f_name = validate_name(str(P(response.history[-1].url).name if len(response.history) > 0 else P(response.url).name))
return (P.home().joinpath("Downloads") if folder is None else P(folder)).joinpath(f_name).create(parents_only=True).write_bytes(response.content)
Expand Down Expand Up @@ -426,7 +426,8 @@ def tmpfile(name: OPLike = None, suffix: str = "", folder: OPLike = None, tstamp
tmp = randstr(noun=noun) if name is not None else str(name)
return P.tmp(file=tmp + "_" + randstr() + (("_" + str(timestamp())) if tstamp else "") + suffix, folder=folder or "tmp_files")
@staticmethod
def tmp(folder: OPLike = None, file: Optional[str] = None, root: str = "~/tmp_results") -> 'P': return P(root).expanduser().create().joinpath(folder or "").joinpath(file or "").create(parents_only=True if file else False)
def tmp(folder: OPLike = None, file: Optional[str] = None, root: str = "~/tmp_results") -> 'P':
return P(root).expanduser().joinpath(folder or "").joinpath(file or "").create(parents_only=True if file else False)
# ====================================== Compression & Encryption ===========================================
def zip(self, path: OPLike = None, folder: OPLike = None, name: OPLike = None, arcname: Optional[str] = None, inplace: bool = False, verbose: bool = True,
content: bool = False, orig: bool = False, use_7z: bool = False, pwd: Optional[str] = None, mode: FILE_MODE = 'w', **kwargs: Any) -> 'P':
Expand Down Expand Up @@ -488,13 +489,19 @@ def unxz(self, folder: OPLike = None, name: OPLike = None, path: OPLike = None,
op_path = self._resolve_path(folder, name, path, self.name.replace(".xz", "")).expanduser().resolve()
unxz(self.expanduser().resolve().str, op_path=op_path.str); return self._return(op_path, inlieu=False, inplace=inplace, operation="delete", orig=orig, verbose=verbose, msg=f"UNXZED {repr(self)} ==> {repr(op_path)}")
def tar_gz(self, folder: OPLike = None, name: OPLike = None, path: OPLike = None, inplace: bool = False, orig: bool = False, verbose: bool = True) -> 'P': return self.tar(inplace=inplace).gz(folder=folder, name=name, path=path, inplace=True, orig=orig, verbose=verbose)
def ungz_untar(self, folder: OPLike = None, name: OPLike = None, path: OPLike = None, inplace: bool = False, orig: bool = False, verbose: bool = True) -> 'P': return self.ungz(name=f"tmp_{randstr()}.tar", inplace=inplace).untar(folder=folder, name=name, path=path, inplace=True, orig=orig, verbose=verbose) # this works for .tgz suffix as well as .tar.gz
def ungz_untar(self, folder: OPLike = None, name: OPLike = None, path: OPLike = None, inplace: bool = False, orig: bool = False, verbose: bool = True) -> 'P':
return self.ungz(name=f"tmp_{randstr()}.tar", inplace=inplace).untar(folder=folder, name=name, path=path, inplace=True, orig=orig, verbose=verbose) # this works for .tgz suffix as well as .tar.gz
def tar_xz(self, folder: OPLike = None, name: OPLike = None, path: OPLike = None, inplace: bool = False, orig: bool = False, verbose: bool = True) -> 'P': return self.tar(inplace=inplace).xz(folder=folder, name=name, path=path, inplace=True, orig=orig, verbose=verbose)
def unxz_untar(self, folder: OPLike = None, name: OPLike = None, path: OPLike = None, inplace: bool = False, orig: bool = False, verbose: bool = True) -> 'P': return self.unxz(inplace=inplace).untar(folder=folder, name=name, path=path, inplace=True, orig=orig, verbose=verbose)
def unbz(self, folder: OPLike = None, name: OPLike = None, path: OPLike = None, inplace: bool = False, orig: bool = False, verbose: bool = True) -> 'P':
op_path = self._resolve_path(folder, name, path, self.name.replace(".bz", "").replace(".tbz", ".tar")).expanduser().resolve()
unbz(self.expanduser().resolve().str, op_path=str(op_path)); return self._return(op_path, inlieu=False, inplace=inplace, operation="delete", orig=orig, verbose=verbose, msg=f"UNBZED {repr(self)} ==> {repr(op_path)}")
def decompress(self, folder: OPLike = None, name: OPLike = None, path: OPLike = None, inplace: bool = False, orig: bool = False, verbose: bool = True) -> 'P': raise NotImplementedError("Not implemented yet.")
def decompress(self, folder: OPLike = None, name: OPLike = None, path: OPLike = None, inplace: bool = False, orig: bool = False, verbose: bool = True) -> 'P':
if "tar.gz" in self or "tgz" in self: res = self.ungz_untar(folder=folder, path=path, name=name, inplace=inplace, verbose=verbose, orig=orig)
elif "zip" in self: res = self.unzip(folder=folder, path=path, name=name, inplace=inplace, verbose=verbose, orig=orig)
elif "tar.xz" in self: res = self.unxz_untar(folder=folder, path=path, name=name, inplace=inplace, verbose=verbose, orig=orig)
else: res = self
return res
def encrypt(self, key: Optional[bytes] = None, pwd: Optional[str] = None, folder: OPLike = None, name: OPLike = None, path: OPLike = None, verbose: bool = True, suffix: str = ".enc", inplace: bool = False, orig: bool = False) -> 'P': # see: https://stackoverflow.com/questions/42568262/how-to-encrypt-text-with-a-password-in-python & https://stackoverflow.com/questions/2490334/simple-way-to-encode-a-string-according-to-a-password"""
slf = self.expanduser().resolve(); path = self._resolve_path(folder, name, path, slf.name + suffix)
assert slf.is_file(), f"Cannot encrypt a directory. You might want to try `zip_n_encrypt`. {self}"; path.write_bytes(encrypt(msg=slf.read_bytes(), key=key, pwd=pwd))
Expand Down
4 changes: 2 additions & 2 deletions myresources/crocodile/msc/ascii_art.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ def character_color(logo: str):
assert platform.system() == 'Windows', 'This function is only for Windows.'
# from rgbprint import gradient_scroll, Color
# gradient_scroll(ArtLib.cowsay("crocodile"), start_color=0x4BBEE3, end_color=Color.medium_violet_red, times=3)
_new_art = P.temp().joinpath("tmp_arts").create().joinpath(f"{randstr()}.txt")
_new_art = P.tmp().joinpath("tmp_arts").create().joinpath(f"{randstr()}.txt")
_new_art.write_text(ArtLib.cowsay(logo)) # utf-8 encoding?
os.system(f'type {_new_art} | lolcatjs') # | lolcat


def character_or_box_color(logo: str):
assert platform.system() == 'Linux', 'This function is only for Linux.'
_new_art = P.temp().joinpath("tmp_arts").create().joinpath(f"{randstr()}.txt")
_new_art = P.tmp().joinpath("tmp_arts").create().joinpath(f"{randstr()}.txt")
get_art(logo, artlib=None, file=str(_new_art), verbose=False)
os.system(f"cat {_new_art} | lolcat") # full path since lolcat might not be in PATH.

Expand Down

0 comments on commit cf36531

Please sign in to comment.