deb-python-yaql/yaql/yaqlization.py
Stan Lagun 8875b7404a Ability to yaqlize any Python class was added
Now any Python class or object can be yaqlified
with single function call or decorator.

It is possible to call methods, access attributes/properties
and index yaqlified objects. It is possible to disable any
of these 3 operations and specify whitelist/blacklist
of methods/attributes/keys that are exposed to the
yaql.

Change-Id: I8973492aa6a86d119b05fd33dd8b00ce283fee98
2016-01-24 16:06:10 +03:00

75 lines
2.7 KiB
Python

# Copyright (c) 2016 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 six
YAQLIZATION_ATTR = '__yaqlization__'
def yaqlize(class_or_object=None, yaqlize_attributes=True,
yaqlize_methods=True, yaqlize_indexer=True,
auto_yaqlize_result=False, whitelist=None, blacklist=None,
attribute_remapping=None, blacklist_remapped_attributes=True):
def func(something):
if not hasattr(something, YAQLIZATION_ATTR):
setattr(something, YAQLIZATION_ATTR, build_yaqlization_settings(
yaqlize_attributes=yaqlize_attributes,
yaqlize_methods=yaqlize_methods,
yaqlize_indexer=yaqlize_indexer,
auto_yaqlize_result=auto_yaqlize_result,
whitelist=whitelist,
blacklist=blacklist,
attribute_remapping=attribute_remapping,
))
return something
if class_or_object is None:
return func
else:
return func(class_or_object)
def get_yaqlization_settings(class_or_object):
return getattr(class_or_object, YAQLIZATION_ATTR, None)
def is_yaqlized(class_or_object):
return hasattr(class_or_object, YAQLIZATION_ATTR)
def build_yaqlization_settings(
yaqlize_attributes=True, yaqlize_methods=True, yaqlize_indexer=True,
auto_yaqlize_result=False, whitelist=None, blacklist=None,
attribute_remapping=None, blacklist_remapped_attributes=True):
whitelist = set(whitelist or [])
blacklist = set(blacklist or [])
attribute_remapping = attribute_remapping or {}
if blacklist_remapped_attributes:
for value in six.itervalues(attribute_remapping):
if not isinstance(value, six.string_types):
name = value[0]
else:
name = value
blacklist.add(name)
return {
'yaqlizeAttributes': yaqlize_attributes,
'yaqlizeMethods': yaqlize_methods,
'yaqlizeIndexer': yaqlize_indexer,
'autoYaqlizeResult': auto_yaqlize_result,
'whitelist': whitelist,
'blacklist': blacklist,
'attributeRemapping': attribute_remapping
}