comparison planemo/lib/python3.7/site-packages/schema_salad/codegen_base.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 """Base class for the generation of loaders from schema-salad definitions."""
2 import collections
3 from typing import Any, Dict, List, MutableSequence, Optional, Union
4
5 from typing_extensions import Text # pylint: disable=unused-import
6
7 from . import schema
8
9 # move to a regular typing import when Python 3.3-3.6 is no longer supported
10
11
12 class TypeDef(object): # pylint: disable=too-few-public-methods
13 """Schema Salad type description."""
14
15 __slots__ = [
16 "name",
17 "init",
18 "is_uri",
19 "scoped_id",
20 "ref_scope",
21 "loader_type",
22 "instance_type",
23 ]
24
25 # switch to class-style typing.NamedTuple once support for Python < 3.6
26 # is dropped
27 def __init__(
28 self, # pylint: disable=too-many-arguments
29 name, # type: Text
30 init, # type: Text
31 is_uri=False, # type: bool
32 scoped_id=False, # type: bool
33 ref_scope=0, # type: Optional[int]
34 loader_type=None, # type: Optional[Text]
35 instance_type=None, # type: Optional[Text]
36 ): # type: (...) -> None
37 self.name = name
38 self.init = init
39 self.is_uri = is_uri
40 self.scoped_id = scoped_id
41 self.ref_scope = ref_scope
42 # Follow attributes used by Java but not Python.
43 self.loader_type = loader_type
44 self.instance_type = instance_type
45
46
47 class CodeGenBase(object):
48 """Abstract base class for schema salad code generators."""
49
50 def __init__(self): # type: () -> None
51 self.collected_types = (
52 collections.OrderedDict()
53 ) # type: collections.OrderedDict[Text, TypeDef]
54 self.vocab = {} # type: Dict[Text, Text]
55
56 def declare_type(self, declared_type): # type: (TypeDef) -> TypeDef
57 """Add this type to our collection, if needed."""
58 if declared_type not in self.collected_types:
59 self.collected_types[declared_type.name] = declared_type
60 return declared_type
61
62 def add_vocab(self, name, uri): # type: (Text, Text) -> None
63 """Add the given name as an abbreviation for the given URI."""
64 self.vocab[name] = uri
65
66 def prologue(self): # type: () -> None
67 """Trigger to generate the prolouge code."""
68 raise NotImplementedError()
69
70 @staticmethod
71 def safe_name(name): # type: (Text) -> Text
72 """Generate a safe version of the given name."""
73 return schema.avro_name(name)
74
75 def begin_class(
76 self, # pylint: disable=too-many-arguments
77 classname, # type: Text
78 extends, # type: MutableSequence[Text]
79 doc, # type: Text
80 abstract, # type: bool
81 field_names, # type: MutableSequence[Text]
82 idfield, # type: Text
83 ): # type: (...) -> None
84 """Produce the header for the given class."""
85 raise NotImplementedError()
86
87 def end_class(self, classname, field_names):
88 # type: (Text, List[Text]) -> None
89 """Signal that we are done with this class."""
90 raise NotImplementedError()
91
92 def type_loader(self, type_declaration):
93 # type: (Union[List[Any], Dict[Text, Any]]) -> TypeDef
94 """Parse the given type declaration and declare its components."""
95 raise NotImplementedError()
96
97 def declare_field(self, name, fieldtype, doc, optional):
98 # type: (Text, TypeDef, Text, bool) -> None
99 """Output the code to load the given field."""
100 raise NotImplementedError()
101
102 def declare_id_field(self, name, fieldtype, doc, optional):
103 # type: (Text, TypeDef, Text, bool) -> None
104 """Output the code to handle the given ID field."""
105 raise NotImplementedError()
106
107 def uri_loader(self, inner, scoped_id, vocab_term, ref_scope):
108 # type: (TypeDef, bool, bool, Union[int, None]) -> TypeDef
109 """Construct the TypeDef for the given URI loader."""
110 raise NotImplementedError()
111
112 def idmap_loader(self, field, inner, map_subject, map_predicate):
113 # type: (Text, TypeDef, Text, Union[Text, None]) -> TypeDef
114 """Construct the TypeDef for the given mapped ID loader."""
115 raise NotImplementedError()
116
117 def typedsl_loader(self, inner, ref_scope):
118 # type: (TypeDef, Union[int, None]) -> TypeDef
119 """Construct the TypeDef for the given DSL loader."""
120 raise NotImplementedError()
121
122 def epilogue(self, root_loader): # type: (TypeDef) -> None
123 """Trigger to generate the epilouge code."""
124 raise NotImplementedError()