本文整理汇总了Python中st2common.content.loader.MetaLoader类的典型用法代码示例。如果您正苦于以下问题:Python MetaLoader类的具体用法?Python MetaLoader怎么用?Python MetaLoader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MetaLoader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_action_alias_db_by_name
def _get_action_alias_db_by_name(self, name):
"""
Retrieve ActionAlias DB object for the provided alias name.
"""
base_pack_path = self._get_base_pack_path()
pack_yaml_path = os.path.join(base_pack_path, MANIFEST_FILE_NAME)
if os.path.isfile(pack_yaml_path):
# 1. 1st try to infer pack name from pack metadata file
meta_loader = MetaLoader()
pack_metadata = meta_loader.load(pack_yaml_path)
pack = get_pack_ref_from_metadata(metadata=pack_metadata)
else:
# 2. If pack.yaml is not available, fail back to directory name
# Note: For this to work, directory name needs to match pack name
_, pack = os.path.split(base_pack_path)
pack_loader = ContentPackLoader()
registrar = AliasesRegistrar(use_pack_cache=False)
aliases_path = pack_loader.get_content_from_pack(pack_dir=base_pack_path,
content_type='aliases')
aliases = registrar._get_aliases_from_pack(aliases_dir=aliases_path)
for alias_path in aliases:
action_alias_db = registrar._get_action_alias_db(pack=pack,
action_alias=alias_path,
ignore_metadata_file_error=True)
if action_alias_db.name == name:
return action_alias_db
raise ValueError('Alias with name "%s" not found' % (name))
示例2: register_runners
def register_runners(runner_dir=None, experimental=False, fail_on_failure=True):
""" Register runners
"""
LOG.debug('Start : register runners')
runner_count = 0
runner_loader = RunnersLoader()
if runner_dir:
assert isinstance(runner_dir, list)
if not runner_dir:
runner_dirs = content_utils.get_runners_base_paths()
runners = runner_loader.get_runners(runner_dirs)
for runner, path in runners.iteritems():
LOG.info('Runner "%s"' % (runner))
runner_manifest = os.path.join(path, MANIFEST_FILE_NAME)
meta_loader = MetaLoader()
runner_types = meta_loader.load(runner_manifest)
for runner_type in runner_types:
runner_count += register_runner(runner_type, experimental)
LOG.debug('End : register runners')
return runner_count
示例3: RuleTester
class RuleTester(object):
def __init__(self, rule_file_path, trigger_instance_file_path):
"""
:param rule_file_path: Path to the file containing rule definition.
:type rule_file_path: ``str``
:param trigger_instance_file_path: Path to the file containg trigger instance definition.
:type trigger_instance_file_path: ``str``
"""
self._rule_file_path = rule_file_path
self._trigger_instance_file_path = trigger_instance_file_path
self._meta_loader = MetaLoader()
def evaluate(self):
"""
Evaluate trigger instance against the rule.
:return: ``True`` if the rule matches, ``False`` otherwise.
:rtype: ``boolean``
"""
rule_db = self._get_rule_db_from_file(file_path=self._rule_file_path)
trigger_instance_db = \
self._get_trigger_instance_db_from_file(file_path=self._trigger_instance_file_path)
trigger_ref = ResourceReference.from_string_reference(trigger_instance_db['trigger'])
trigger_db = TriggerDB()
trigger_db.pack = trigger_ref.pack
trigger_db.name = trigger_ref.name
trigger_db.type = trigger_ref.ref
matcher = RulesMatcher(trigger_instance=trigger_instance_db, trigger=trigger_db,
rules=[rule_db])
matching_rules = matcher.get_matching_rules()
return len(matching_rules) >= 1
def _get_rule_db_from_file(self, file_path):
data = self._meta_loader.load(file_path=file_path)
rule_db = RuleDB()
rule_db.trigger = data['trigger']['type']
rule_db.criteria = data.get('criteria', None)
rule_db.action = {}
rule_db.enabled = True
return rule_db
def _get_trigger_instance_db_from_file(self, file_path):
data = self._meta_loader.load(file_path=file_path)
instance = TriggerInstanceDB(**data)
return instance
示例4: __init__
def __init__(self, runner_id):
super(ActionChainRunner, self).__init__(runner_id=runner_id)
self.chain_holder = None
self._meta_loader = MetaLoader()
self._skip_notify_tasks = []
self._display_published = True
self._chain_notify = None
示例5: __init__
def __init__(self, runner_id):
super(ActionChainRunner, self).__init__(runner_id=runner_id)
self.chain_holder = None
self._meta_loader = MetaLoader()
self._stopped = False
self._skip_notify_tasks = []
self._chain_notify = None
示例6: __init__
def __init__(self):
base_path = cfg.CONF.system.base_path
rbac_definitions_path = os.path.join(base_path, 'rbac/')
self._role_definitions_path = os.path.join(rbac_definitions_path, 'roles/')
self._role_assignments_path = os.path.join(rbac_definitions_path, 'assignments/')
self._meta_loader = MetaLoader()
示例7: __init__
def __init__(self, use_pack_cache=True):
"""
:param use_pack_cache: True to cache which packs have been registered in memory and making
sure packs are only registered once.
:type use_pack_cache: ``bool``
"""
self._use_pack_cache = use_pack_cache
self._meta_loader = MetaLoader()
self._pack_loader = ContentPackLoader()
示例8: __init__
def __init__(self, rule_file_path, trigger_instance_file_path):
"""
:param rule_file_path: Path to the file containing rule definition.
:type rule_file_path: ``str``
:param trigger_instance_file_path: Path to the file containg trigger instance definition.
:type trigger_instance_file_path: ``str``
"""
self._rule_file_path = rule_file_path
self._trigger_instance_file_path = trigger_instance_file_path
self._meta_loader = MetaLoader()
示例9: __init__
def __init__(self, use_pack_cache=True, fail_on_failure=False):
"""
:param use_pack_cache: True to cache which packs have been registered in memory and making
sure packs are only registered once.
:type use_pack_cache: ``bool``
:param fail_on_failure: Throw an exception if resource registration fails.
:type fail_on_failure: ``bool``
"""
self._use_pack_cache = use_pack_cache
self._fail_on_failure = fail_on_failure
self._meta_loader = MetaLoader()
self._pack_loader = ContentPackLoader()
示例10: RulesRegistrar
class RulesRegistrar(object):
def __init__(self):
self._meta_loader = MetaLoader()
def _get_json_rules_from_pack(self, rules_dir):
return glob.glob(rules_dir + '/*.json')
def _get_yaml_rules_from_pack(self, rules_dir):
rules = glob.glob(rules_dir + '/*.yaml')
rules.extend(glob.glob(rules_dir + '*.yml'))
return rules
def _get_rules_from_pack(self, rules_dir):
rules = self._get_json_rules_from_pack(rules_dir) or []
rules.extend(self._get_yaml_rules_from_pack(rules_dir) or [])
return rules
def _register_rules_from_pack(self, pack, rules):
for rule in rules:
LOG.debug('Loading rule from %s.', rule)
try:
content = self._meta_loader.load(rule)
rule_api = RuleAPI(**content)
rule_db = RuleAPI.to_model(rule_api)
try:
rule_db.id = Rule.get_by_name(rule_api.name).id
except ValueError:
LOG.info('Rule %s not found. Creating new one.', rule)
try:
rule_db = Rule.add_or_update(rule_db)
LOG.audit('Rule updated. Rule %s from %s.', rule_db, rule)
except Exception:
LOG.exception('Failed to create rule %s.', rule_api.name)
except:
LOG.exception('Failed registering rule from %s.', rule)
def register_rules_from_packs(self, base_dir):
pack_loader = ContentPackLoader()
dirs = pack_loader.get_content(base_dir=base_dir,
content_type='rules')
for pack, rules_dir in six.iteritems(dirs):
try:
LOG.info('Registering rules from pack: %s', pack)
rules = self._get_rules_from_pack(rules_dir)
self._register_rules_from_pack(pack, rules)
except:
LOG.exception('Failed registering all rules from pack: %s', rules_dir)
示例11: __init__
def __init__(self, use_pack_cache=True, use_runners_cache=False, fail_on_failure=False):
"""
:param use_pack_cache: True to cache which packs have been registered in memory and making
sure packs are only registered once.
:type use_pack_cache: ``bool``
:param use_runners_cache: True to cache RunnerTypeDB objects in memory to reduce load on
the database.
:type use_runners_cache: ``bool``
:param fail_on_failure: Throw an exception if resource registration fails.
:type fail_on_failure: ``bool``
"""
self._use_pack_cache = use_pack_cache
self._use_runners_cache = use_runners_cache
self._fail_on_failure = fail_on_failure
self._meta_loader = MetaLoader()
self._pack_loader = ContentPackLoader()
# Maps runner name -> RunnerTypeDB
self._runner_type_db_cache = {}
示例12: ResourceRegistrar
class ResourceRegistrar(object):
ALLOWED_EXTENSIONS = []
def __init__(self):
self._meta_loader = MetaLoader()
self._pack_loader = ContentPackLoader()
def get_resources_from_pack(self, resources_dir):
resources = []
for ext in self.ALLOWED_EXTENSIONS:
resources_glob = resources_dir
if resources_dir.endswith('/'):
resources_glob = resources_dir + ext
else:
resources_glob = resources_dir + '/*' + ext
resource_files = glob.glob(resources_glob)
resources.extend(resource_files)
resources = sorted(resources)
return resources
def register_packs(self, base_dirs):
"""
Register packs in all the provided directories.
"""
packs = self._pack_loader.get_packs(base_dirs=base_dirs)
registered_count = 0
for pack_name, pack_path in six.iteritems(packs):
self.register_pack(pack_name=pack_name, pack_dir=pack_path)
registered_count += 1
return registered_count
def register_pack(self, pack_name, pack_dir):
"""
Register pack in the provided directory.
"""
if pack_name in REGISTERED_PACKS_CACHE:
# This pack has already been registered during this register content run
return
LOG.debug('Registering pack: %s' % (pack_name))
REGISTERED_PACKS_CACHE[pack_name] = True
try:
pack_db = self._register_pack(pack_name=pack_name, pack_dir=pack_dir)
except Exception:
LOG.exception('Failed to register pack "%s"' % (pack_name))
return None
return pack_db
def _register_pack(self, pack_name, pack_dir):
"""
Register a pack (create a DB object in the system).
Note: Pack registration now happens when registering the content and not when installing
a pack using packs.install. Eventually this will be moved to the pack management API.
"""
manifest_path = os.path.join(pack_dir, MANIFEST_FILE_NAME)
if not os.path.isfile(manifest_path):
raise ValueError('Pack "%s" is missing %s file' % (pack_name, MANIFEST_FILE_NAME))
content = self._meta_loader.load(manifest_path)
if not content:
raise ValueError('Pack "%s" metadata file is empty' % (pack_name))
content['ref'] = pack_name
pack_api = PackAPI(**content)
pack_db = PackAPI.to_model(pack_api)
try:
pack_db.id = Pack.get_by_ref(pack_name).id
except ValueError:
LOG.debug('Pack %s not found. Creating new one.', pack_name)
pack_db = Pack.add_or_update(pack_db)
LOG.debug('Pack %s registered.' % (pack_name))
return pack_db
示例13: __init__
def __init__(self):
self.meta_loader = MetaLoader()
示例14: __init__
def __init__(self, runner_id):
super(ActionChainRunner, self).__init__(runner_id=runner_id)
self.chain_holder = None
self._meta_loader = MetaLoader()
示例15: ActionsRegistrar
class ActionsRegistrar(object):
def __init__(self):
self._meta_loader = MetaLoader()
def _get_actions_from_pack(self, actions_dir):
actions = []
for ext in ALLOWED_EXTS:
actions_ext = glob.glob(actions_dir + '/*' + ext)
# Exclude global actions configuration file
config_file = 'actions/config' + ext
actions_ext = [file_path for file_path in actions_ext if
config_file not in file_path] or []
actions.extend(actions_ext)
return actions
def _register_action(self, pack, action):
content = self._meta_loader.load(action)
action_ref = ResourceReference(pack=pack, name=str(content['name']))
model = action_utils.get_action_by_ref(action_ref)
if not model:
model = ActionDB()
model.name = content['name']
model.description = content['description']
model.enabled = content['enabled']
model.pack = pack
model.entry_point = content['entry_point']
model.parameters = content.get('parameters', {})
runner_type = str(content['runner_type'])
valid_runner_type, runner_type_db = self._has_valid_runner_type(runner_type)
if valid_runner_type:
model.runner_type = {'name': runner_type_db.name}
else:
LOG.exception('Runner type %s doesn\'t exist.', runner_type)
raise
try:
model = Action.add_or_update(model)
LOG.audit('Action created. Action %s from %s.', model, action)
except Exception:
LOG.exception('Failed to write action to db %s.', model.name)
raise
def _has_valid_runner_type(self, runner_type):
try:
return True, action_utils.get_runnertype_by_name(runner_type)
except:
return False, None
def _register_actions_from_pack(self, pack, actions):
for action in actions:
try:
LOG.debug('Loading action from %s.', action)
self._register_action(pack, action)
except Exception:
LOG.exception('Unable to register action: %s', action)
continue
# XXX: Requirements for actions is tricky because actions can execute remotely.
# Currently, this method is unused.
def _is_requirements_ok(self, actions_dir):
rqmnts_file = os.path.join(actions_dir, 'requirements.txt')
if not os.path.exists(rqmnts_file):
return True
missing = RequirementsValidator.validate(rqmnts_file)
if missing:
LOG.warning('Actions in %s missing dependencies: %s', actions_dir, ','.join(missing))
return False
return True
def register_actions_from_packs(self, base_dir):
pack_loader = ContentPackLoader()
dirs = pack_loader.get_content(base_dir=base_dir,
content_type='actions')
for pack, actions_dir in six.iteritems(dirs):
try:
LOG.debug('Registering actions from pack %s:, dir: %s', pack, actions_dir)
actions = self._get_actions_from_pack(actions_dir)
self._register_actions_from_pack(pack, actions)
except:
LOG.exception('Failed registering all actions from pack: %s', actions_dir)