Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_all_metric_mods(check_reqs=True):
ret = []
for metric_path in iter_find_files(METRICS_PATH, '*.py', ignored='__init__.py'):
mod_name = os.path.splitext(os.path.split(metric_path)[-1])[0]
metric_mod = imp.load_source(mod_name, metric_path)
if not callable(getattr(metric_mod, 'collect', None)):
print_err('skipping non-metric module at %r' % metric_path)
continue
if not check_reqs:
ret.append(metric_mod)
continue
missing_env_vars = _check_required_env_vars(metric_mod)
missing_cmds = _check_required_cmds(metric_mod)
if missing_cmds:
print_err('omitting metric "%s" due to missing commands: %s (see installation instructions above)'
% (metric_mod.__name__, ', '.join(missing_cmds)))
elif missing_env_vars:
print_err('omitting metric "%s" due to missing ENV variables: %s'
% (metric_mod.__name__, ', '.join(missing_env_vars)))
def _get_all_metric_mods(check_reqs=True):
ret = []
for metric_path in iter_find_files(METRICS_PATH, '*.py', ignored='__init__.py'):
mod_name = os.path.splitext(os.path.split(metric_path)[-1])[0]
metric_mod = imp.load_source(mod_name, metric_path)
if not callable(getattr(metric_mod, 'collect', None)):
print_err('skipping non-metric module at %r' % metric_path)
continue
if not check_reqs:
ret.append(metric_mod)
continue
missing_env_vars = _check_required_env_vars(metric_mod)
missing_cmds = _check_required_cmds(metric_mod)
if missing_cmds:
print_err('omitting metric "%s" due to missing commands: %s (see installation instructions above)'
% (metric_mod.__name__, ', '.join(missing_cmds)))
elif missing_env_vars:
print_err('omitting metric "%s" due to missing ENV variables: %s'
% (metric_mod.__name__, ', '.join(missing_env_vars)))
def from_timestamp(cls, campaign, timestamp, full=True):
strf_tmpl = STATE_FULL_PATH_TMPL if full else STATE_PATH_TMPL
# this handles when a date object is passed in for timestamp
# (instead of a datetime)
strf_tmpl = strf_tmpl.replace('000000', '*')
start_pattern = timestamp.strftime(strf_tmpl)
dir_path = campaign.base_path + os.path.split(start_pattern)[0]
file_paths = sorted(iter_find_files(dir_path, os.path.split(start_pattern)[1]))
try:
first_path = file_paths[0]
except IndexError:
raise StateNotFound('no state found for campaign %r at timestamp %s'
% (campaign, timestamp))
return cls.from_json_path(campaign, first_path, full=full)
plat_map = plist.get_projects_by_type('platform')
plat_toc_text = format_tag_toc(plat_map)
projects_by_plat = format_all_categories(plat_map)
context = {'TOPIC_TOC': topic_toc_text,
'TOPIC_TEXT': projects_by_topic,
'PLATFORM_TOC': plat_toc_text,
'PLATFORM_TEXT': projects_by_plat,
'TOTAL_COUNT': len(plist.project_list)}
templates_path = pdir + '/templates/'
if not os.path.isdir(templates_path):
raise APACLIError('expected "templates" directory at %r' % templates_path)
for filename in iter_find_files(templates_path, '*.tmpl.md'):
tmpl_text = open(filename).read()
target_filename = os.path.split(filename)[1].replace('.tmpl', '')
output_text = tmpl_text.format(**context)
with atomic_save(pdir + '/' + target_filename) as f:
f.write(output_text.encode('utf8'))
return
@chlog.wrap('critical', 'load site')
def load(self):
self.last_load = time.time()
self._load_custom_mod()
self._call_custom_hook('pre_load')
self.html_renderer = AshesEnv(paths=[self.theme_path])
self.html_renderer.load_all()
self.md_renderer = AshesEnv(paths=[self.theme_path],
exts=['md'],
keep_whitespace=False)
self.md_renderer.autoescape_filter = ''
self.md_renderer.load_all()
entries_path = self.paths['entries_path']
entry_paths = []
for entry_path in iter_find_files(entries_path, ENTRY_PATS):
entry_paths.append(entry_path)
entry_paths.sort()
for ep in entry_paths:
with chlog.info('entry load') as rec:
try:
entry = self._entry_type.from_path(ep)
rec['entry_title'] = entry.title
rec['entry_length'] = round(entry.get_reading_time(), 1)
except IOError:
rec.exception('unopenable entry path: {}', ep)
continue
except:
rec['entry_path'] = ep
rec.exception('entry {entry_path} load error: {exc_message}')
continue
else:
def generate_stubs(self, path: Path) -> List[Tuple[Path, Path]]:
"""Generate Stub Files from a package.
Args:
path (Path): Path to package.
Returns:
List[Tuple[Path, Path]]: List of tuples containing
a path to the original file and stub, respectively.
"""
py_files = fileutils.iter_find_files(str(path), patterns='*.py', ignored=self._ignore_stubs)
stubs = [utils.generate_stub(f) for f in py_files]
return stubs
def get_state_filepaths(data_dir, full=True):
pattern = STATE_FULL_FN_GLOB if full else STATE_FN_GLOB
return sorted(iter_find_files(data_dir, pattern))
def _iter_changed_files(entries_path, theme_path, config_path, interval=0.5):
mtimes = {}
while True:
changed = []
to_check = itertools.chain([config_path],
iter_find_files(entries_path, ENTRY_PATS),
iter_find_files(theme_path, '*'))
for path in to_check:
try:
new_mtime = os.stat(path).st_mtime
except OSError:
continue
old_mtime = mtimes.get(path)
if not old_mtime or new_mtime > old_mtime:
mtimes[path] = new_mtime
changed.append(path)
if changed:
yield changed
time.sleep(interval)