def __init__(self, stream): Phase.__init__(self) self.stream = stream self.schema = None self.klass = None self.prop = None self.in_list = False
def __init__(self, store): Phase.__init__(self) self.store = store self.repo = store.repo self.commit = None self.tree = None self.klass = None self.uuid = None self.schema = None self.in_list_property = False
def _apply_create_action(self, action, commit, schema, tree): # make sure the class of the new object is known in the schema self._validate_object_class(action, action.klass, schema) with Phase() as phase: # make sure properties to be set exist in the new object's class self._validate_object_properties(phase, action, schema, action.klass, action.properties.keys()) # make sure none of the properties to set are raw properties # as we have separate actions for them self._validate_object_properties_not_raw(phase, action, schema, action.klass, action.properties) # validate object references to other actions self._validate_action_object_references(phase, action, schema, action.klass, action.properties) uuid = self.store.generate_uuid(commit, action.klass) object_tree = self._create_object_tree(schema, action.klass, uuid, action.properties) # build new class tree with the object added try: class_entry = tree[action.klass] class_tree = self.store.repo[class_entry.oid] builder = self.store.repo.TreeBuilder(class_tree) except KeyError: # this is the first object of this class in the commit, create # a new tree from scratch builder = self.store.repo.TreeBuilder() builder.insert(uuid, object_tree.oid, pygit2.GIT_FILEMODE_TREE) new_class_oid = builder.write() # build and return a new overall store tree builder = self.store.repo.TreeBuilder(tree) builder.insert(action.klass, new_class_oid, pygit2.GIT_FILEMODE_TREE) new_tree_oid = builder.write() new_tree = self.store.repo[new_tree_oid] # load the updated class from the new tree context = loaders.LoaderContext(self.store) context.set_tree(new_tree) new_class_entry = new_tree[action.klass] klass = self.loader.class_in_tree(context, new_class_entry) # load the new object from the new tree context.set_class(klass) context.set_uuid(uuid) obj = self.loader.object_in_tree(context) return obj, new_tree
def _apply_update_action(self, action, commit, schema, tree): # load the object from the commit or from a previous action obj = self._validate_and_resolve_target_object(schema, action, commit) with Phase() as phase: # make sure properties to be set exist in the object's class self._validate_object_properties(phase, action, schema, obj.klass.name, action.properties.keys()) # make sure none of the properties to set are raw properties # as we have separate actions for them self._validate_object_properties_not_raw(phase, action, schema, obj.klass.name, action.properties) # validate object references to other actions self._validate_action_object_references(phase, action, schema, obj.klass.name, action.properties) # obtain the class and object tree from the store old_class_entry = tree[obj.klass.name] old_class_tree = self.store.repo[old_class_entry.oid] old_object_entry = old_class_tree[obj.uuid] old_object_tree = self.store.repo[old_object_entry.oid] # build a new object tree object_tree = self._update_object_tree(schema, obj, action.properties, old_object_tree) # build a new class tree with the object changed builder = self.store.repo.TreeBuilder(old_class_tree) builder.insert(obj.uuid, object_tree.oid, pygit2.GIT_FILEMODE_TREE) new_class_oid = builder.write() # build and return a new overall store tree builder = self.store.repo.TreeBuilder(tree) builder.insert(obj.klass.name, new_class_oid, pygit2.GIT_FILEMODE_TREE) new_tree_oid = builder.write() new_tree = self.store.repo[new_tree_oid] # load the updated class from the new tree context = loaders.LoaderContext(self.store) context.set_tree(new_tree) new_class_entry = new_tree[obj.klass.name] updated_class = self.loader.class_in_tree(context, new_class_entry) # load the updated object from the updated class context.set_class(updated_class) context.set_uuid(obj.uuid) updated_obj = self.loader.object_in_tree(context) return updated_obj, new_tree
def _apply_update_raw_property_action(self, action, commit, schema, tree): # load the object from the commit or from a previous action obj = self._validate_and_resolve_target_object(schema, action, commit) with Phase() as phase: # make sure the property to be updated exists in the object's class self._validate_object_properties(phase, action, schema, obj.klass.name, [action.property]) # make sure the property to update is a raw property self._validate_object_properties_raw(phase, action, schema, obj.klass.name, [action.property]) # build a new object tree old_object_entry = tree[obj.klass.name] old_object_tree = self.store.repo[old_object_entry.oid] object_tree = self._update_raw_property(schema, obj, action.property, action.content_type, action.data, old_object_tree) # build a new class tree with the object changed class_entry = tree[obj.klass.name] class_tree = self.store.repo[class_entry.oid] builder = self.store.repo.TreeBuilder(class_tree) builder.insert(obj.uuid, object_tree.oid, pygit2.GIT_FILEMODE_TREE) new_class_oid = builder.write() # build and return a new overall store tree builder = self.store.repo.TreeBuilder(tree) builder.insert(obj.klass.name, new_class_oid, pygit2.GIT_FILEMODE_TREE) new_tree_oid = builder.write() new_tree = self.store.repo[new_tree_oid] # load the updated class from the new tree context = loaders.LoaderContext(self.store) context.set_tree(new_tree) new_class_entry = new_tree[obj.klass.name] updated_class = self.loader.class_in_tree(context, new_class_entry) # load the updated object from the updated class context.set_class(updated_class) context.set_uuid(obj.uuid) updated_obj = self.loader.object_in_tree(context) return updated_obj, new_tree
def _load_register_files(self): config_dirs = self._collect_config_dirs() filenames = [ os.path.join(x, 'consonant', 'register.yaml') for x in config_dirs ] # first phase: load YAML data from register files data = [] with Phase() as phase: for filename in filenames: if os.path.exists(filename): try: with open(filename) as f: data.append( (filename, yaml.load(f, Loader=yaml.CLoader))) except Exception, e: phase.error(e)
class Register(yaml.YAMLObject): """Class to access the system and user register.""" yaml_tag = u'!Register' def __init__(self): self.schemas = {} self.services = {} self._load_register_files() def _load_register_files(self): config_dirs = self._collect_config_dirs() filenames = [ os.path.join(x, 'consonant', 'register.yaml') for x in config_dirs ] # first phase: load YAML data from register files data = [] with Phase() as phase: for filename in filenames: if os.path.exists(filename): try: with open(filename) as f: data.append( (filename, yaml.load(f, Loader=yaml.CLoader))) except Exception, e: phase.error(e) # second phase: validate the data with Phase() as phase: for filename, data in data: schemas = data.get('schemas', {}) if not isinstance(schemas, dict): phase.error( RegisterFormatError( filename, 'Schemas are not specified as a dictionary')) for key, val in schemas.iteritems(): if not isinstance(key, basestring): phase.error( RegisterFormatError( filename, 'Schema name "%s" is not a string' % key)) if not expressions.schema_name.match(key): phase.error( RegisterFormatError( filename, 'Schema name "%s" is invalid' % key)) if not isinstance(val, basestring): phase.error( RegisterFormatError( filename, 'Schema name "%s" is mapped to "%s", ' 'which is not a string' % (key, val))) self.schemas.update(schemas) services = data.get('services', {}) if not isinstance(services, dict): phase.error( RegisterFormatError( filename, 'Services are not specified as a dictionary')) for key, val in services.iteritems(): if not isinstance(key, basestring): phase.error( RegisterFormatError( filename, 'Service name "%s" is not a string' % key)) if not expressions.service_name.match(key): phase.error( RegisterFormatError( filename, 'Service name "%s" is invalid' % key)) if not isinstance(val, basestring): phase.error( RegisterFormatError( filename, 'Service name "%s" is mapped to "%s", ' 'which is not a string' % (key, val))) self.services.update(services)
def __init__(self): Phase.__init__(self)
def run(): with Phase() as phase: phase.error(Exception('an error')) phase.error(Exception('an absolutely fatal error'), now=True) phase.error(Exception('an unreached exception'))
def run(): with Phase() as phase: phase.error(Exception('an error')) phase.error(RuntimeError('another error'))
def run(): with Phase() as phase: phase.error(Exception('an error'))
def test_a_phase_with_no_errors_raises_no_exception(self): """Verify that a phase with no errors raises no exception.""" with Phase(): 'somevalue'