本文整理汇总了Python中staticconf.loader.yaml_loader函数的典型用法代码示例。如果您正苦于以下问题:Python yaml_loader函数的具体用法?Python yaml_loader怎么用?Python yaml_loader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了yaml_loader函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_rules
def load_rules(args):
""" Creates a conf dictionary for ElastAlerter. Loads the global
config file and then each rule found in rules_folder.
:param args: The parsed arguments to ElastAlert
:return: The global configuration, a dictionary.
"""
names = []
filename = args.config
conf = yaml_loader(filename)
use_rule = args.rule
for env_var, conf_var in env_settings.items():
val = env(env_var, None)
if val is not None:
conf[conf_var] = val
# Make sure we have all required globals
if required_globals - frozenset(conf.keys()):
raise EAException('%s must contain %s' % (filename, ', '.join(required_globals - frozenset(conf.keys()))))
conf.setdefault('max_query_size', 10000)
conf.setdefault('scroll_keepalive', '30s')
conf.setdefault('disable_rules_on_error', True)
conf.setdefault('scan_subdirectories', True)
# Convert run_every, buffer_time into a timedelta object
try:
conf['run_every'] = datetime.timedelta(**conf['run_every'])
conf['buffer_time'] = datetime.timedelta(**conf['buffer_time'])
if 'alert_time_limit' in conf:
conf['alert_time_limit'] = datetime.timedelta(**conf['alert_time_limit'])
else:
conf['alert_time_limit'] = datetime.timedelta(days=2)
if 'old_query_limit' in conf:
conf['old_query_limit'] = datetime.timedelta(**conf['old_query_limit'])
else:
conf['old_query_limit'] = datetime.timedelta(weeks=1)
except (KeyError, TypeError) as e:
raise EAException('Invalid time format used: %s' % (e))
global base_config
base_config = copy.deepcopy(conf)
# Load each rule configuration file
rules = []
rule_files = get_file_paths(conf, use_rule)
for rule_file in rule_files:
try:
rule = load_configuration(rule_file, conf, args)
if rule['name'] in names:
raise EAException('Duplicate rule named %s' % (rule['name']))
except EAException as e:
raise EAException('Error loading file %s: %s' % (rule_file, e))
rules.append(rule)
names.append(rule['name'])
conf['rules'] = rules
return conf
示例2: load_configuration
def load_configuration(filename, conf, args=None):
""" Load a yaml rule file and fill in the relevant fields with objects.
:param filename: The name of a rule configuration file.
:param conf: The global configuration dictionary, used for populating defaults.
:return: The rule configuration, a dictionary.
"""
rule = {
'rule_file': filename
}
while True:
try:
loaded = yaml_loader(filename)
except yaml.scanner.ScannerError as e:
raise EAException('Could not parse file %s: %s' % (filename, e))
# Special case for merging filters - if both files specify a filter merge (AND) them
if 'filter' in rule and 'filter' in loaded:
rule['filter'] = loaded['filter'] + rule['filter']
loaded.update(rule)
rule = loaded
if 'import' in rule:
# Find the path of the next file.
filename = os.path.join(os.path.dirname(filename), rule['import'])
del(rule['import']) # or we could go on forever!
else:
break
load_options(rule, conf, args)
load_modules(rule, args)
return rule
示例3: load_rule_yaml
def load_rule_yaml(filename):
rule = {
'rule_file': filename,
}
while True:
try:
loaded = yaml_loader(filename)
except yaml.scanner.ScannerError as e:
raise EAException('Could not parse file %s: %s' % (filename, e))
# Special case for merging filters - if both files specify a filter merge (AND) them
if 'filter' in rule and 'filter' in loaded:
rule['filter'] = loaded['filter'] + rule['filter']
loaded.update(rule)
rule = loaded
if 'import' in rule:
# Find the path of the next file.
filename = os.path.join(os.path.dirname(filename), rule['import'])
del(rule['import']) # or we could go on forever!
else:
break
return rule
示例4: load_rule_yaml
def load_rule_yaml(filename):
rule = {
'rule_file': filename,
}
import_rules.pop(filename, None) # clear `filename` dependency
while True:
try:
loaded = yaml_loader(filename)
except yaml.scanner.ScannerError as e:
raise EAException('Could not parse file %s: %s' % (filename, e))
# Special case for merging filters - if both files specify a filter merge (AND) them
if 'filter' in rule and 'filter' in loaded:
rule['filter'] = loaded['filter'] + rule['filter']
loaded.update(rule)
rule = loaded
if 'import' in rule:
# Find the path of the next file.
if os.path.isabs(rule['import']):
import_filename = rule['import']
else:
import_filename = os.path.join(os.path.dirname(filename), rule['import'])
# set dependencies
rules = import_rules.get(filename, [])
rules.append(import_filename)
import_rules[filename] = rules
filename = import_filename
del(rule['import']) # or we could go on forever!
else:
break
return rule
示例5: load_rules
def load_rules(filename, use_rule=None):
""" Creates a conf dictionary for ElastAlerter. Loads the global
config file and then each rule found in rules_folder.
:param filename: Name of the global configuration file.
:param use_rule: Only load the rule which has this filename.
:return: The global configuration, a dictionary.
"""
names = []
conf = yaml_loader(filename)
# Make sure we have all required globals
if required_globals - frozenset(conf.keys()):
raise EAException('%s must contain %s' % (filename, ', '.join(required_globals - frozenset(conf.keys()))))
conf.setdefault('max_query_size', 100000)
# Convert run_every, buffer_time into a timedelta object
try:
conf['run_every'] = datetime.timedelta(**conf['run_every'])
conf['buffer_time'] = datetime.timedelta(**conf['buffer_time'])
if 'alert_time_limit' in conf:
conf['alert_time_limit'] = datetime.timedelta(**conf['alert_time_limit'])
else:
conf['alert_time_limit'] = datetime.timedelta(days=2)
if 'old_query_limit' in conf:
conf['old_query_limit'] = datetime.timedelta(**conf['old_query_limit'])
else:
conf['old_query_limit'] = datetime.timedelta(weeks=1)
except (KeyError, TypeError) as e:
raise EAException('Invalid time format used: %s' % (e))
# Load each rule configuration file
rule_folder = conf['rules_folder']
rule_files = os.listdir(rule_folder)
rules = []
for rule_file in rule_files:
if use_rule and rule_file != use_rule:
continue
if '.yaml' == rule_file[-5:]:
try:
rule = load_configuration(os.path.join(rule_folder, rule_file))
if rule['name'] in names:
raise EAException('Duplicate rule named %s' % (rule['name']))
except EAException as e:
raise EAException('Error loading file %s: %s' % (rule_file, e))
rules.append(rule)
names.append(rule['name'])
if not rules:
logging.exception('No rules loaded. Exiting')
exit(1)
conf['rules'] = rules
return conf
示例6: load_rules
def load_rules(args):
""" Creates a conf dictionary for ElastAlerter. Loads the global
config file and then each rule found in rules_folder.
:param args: The parsed arguments to ElastAlert
:return: The global configuration, a dictionary.
"""
names = []
filename = args.config
conf = yaml_loader(filename)
use_rule = args.rule
# Make sure we have all required globals
if required_globals - frozenset(conf.keys()):
raise EAException("%s must contain %s" % (filename, ", ".join(required_globals - frozenset(conf.keys()))))
conf.setdefault("max_query_size", 10000)
conf.setdefault("scroll_keepalive", "30s")
conf.setdefault("disable_rules_on_error", True)
conf.setdefault("scan_subdirectories", True)
# Convert run_every, buffer_time into a timedelta object
try:
conf["run_every"] = datetime.timedelta(**conf["run_every"])
conf["buffer_time"] = datetime.timedelta(**conf["buffer_time"])
if "alert_time_limit" in conf:
conf["alert_time_limit"] = datetime.timedelta(**conf["alert_time_limit"])
else:
conf["alert_time_limit"] = datetime.timedelta(days=2)
if "old_query_limit" in conf:
conf["old_query_limit"] = datetime.timedelta(**conf["old_query_limit"])
else:
conf["old_query_limit"] = datetime.timedelta(weeks=1)
except (KeyError, TypeError) as e:
raise EAException("Invalid time format used: %s" % (e))
# Load each rule configuration file
rules = []
rule_files = get_file_paths(conf, use_rule)
for rule_file in rule_files:
try:
rule = load_configuration(rule_file, conf, args)
if rule["name"] in names:
raise EAException("Duplicate rule named %s" % (rule["name"]))
except EAException as e:
raise EAException("Error loading file %s: %s" % (rule_file, e))
rules.append(rule)
names.append(rule["name"])
if not rules:
logging.exception("No rules loaded. Exiting")
exit(1)
conf["rules"] = rules
return conf
示例7: get_account
def get_account(self, account_file):
""" Gets the username and password from an account file.
:param account_file: Name of the file which contains user and password information.
"""
account_conf = yaml_loader(account_file)
if 'user' not in account_conf or 'password' not in account_conf:
raise EAException('Account file must have user and password fields')
self.user = account_conf['user']
self.password = account_conf['password']
示例8: load_configuration
def load_configuration(filename):
""" Load a yaml rule file and fill in the relevant fields with objects.
:param filename: The name of a rule configuration file.
:return: The rule configuration, a dictionary.
"""
try:
rule = yaml_loader(filename)
except yaml.scanner.ScannerError as e:
raise EAException('Could not parse file %s: %s' % (filename, e))
rule['rule_file'] = os.path.split(filename)[-1]
load_options(rule)
load_modules(rule)
return rule
示例9: load_configuration
def load_configuration(filename, conf, args=None):
""" Load a yaml rule file and fill in the relevant fields with objects.
:param filename: The name of a rule configuration file.
:param conf: The global configuration dictionary, used for populating defaults.
:return: The rule configuration, a dictionary.
"""
try:
rule = yaml_loader(filename)
except yaml.scanner.ScannerError as e:
raise EAException('Could not parse file %s: %s' % (filename, e))
rule['rule_file'] = filename
load_options(rule, conf, args)
load_modules(rule, args)
return rule
示例10: load_rules
def load_rules(args):
""" Creates a conf dictionary for ElastAlerter. Loads the global
config file and then each rule found in rules_folder.
:param args: The parsed arguments to ElastAlert
:return: The global configuration, a dictionary.
"""
names = []
filename = args.config
conf = yaml_loader(filename)
use_rule = args.rule
# Make sure we have all required globals
if required_globals - frozenset(conf.keys()):
raise EAException('%s must contain %s' % (filename, ', '.join(required_globals - frozenset(conf.keys()))))
conf.setdefault('max_query_size', 10000)
conf.setdefault('scroll_keepalive', '30s')
conf.setdefault('disable_rules_on_error', True)
conf.setdefault('scan_subdirectories', True)
# Convert run_every, buffer_time into a timedelta object
try:
conf['run_every'] = datetime.timedelta(**conf['run_every'])
conf['buffer_time'] = datetime.timedelta(**conf['buffer_time'])
if 'alert_time_limit' in conf:
conf['alert_time_limit'] = datetime.timedelta(**conf['alert_time_limit'])
else:
conf['alert_time_limit'] = datetime.timedelta(days=2)
if 'old_query_limit' in conf:
conf['old_query_limit'] = datetime.timedelta(**conf['old_query_limit'])
else:
conf['old_query_limit'] = datetime.timedelta(weeks=1)
except (KeyError, TypeError) as e:
raise EAException('Invalid time format used: %s' % (e))
# Load each rule configuration file
rules = []
rule_files = get_file_paths(conf, use_rule)
for rule_file in rule_files:
try:
rule = load_configuration(rule_file, conf, args)
if rule['name'] in names:
raise EAException('Duplicate rule named %s' % (rule['name']))
except EAException as e:
raise EAException('Error loading file %s: %s' % (rule_file, e))
rules.append(rule)
names.append(rule['name'])
if not rules:
logging.exception('No rules loaded. Exiting')
exit(1)
# Warn if use_strf_index is used with %y, %M or %D
# (%y = short year, %M = minutes, %D = full date)
# añado el indice donde quiero que me busque las cosas que escribió: 'writeback_index*'
conf['readback_index']=conf['writeback_index'] + '*'
if (conf.get('writeback_index') and conf.get('writeback_index_fmt')):
try:
conf['writeback_index']=conf.get('writeback_index') + '%s' % (now.strftime(conf.get('writeback_index_fmt')))
except SyntaxError:
raise SyntaxError('error with the format of writeback_index_fmt in the config.yaml')
conf['rules'] = rules
return conf