Example #1
0
 def testIsDirectlyConstructableReturnsTrue(self):
     self.assertTrue(
         classes.IsDirectlyConstructable(ClassWithoutInitDefOne))
     self.assertTrue(
         classes.IsDirectlyConstructable(ClassWithoutInitDefTwo))
     self.assertTrue(
         classes.IsDirectlyConstructable(ClassWhoseInitOnlyHasSelf))
     self.assertTrue(
         classes.IsDirectlyConstructable(
             ClassWhoseInitWithDefaultArguments))
Example #2
0
def DiscoverClassesInModule(module,
                            base_class,
                            index_by_class_name=False,
                            directly_constructable=False):
  """Discover all classes in |module| which subclass |base_class|.

  Base classes that contain subclasses are ignored by default.

  Args:
    module: The module to search.
    base_class: The base class to search for.
    index_by_class_name: If True, use class name converted to
        lowercase_with_underscores instead of module name in return dict keys.

  Returns:
    dict of {module_name: class} or {underscored_class_name: class}
  """
  classes = {}
  for _, obj in inspect.getmembers(module):
    # Ensure object is a class.
    if not inspect.isclass(obj):
      continue
    # Include only subclasses of base_class.
    if not issubclass(obj, base_class):
      continue
    # Exclude the base_class itself.
    if obj is base_class:
      continue
    # Exclude protected or private classes.
    if obj.__name__.startswith('_'):
      continue
    # Include only the module in which the class is defined.
    # If a class is imported by another module, exclude those duplicates.
    if obj.__module__ != module.__name__:
      continue

    if index_by_class_name:
      key_name = camel_case.ToUnderscore(obj.__name__)
    else:
      key_name = module.__name__.split('.')[-1]
    if (not directly_constructable or
        classes_module.IsDirectlyConstructable(obj)):
      if key_name in classes and index_by_class_name:
        assert classes[key_name] is obj, (
          'Duplicate key_name with different objs detected: '
          'key=%s, obj1=%s, obj2=%s' % (key_name, classes[key_name], obj))
      else:
        classes[key_name] = obj

  return classes
Example #3
0
def DiscoverClassesInModule(module,
                            base_class,
                            one_class_per_module=False,
                            directly_constructable=False):
    """Discover all classes in |module| which subclass |base_class|.

  Base classes that contain subclasses are ignored by default.

  Args:
    module: The module to search.
    base_class: The base class to search for.
    one_class_per_module: If True, will only include the first class found in
                each module.

  Returns: A list of classes.
  """
    classes = []
    for _, obj in inspect.getmembers(module):
        # Ensure object is a class.
        if not inspect.isclass(obj):
            continue
        # Include only subclasses of base_class.
        if not issubclass(obj, base_class):
            continue
        # Exclude the base_class itself.
        if obj is base_class:
            continue
        # Exclude protected or private classes.
        if obj.__name__.startswith('_'):
            continue
        # Include only the module in which the class is defined.
        # If a class is imported by another module, exclude those duplicates.
        if obj.__module__ != module.__name__:
            continue

        if (not directly_constructable
                or classes_module.IsDirectlyConstructable(obj)):
            classes.append(obj)
            if one_class_per_module:
                return classes
    return classes
Example #4
0
 def testIsDirectlyConstructableReturnsFalse(self):
     self.assertFalse(
         classes.IsDirectlyConstructable(
             ClassWhoseInitWithDefaultArgumentsAndNonDefaultArguments))