<commit_before>require "digest/md5"
require "extend/cachable"

# The Formulary is responsible for creating instances of Formula.
# It is not meant to be used directly from formulae.

module Formulary
  extend Cachable

  def self.formula_class_defined?(path)
    cache.key?(path)
  end

  def self.formula_class_get(path)
    cache.fetch(path)
  end

  def self.load_formula(name, path, contents, namespace)
    if ENV["HOMEBREW_DISABLE_LOAD_FORMULA"]
      raise "Formula loading disabled by HOMEBREW_DISABLE_LOAD_FORMULA!"
    end

    mod = Module.new
    const_set(namespace, mod)
    begin
      mod.module_eval(contents, path)
    rescue ScriptError => e
      raise FormulaUnreadableError.new(name, e)
    end
    class_name = class_s(name)

    begin
      mod.const_get(class_name)
    rescue NameError => original_exception
      class_list = mod.constants
                      .map { |const_name| mod.const_get(const_name) }
                      .select { |const| const.is_a?(Class) }
      e = FormulaClassUnavailableError.new(name, path, class_name, class_list)
      raise e, "", original_exception.backtrace
    end
  end

  def self.load_formula_from_path(name, path)
    contents = path.open("r") { |f| ensure_utf8_encoding(f).read }
    namespace = "FormulaNamespace#{Digest::MD5.hexdigest(path.to_s)}"
    klass = load_formula(name, path, contents, namespace)
    cache[path] = klass<commit_msg>formulary: catch argument errors.<commit_after>require "digest/md5"
require "extend/cachable"

# The Formulary is responsible for creating instances of Formula.
# It is not meant to be used directly from formulae.

module Formulary
  extend Cachable

  def self.formula_class_defined?(path)
    cache.key?(path)
  end

  def self.formula_class_get(path)
    cache.fetch(path)
  end

  def self.load_formula(name, path, contents, namespace)
    if ENV["HOMEBREW_DISABLE_LOAD_FORMULA"]
      raise "Formula loading disabled by HOMEBREW_DISABLE_LOAD_FORMULA!"
    end

    mod = Module.new
    const_set(namespace, mod)
    begin
      mod.module_eval(contents, path)
    rescue ArgumentError, ScriptError => e
      raise FormulaUnreadableError.new(name, e)
    end
    class_name = class_s(name)

    begin
      mod.const_get(class_name)
    rescue NameError => original_exception
      class_list = mod.constants
                      .map { |const_name| mod.const_get(const_name) }
                      .select { |const| const.is_a?(Class) }
      e = FormulaClassUnavailableError.new(name, path, class_name, class_list)
      raise e, "", original_exception.backtrace
    end
  end

  def self.load_formula_from_path(name, path)
    contents = path.open("r") { |f| ensure_utf8_encoding(f).read }
    namespace = "FormulaNamespace#{Digest::MD5.hexdigest(path.to_s)}"
    klass = load_formula(name, path, contents, namespace)
    cache[path] = klass
{'fraction_non_alphanumeric': 0.09312870596442274, 'fraction_numerical': 0.0020927799093128706, 'mean_word_length': 3.3719512195121952, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6485016', 'n_tokens_mistral': 936, 'n_tokens_neox': 917, 'n_words': 211}