altimeter.core.graph.field package¶
Submodules¶
altimeter.core.graph.field.base module¶
Base classes for Fields. Fields define how individual elements of input JSON are parsed into a LinkCollection.
-
class
Field
¶ Bases:
abc.ABC
Abstract base class for all fields
-
abstract
parse
(data, context)¶ Parse data into a LinkCollection using this field’s definition.
- Return type
-
abstract
-
class
SubField
¶ Bases:
altimeter.core.graph.field.base.Field
SubFields are fields which must have a non-anonymous parent Field.
-
get_parent_alti_key
(data, context)¶ Get the alti_key of the parent of this SubField.
- Parameters
data (
Any
) – field datacontext (
Dict
[str
,Any
]) –contains auxiliary information which can be passed through the parse process.
- Returns:
alti_key of parent of this SubField
- Return type
str
-
altimeter.core.graph.field.dict_field module¶
Dict Fields represent fields which consist of dict-like data.
-
class
AnonymousDictField
(source_key, *fields, optional=False, nullable=False)¶ Bases:
altimeter.core.graph.field.base.Field
An AnonymousDictField is a DictField where the source_key of the field is discarded and not used as a name in the resulting graph. See Examples below for more clarity.
- Parameters
source_key (
str
) – Name of the key in the input JSONfields (
Field
) – fields inside this DictFieldoptional (
bool
) – Whether this key is optional. Defaults to False.nullable (
bool
) – Whether this field’s value can be null.
Examples
- A dict containing 3 ScalarFields
>>> from altimeter.core.graph.field.scalar_field import ScalarField >>> input = {"Person": {"FirstName": "Bob", "LastName": "Smith"}} >>> field = AnonymousDictField("Person", ScalarField("FirstName"), ScalarField("LastName")) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'simple_links': ({'pred': 'first_name', 'obj': 'Bob'}, {'pred': 'last_name', 'obj': 'Smith'})}
-
parse
(data, context)¶ Parse this field and return a LinkCollection.
- Parameters
data (
Dict
[str
,Any
]) – dictionary of data to parsecontext (
Dict
[str
,Any
]) –context dict containing data from higher level parsing code.
- Returns:
LinkCollection
- Raises:
DictFieldSourceKeyNotFoundException if self.source_key is not in data. DictFieldValueNotADictException if the data does not appear to represent a dict.
- Return type
-
class
AnonymousEmbeddedDictField
(*fields)¶ Bases:
altimeter.core.graph.field.base.Field
An AnonymousEmbeddedDictField is a EmbeddedDictField where the source_key of the parent field is discarded and not used as a name in the resulting graph. See Examples below for more clarity.
- Parameters
fields (
Field
) – fields inside this DictField
Examples
- A ListField containing an AnonymousEmbeddedDictField with two ScalarFields:
>>> from altimeter.core.graph.field.list_field import ListField >>> from altimeter.core.graph.field.scalar_field import ScalarField >>> input = {"People": [{"FirstName": "Bob", "LastName": "Smith"}, {"FirstName": "Alice", "LastName": "Smith"}]} >>> field = ListField("People", AnonymousEmbeddedDictField(ScalarField("FirstName"), ScalarField("LastName"))) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'simple_links': ({'pred': 'first_name', 'obj': 'Bob'}, {'pred': 'last_name', 'obj': 'Smith'}, {'pred': 'first_name', 'obj': 'Alice'}, {'pred': 'last_name', 'obj': 'Smith'})}
-
parse
(data, context)¶ Parse this field and return a list of Links.
- Parameters
data (
Dict
[str
,Any
]) – dictionary of data to parsecontext (
Dict
[str
,Any
]) – context dict containing data from higher level parsing code.
- Return type
- Returns
LinkCollection
- Raises
DictFieldValueNotADictException if the data does not appear to represent a dict. –
-
class
DictField
(source_key, *fields, alti_key=None, optional=False)¶ Bases:
altimeter.core.graph.field.base.Field
A DictField is a field where the input is a JSON object containing a key (source_key) where the corresponding value is a dictionary.
Examples
- A dictionary containing two ScalarFields:
>>> from altimeter.core.graph.field.scalar_field import ScalarField >>> input = {"Person": {"FirstName": "Bob", "LastName": "Smith"}} >>> field = DictField("Person", ScalarField("FirstName"), ScalarField("LastName")) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'multi_links': ({'pred': 'person', 'obj': {'simple_links': ({'pred': 'first_name', 'obj': 'Bob'}, {'pred': 'last_name', 'obj': 'Smith'})}},)}
- Parameters
source_key (
str
) – Name of the key in the input JSONfields (
Field
) – fields inside this DictFieldalti_key (
Optional
[str
]) – Optional key name to be used in the graph. By default this is set to the source key converted to snake case.optional (
bool
) – Whether this key is optional. Defaults to False.
-
parse
(data, context)¶ Parse this field and return a LinkCollection.
- Parameters
data (
Dict
[str
,Any
]) – dictionary of data to parsecontext (
Dict
[str
,Any
]) –context dict containing data from higher level parsing code.
- Returns:
LinkCollection
- Raises:
DictFieldSourceKeyNotFoundException if self.source_key is not in data. DictFieldValueNotADictException if the data does not appear to represent a dict.
- Return type
-
class
EmbeddedDictField
(*fields)¶ Bases:
altimeter.core.graph.field.base.SubField
An EmbeddedDictField is a field where the input is a JSON object. Generally this field is used inside a ListField.
- Parameters
fields (
Field
) – fields inside this DictField
Examples
- A ListField containing an EmbeddedDictField with two ScalarFields:
>>> from altimeter.core.graph.field.list_field import ListField >>> from altimeter.core.graph.field.scalar_field import ScalarField >>> input = {"People": [{"FirstName": "Bob", "LastName": "Smith"}, {"FirstName": "Alice", "LastName": "Smith"}]} >>> field = ListField("People", EmbeddedDictField(ScalarField("FirstName"), ScalarField("LastName"))) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'multi_links': ({'pred': 'people', 'obj': {'simple_links': ({'pred': 'first_name', 'obj': 'Bob'}, {'pred': 'last_name', 'obj': 'Smith'})}}, {'pred': 'people', 'obj': {'simple_links': ({'pred': 'first_name', 'obj': 'Alice'}, {'pred': 'last_name', 'obj': 'Smith'})}})}
-
parse
(data, context)¶ Parse this field and return a LinkCollection.
- Parameters
data (
Dict
[str
,Any
]) – dictionary of data to parsecontext (
Dict
[str
,Any
]) – context dict containing data from higher level parsing code.
- Return type
- Returns
LinkCollection
- Raises
DictFieldSourceKeyNotFoundException if self.source_key is not in data. –
DictFieldValueNotADictException if the data does not appear to represent a dict. –
altimeter.core.graph.field.exceptions module¶
Field parsing related exceptions.
-
exception
DictFieldSourceKeyNotFoundException
¶ Bases:
altimeter.core.exceptions.AltimeterException
The source_key of a DictField was not found
-
exception
DictFieldValueIsNullException
¶ Bases:
altimeter.core.exceptions.AltimeterException
The value of a non-nullable DictField was null
-
exception
DictFieldValueNotADictException
¶ Bases:
altimeter.core.exceptions.AltimeterException
A DictField does not contain a dict.
-
exception
InvalidParentKeyException
¶ Bases:
altimeter.core.exceptions.AltimeterException
A ParentKey is invalid.
-
exception
ParentKeyMissingException
¶ Bases:
altimeter.core.exceptions.AltimeterException
A required ParentKey is missing.
-
exception
ResourceLinkFieldSourceKeyNotFoundException
¶ Bases:
altimeter.core.exceptions.AltimeterException
The source_key of a ResourceLinkField is not found
-
exception
ResourceLinkFieldValueNotAStringException
¶ Bases:
altimeter.core.exceptions.AltimeterException
The value of a ResourceLinkField is not a string.
-
exception
ScalarFieldSourceKeyNotFoundException
¶ Bases:
altimeter.core.exceptions.AltimeterException
The source_key of a ScalarField is not found
-
exception
ScalarFieldValueNotAScalarException
¶ Bases:
altimeter.core.exceptions.AltimeterException
The value of a ScalarField is not a string, bool, int or float.
-
exception
TagsFieldMissingTagsKeyException
¶ Bases:
altimeter.core.exceptions.AltimeterException
A TagsField data is missing key ‘Tags’
altimeter.core.graph.field.list_field module¶
List Fields represent fields which consist of list data.
-
class
AnonymousListField
(source_key, field, optional=False, allow_scalar=False)¶ Bases:
altimeter.core.graph.field.base.Field
An AnonymousListField is a ListField where the source_key of the field is discarded and not used as a name in the resulting graph. See Examples below for more clarity.
- Parameters
source_key (
str
) – Name of the key in the input JSONfield (
Field
) – Field object representing the type that is contained in this list.optional (
bool
) – Whether this key is optional. Defaults to False.allow_scalar (
bool
) – Whether this field can sometimes contain a scalar rather than a list - if this is set to True the scalar will be treated as a list of one. Defaults to False.
Examples
- A DictField containing an AnonymousListField:
>>> from altimeter.core.graph.field.dict_field import DictField >>> from altimeter.core.graph.field.scalar_field import EmbeddedScalarField >>> input = {"Biota": {"Animals": ["cow", "pig", "human"], "Plants": ["tree", "fern"]}} >>> field = DictField("Biota", AnonymousListField("Animals", EmbeddedScalarField())) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'multi_links': ({'pred': 'biota', 'obj': {'simple_links': ({'pred': 'biota', 'obj': 'cow'}, {'pred': 'biota', 'obj': 'pig'}, {'pred': 'biota', 'obj': 'human'})}},)}
-
parse
(data, context)¶ Parse this field and return a list of Links.
- Parameters
data (
Dict
[str
,Any
]) – dictionary of data to parsecontext (
Dict
[str
,Any
]) –context dict containing data from higher level parsing code.
- Returns:
LinkCollection
- Raises:
ListFieldSourceKeyNotFoundException if self.source_key is not in data. ListFieldValueNotAListException if the data does not appear to represent a list.
- Return type
-
class
ListField
(source_key, sub_field, alti_key=None, optional=False, allow_scalar=False)¶ Bases:
altimeter.core.graph.field.base.Field
A ListField is a field where the input is a JSON object containing a key (source_key) where the corresponding value is a list of homogeneous items.
Examples
- A list of strings:
>>> from altimeter.core.graph.field.scalar_field import EmbeddedScalarField >>> input = {"Animals": ["cow", "pig", "human"]} >>> field = ListField("Animals", EmbeddedScalarField()) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'simple_links': ({'pred': 'animals', 'obj': 'cow'}, {'pred': 'animals', 'obj': 'pig'}, {'pred': 'animals', 'obj': 'human'})}
- A list of dicts:
>>> from altimeter.core.graph.field.dict_field import EmbeddedDictField >>> from altimeter.core.graph.field.scalar_field import ScalarField >>> input = {"People": [{"Name": "Bob", "Age": 49}, {"Name": "Sue", "Age": 42}]} >>> field = ListField("People", EmbeddedDictField(ScalarField("Name"), ScalarField("Age"))) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'multi_links': ({'pred': 'people', 'obj': {'simple_links': ({'pred': 'name', 'obj': 'Bob'}, {'pred': 'age', 'obj': 49})}}, {'pred': 'people', 'obj': {'simple_links': ({'pred': 'name', 'obj': 'Sue'}, {'pred': 'age', 'obj': 42})}})}
- Parameters
source_key (
str
) – Name of the key in the input JSONsub_field (
SubField
) – SubField object representing the type that is contained in this list.alti_key (
Optional
[str
]) – Optional key name to be used in the graph. By default this is set to the source key converted to snake case.optional (
bool
) – Whether this key is optional. Defaults to False.allow_scalar (
bool
) – Whether this field can sometimes contain a scalar rather than a list - if this is set to True the scalar will be treated as a list of one. Defaults to False.
-
parse
(data, context)¶ Parse this field and return a LinkCollection
- Parameters
data (
Dict
[str
,Any
]) – dictionary of data to parsecontext (
Dict
[str
,Any
]) –context dict containing data from higher level parsing code.
- Returns:
LinkCollection
- Raises:
ListFieldSourceKeyNotFoundException if self.source_key is not in data. ListFieldValueNotAListException if the data does not appear to represent a list.
- Return type
altimeter.core.graph.field.resource_link_field module¶
Resource Link Fields represent field containing ids of other top level resources in the graph. For example, an EC2 instance has a ResourceLinkField with source_key ‘VpcId’ pointing to a VPC.
-
class
EmbeddedResourceLinkField
(resource_spec_class, alti_key=None, optional=False, value_is_id=False)¶ Bases:
altimeter.core.graph.field.base.SubField
An EmbeddedResourceLinkField is a ResourceLinkField where the input is the resource id only, not a key/value where the value is a resource id.
Examples
- A link to a TestResourceSpec resource::
>>> from altimeter.core.graph.field.list_field import ListField >>> from altimeter.core.resource.resource_spec import ResourceSpec >>> class TestResourceSpec(ResourceSpec): type_name="thing" >>> input = {"Thing": ["123", "456"]} >>> field = ListField("Thing", EmbeddedResourceLinkField(TestResourceSpec)) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'resource_links': ({'pred': 'thing', 'obj': 'thing:123'}, {'pred': 'thing', 'obj': 'thing:456'})}
- Parameters
resource_spec_class (
Union
[Type
[ResourceSpec
],str
]) – The name of the ResourceSpec class or the ResourceSpec class which this link represents.optional (
bool
) – Whether this key is optional. Defaults to False.value_is_id (
bool
) – Whether the value for this key contains the entire resource id. For AWS resources set this to True if the value is a complete arn.
-
parse
(data, context)¶ Parse this field and return a LinkCollection.
- Parameters
data (
str
) – data to parsecontext (
Dict
[str
,Any
]) – contains data from higher level parsing code.
- Return type
- Returns
LinkCollection
-
class
ResourceLinkField
(source_key, resource_spec_class, alti_key=None, optional=False, value_is_id=False)¶ Bases:
altimeter.core.graph.field.base.Field
A ResourceLinkField represents a field containing ids of other top level resources in the graph. For example, an EC2 instance has a ResourceLinkField with source_key ‘VpcId’ pointing to a VPC.
Examples
- A link to a TestResourceSpec resource::
>>> from altimeter.core.resource.resource_spec import ResourceSpec >>> class TestResourceSpec(ResourceSpec): type_name="thing" >>> input = {"ThingId": "123"} >>> field = ResourceLinkField("ThingId", TestResourceSpec) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'resource_links': ({'pred': 'thing', 'obj': 'thing:123'},)}
- A link to a TestResourceSpec resource using value_is_id::
>>> from altimeter.core.resource.resource_spec import ResourceSpec >>> class TestResourceSpec(ResourceSpec): type_name="thing" >>> input = {"ThingId": "thing:123"} >>> field = ResourceLinkField("ThingId", TestResourceSpec, value_is_id=True) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'resource_links': ({'pred': 'thing', 'obj': 'thing:123'},)}
- Parameters
source_key (
str
) – Name of the key in the input JSONresource_spec_class (
Union
[Type
[ResourceSpec
],str
]) – The name of the ResourceSpec class or the ResourceSpec class which this link represents.alti_key (
Optional
[str
]) – Optional key name to be used in the graph. By default this is set to the resource_spec_class’ type_name attribute.optional (
bool
) – Whether this key is optional. Defaults to False.value_is_id (
bool
) – Whether the value for this key contains the entire resource id. For AWS resources set this to True if the value is a complete arn.
-
parse
(data, context)¶ Parse this field and return a LinkCollection
- Parameters
data (
Dict
[str
,Any
]) – data to parsecontext (
Dict
[str
,Any
]) – contains data from higher level parsing code.
- Return type
- Returns
LinkCollection
-
class
TransientResourceLinkField
(source_key, resource_spec_class, alti_key=None, optional=False, value_is_id=False)¶ Bases:
altimeter.core.graph.field.base.Field
Transient Resource Link Fields represent field containing ids of other top level resources in the graph which may not exist. For example, a Lambda can refer to a VPC however VPCs can be deleted from lambdas.
- Parameters
source_key (str) – Name of the key in the input JSON
resource_spec_class (str|Type[ResourceSpec]) – The name of the ResourceSpec class or the ResourceSpec class which this link represents.
alti_key (str) – Optional key name to be used in the graph. By default this is set to the resource_spec_class’ type_name attribute.
optional (bool) – Whether this key is optional. Defaults to False.
value_is_id (bool) – Whether the value for this key contains the entire resource id. For AWS resources set this to True if the value is a complete arn.
Examples
- A link to a TestResourceSpec resource::
>>> from altimeter.core.resource.resource_spec import ResourceSpec >>> class TestResourceSpec(ResourceSpec): type_name="thing" >>> input = {"ThingId": "123"} >>> field = TransientResourceLinkField("ThingId", TestResourceSpec) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'transient_resource_links': ({'pred': 'thing', 'obj': 'thing:123'},)}
-
parse
(data, context)¶ Parse this field and return a LinkCollection.
- Parameters
data (
Dict
[str
,Any
]) – data to parsecontext (
Dict
[str
,Any
]) – contains data from higher level parsing code.
- Return type
- Returns
LinkCollection
altimeter.core.graph.field.scalar_field module¶
Scalar Fields represent field that contain strings, bools or numbers.
-
class
EmbeddedScalarField
¶ Bases:
altimeter.core.graph.field.base.SubField
An EmbeddedScalarField is a field where the input is a string, number or bool. An EmbeddedScalarField assumes the key of the enclosing field.
Examples
- An EmbeddedScalarField inside a ListField::
>>> from altimeter.core.graph.field.list_field import ListField >>> input = {"Animal": ["Value1", "Value2"]} >>> field = ListField("Animal", EmbeddedScalarField()) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'simple_links': ({'pred': 'animal', 'obj': 'Value1'}, {'pred': 'animal', 'obj': 'Value2'})}
-
parse
(data, context)¶ Parse this field and return a LinkCollection.
- Parameters
data (
Union
[str
,bool
,int
,float
]) – scalar valuecontext (
Dict
[str
,Any
]) – context dict containing data from higher level parsing code.
- Return type
- Returns
LinkCollection
-
class
ScalarField
(source_key, alti_key=None, optional=False, default_value=None)¶ Bases:
altimeter.core.graph.field.base.Field
A ScalarField is a field where the input is a JSON object and the corresponding value is a string, number or bool.
Examples
- A ScalarField with a string value::
>>> input = {"FieldName": "Value"} >>> field = ScalarField("FieldName") >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'simple_links': ({'pred': 'field_name', 'obj': 'Value'},)}
- A ScalarField with a string value and an alti_key specified::
>>> input = {"FieldName": "Value"} >>> field = ScalarField("FieldName", alti_key="custom_name") >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'simple_links': ({'pred': 'custom_name', 'obj': 'Value'},)}
- An optional ScalarField with no default value::
>>> input = {"SomeOtherFieldName": "Value"} >>> field = ScalarField("FieldName", optional=True) >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {}
- An optional ScalarField with a default value::
>>> input = {"SomeOtherFieldName": "Value"} >>> field = ScalarField("FieldName", optional=True, default_value="DefaultValue") >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'simple_links': ({'pred': 'field_name', 'obj': 'DefaultValue'},)}
- Parameters
source_key (
str
) – Name of the key in the input JSONalti_key (
Optional
[str
]) – Optional key name to be used in the graph. By default this is set to the source key converted to snake case.optional (
bool
) – Whether this key is optional. Defaults to False.default_value (
Union
[str
,bool
,int
,float
,None
]) – A default value to use if the source_key is not present.
-
parse
(data, context)¶ Parse this field and return a LinkCollection
- Parameters
data (
Dict
[str
,Any
]) – dictionary of data to parsecontext (
Dict
[str
,Any
]) –context dict containing data from higher level parsing code.
- Returns:
LinkCollection
- Return type
altimeter.core.graph.field.tags_field module¶
Tags Fields represent AWS Tags.
Bases:
altimeter.core.graph.field.base.Field
A TagsField is a field used to parse data in the form
{‘Tags’: [{‘Key’: ‘tag_key’, ‘Value’: ‘tag_value’}, …]}
Examples
>>> input = {"Tags": [{"Key": "Name", "Value": "Jerry"}, {"Key": "DOB", "Value": "1942-08-01"}]} >>> field = TagsField() >>> link_collection = field.parse(data=input, context={}) >>> print(link_collection.dict(exclude_unset=True)) {'tag_links': ({'pred': 'Name', 'obj': 'Jerry'}, {'pred': 'DOB', 'obj': '1942-08-01'})}
- Parameters
optional (
bool
) – Whether this key is optional. Defaults to False.
Parse this field and return a list of Links.
- Parameters
data (
Dict
[str
,Any
]) – dictionary of data to parsecontext (
Dict
[str
,Any
]) –context dict containing data from higher level parsing code.
- Returns:
List of TagLink objects, one for each tag.
- Return type
altimeter.core.graph.field.util module¶
Grab-bag functions for Field parsing
-
camel_case_to_snake_case
(name)¶ Convert a string from CamelCase into snake_case. :type name:
str
:param name: string to convert- Return type
str
- Returns
snake cased string