Skip to content

Commit

Permalink
feat(tls/client)!: Initial commit of TLS client capabilities (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
c0r0n3r committed Apr 3, 2023
1 parent 4414ce7 commit eec508e
Show file tree
Hide file tree
Showing 8 changed files with 4,465 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ Changelog

- convert Python classes of CryptoParser to JSON (#1)
- add Python warepper to JSON data (#1)

- TLS

- add capabilities of Chromium, Firefox and Opera browsers

- `Chromium <https://en.wikipedia.org/wiki/Chromium_(web_browser)>`__
- `Firefox <https://en.wikipedia.org/wiki/Firefox>`__
- `Opera <https://en.wikipedia.org/wiki/Opera_(web_browser)>`__
133 changes: 130 additions & 3 deletions cryptodatahub/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import json
import inspect
import re

import six
from six.moves import collections_abc

try:
import pathlib
Expand All @@ -18,6 +20,28 @@
from cryptodatahub.common.exception import InvalidValue


@attr.s(repr=False, slots=True, hash=True)
class _DictObjectConverter(object):
object_type = attr.ib(validator=attr.validators.instance_of(type))

def __call__(self, value):
if value is None:
return None
try:
return self.object_type(**value)
except TypeError:
pass

return value

def __repr__(self):
return '<dict to object converter>'


def convert_dict_to_object(object_type):
return _DictObjectConverter(object_type)


@attr.s(repr=False, slots=True, hash=True)
class _EnumConverter(object):
enum_type = attr.ib(validator=attr.validators.instance_of(type))
Expand All @@ -44,8 +68,111 @@ def convert_enum(enum_type):
return _EnumConverter(enum_type)


@attr.s(repr=False, slots=True, hash=True)
class _IterableConverter(object):
member_converter = attr.ib(validator=attr.validators.instance_of(collections_abc.Callable))

def __call__(self, iterable):
if iterable is None:
return None

try:
for idx, member in enumerate(iterable):
iterable[idx] = self.member_converter(member)
except (TypeError, ValueError):
pass

return iterable

def __repr__(self):
return '<iterable converter>'


def convert_iterable(member_converter):
return _IterableConverter(member_converter)


@attr.s(repr=False, slots=True, hash=True)
class _MappingConverter(object):
key_converter = attr.ib(validator=attr.validators.optional(attr.validators.instance_of(collections_abc.Callable)))
value_converter = attr.ib(validator=attr.validators.optional(attr.validators.instance_of(collections_abc.Callable)))

def __call__(self, mapping):
if mapping is None:
return None

if not isinstance(mapping, collections_abc.Mapping):
return mapping

try:
key_value_pairs = [[key, value] for key, value in mapping.items()]
if self.key_converter is not None:
for pair in key_value_pairs:
pair[0] = self.key_converter(pair[0])
if self.value_converter is not None:
for pair in key_value_pairs:
pair[1] = self.value_converter(pair[1])
mapping = type(mapping)(key_value_pairs)
except (TypeError, ValueError):
pass

return mapping

def __repr__(self):
return '<mapping converter>'


def convert_mapping(key_converter=None, value_converter=None):
return _MappingConverter(key_converter, value_converter)


@attr.s(frozen=True)
class ClientVersion(object):
parts = attr.ib(validator=attr.validators.deep_iterable(attr.validators.instance_of(int)))

@classmethod
def from_str(cls, version_str):
try:
return cls((int(version_str), ))
except ValueError:
pass

return cls(tuple(map(int, version_str.split('.'))))

def __str__(self):
return '.'.join(map(str, self.parts))


@attr.s(repr=False, slots=True, hash=True)
class _ClientVersionConverter(object):
def __call__(self, version):
if version is None:
return None

if not isinstance(version, six.string_types):
return version

try:
version = ClientVersion.from_str(version)
except (TypeError, ValueError):
pass

return version

def __repr__(self):
return '<client version converter>'


def convert_client_version():
return _ClientVersionConverter()


class CryptoDataParamsBase(object):
pass


@attr.s(frozen=True)
class CryptoDataParamsNamed(object):
class CryptoDataParamsNamed(CryptoDataParamsBase):
name = attr.ib(validator=attr.validators.instance_of(six.string_types))
long_name = attr.ib(validator=attr.validators.optional(attr.validators.instance_of(six.string_types)))

Expand All @@ -54,7 +181,7 @@ def __str__(self):


@attr.s(frozen=True)
class CryptoDataParamsEnumNumeric(object):
class CryptoDataParamsEnumNumeric(CryptoDataParamsBase):
code = attr.ib()

@code.validator
Expand All @@ -72,7 +199,7 @@ def get_code_size(cls):


@attr.s(frozen=True)
class CryptoDataParamsEnumString(object):
class CryptoDataParamsEnumString(CryptoDataParamsBase):
code = attr.ib(validator=attr.validators.instance_of(six.string_types))

def __str__(self):
Expand Down
Loading

0 comments on commit eec508e

Please sign in to comment.