Skip to content

Commit ccb87b0

Browse files
committed
improve documentation
1 parent e7d3fb2 commit ccb87b0

7 files changed

Lines changed: 37 additions & 9 deletions

File tree

schema_salad/avro/schema.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class Schema:
9595
"""Base class for all Schema classes."""
9696

9797
def __init__(self, atype: str, other_props: PropsType | None = None) -> None:
98+
"""Avro Schema initializer."""
9899
# Ensure valid ctor args
99100
if not isinstance(atype, str):
100101
raise SchemaParseException(
@@ -119,9 +120,11 @@ def props(self) -> PropsType:
119120

120121
# utility functions to manipulate properties dict
121122
def get_prop(self, key: str) -> PropType | None:
123+
"""Retrieve a property from the Schema."""
122124
return self._props.get(key)
123125

124126
def set_prop(self, key: str, value: PropType | None) -> None:
127+
"""Set a Schema property."""
125128
self._props[key] = value
126129

127130

@@ -169,6 +172,7 @@ def validate(val: str | None, name: str) -> None:
169172

170173
@property
171174
def fullname(self) -> str | None:
175+
"""Retrieve the computed full name."""
172176
return self._full
173177

174178
def get_space(self) -> str | None:
@@ -185,10 +189,12 @@ class Names:
185189
"""Track name set and default namespace during parsing."""
186190

187191
def __init__(self, default_namespace: str | None = None) -> None:
192+
"""Create a namespace tracker."""
188193
self.names: dict[str, NamedSchema] = {}
189194
self.default_namespace = default_namespace
190195

191196
def has_name(self, name_attr: str, space_attr: str | None) -> bool:
197+
"""Test if the given namespace is stored."""
192198
test = Name(name_attr, space_attr, self.default_namespace).fullname
193199
return test in self.names
194200

@@ -318,13 +324,16 @@ def __init__(
318324
# read-only properties
319325
@property
320326
def default(self) -> Any | None:
327+
"""Return the default value, if any."""
321328
return self.get_prop("default")
322329

323330
# utility functions to manipulate properties dict
324331
def get_prop(self, key: str) -> PropType | None:
332+
"""Retrieve a property from the Field."""
325333
return self._props.get(key)
326334

327335
def set_prop(self, key: str, value: PropType | None) -> None:
336+
"""Set a Field property."""
328337
self._props[key] = value
329338

330339

@@ -335,6 +344,7 @@ class PrimitiveSchema(Schema):
335344
"""Valid primitive types are in PRIMITIVE_TYPES."""
336345

337346
def __init__(self, atype: str, other_props: PropsType | None = None) -> None:
347+
"""Create a PrimitiveSchema."""
338348
# Ensure valid ctor args
339349
if atype not in PRIMITIVE_TYPES:
340350
raise AvroException(f"{atype} is not a valid primitive type.")

schema_salad/cpp_codegen.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
C++17 code generator for a given Schema Salad definition.
33
4-
Currently only supports emiting YAML from the C++ objects, not yet parsing
4+
Currently only supports emitting YAML from the C++ objects, not yet parsing
55
YAML into C++ objects.
66
77
The generated code requires the libyaml-cpp library & headers
@@ -489,7 +489,7 @@ def writeDefinition(self, target: IO[str], ind: str, common_namespace: str) -> N
489489

490490
# !TODO way to many functions, most of these shouldn't exists
491491
def isPrimitiveType(v: Any) -> bool:
492-
"""Check if v is a primitve type."""
492+
"""Check if v is a primitive type."""
493493
if not isinstance(v, str):
494494
return False
495495
return v in ["null", "boolean", "int", "long", "float", "double", "string"]
@@ -680,6 +680,7 @@ def convertTypeToCpp(self, type_declaration: list[Any] | dict[str, Any] | str) -
680680
return safenamespacename(namespace) + "::" + safename(classname)
681681

682682
def epilogue(self, root_loader: TypeDef | None) -> None:
683+
"""Trigger to generate the epilouge code."""
683684
# find common namespace
684685

685686
common_namespace = os.path.commonprefix(

schema_salad/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def as_warning(self) -> "SchemaSaladException":
6565
return self
6666

6767
def with_sourceline(self, sl: SourceLine | None) -> "SchemaSaladException":
68+
"""Use the provided SourceLine to set the causal location."""
6869
if sl and sl.file():
6970
self.file = sl.file()
7071
self.start = sl.start()

schema_salad/java_codegen.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _ensure_directory_and_write(path: Path, contents: str) -> None:
3232
f.write(contents)
3333

3434

35-
def doc_to_doc_string(doc: str | None, indent_level: int = 0) -> str:
35+
def _doc_to_doc_string(doc: str | None, indent_level: int = 0) -> str:
3636
lead = " " + " " * indent_level + "* " * indent_level
3737
if doc:
3838
doc_str = f"{lead}<BLOCKQUOTE>\n"
@@ -185,9 +185,9 @@ def begin_class(
185185
if not abstract:
186186
implemented_by = "This interface is implemented by {{@link {}Impl}}<BR>"
187187
interface_doc_str += implemented_by.format(cls)
188-
interface_doc_str += doc_to_doc_string(doc)
188+
interface_doc_str += _doc_to_doc_string(doc)
189189
class_doc_str = f"* Auto-generated class implementation for <I>{classname}</I><BR>"
190-
class_doc_str += doc_to_doc_string(doc)
190+
class_doc_str += _doc_to_doc_string(doc)
191191
target = self.main_src_dir / f"{cls}.java"
192192
with open(target, "w") as f:
193193
_logger.info("Writing file: %s", target)
@@ -682,7 +682,7 @@ def declare_field(
682682
{field_doc_str}
683683
*/
684684
""".format(
685-
fieldname=fieldname, field_doc_str=doc_to_doc_string(doc, indent_level=1)
685+
fieldname=fieldname, field_doc_str=_doc_to_doc_string(doc, indent_level=1)
686686
)
687687
target = self.main_src_dir / f"{self.current_class}.java"
688688
with open(target, "a") as f:
@@ -849,6 +849,7 @@ def idmap_loader(
849849
)
850850

851851
def typedsl_loader(self, inner: TypeDef, ref_scope: int | None) -> TypeDef:
852+
"""Construct the TypeDef for the given DSL loader."""
852853
instance_type = inner.instance_type or "Object"
853854
return self.declare_type(
854855
TypeDef(

schema_salad/ref_resolver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def add_namespaces(self, ns: dict[str, str]) -> None:
285285
self.vocab.update(ns)
286286

287287
def add_schemas(self, ns: list[str] | str, base_url: str) -> None:
288-
"""Fetch exernal schemas and add them to the graph."""
288+
"""Fetch external schemas and add them to the graph."""
289289
if self.skip_schemas:
290290
return
291291
for sch in aslist(ns):
@@ -1102,6 +1102,7 @@ def validate_link(
11021102
return link
11031103

11041104
def getid(self, d: Any) -> str | None:
1105+
"""Use our identifiers to extract the first match from the document."""
11051106
if isinstance(d, MutableMapping):
11061107
for i in self.identifiers:
11071108
if i in d:

schema_salad/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ def extend_and_specialize(items: list[dict[str, Any]], loader: Loader) -> list[d
646646
# because we have unit tests that rely on the order that
647647
# fields are written. Codegen output changes as well.
648648
# We are relying on the insertion order preserving
649-
# property of python dicts (i.e. relyig on Py3.5+).
649+
# property of Python dicts (i.e. relying on Py3.5+).
650650

651651
# First pass adding the exfields.
652652
for sn_exfield, exfield in sns_exfields.items():

schema_salad/sourceline.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import ruamel.yaml
77
from ruamel.yaml.comments import CommentedBase, CommentedMap, CommentedSeq
88

9-
lineno_re = re.compile("^(.*?:[0-9]+:[0-9]+: )(( *)(.*))")
9+
lineno_re = re.compile(r"^(.*?:[0-9]+:[0-9]+: )(( *)(.*))")
1010

1111

1212
def _add_lc_filename(r: ruamel.yaml.comments.CommentedBase, source: AnyStr) -> None:
@@ -32,6 +32,7 @@ def add_lc_filename(r: ruamel.yaml.comments.CommentedBase, source: str) -> None:
3232

3333

3434
def reflow_all(text: str, maxline: int | None = None) -> str:
35+
"""Reflow text respecting a common prefix with line & col info."""
3536
if maxline is None:
3637
maxline = int(os.environ.get("COLUMNS", "100"))
3738
maxno = 0
@@ -59,6 +60,7 @@ def reflow_all(text: str, maxline: int | None = None) -> str:
5960

6061

6162
def reflow(text: str, maxline: int, shift: str | None = "") -> str:
63+
"""Reflow a single line of text."""
6264
maxline = max(maxline, 20)
6365
if len(text) > maxline:
6466
sp = text.rfind(" ", 0, maxline)
@@ -120,6 +122,7 @@ def strip_duplicated_lineno(text: str) -> str:
120122

121123

122124
def strip_dup_lineno(text: str, maxline: int | None = None) -> str:
125+
"""Strip duplicated line numbers."""
123126
if maxline is None:
124127
maxline = int(os.environ.get("COLUMNS", "100"))
125128
pre: str | None = None
@@ -163,6 +166,16 @@ def cmap(
163166
lc: list[int] | None = None,
164167
fn: str | None = None,
165168
) -> int | float | str | CommentedMap | CommentedSeq | None:
169+
"""
170+
Apply line+column & filename data through to the provided data.
171+
172+
:param d: Target datastructure: number and strings will be passed through
173+
unchanged. Non-Commented container types with be transformed into
174+
the relevant Commented container types.
175+
:param lc: Line & Column information to be applied.
176+
:param fn: The Filename to store.
177+
:returns: The (transformed) datastructure.
178+
"""
166179
if lc is None:
167180
lc = [0, 0, 0, 0]
168181
if fn is None:
@@ -241,6 +254,7 @@ def __exit__(
241254
raise self.makeError(str(exc_value)) from exc_value
242255

243256
def file(self) -> str | None:
257+
"""Return the embedded filename."""
244258
if hasattr(self.item, "lc") and hasattr(self.item.lc, "filename"):
245259
return str(self.item.lc.filename)
246260
return None

0 commit comments

Comments
 (0)