def import_class_from_config_section(config,section):
  """
  Imports the section as a class, calling the relevant class initialization
  method using the options as parameters. 
  """
  if not config.has_option(section,"config2class_import_module_name") or not config.has_option(section,"config2class_import_class_name"):
    raise Exception("The config file, " + config_file + ", does not have the appropriate"
      "required config2class options in the section name "+ section)
  module_name = config.get(section,"config2class_import_module_name")
  class_name = config.get(section,"config2class_import_class_name")
  config2class_options = ["config2class_import_module_name",
                          "config2class_import_class_name"]
  
  class_obj = get_class_obj(module_name,class_name)
  arg_spec = inspect.getargspec(class_obj.__init__)
  args_list = list(arg_spec[0][1:-len(arg_spec[3])]) #This is a list of the required args in order.

  input_args =[]
  for arg in args_list:
    input_args.append(config.get(section,arg))
  kwargs = {}
  for option in config.options(section):
    if option in args_list + config2class_options:
      continue
    kwargs[option] = config.get(section,option)
  
  return class_obj(*input_args,**kwargs)
Example #2
0
def import_class_from_config_section(config, section):
    """
  Imports the section as a class, calling the relevant class initialization
  method using the options as parameters. 
  """
    if not config.has_option(
            section,
            "config2class_import_module_name") or not config.has_option(
                section, "config2class_import_class_name"):
        raise Exception("The config file, " + config_file +
                        ", does not have the appropriate"
                        "required config2class options in the section name " +
                        section)
    module_name = config.get(section, "config2class_import_module_name")
    class_name = config.get(section, "config2class_import_class_name")
    config2class_options = [
        "config2class_import_module_name", "config2class_import_class_name"
    ]

    class_obj = get_class_obj(module_name, class_name)
    arg_spec = inspect.getargspec(class_obj.__init__)
    args_list = list(
        arg_spec[0]
        [1:-len(arg_spec[3])])  #This is a list of the required args in order.

    input_args = []
    for arg in args_list:
        input_args.append(config.get(section, arg))
    kwargs = {}
    for option in config.options(section):
        if option in args_list + config2class_options:
            continue
        kwargs[option] = config.get(section, option)

    return class_obj(*input_args, **kwargs)
Example #3
0
def export_class_as_config(module_name,
                           class_name,
                           instance_name=None,
                           config=None,
                           **kwargs):
    """
  Takes a class from the specified module and returns a config object with the
  section specifying the instance name and the options specifying the arguments
  for the __init__() method. 
  Args:
    module_name: The name of the module where the code for the class is.
    class_name: The name of the class to be converted to a config object.
    instance_name: The name of the section in the config file.  If 
      this is None, the section will be assumed to be class_name.
    config: A SageConfigParser object to which the class will be added.
      If this object is None, then a new object is initialized and
      passed back as the output.
  Return value:
    config: A SafeConfigParser object now containing the class_name
    from module_name. 
  """
    #Handle optional arguments
    if config is None:
        config = SafeConfigParser()
        config.read('allow_no_value.ini')
    if instance_name is None:
        instance_name = class_name

    #Load module and get the needed class objects.
    class_obj = get_class_obj(module_name, class_name)

    #Obtain all of the arguments for initiatilization of the class.
    all_args = get_all_init_args(class_obj, **kwargs)
    documentation = get_class_documentation(class_obj)

    #Create the section.
    config.add_section(instance_name)
    for line in documentation:
        config.set(instance_name, ";" + line, "")
    config.set(instance_name, "config2class_import_module_name", module_name)
    config.set(instance_name, "config2class_import_class_name", class_name)
    for arg in all_args["args"]:
        config.set(instance_name, arg, "")
    for i in range(len(all_args["default_args"])):
        arg = "#" + all_args["default_args"][i]
        value = str(all_args["default_values"][i])
        config.set(instance_name, arg, value)
    return config
def export_class_as_config(module_name,class_name,instance_name=None,config=None,**kwargs):
  """
  Takes a class from the specified module and returns a config object with the
  section specifying the instance name and the options specifying the arguments
  for the __init__() method. 
  Args:
    module_name: The name of the module where the code for the class is.
    class_name: The name of the class to be converted to a config object.
    instance_name: The name of the section in the config file.  If 
      this is None, the section will be assumed to be class_name.
    config: A SageConfigParser object to which the class will be added.
      If this object is None, then a new object is initialized and
      passed back as the output.
  Return value:
    config: A SafeConfigParser object now containing the class_name
    from module_name. 
  """
  #Handle optional arguments
  if config is None:
    config = SafeConfigParser()
    config.read('allow_no_value.ini')
  if instance_name is None:
    instance_name = class_name
  
  #Load module and get the needed class objects.
  class_obj = get_class_obj(module_name,class_name)

  #Obtain all of the arguments for initiatilization of the class.
  all_args = get_all_init_args(class_obj,**kwargs)
  documentation = get_class_documentation(class_obj)

  #Create the section.
  config.add_section(instance_name)
  for line in documentation:
    config.set(instance_name,";" + line,"")
  config.set(instance_name,"config2class_import_module_name",module_name)
  config.set(instance_name,"config2class_import_class_name",class_name)
  for arg in all_args["args"]:
    config.set(instance_name,arg,"")
  for i in range(len(all_args["default_args"])):
    arg = "#"+all_args["default_args"][i]
    value = str(all_args["default_values"][i])
    config.set(instance_name,arg,value)
  return config