-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from pkyosx/feature/new-tools
New tool for code formatting and package publishing
- Loading branch information
Showing
15 changed files
with
216 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,3 +127,9 @@ dmypy.json | |
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# customized | ||
.direnv | ||
.envrc | ||
junit_report.xml | ||
lcov.info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,96 @@ | ||
import json | ||
import typing | ||
from .internal import BelieveBase | ||
from .internal import validate | ||
from .internal import NO_CHECK | ||
|
||
|
||
class Dict(BelieveBase): | ||
def __init__(self, dict_obj: typing.Dict): | ||
def __init__(self, dict_obj: typing.Dict) -> None: | ||
assert isinstance(dict_obj, dict) | ||
|
||
super().__init__(dict_obj) | ||
self.__dict_obj = dict_obj | ||
|
||
def validate(self, rhs, e_path=""): | ||
def validate(self, rhs: typing.Dict, e_path: str = "") -> None: | ||
if not isinstance(rhs, dict): | ||
self.raise_validate_error(rhs, e_path=e_path, e_msg="not_dict") | ||
|
||
# validate required field | ||
for k, v in self.__dict_obj.items(): | ||
if not isinstance(v, Optional): | ||
if k not in rhs: | ||
self.raise_validate_error(rhs, e_path=e_path, e_msg=f'missing_required_field: {k}') | ||
self.raise_validate_error( | ||
rhs, e_path=e_path, e_msg=f"missing_required_field: {k}" | ||
) | ||
|
||
# validate field value | ||
for k, v in rhs.items(): | ||
if k in self.__dict_obj: | ||
validate(self.__dict_obj[k], v, "%s.%s" % (e_path, k)) | ||
else: | ||
self.raise_validate_error(rhs, e_path=e_path, e_msg="unknown_field", e_unsafe_msg=f'unknown_field: {k}') | ||
self.raise_validate_error( | ||
rhs, | ||
e_path=e_path, | ||
e_msg="unknown_field", | ||
e_unsafe_msg=f"unknown_field: {k}", | ||
) | ||
|
||
|
||
class DictOf(BelieveBase): | ||
def __init__(self, key: typing.Any, value: typing.Any, n_item: int = NO_CHECK, min_item: int = NO_CHECK, max_item: int = NO_CHECK): | ||
def __init__( | ||
self, | ||
key: typing.Any, | ||
value: typing.Any, | ||
n_item: int = NO_CHECK, | ||
min_item: int = NO_CHECK, | ||
max_item: int = NO_CHECK, | ||
) -> None: | ||
assert n_item == NO_CHECK or isinstance(n_item, int) | ||
assert min_item == NO_CHECK or isinstance(min_item, int) | ||
assert max_item == NO_CHECK or isinstance(max_item, int) | ||
|
||
super().__init__(key, value, n_item=n_item, min_item=min_item, max_item=max_item) | ||
super().__init__( | ||
key, value, n_item=n_item, min_item=min_item, max_item=max_item | ||
) | ||
self.__key = key | ||
self.__value = value | ||
self.__n_item = n_item | ||
self.__min_item = min_item | ||
self.__max_item = max_item | ||
|
||
def validate(self, rhs, e_path=""): | ||
def validate(self, rhs: Dict, e_path: str = "") -> None: | ||
if not isinstance(rhs, dict): | ||
self.raise_validate_error(rhs, e_path=e_path, e_msg="not_dict") | ||
if self.__n_item != NO_CHECK: | ||
if not len(rhs) == self.__n_item: | ||
self.raise_validate_error(rhs, e_path=e_path, e_msg=f'mismatch_item_count: {len(rhs)} != {self.__n_item}') | ||
self.raise_validate_error( | ||
rhs, | ||
e_path=e_path, | ||
e_msg=f"mismatch_item_count: {len(rhs)} != {self.__n_item}", | ||
) | ||
if self.__min_item != NO_CHECK: | ||
if len(rhs) < self.__min_item: | ||
self.raise_validate_error(rhs, e_path=e_path, e_msg=f'too_few_items: {len(rhs)} < {self.__min_item}') | ||
self.raise_validate_error( | ||
rhs, | ||
e_path=e_path, | ||
e_msg=f"too_few_items: {len(rhs)} < {self.__min_item}", | ||
) | ||
if self.__max_item != NO_CHECK: | ||
if len(rhs) > self.__max_item: | ||
self.raise_validate_error(rhs, e_path=e_path, e_msg=f'too_many_items: {len(rhs)} > {self.__max_item}') | ||
self.raise_validate_error( | ||
rhs, | ||
e_path=e_path, | ||
e_msg=f"too_many_items: {len(rhs)} > {self.__max_item}", | ||
) | ||
for k, v in rhs.items(): | ||
validate(self.__key, k, "%s.%s" % (e_path, k)) | ||
validate(self.__value, v, "%s.%s" % (e_path, k)) | ||
|
||
|
||
class Optional(BelieveBase): | ||
def __init__(self, value: typing.Any): | ||
def __init__(self, value: typing.Any) -> None: | ||
super().__init__(value) | ||
self.__value = value | ||
|
||
def validate(self, rhs: typing.Any, e_path: str = ""): | ||
def validate(self, rhs: typing.Any, e_path: str = "") -> None: | ||
validate(rhs, self.__value, e_path=e_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
|
||
from typing import Any, List | ||
|
||
|
||
class ValidateError(Exception): | ||
def __init__(self, v1: Any, v2: Any, **kwargs: Any): | ||
def __init__(self, v1: Any, v2: Any, **kwargs: Any) -> None: | ||
self.v1 = repr(v1) | ||
self.v2 = repr(v2) | ||
self.kwargs = kwargs | ||
|
||
def kwargs_to_string(self, fields: List[str], with_v2: bool): | ||
def kwargs_to_string(self, fields: List[str], with_v2: bool) -> str: | ||
result = [] | ||
for k in fields: | ||
if self.kwargs.get(k): | ||
if k == 'e_path': | ||
result.append(f'[{k}=${self.kwargs[k]}]') | ||
if k == "e_path": | ||
result.append(f"[{k}=${self.kwargs[k]}]") | ||
else: | ||
result.append(f'[{k}={self.kwargs[k]}]') | ||
result.append(f"[{k}={self.kwargs[k]}]") | ||
if with_v2: | ||
result.append(f'{self.v2} != {self.v1}') | ||
result.append(f"{self.v2} != {self.v1}") | ||
return " ".join(result) | ||
|
||
def xss_unsafe_message(self): | ||
return self.kwargs_to_string(['e_path', 'e_msg', 'e_unsafe_msg'], True) | ||
def xss_unsafe_message(self) -> str: | ||
return self.kwargs_to_string(["e_path", "e_msg", "e_unsafe_msg"], True) | ||
|
||
def xss_safe_message(self): | ||
return self.kwargs_to_string(['e_path', 'e_msg'], False) | ||
def xss_safe_message(self) -> str: | ||
return self.kwargs_to_string(["e_path", "e_msg"], False) | ||
|
||
def __str__(self): | ||
def __str__(self) -> str: | ||
return self.xss_unsafe_message() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
8a92aa4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage for this commit
Coverage Report