Skip to content

Commit

Permalink
Clean up Python code generation. (#301)
Browse files Browse the repository at this point in the history
* python codegen - this schema stuff doesn't seem to ever be used
* python codegen - improve type annotation
* python codegen - clean overly defensive code block
  • Loading branch information
jmchilton authored and mr-c committed Nov 28, 2019
1 parent 9d1c5e1 commit 4806936
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions schema_salad/python_codegen_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ def __init__(
namespaces=None, # type: Optional[Dict[Text, Text]]
fileuri=None, # type: Optional[Text]
copyfrom=None, # type: Optional[LoadingOptions]
schemas=None, # type: Optional[List[Text]]
original_doc=None, # type: Optional[Any]
): # type: (...) -> None
self.idx = {} # type: Dict[Text, Dict[Text, Any]]
self.fileuri = fileuri # type: Optional[Text]
self.namespaces = namespaces
self.schemas = schemas
self.original_doc = original_doc
if copyfrom is not None:
self.idx = copyfrom.idx
Expand All @@ -65,8 +63,6 @@ def __init__(
self.fileuri = copyfrom.fileuri
if namespaces is None:
self.namespaces = copyfrom.namespaces
if namespaces is None:
schemas = copyfrom.schemas

if fetcher is None:
import requests
Expand Down Expand Up @@ -165,10 +161,6 @@ def expand_url(
scoped_ref=None, # type: Optional[int]
):
# type: (...) -> Text

if not isinstance(url, string_types):
return url

url = Text(url)

if url in (u"@id", u"@type"):
Expand Down Expand Up @@ -388,16 +380,21 @@ def __init__(self, inner, refScope):
self.inner = inner
self.refScope = refScope

def resolve(self, doc, baseuri, loadingOptions):
# type: (Any, Text, LoadingOptions) -> Any
def resolve(
self,
doc, # type: Text
baseuri, # type: Text
loadingOptions, # type: LoadingOptions
):
# type: (...) -> Union[List[Union[Dict[Text, Text], Text]], Dict[Text, Text], Text]
m = self.typeDSLregex.match(doc)
if m:
first = expand_url(
m.group(1), baseuri, loadingOptions, False, True, self.refScope
)
second = third = None
if bool(m.group(2)):
second = {"type": "array", "items": first}
second = {u"type": u"array", u"items": first}
# second = CommentedMap((("type", "array"),
# ("items", first)))
# second.lc.add_kv_line_col("type", lc)
Expand All @@ -409,7 +406,7 @@ def resolve(self, doc, baseuri, loadingOptions):
# third.lc.add_kv_line_col(0, lc)
# third.lc.add_kv_line_col(1, lc)
# third.lc.filename = filename
doc = third or second or first
return third or second or first
return doc

def load(self, doc, baseuri, loadingOptions, docRoot=None):
Expand Down Expand Up @@ -483,12 +480,6 @@ def _document_load(loader, doc, baseuri, loadingOptions):
)
doc = {k: v for k, v in doc.items() if k != "$namespaces"}

if "$schemas" in doc:
loadingOptions = LoadingOptions(
copyfrom=loadingOptions, schemas=doc["$schemas"]
)
doc = {k: v for k, v in doc.items() if k != "$schemas"}

if "$base" in doc:
baseuri = doc["$base"]

Expand Down

0 comments on commit 4806936

Please sign in to comment.