# Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: david@reciprocitylabs.com
# Maintained By: david@reciprocitylabs.com

import sys
from ggrc import settings

def get_extension_name(extension_setting, default):
  extension_name = getattr(settings, extension_setting, default)
  if extension_name is not None:
    extension_name = extension_name if not callable(extension_name) else \
        extension_name()
  else:
    extension_name = default
  return extension_name

def get_extension_modules(modules=[]):
  if len(modules) == 0:
    extension_names = getattr(settings, 'EXTENSIONS')
    if extension_names is None:
      modules.append(None)
    else:
      for m in settings.EXTENSIONS:
        modules.append(get_extension_module(m))
  if len(modules) == 0 or modules[0] is None:
    return []
  else:
    return modules

def get_extension_module(module_name):
  __import__(module_name)
  return sys.modules[module_name]

def get_extension_module_for(extension_setting, default, extension_modules={}):
  if extension_setting not in extension_modules:
    extension_name = get_extension_name(extension_setting, default)
    if not extension_name:
      extension_modules[extension_setting] = extension_name
    else:
      __import__(extension_name)
      extension_modules[extension_setting] = sys.modules[extension_name]
  return extension_modules[extension_setting]

def get_extension_instance(extension_setting, default, extensions={}):
  if extension_setting not in extensions:
    from ggrc import settings
    extension_name = get_extension_name(extension_setting, default)
    idx = extension_name.rfind('.')
    module_name = extension_name[0:idx]
    class_name = extension_name[idx+1:]
    __import__(module_name)
    module = sys.modules[module_name]
    extensions[extension_setting] = getattr(module, class_name)(settings)
  return extensions[extension_setting]


@@ -272,16 +272,29 @@
 ettings%0A
+import ggrc%0A%0A
 %0Adef get
@@ -598,16 +598,17 @@
 n_name%0A%0A
+%0A
 def get_
@@ -957,16 +957,17 @@
 odules%0A%0A
+%0A
 def get_
@@ -1058,16 +1058,17 @@
 _name%5D%0A%0A
+%0A
 def get_
@@ -1506,16 +1506,17 @@
 tting%5D%0A%0A
+%0A
 def get_
@@ -1624,38 +1624,8 @@
 ns:%0A
-    from ggrc import settings%0A
     
@@ -1803,9 +1803,11 @@
 %5Bidx
-+
+ + 
 1:%5D%0A
@@ -1966,28 +1966,1630 @@
 ensions%5Bextension_setting%5D%0A%0A
+%0Adef _get_contribution(module, name):%0A  %22%22%22Fetch contributions from a single module.%0A%0A  Args:%0A    module: Python module that will be checked for a given attribute.%0A    name: Name of the attribute that we want to collect from a module. The%0A      attribute must be a list or a callable that returns a list.%0A%0A  Returns:%0A    List of contributions found%0A%0A  Raises:%0A    TypeError: If the attribute is not a list or a callable that returns a%0A      list.%0A  %22%22%22%0A  contributions = getattr(module, name, %5B%5D)%0A  if callable(contributions):%0A    contributions = contributions()%0A%0A  if not isinstance(contributions, list):%0A    raise TypeError(%22Contributed item must be a list or a callable that %22%0A                    %22returns a list%22)%0A  return contributions%0A%0A%0Adef get_module_contributions(name):%0A  %22%22%22Fetch contributions from all modules if they exist.%0A%0A  This function loops through all modules and checks if the main module package%0A  contains attribute with a given name or if it cotnains contributions which%0A  have an attribute with the said name. It gathers all such attributes in a%0A  list and returns it.%0A%0A  Args:%0A    name (string): name of the contributed attribute that will be collected.%0A%0A  Returns:%0A    A list of all collected atributes.%0A%0A  %22%22%22%0A  all_contributions = %5B%5D%0A  all_modules = %5Bggrc%5D + get_extension_modules()%0A  for module in all_modules:%0A    all_contributions.extend(_get_contribution(module, name))%0A    contributions_module = getattr(module, %22contributions%22, None)%0A    if contributions_module:%0A      all_contributions.extend(_get_contribution(contributions_module, name))%0A  return all_contributions%0A

Add get_module_contributions function