Skip to content

Commit

Permalink
Merge pull request #254 from EliahKagan/style
Browse files Browse the repository at this point in the history
cleanup: Improve style, formatting; use flake8, isort
  • Loading branch information
cwacek authored Sep 14, 2023
2 parents abf38dd + 7158e31 commit d122ce8
Show file tree
Hide file tree
Showing 37 changed files with 161 additions and 150 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ __pycache__/
# C extensions
*.so

# Virtual environment
env/
.env/
venv/
.venv/

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
Expand Down
2 changes: 2 additions & 0 deletions development.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
black
coverage
flake8
isort
pandoc
pyandoc
pytest
Expand Down
3 changes: 0 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os

# If extensions (or modules to document with autodoc) are in another directory,
Expand Down Expand Up @@ -177,8 +176,6 @@
# If true, the index is split into individual pages for each letter.
# html_split_index = False

import os

on_rtd = os.environ.get("READTHEDOCS", None) == "True"

if not on_rtd: # only import and set the theme if we're building docs locally
Expand Down
29 changes: 13 additions & 16 deletions python_jsonschema_objects/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__all__ = ["ObjectBuilder", "markdown_support", "ValidationError"]

import codecs
import copy
import json
Expand All @@ -10,16 +12,11 @@
import six
from jsonschema import Draft4Validator

import python_jsonschema_objects.classbuilder as classbuilder
import python_jsonschema_objects.markdown_support
import python_jsonschema_objects.util
from python_jsonschema_objects import classbuilder, markdown_support, util
from python_jsonschema_objects.validators import ValidationError


logger = logging.getLogger(__name__)

__all__ = ["ObjectBuilder", "markdown_support", "ValidationError"]

FILE = __file__

