<commit_before>import os
from traceback import format_exc

import settings
import functions_general
"""
This module is responsible for managing scripts and their connection to the
Object class model. It is important to keep this as independent from the
codebase as possible in order to allow for drop-in replacements. All
interaction with actual script methods should happen via calls to Objects.
"""

# A dictionary with keys equivalent to the script's name and values that
# contain references to the associated module for each key.
cached_scripts = {}

def scriptlink(source_obj, scriptname):
   """
   Each Object will refer to this function when trying to execute a function
   contained within a scripted module. For the sake of ease of management,
   modules are cached and compiled as they are requested and stored in
   the cached_scripts dictionary.
   
   Returns a reference to an instance of the script's class as per it's
   class_factory() method.
   
   source_obj: (Object) A reference to the object being scripted.
   scriptname: (str) Name of the module to load (minus 'scripts').
   """
   # The module is already cached, just return it rather than re-load.
   retval = cached_scripts.get(scriptname, False)
   if retval:
      return retval.class_factory(source_obj)
   
   # Split the script name up by periods to give us the directory we need
   # to change to. I really wish we didn't have to do this, but there's some
   # strange issue with __import__ and more than two directories worth of
   # nesting.
   path_split = scriptname.split('.')
   newpath_str = '/'.join(path_split[:-1])
   # Lop the module name off the end.
   modname = path_split[-1]

   try:
      # Change the working directory to the location of the script and import.
      os.chdir('%s/%s/' % (settings.SCRIPT_ROOT, newpath_str))
      functions_general.log_infomsg("SCRIPT: Caching and importing %s." % (modname))
      modreference = __import__(modname)
      # Store the module reference for later fast retrieval.
      cached_scripts[scriptname] = modreference
   except ImportError:<commit_msg>Comment clarification.<commit_after>import os
from traceback import format_exc

import settings
import functions_general
"""
This module is responsible for managing scripts and their connection to the
Object class model. It is important to keep this as independent from the
codebase as possible in order to allow for drop-in replacements. All
interaction with actual script methods should happen via calls to Objects.
"""

# A dictionary with keys equivalent to the script's name and values that
# contain references to the associated module for each key.
cached_scripts = {}

def scriptlink(source_obj, scriptname):
   """
   Each Object will refer to this function when trying to execute a function
   contained within a scripted module. For the sake of ease of management,
   modules are cached and compiled as they are requested and stored in
   the cached_scripts dictionary.
   
   Returns a reference to an instance of the script's class as per it's
   class_factory() method.
   
   source_obj: (Object) A reference to the object being scripted.
   scriptname: (str) Name of the module to load (minus 'scripts').
   """
   # The module is already cached, just return it rather than re-load.
   retval = cached_scripts.get(scriptname, False)
   if retval:
      return retval.class_factory(source_obj)

   ##
   ## NOTE: Only go past here when the script isn't already cached.
   ##
   
   # Split the script name up by periods to give us the directory we need
   # to change to. I really wish we didn't have to do this, but there's some
   # strange issue with __import__ and more than two directories worth of
   # nesting.
   path_split = scriptname.split('.')
   newpath_str = '/'.join(path_split[:-1])
   # Lop the module name off the end.
   modname = path_split[-1]

   try:
      # Change the working directory to the location of the script and import.
      os.chdir('%s/%s/' % (settings.SCRIPT_ROOT, newpath_str))
      functions_general.log_infomsg("SCRIPT: Caching and importing %s." % (modname))
      modreference = __import__(modname)
      # Store the module reference for later fast retrieval.
      cached_scripts[scriptname] = modreference
   except ImportError:
{'fraction_non_alphanumeric': 0.06439570695286981, 'fraction_numerical': 0.0009332711152589828, 'mean_word_length': 3.6699346405228757, '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': '6767092', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1114, 'n_words': 539}