Skip to content

Commit 8becb15

Browse files
committed
improve documentation
1 parent 48c5618 commit 8becb15

File tree

7 files changed

+37
-9
lines changed

7 files changed

+37
-9
lines changed

schema_salad/avro/schema.py

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

101101
def __init__(self, atype: str, other_props: Optional[PropsType] = None) -> None:
102+
"""Avro Schema initializer."""
102103
# Ensure valid ctor args
103104
if not isinstance(atype, str):
104105
raise SchemaParseException(
@@ -123,9 +124,11 @@ def props(self) -> PropsType:
123124

124125
# utility functions to manipulate properties dict
125126
def get_prop(self, key: str) -> Optional[PropType]:
127+
"""Retrieve a property from the Schema."""
126128
return self._props.get(key)
127129

128130
def set_prop(self, key: str, value: Optional[PropType]) -> None:
131+
"""Set a Schema property."""
129132
self._props[key] = value
130133

131134

@@ -173,6 +176,7 @@ def validate(val: Optional[str], name: str) -> None:
173176

174177
@property
175178
def fullname(self) -> Optional[str]:
179+
"""Retrieve the computed full name."""
176180
return self._full
177181

178182
def get_space(self) -> Optional[str]:
@@ -189,10 +193,12 @@ class Names:
189193
"""Track name set and default namespace during parsing."""
190194

191195
def __init__(self, default_namespace: Optional[str] = None) -> None:
196+
"""Create a namespace tracker."""
192197
self.names: dict[str, NamedSchema] = {}
193198
self.default_namespace = default_namespace
194199

195200
def has_name(self, name_attr: str, space_attr: Optional[str]) -> bool:
201+
"""Test if the given namespace is stored."""
196202
test = Name(name_attr, space_attr, self.default_namespace).fullname
197203
return test in self.names
198204

@@ -324,13 +330,16 @@ def __init__(
324330
# read-only properties
325331
@property
326332
def default(self) -> Optional[Any]:
333+
"""Return the default value, if any."""
327334
return self.get_prop("default")
328335

329336
# utility functions to manipulate properties dict
330337
def get_prop(self, key: str) -> Optional[PropType]:
338+
"""Retrieve a property from the Field."""
331339
return self._props.get(key)
332340

333341
def set_prop(self, key: str, value: Optional[PropType]) -> None:
342+
"""Set a Field property."""
334343
self._props[key] = value
335344

336345

@@ -341,6 +350,7 @@ class PrimitiveSchema(Schema):
341350
"""Valid primitive types are in PRIMITIVE_TYPES."""
342351

343352
def __init__(self, atype: str, other_props: Optional[PropsType] = None) -> None:
353+
"""Create a PrimitiveSchema."""
344354
# Ensure valid ctor args
345355
if atype not in PRIMITIVE_TYPES:
346356
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"]
@@ -714,6 +714,7 @@ def convertTypeToCpp(self, type_declaration: Union[list[Any], dict[str, Any], st
714714
return f"std::variant<{type_declaration}>"
715715

716716
def epilogue(self, root_loader: Optional[TypeDef]) -> None:
717+
"""Trigger to generate the epilouge code."""
717718
# find common namespace
718719

719720
common_namespace = os.path.commonprefix(

schema_salad/exceptions.py

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

6868
def with_sourceline(self, sl: Optional[SourceLine]) -> "SchemaSaladException":
69+
"""Use the provided SourceLine to set the causal location."""
6970
if sl and sl.file():
7071
self.file = sl.file()
7172
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: Optional[str], 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)
@@ -683,7 +683,7 @@ def declare_field(
683683
{field_doc_str}
684684
*/
685685
""".format(
686-
fieldname=fieldname, field_doc_str=doc_to_doc_string(doc, indent_level=1)
686+
fieldname=fieldname, field_doc_str=_doc_to_doc_string(doc, indent_level=1)
687687
)
688688
target = self.main_src_dir / f"{self.current_class}.java"
689689
with open(target, "a") as f:
@@ -850,6 +850,7 @@ def idmap_loader(
850850
)
851851

852852
def typedsl_loader(self, inner: TypeDef, ref_scope: Union[int, None]) -> TypeDef:
853+
"""Construct the TypeDef for the given DSL loader."""
853854
instance_type = inner.instance_type or "Object"
854855
return self.declare_type(
855856
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: Union[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) -> Optional[str]:
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: Optional[int] = 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: Optional[int] = None) -> str:
5960

6061

6162
def reflow(text: str, maxline: int, shift: Optional[str] = "") -> 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: Optional[int] = None) -> str:
125+
"""Strip duplicated line numbers."""
123126
if maxline is None:
124127
maxline = int(os.environ.get("COLUMNS", "100"))
125128
pre: Optional[str] = None
@@ -163,6 +166,16 @@ def cmap(
163166
lc: Optional[list[int]] = None,
164167
fn: Optional[str] = None,
165168
) -> Union[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) -> Optional[str]:
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)