Skip to content

Commit

Permalink
Introduced get_class_from_module and def get_class_from_name.
Browse files Browse the repository at this point in the history
Refatored get_class_from config to use get_class_from_module.

Refs: candango#251
  • Loading branch information
piraz committed Dec 21, 2018
1 parent fdad9b7 commit 96b986b
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions firenado/config.py
Expand Up @@ -14,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib
import yaml
import logging

Expand Down Expand Up @@ -56,17 +55,55 @@ def get_config_from_package(package):
return package_conf


def get_class_from_config(config, index="class"):
def get_class_from_name(name):
""" Return a class reference from the class name provided as a parameter.
Class name must be the full class reference, in another words, the module
with the class absolute reference.
Example:
>>> get_class_from_name("my.module.Myclass")
:param basestring name: Class absolute reference.
:return: The class resolved from the absolute reference name provided.
"""
return get_class_from_module(
".".join(name.split(".")[:-1]),
name.split(".")[-1]
)


def get_class_from_module(module, class_name):
""" Returns a class from a config dict bit containing the module
and class references.
:param
config: The config bit with module and class references.
index:
:param basestring module: The module name.
:param basestring class_name: The class name.
:return: The class located at the module referred by the config.
"""
module = importlib.import_module(config['module'])
return getattr(module, config[index])
import importlib
module = importlib.import_module(module)
return getattr(module, class_name)


def get_class_from_config(config, index="class"):
""" Return a class from a config dict bit containing the indexes module and
class.
Examples:
When class name index into the config is class:
>>> config = {'module': "my.module", 'index': "MyClass"}
>>> get_class_from_config(config)
When class name index into config is custom:
>>> config = {'module': "my.module", 'my_class': "MyClass"}
>>> get_class_from_config(config, index="my_class")
:param dict config: Config containing index and module information.
:param basestring index: Index to be used to get the class name
:return: The class resolved at the module referred into the config.
"""
return get_class_from_module(config['module'], config[index])


def load_yaml_config_file(path):
Expand Down

0 comments on commit 96b986b

Please sign in to comment.