Commit 3cdcf324 by Oleksandr Barabash

version bumped, rebuilt

parent 4e16ad73
from __future__ import absolute_import
try:
import simplejson as json
except ImportError as e:
import json
from .fields.base_field import BaseField
from .functions import jsonize_str
from .functions import pythonize_str
from .decorators import with_metaclass
TAG = "Serializable"
_field_name = "_jsoner_data"
class MetaSerializable(type):
def __new__(cls, name, bases, attrs):
jsoner_field = attrs.pop(_field_name, {})
new_cls = super(MetaSerializable, cls).__new__(cls, name, bases, attrs)
for base in bases:
if hasattr(base, _field_name):
jsoner_field.update(getattr(base, _field_name, {}))
for k, v in attrs.items():
if isinstance(v, BaseField):
# Here's the place where the magic comes!
jsoner_field[k] = v
setattr(new_cls, k, None)
setattr(new_cls, _field_name, jsoner_field)
return new_cls
def __call__(cls, *args, **kwargs):
return type.__call__(cls, *args, **kwargs)
@with_metaclass(MetaSerializable)
class Serializable(object):
def loads(self, data):
# type: () -> self
loaded = json.loads(data)
if not isinstance(loaded, dict):
raise AttributeError("data is not dict instance: '{}'".format(
type(data).__name__
))
for field_name, field in getattr(self, _field_name, {}).items():
if isinstance(field, BaseField):
j_field_name = pythonize_str(field_name) if field.jsonize else \
field_name
j_value = loaded.get(j_field_name, None)
loaded_value = field.loads(j_value)
setattr(self, field_name, loaded_value)
return self
def dumps(self):
# type: () -> self
dumped = dict()
for field_name, field in getattr(self, _field_name, {}).items():
if isinstance(field, BaseField):
value = getattr(self, field_name, None)
j_field_name = jsonize_str(field_name) if field.jsonize else \
field_name
j_value = field.dumps(value)
dumped[j_field_name] = j_value
return json.dumps(dumped)
def with_metaclass(mcls):
def decorator(cls):
body = vars(cls).copy()
body.pop('__dict__', None)
body.pop('__weakref__', None)
return mcls(cls.__name__, cls.__bases__, body)
return decorator
from __future__ import absolute_import
from ..serializers.simple_serializer import SimpleSerializer
TAG = "BaseField"
class BaseField(object):
serializer = None
null = None
jsonize = None
def __init__(self, value_type, serializer=SimpleSerializer, null=True,
jsonize=True):
"""
:param value_type: type of the value we serialize
:type value_type: any
:param serializer: Serializer class
:type serializer: any
:param null: Do we need to send null if None is set
:type null: bool
:param jsonize: Should we convert "test_name" to "testName" and back
:type jsonize: bool
"""
self.serializer = serializer(value_type)
self.null = null
self.jsonize = jsonize
def check_null(self, value):
if value is None and self.null is not True:
raise AttributeError("Value has to be not null: {}".format(value))
return value
def loads(self, value):
return self.serializer.loads(value)
def dumps(self, value):
self.check_null(value)
return self.serializer.dumps(value)
def pythonize_str(data):
# Converts name to a python style names:
# simpleTestValue -> simple_test_value
name_len = len(data)
new_data = ""
for i in range(name_len):
c = data[i]
if c.isupper():
new_data += "_" + c.lower()
else:
new_data += c
return new_data
def jsonize_str(data):
# Converts data to a json style names:
# simple_test_value -> simpleTestValue
if data.find("_") == -1:
return data
name_len = len(data)
new_data = ""
for i in range(name_len):
c = data[i]
if c == "_":
continue
elif i == 0 and c.isalpha():
new_data += c
elif c.isalpha():
if data[i - 1] == "_":
new_data += c.upper()
else:
new_data += c
return new_data
from __future__ import absolute_import
_field_name = "_jsoner_data"
from __future__ import absolute_import
from abc import ABCMeta
from abc import abstractmethod
from ..decorators import with_metaclass
@with_metaclass(ABCMeta)
class BaseSerializer(object):
@abstractmethod
def loads(self, data):
# type: (bytes) -> BaseSerializer
raise NotImplementedError("IMPL ME")
@abstractmethod
def dumps(self, data):
# type: (any) -> bytes
raise NotImplementedError("IMPL ME")
\ No newline at end of file
from __future__ import absolute_import
from utils.log import Log
from .base_serializer import BaseSerializer
TAG = "SimpleSerializer"
class SimpleSerializer(BaseSerializer):
value_is_list = None
value_type = None
handle_types = (str, bytes, int, float, bool)
def __init__(self, value_type):
self.value_is_list, self.value_type = self.get_value_type(value_type)
Log.d(TAG, "value_is_list, value_type: {},{}".format(
self.value_is_list, self.value_type
))
@staticmethod
def raise_value_error(value):
raise AttributeError("Could not handle value: {}".format(value))
def get_value_type(self, value_type):
# If the value type is an array
if isinstance(value_type, list) and len(value_type) == 1:
return True, value_type[0]
# Check if this is a simple value
elif value_type in self.handle_types:
return False, value_type
return self.raise_value_error(value_type)
def check_type(self, values):
# type: (list) -> list
dumped = list()
for raw_data in values:
if not isinstance(raw_data, self.value_type):
self.raise_value_error(raw_data)
dumped.append(raw_data)
return dumped
def loads(self, value):
if not self.value_is_list and not isinstance(value, list):
self.check_type([value])
return value
elif self.value_is_list and isinstance(value, list):
return self.check_type(value)
return self.raise_value_error(value)
def dumps(self, value):
if not self.value_is_list and not isinstance(value, list):
self.check_type([value])
return value
elif self.value_is_list and isinstance(value, list):
return self.check_type(value)
return self.raise_value_error(value)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import traceback
import logging
try:
import threading
except ImportError: #pragma: no cover
threading = None
if threading:
_lock = threading.RLock()
else: #pragma: no cover
_lock = None
def _acquireLock():
"""
Acquire the module-level lock for serializing access to shared data.
This should be released with _releaseLock().
"""
if _lock:
_lock.acquire()
def _releaseLock():
"""
Release the module-level lock acquired by calling _acquireLock().
"""
if _lock:
_lock.release()
class Log(object):
logger = logging.getLogger()
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
tags = dict()
@staticmethod
def get_or_create_tag(tag):
# TODO(s1z): Finish this please
_acquireLock()
try:
if tag in Log.tags:
return Log.tags[tag]
finally:
_releaseLock()
@staticmethod
def log(level, tag, message, ex):
if ex is not None:
# exc_type, exc_value, exc_traceback = sys.exc_info()
message = "%s\n%s" % (message, traceback.format_exc())
msg = "{}::{}".format(tag, message)
Log.logger.log(level, msg)
@staticmethod
def v(tag, message, ex=None):
""" Thers is no VERBOSE in python logging we use DEBUG """
return Log.log(logging.DEBUG, tag, message, ex)
@staticmethod
def d(tag, message, ex=None):
return Log.log(logging.DEBUG, tag, message, ex)
@staticmethod
def i(tag, message, ex=None):
return Log.log(logging.INFO, tag, message, ex)
@staticmethod
def w(tag, message, ex=None):
return Log.log(logging.WARN, tag, message, ex)
@staticmethod
def e(tag, message, ex=None):
return Log.log(logging.ERROR, tag, message, ex)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
from hashlib import new
try:
range = xrange
except NameError:
buffer = memoryview
try:
# OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA
from _hashlib import pbkdf2_hmac
except ImportError:
import binascii
import struct
_trans_5C = bytes(bytearray([x ^ 0x5C for x in range(256)]))
_trans_36 = bytes(bytearray([x ^ 0x36 for x in range(256)]))
def pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None):
"""Password based key derivation function 2 (PKCS #5 v2.0)
This Python implementations based on the hmac module about as fast
as OpenSSL's PKCS5_PBKDF2_HMAC for short passwords and much faster
for long passwords.
"""
if not isinstance(hash_name, str):
raise TypeError(hash_name)
if not isinstance(password, (bytes, bytearray)):
raise TypeError("a bytes-like object is required, not '{}'".format(
type(password).__name__
))
if not isinstance(salt, (bytes, bytearray)):
raise TypeError("a bytes-like object is required, not '{}'".format(
type(salt).__name__
))
# Fast inline HMAC implementation
inner = new(hash_name)
outer = new(hash_name)
blocksize = getattr(inner, 'block_size', 64)
if len(password) > blocksize:
password = new(hash_name, password).digest()
password = password + b'\x00' * (blocksize - len(password))
inner.update(password.translate(_trans_36))
outer.update(password.translate(_trans_5C))
def prf(msg, inner=inner, outer=outer):
# PBKDF2_HMAC uses the password as key. We can re-use the same
# digest objects and just update copies to skip initialization.
icpy = inner.copy()
ocpy = outer.copy()
icpy.update(msg)
ocpy.update(icpy.digest())
return ocpy.digest()
if iterations < 1:
raise ValueError(iterations)
if dklen is None:
dklen = outer.digest_size
if dklen < 1:
raise ValueError(dklen)
hex_format_string = "%%0%ix" % (new(hash_name).digest_size * 2)
dkey = b''
loop = 1
while len(dkey) < dklen:
prev = prf(salt + struct.pack(b'>I', loop))
rkey = int(binascii.hexlify(prev), 16)
for i in range(iterations - 1):
prev = prf(prev)
rkey ^= int(binascii.hexlify(prev), 16)
loop += 1
dkey += binascii.unhexlify(hex_format_string % rkey)
return dkey[:dklen]
#! /bin/bash
python3 setup.py sdist bdist_wheel
Metadata-Version: 2.1
Name: s1zlibs
Version: 1.0.0
Version: 1.0.1
Summary: s1zlibs
Home-page: https://git.s1z.info/skal/s1zlibs
Author: Barabash Oleksandr
......
......@@ -4,4 +4,17 @@ s1zlibs/__init__.py
s1zlibs.egg-info/PKG-INFO
s1zlibs.egg-info/SOURCES.txt
s1zlibs.egg-info/dependency_links.txt
s1zlibs.egg-info/top_level.txt
\ No newline at end of file
s1zlibs.egg-info/top_level.txt
s1zlibs/jsoner/__init__.py
s1zlibs/jsoner/decorators.py
s1zlibs/jsoner/functions.py
s1zlibs/jsoner/fields/__init__.py
s1zlibs/jsoner/fields/base_field.py
s1zlibs/jsoner/serializable/__init__.py
s1zlibs/jsoner/serializable/base_serializable.py
s1zlibs/jsoner/serializers/__init__.py
s1zlibs/jsoner/serializers/base_serializer.py
s1zlibs/jsoner/serializers/simple_serializer.py
s1zlibs/utils/__init__.py
s1zlibs/utils/log.py
s1zlibs/utils/pbkdf2_hmac.py
\ No newline at end of file
......@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
setuptools.setup(
name="s1zlibs",
version="1.0.0",
version="1.0.1",
author="Barabash Oleksandr",
author_email="skaltmn@gmail.com",
description="s1zlibs",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment