Пример #1
0
 def _register_mpl_classes(self, data, name=None):
     type_obj = self._classes.get(name)
     if type_obj is not None:
         return type_obj
     if callable(data):
         data = data()
     data = helpers.list_value(data)
     unnamed_class = None
     last_ns = {}
     for cls_data in data:
         last_ns = cls_data.setdefault('Namespaces', last_ns.copy())
         if len(cls_data) == 1:
             continue
         cls_name = cls_data.get('Name')
         if not cls_name:
             if unnamed_class:
                 raise exceptions.AmbiguousClassName(name)
             unnamed_class = cls_data
         else:
             ns_resolver = namespace_resolver.NamespaceResolver(last_ns)
             cls_name = ns_resolver.resolve_name(cls_name)
             if cls_name == name:
                 type_obj = murano_type.create(cls_data, self, cls_name,
                                               ns_resolver)
                 self._classes[name] = type_obj
             else:
                 self._load_queue.setdefault(cls_name, cls_data)
     if type_obj is None and unnamed_class:
         unnamed_class['Name'] = name
         return self._register_mpl_classes(unnamed_class, name)
     return type_obj
Пример #2
0
    def create(cls, data, package, name=None):
        namespaces = data.get('Namespaces') or {}
        ns_resolver = namespace_resolver.NamespaceResolver(namespaces)

        if not name:
            name = ns_resolver.resolve_name(data['Name'])

        parent_class_names = data.get('Extends')
        parent_classes = []
        if parent_class_names:
            if not utils.is_sequence(parent_class_names):
                parent_class_names = [parent_class_names]
            for parent_name in parent_class_names:
                full_name = ns_resolver.resolve_name(parent_name)
                parent_classes.append(package.find_class(full_name))

        type_obj = cls(ns_resolver, name, package, parent_classes)

        properties = data.get('Properties') or {}
        for property_name, property_spec in properties.iteritems():
            spec = typespec.PropertySpec(property_spec, type_obj)
            type_obj.add_property(property_name, spec)

        methods = data.get('Methods') or data.get('Workflow') or {}

        method_mappings = {'initialize': '.init', 'destroy': '.destroy'}

        for method_name, payload in methods.iteritems():
            type_obj.add_method(method_mappings.get(method_name, method_name),
                                payload)

        return type_obj
Пример #3
0
    def test_resolves_current_ns(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})
        short_name, full_name = 'Resource', 'com.example.murano.Resource'

        resolved_name = resolver.resolve_name(short_name)

        self.assertEqual(full_name, resolved_name)
Пример #4
0
    def test_resolves_specified_ns_prefix(self):
        ns = {'sys': 'com.example.murano.system'}
        resolver = ns_resolver.NamespaceResolver(ns)
        short_name, full_name = 'sys:File', 'com.example.murano.system.File'

        resolved_name = resolver.resolve_name(short_name)

        self.assertEqual(full_name, resolved_name)
Пример #5
0
    def test_cuts_empty_prefix(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})
        # name without prefix delimiter
        name = 'some.arbitrary.name'

        resolved_name = resolver.resolve_name(':' + name)

        self.assertEqual(name, resolved_name)
Пример #6
0
    def _load_class(self, class_def_file):
        with open(class_def_file) as stream:
            data = yaml.load(stream, yaql_yaml_loader.YaqlYamlLoader)

        if 'Name' not in data:
            return

        for name, method in (data.get('Methods') or data.get('Workflow')
                             or {}).iteritems():
            if name.startswith('test'):
                method['Usage'] = 'Action'

        ns = namespace_resolver.NamespaceResolver(data.get('Namespaces', {}))
        class_name = ns.resolve_name(data['Name'])
        self._classes[class_name] = data
Пример #7
0
    def _load_classes(self, class_def_file):
        with open(class_def_file, 'rb') as stream:
            data_lst = self._yaml_loader(stream.read(), class_def_file)

        last_ns = {}
        for data in data_lst:
            last_ns = data.get('Namespaces', last_ns.copy())
            if 'Name' not in data:
                continue

            for name, method in six.iteritems(
                    data.get('Methods') or data.get('Workflow') or {}):
                if name.startswith('test'):
                    method['Usage'] = 'Action'

            ns = namespace_resolver.NamespaceResolver(last_ns)
            class_name = ns.resolve_name(data['Name'])
            self._classes[class_name] = data_lst
Пример #8
0
    def get_class(self, name, create_missing=False):
        if name in self._loaded_types:
            return self._loaded_types[name]

        try:
            data = self.load_definition(name)
            package = self._get_package_for_class(name)
        except (exceptions.NoPackageForClassFound, exceptions.NoClassFound):
            if create_missing:
                data = {'Name': name}
                package = None
            else:
                raise

        namespaces = data.get('Namespaces', {})
        ns_resolver = namespace_resolver.NamespaceResolver(namespaces)

        parent_class_names = data.get('Extends')
        parent_classes = []
        if parent_class_names:
            if not isinstance(parent_class_names, types.ListType):
                parent_class_names = [parent_class_names]
            for parent_name in parent_class_names:
                full_name = ns_resolver.resolve_name(parent_name)
                parent_classes.append(self.get_class(full_name))

        type_obj = murano_class.MuranoClass(self, ns_resolver, name,
                                            package, parent_classes)

        properties = data.get('Properties', {})
        for property_name, property_spec in properties.iteritems():
            spec = typespec.PropertySpec(property_spec, type_obj)
            type_obj.add_property(property_name, spec)

        methods = data.get('Methods') or data.get('Workflow') or {}
        for method_name, payload in methods.iteritems():
            type_obj.add_method(method_name, payload)

        self._loaded_types[name] = type_obj
        return type_obj
Пример #9
0
 def _create_package_class(self):
     ns_resolver = namespace_resolver.NamespaceResolver(None)
     return murano_type.MuranoClass(ns_resolver, self.name, self,
                                    utils.NO_VALUE)
Пример #10
0
    def test_resolves_w_empty_namespaces(self):
        resolver = ns_resolver.NamespaceResolver({})

        resolved_name = resolver.resolve_name('Resource')

        self.assertEqual('Resource', resolved_name)
Пример #11
0
    def test_resolves_explicit_base_w_empty_namespaces(self):
        resolver = ns_resolver.NamespaceResolver({})

        resolved_name = resolver.resolve_name('File', 'com.base')

        self.assertEqual('com.base.File', resolved_name)
Пример #12
0
    def test_resolves_explicit_base(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})

        resolved_name = resolver.resolve_name('Resource', relative='com.base')

        self.assertEqual('com.base.Resource', resolved_name)
Пример #13
0
    def test_fails_w_excessive_prefix(self):
        ns = {'sys': 'com.example.murano.system'}
        resolver = ns_resolver.NamespaceResolver(ns)
        invalid_name = 'sys:excessive_ns:muranoResource'

        self.assertRaises(NameError, resolver.resolve_name, invalid_name)
Пример #14
0
    def test_fails_w_prefix_wo_name(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})
        name = 'sys:'

        self.assertRaises(NameError, resolver.resolve_name, name)
Пример #15
0
    def test_fails_w_unknown_prefix(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})
        name = 'unknown_prefix:example.murano'

        self.assertRaises(KeyError, resolver.resolve_name, name)
Пример #16
0
    def test_fails_w_empty_name(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})

        self.assertRaises(ValueError, resolver.resolve_name, None)