Files
deb-murano/murano/dsl/namespace_resolver.py
Stan Lagun 94c904e1cb Support for static methods/properties
Both properties and methods can be marked as Usage: Static

Statics can be accessed using ns:Class.property / ns:Class.method(),
:Class.property / :Class.method() to access class from current namespace
or type('full.name').property / type('full.name').method() to use full type name.

In static method $ / $this are referencing current class rather than object.
Static properties are not loaded from object model.

Also methods of io.murano.configuration.Linux class are now static.
Since static methods can be called on the instance it doesn't break
backward compatibility.

Implements blueprint: muranopl-statics
Change-Id: Ic7c6beed9222f4bca118877a60fdabfdd9d65e5a
2016-02-18 22:23:19 +00:00

54 lines
1.9 KiB
Python

# Copyright (c) 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
TYPE_NAME_RE = re.compile(r'^([a-zA-Z_]\w*:|:)?[a-zA-Z_]\w*(\.[a-zA-Z_]\w*)*$')
NS_RE = re.compile(r'^([a-zA-Z_]\w*(\.[a-zA-Z_]\w*)*)?$')
PREFIX_RE = re.compile(r'^([a-zA-Z_]\w*|=)$')
class NamespaceResolver(object):
def __init__(self, namespaces):
for prefix, ns in namespaces.items():
if PREFIX_RE.match(prefix) is None:
raise ValueError(
'Invalid namespace prefix "{0}"'.format(prefix))
if NS_RE.match(ns) is None:
raise ValueError('Invalid namespace "{0}"'.format(ns))
self._namespaces = namespaces.copy()
self._namespaces.setdefault('=', '')
self._namespaces[''] = ''
def resolve_name(self, name):
if name is None or TYPE_NAME_RE.match(name) is None:
raise ValueError('Invalid type name "{0}"'.format(name))
if ':' not in name:
if '.' in name:
parts = ['', name]
else:
parts = ['=', name]
else:
parts = name.split(':')
if not parts[0]:
parts[0] = '='
if parts[0] not in self._namespaces:
raise KeyError('Unknown namespace prefix ' + parts[0])
ns = self._namespaces[parts[0]]
if not ns:
return name
return '.'.join((ns, parts[1]))