From f8c0b9ddc8e27388d987433e0ffd4e7ff1d37f16 Mon Sep 17 00:00:00 2001 From: "Joab Leite S. Neto" Date: Fri, 25 Jun 2021 12:22:26 -0300 Subject: [PATCH] v1.4.5 (#141) * chore: add tempdir * fix: fixed base64 util --- cereja/__init__.py | 2 +- cereja/hashtools/_hash.py | 20 +++++++------------- cereja/system/_path.py | 38 ++++++++++++++++++++++++++++++++++++-- tests/testhashtools.py | 23 +++++++++++++++++++++++ tests/tests.py | 2 +- 5 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 tests/testhashtools.py diff --git a/cereja/__init__.py b/cereja/__init__.py index 0284cc2..5b50a2f 100644 --- a/cereja/__init__.py +++ b/cereja/__init__.py @@ -47,7 +47,7 @@ from . import experimental from ._requests import request -VERSION = "1.4.4.final.1" +VERSION = "1.4.5.alpha.0" __version__ = get_version_pep440_compliant(VERSION) diff --git a/cereja/hashtools/_hash.py b/cereja/hashtools/_hash.py index 3aa2e98..e0959be 100644 --- a/cereja/hashtools/_hash.py +++ b/cereja/hashtools/_hash.py @@ -25,6 +25,7 @@ from typing import Union import ast +from cereja import string_to_literal from cereja.config.cj_types import Number __all__ = ['md5', 'base64_encode', 'base64_decode', 'is_base64'] @@ -36,22 +37,15 @@ def md5(o: Union[list, dict, set, str, Number]): return hashlib.md5(o.encode()).hexdigest() -def base64_encode(content: Union[list, dict, set, Number]): - assert isinstance(content, - (list, dict, tuple, set, int, float, bytes, - complex)), f"Type of content {type(content)} is not valid." - return _b64.b64encode(repr(content).encode()).decode() +def base64_encode(content) -> bytes: + if not isinstance(content, bytes): + content = str(content).encode() + return _b64.b64encode(content) -def base64_decode(content): +def base64_decode(content, eval_str=True): decode_val = _b64.b64decode(content).decode() - if isinstance(decode_val, str): - try: - decode_val = ast.literal_eval(decode_val) - return decode_val if isinstance(decode_val, (int, float, complex)) else decode_val - except: - pass - return decode_val + return string_to_literal(decode_val) if eval_str else decode_val def is_base64(content): diff --git a/cereja/system/_path.py b/cereja/system/_path.py index 0b0438c..d7b4349 100644 --- a/cereja/system/_path.py +++ b/cereja/system/_path.py @@ -25,6 +25,7 @@ import os import random import shutil +import tempfile import time import warnings from datetime import datetime @@ -33,11 +34,10 @@ from cereja.array import group_items_in_batches from pathlib import Path as Path_ - logger = logging.getLogger(__name__) __all__ = ['Path', 'change_date_from_path', 'clean_dir', 'file_name', 'get_base_dir', 'group_path_from_dir', 'listdir', - 'mkdir', 'normalize_path'] + 'mkdir', 'normalize_path', 'TempDir'] def mkdir(path_dir: str): @@ -401,6 +401,40 @@ def list_files(cls, dir_path: Union[str, 'Path'], ext: str = None, contains_in_n return files +class TempDir: + def __init__(self, start_name='cj_', end_name='_temp', create_on=None): + self._tmpdir = tempfile.TemporaryDirectory(suffix=end_name, prefix=start_name, dir=create_on) + self._path = Path(self._tmpdir.name) + + def __del__(self): + self.__delete() + + def __repr__(self): + return f"{self.__class__.__name__}({self.path})" + + def __delete(self): + self._tmpdir.cleanup() + + def delete(self): + self.__delete() + + @property + def path(self): + if self._path.exists: + return self._path + raise NotADirectoryError("Not Found.") + + @property + def files(self): + return self.path.list_files(self.path, recursive=True) + + def __enter__(self, *args, **kwargs): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.__delete() + + def normalize_path(path_: str) -> Path: return Path(path_) diff --git a/tests/testhashtools.py b/tests/testhashtools.py new file mode 100644 index 0000000..c768812 --- /dev/null +++ b/tests/testhashtools.py @@ -0,0 +1,23 @@ +import unittest +from cereja import hashtools + + +class HashtoolsTest(unittest.TestCase): + + def test_base64_decode(self): + self.assertEqual(hashtools.base64_decode(b'YWJjZGVmIGcgaCBpIGo='), 'abcdef g h i j') + self.assertEqual(hashtools.base64_decode(b'eydvaSc6ICd0dWRvIGJlbSd9'), {'oi': 'tudo bem'}) + + def test_base64_encode(self): + self.assertEqual(hashtools.base64_encode('abcdef g h i j'), b'YWJjZGVmIGcgaCBpIGo=') + self.assertEqual(hashtools.base64_encode({'oi': 'tudo bem'}), b'eydvaSc6ICd0dWRvIGJlbSd9') + + def test_is_base64(self): + pass + + def test_md5(self): + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/tests.py b/tests/tests.py index 8506f9c..99f81b9 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -279,7 +279,7 @@ def test_sanity(self): {'hi': 1, 'hello': 3, 'deep': {'oi'}}, 1, 2, - b'a') + 'a') for val in vals: res = base64_encode(val) self.assertTrue(is_base64(res))