<commit_before>"""
kit (command-line interface) specific data and code

Importing this module could potentially be a performance hit as it tries to
pre-cache recipes which would be wasteful for a library to do.
"""

from . import load
from .exceptions import ParseError
import glob
import os
import warnings
import yaml

config_path = os.path.expanduser("~/.pyprika")
if os.path.exists(config_path):
  with open(config_path) as fp:
    config = yaml.load(fp)
else:
  config = {}
if not isinstance(config, dict):
  warnings.warn('invalid user configuration; skipping')
  config = {}

class _Registry(object):
  def __init__(self):
    self.recipes = {}
    self.search(os.path.curdir)
    paths = config.get('paths', [])
    recursive = config.get('recursive', False)
    search = self.search if not recursive else self.recursive_search
    for p in paths:
      search(p)

  def recursive_search(self, path):
    skip_hidden = config.get('skip_hidden', True)
    for root, dirnames, filenames in os.walk(path, topdown=True):
      if skip_hidden:
        dirnames[:] = [d for d in dirnames if not d.startswith('.')]
        filenames[:] = [f for f in filenames if not f.startswith('.')]
      filenames[:] = [f for f in filenames if f.endswith('.yaml')]
      for f in filenames:
        self.add(os.path.join(root, f))

  def search(self, path):
    files = glob.glob(os.path.join(path, '*.yaml'))
    for f in files:
      try:
        self.add(f)
      except ParseError:
        pass
    
  def add(self, path):
    recipe = load(path)
    self.recipes[recipe.name] = recipe

registry = _Registry()<commit_msg>catch LoadError when searching directories<commit_after>"""
kit (command-line interface) specific data and code

Importing this module could potentially be a performance hit as it tries to
pre-cache recipes which would be wasteful for a library to do.
"""

from . import load
from .exceptions import LoadError 
import glob
import os
import warnings
import yaml

config_path = os.path.expanduser("~/.pyprika")
if os.path.exists(config_path):
  with open(config_path) as fp:
    config = yaml.load(fp)
else:
  config = {}
if not isinstance(config, dict):
  warnings.warn('invalid user configuration; skipping')
  config = {}

class _Registry(object):
  def __init__(self):
    self.recipes = {}
    self.search(os.path.curdir)
    paths = config.get('paths', [])
    recursive = config.get('recursive', False)
    search = self.search if not recursive else self.recursive_search
    for p in paths:
      search(p)

  def recursive_search(self, path):
    skip_hidden = config.get('skip_hidden', True)
    for root, dirnames, filenames in os.walk(path, topdown=True):
      if skip_hidden:
        dirnames[:] = [d for d in dirnames if not d.startswith('.')]
        filenames[:] = [f for f in filenames if not f.startswith('.')]
      filenames[:] = [f for f in filenames if f.endswith('.yaml')]
      for f in filenames:
        try:
          self.add(os.path.join(root, f))
        except LoadError:
          pass

  def search(self, path):
    files = glob.glob(os.path.join(path, '*.yaml'))
    for f in files:
      try:
        self.add(f)
      except LoadError:
        pass
    
  def add(self, path):
    recipe = load(path)
    self.recipes[recipe.name] = recipe

registry = _Registry()
{'fraction_non_alphanumeric': 0.11835748792270531, 'fraction_numerical': 0.0, 'mean_word_length': 3.172544080604534, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5124710', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1053, 'n_words': 331}