def __init__(self, fully_qualified_name: str, schema_loader: SchemaLoader) -> None: super().__init__(fully_qualified_name, schema_loader) # Load type specific attributes self.split: Expression = Expression( self._spec[self.ATTRIBUTE_SPLIT] ) if self.ATTRIBUTE_SPLIT in self._spec else None
def __init__(self, fully_qualified_name: str, schema_loader: SchemaLoader) -> None: super().__init__(fully_qualified_name, schema_loader) self.condition = Expression( self._spec[self.ATTRIBUTE_CONDITION] ) if self.ATTRIBUTE_CONDITION in self._spec else None self.max = self._spec[ self.ATTRIBUTE_MAX] if self.ATTRIBUTE_MAX in self._spec else None
def build_expression(self, attribute: str) -> Optional[Expression]: """ Builds an expression object. Adds an error if expression creation has errors. """ expression_string = self._spec.get(attribute, None) if expression_string: try: return Expression(str(expression_string)) except Exception as err: self.add_errors( InvalidExpressionError(self.fully_qualified_name, self._spec, attribute, err)) return None
def _generate_import_statements(self) -> List[Expression]: import_expression_list = [] if self.import_spec is None: return import_expression_list for custom_import in self.import_spec: module = custom_import[self.ATTRIBUTE_IMPORT_MODULE] identifier_list = custom_import.get( self.ATTRIBUTE_IMPORT_IDENTIFIERS, None) statement = ' '.join( ['import', module]) if not identifier_list else ' '.join( ['from', module, 'import', ','.join(identifier_list)]) import_expression_list.append( Expression(statement, ExpressionType.EXEC)) return import_expression_list
def test_anchor_condition(anchor_schema_max_one: AnchorSchema, block_item: BlockAggregate) -> None: anchor_schema_max_one.condition = Expression('session.events > 3') eval_context = EvaluationContext() anchor = Anchor(anchor_schema_max_one) eval_context.local_context.add(block_item._schema.fully_qualified_name, block_item) assert anchor.evaluate_anchor(block_item, eval_context) is False block_item.run_restore({ 'events': 4, '_start_time': datetime(2018, 3, 7, 22, 36, 31, 0, timezone.utc).isoformat(), '_end_time': datetime(2018, 3, 7, 22, 37, 31, 0, timezone.utc).isoformat() }) assert anchor.evaluate_anchor(block_item, eval_context) is True anchor.add_condition_met() assert anchor.evaluate_anchor(block_item, eval_context) is False
def __init__(self, fully_qualified_name: str, schema_loader: SchemaLoader) -> None: """ Initializes a schema by providing the path to the schema and the schema loader resource :param fully_qualified_name: Fully qualified path to the schema :param schema_loader: Schema repository that returns schema spec by fully qualified name """ self.schema_loader: SchemaLoader = schema_loader self.fully_qualified_name: str = fully_qualified_name self._spec: Dict[str, Any] = self.schema_loader.get_schema_spec( self.fully_qualified_name) self.validate_schema_spec() self.extend_schema_spec() self.name: str = self._spec[self.ATTRIBUTE_NAME] self.type: str = self._spec[self.ATTRIBUTE_TYPE] self.when: Expression = Expression( self._spec[self.ATTRIBUTE_WHEN] ) if self.ATTRIBUTE_WHEN in self._spec else None self.description: str = self._spec.get(self.ATTRIBUTE_DESCRIPTION, None)
def __init__(self, fully_qualified_name: str, schema_loader: SchemaLoader) -> None: super().__init__(fully_qualified_name, schema_loader) self.value: Expression = Expression( self._spec.get(self.ATTRIBUTE_VALUE, None))