SUPPORTED_VERSIONS = (
Expand Down Expand Up @@ -110,17 +107,17 @@ def build_classes(self, strict=False, named_only=False, standardize_names=True):
Class names will be transformed using inflection by default, so names
with spaces in the schema will be camelcased, while names without
spaces will have internal capitalization dropped. Thus "Home Address"
becomes "HomeAddress", while "HomeAddress" becomes "Homeaddress" To
disable this behavior, pass standardize_names=False, but be aware
that accessing names with spaces from the namespace can be
problematic.
becomes "HomeAddress", while "HomeAddress" becomes "Homeaddress". To
disable this behavior, pass standardize_names=False, but be aware that
accessing names with spaces from the namespace can be problematic.
Args:
strict: (bool) use this to validate required fields while creating the class
named_only: (bool) If true, only properties with an actual title attribute will
be included in the resulting namespace (although all will be generated).
standardize_names: (bool) If true (the default), class names will be tranformed
by camel casing
named_only: (bool) If true, only properties with an actual title attribute
will be included in the resulting namespace (although all will be
generated).
standardize_names: (bool) If true (the default), class names will be
transformed by camel casing
Returns:
A namespace containing all the generated classes
Expand All @@ -129,7 +126,7 @@ def build_classes(self, strict=False, named_only=False, standardize_names=True):
kw = {"strict": strict}
builder = classbuilder.ClassBuilder(self.resolver)
for nm, defn in six.iteritems(self.schema.get("definitions", {})):
uri = python_jsonschema_objects.util.resolve_ref_uri(
uri = util.resolve_ref_uri(
self.resolver.resolution_scope, "#/definitions/" + nm
)
builder.construct(uri, defn, **kw)
Expand All @@ -155,7 +152,7 @@ def build_classes(self, strict=False, named_only=False, standardize_names=True):
elif not named_only:
classes[name_transform(uri.split("/")[-1])] = klass

return python_jsonschema_objects.util.Namespace.from_mapping(classes)
return util.Namespace.from_mapping(classes)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion python_jsonschema_objects/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"""Git implementation of _version.py."""

import errno
import functools
import os
import re
import subprocess
import sys
from typing import Any, Callable, Dict, List, Optional, Tuple
import functools


def get_keywords() -> Dict[str, str]:
Expand Down
35 changes: 17 additions & 18 deletions python_jsonschema_objects/classbuilder.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import python_jsonschema_objects.util as util
import python_jsonschema_objects.validators as validators
import python_jsonschema_objects.pattern_properties as pattern_properties
from python_jsonschema_objects.literals import LiteralValue

import copy
import collections
import copy
import itertools
import six
import logging
import sys

import logging
import six

import python_jsonschema_objects.wrapper_types
from python_jsonschema_objects import (
pattern_properties,
util,
validators,
wrapper_types,
)
from python_jsonschema_objects.literals import LiteralValue

logger = logging.getLogger(__name__)

logger.addHandler(logging.NullHandler())


# Long is no longer a thing in python3.x
if sys.version_info > (3,):
long = int
Expand Down Expand Up @@ -462,7 +461,9 @@ def expand_references(self, source_uri, iterable):
return pp

def resolve_type(self, ref, source):
"""Return a resolved type for a URI, potentially constructing one if necessary"""
"""Return a resolved type for a URI, potentially constructing one if
necessary.
"""
uri = util.resolve_ref_uri(self.resolver.resolution_scope, ref)
if uri in self.resolved:
return self.resolved[uri]
Expand Down Expand Up @@ -556,9 +557,7 @@ def _construct(self, uri, clsdata, parent=(ProtocolBase,), **kw):
elif clsdata.get("type") == "array" and "items" in clsdata:
clsdata_copy = {}
clsdata_copy.update(clsdata)
self.resolved[
uri
] = python_jsonschema_objects.wrapper_types.ArrayWrapper.create(
self.resolved[uri] = wrapper_types.ArrayWrapper.create(
uri,
item_constraint=clsdata_copy.pop("items"),
classbuilder=self,
Expand Down Expand Up @@ -684,7 +683,7 @@ def _build_object(self, nm, clsdata, parents, **kw):
constraints["strict"] = kw.get("strict")
propdata = {
"type": "array",
"validator": python_jsonschema_objects.wrapper_types.ArrayWrapper.create(
"validator": wrapper_types.ArrayWrapper.create(
nm, item_constraint=typ, **constraints
),
}
Expand All @@ -705,7 +704,7 @@ def _build_object(self, nm, clsdata, parents, **kw):
constraints["strict"] = kw.get("strict")
propdata = {
"type": "array",
"validator": python_jsonschema_objects.wrapper_types.ArrayWrapper.create(
"validator": wrapper_types.ArrayWrapper.create(
uri, item_constraint=typ, **constraints
),
}
Expand All @@ -716,7 +715,7 @@ def _build_object(self, nm, clsdata, parents, **kw):
constraints["strict"] = kw.get("strict")
propdata = {
"type": "array",
"validator": python_jsonschema_objects.wrapper_types.ArrayWrapper.create(
"validator": wrapper_types.ArrayWrapper.create(
uri, item_constraint=typ, **constraints
),
}
Expand Down
14 changes: 7 additions & 7 deletions python_jsonschema_objects/descriptors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import validators, util, wrapper_types
from .classbuilder import ProtocolBase, TypeProxy, TypeRef
from python_jsonschema_objects import util, validators, wrapper_types
from python_jsonschema_objects.classbuilder import ProtocolBase, TypeProxy, TypeRef


class AttributeDescriptor(object):
Expand Down Expand Up @@ -58,7 +58,7 @@ def __set__(self, obj, val):
ok = True
break
elif util.safe_issubclass(typ, ProtocolBase):
# force conversion- thus the val rather than validator assignment
# Force conversion- thus the val rather than validator assignment.
try:
val = typ(**util.coerce_for_expansion(val))
val.validate()
Expand All @@ -80,8 +80,8 @@ def __set__(self, obj, val):
break
elif isinstance(typ, TypeProxy):
try:
# handle keyword expansion according to expected types
# using keywords like oneOf, value can be an object, array or literal
# Handle keyword expansion according to expected types. Using
# keywords like oneOf, value can be an object, array or literal.
val = util.coerce_for_expansion(val)
if isinstance(val, dict):
val = typ(**val)
Expand All @@ -106,7 +106,7 @@ def __set__(self, obj, val):
val.validate()

elif util.safe_issubclass(info["type"], wrapper_types.ArrayWrapper):
# An array type may have already been converted into an ArrayValidator
# An array type may have already been converted into an ArrayValidator.
val = info["type"](val)
val.validate()

Expand All @@ -115,7 +115,7 @@ def __set__(self, obj, val):
validator = info["type"](val)
validator.validate()
if validator._value is not None:
# This allows setting of default Literal values
# This allows setting of default Literal values.
val = validator

elif util.safe_issubclass(info["type"], ProtocolBase):
Expand Down
11 changes: 6 additions & 5 deletions python_jsonschema_objects/literals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from python_jsonschema_objects import util
from python_jsonschema_objects import validators
import functools
import logging
import six
import operator

import six

from python_jsonschema_objects import util, validators


def MakeLiteral(name, typ, value, **properties):
properties.update({"type": typ})
Expand Down Expand Up @@ -76,7 +76,8 @@ def __str__(self):
def validate(self):
info = self.propinfo("__literal__")

# TODO: this duplicates logic in validators.ArrayValidator.check_items; unify it.
# TODO: this duplicates logic in validators.ArrayValidator.check_items;
# unify it.
for param, paramval in sorted(
six.iteritems(info), key=lambda x: x[0].lower() != "type"
):
Expand Down
4 changes: 2 additions & 2 deletions python_jsonschema_objects/markdown_support.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import re

import markdown
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
import re
import json

try:
from markdown import __version_info__ as markdown_version_info
Expand Down
17 changes: 6 additions & 11 deletions python_jsonschema_objects/pattern_properties.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import six
import re
import python_jsonschema_objects.validators as validators
import python_jsonschema_objects.util as util
from python_jsonschema_objects.literals import MakeLiteral

import collections

import logging
import re

import six

import python_jsonschema_objects.wrapper_types
from python_jsonschema_objects import util, validators, wrapper_types
from python_jsonschema_objects.literals import MakeLiteral

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -66,9 +63,7 @@ def _make_type(self, typ, val):
if util.safe_issubclass(typ, cb.ProtocolBase):
return typ(**util.coerce_for_expansion(val))

if util.safe_issubclass(
typ, python_jsonschema_objects.wrapper_types.ArrayWrapper
):
if util.safe_issubclass(typ, wrapper_types.ArrayWrapper):
return typ(val)

if isinstance(typ, cb.TypeProxy):
Expand Down
26 changes: 12 additions & 14 deletions python_jsonschema_objects/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import six
"""Utility and namespace module."""

__all__ = ["Namespace", "as_namespace"]

import copy
import json
from collections.abc import Mapping, Sequence

import six


class lazy_format(object):
Expand All @@ -22,7 +28,8 @@ def safe_issubclass(x, y):
in the underlying implementation throwing TypeError's from trying to
memoize the result- 'object' isn't a usable weakref target at that level.
Unfortunately this gets exposed all the way up to our code; thus a
'safe' version of the function."""
'safe' version of the function.
"""
try:
return issubclass(x, y)
except TypeError:
Expand All @@ -34,16 +41,16 @@ def coerce_for_expansion(mapping):
In py2.7, the value must be a dictionary- thus a as_dict() method
will be invoked if available. In py3k, the raw mapping is returned
unmodified."""
unmodified.
"""
if six.PY2 and hasattr(mapping, "as_dict"):
return mapping.as_dict()
return mapping


class ProtocolJSONEncoder(json.JSONEncoder):
def default(self, obj):
from python_jsonschema_objects import classbuilder
from python_jsonschema_objects import wrapper_types
from python_jsonschema_objects import classbuilder, wrapper_types

if isinstance(obj, classbuilder.LiteralValue):
return obj._value
Expand Down Expand Up @@ -112,13 +119,6 @@ def resolve_ref_uri(base, ref):
return uri


"""namespace module"""

__all__ = ("Namespace", "as_namespace")

from collections.abc import Mapping, Sequence


class _Dummy:
pass

Expand All @@ -129,12 +129,10 @@ class _Dummy:


class Namespace(dict):

"""A dict subclass that exposes its items as attributes.
Warning: Namespace instances do not have direct access to the
dict methods.
"""

def __init__(self, obj={}):
Expand Down
Loading

0 comments on commit d122ce8

Please sign in to comment.