Skip to content

Commit

Permalink
v1.4.5 (#141)
Browse files Browse the repository at this point in the history
* chore: add tempdir

* fix: fixed base64 util
  • Loading branch information
jlsneto authored Jun 25, 2021
1 parent 32af2ae commit f8c0b9d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cereja/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 7 additions & 13 deletions cereja/hashtools/_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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):
Expand Down
38 changes: 36 additions & 2 deletions cereja/system/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
import random
import shutil
import tempfile
import time
import warnings
from datetime import datetime
Expand All @@ -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):
Expand Down Expand Up @@ -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_)

Expand Down
23 changes: 23 additions & 0 deletions tests/testhashtools.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit f8c0b9d

Please sign in to comment.