Browse Source

More robust module loading

main
Lakshmi Vyasarajan 14 years ago
parent
commit
a5e390648b
4 changed files with 96 additions and 33 deletions
  1. +42
    -0
      hyde/loader.py
  2. +54
    -0
      hyde/tests/test_loader.py
  3. +0
    -19
      hyde/tests/test_util.py
  4. +0
    -14
      hyde/util.py

+ 42
- 0
hyde/loader.py View File

@@ -0,0 +1,42 @@
"""
Generic loader of extensions (plugins & templates)
"""
import sys

from hyde.exceptions import HydeException

plugins = {}
templates = {}


def load_python_object(name):
"""
Loads a python module from string
"""
(module_name, _, object_name) = name.rpartition(".")
if module_name == '':
(module_name, object_name) = (object_name, module_name)
try:
module = __import__(module_name)
except ImportError:
raise HydeException("The given module name [%s] is invalid." %
module_name)

if object_name == '':
return module

try:
module = sys.modules[module_name]
except KeyError:
raise HydeException("Error occured when loading module [%s]" %
module_name)

try:
return getattr(module, object_name)
except AttributeError:
raise HydeException("Cannot load the specified plugin [%s]. "
"The given module [%s] does not contain the "
"desired object [%s]. Please fix the"
"configuration or ensure that the module is "
"installed properly" %
(name, module_name, object_name))

+ 54
- 0
hyde/tests/test_loader.py View File

@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""
Use nose
`$ pip install nose`
`$ nosetests`
"""

from hyde.loader import load_python_object
from nose.tools import raises
import os

from hyde.exceptions import HydeException


def test_can_load_locals():

file_class = load_python_object('hyde.fs.File')
assert file_class

f = file_class(__file__)
assert f

assert f.name == os.path.basename(__file__)


def test_can_load_from_python_path():

markdown = load_python_object('markdown.markdown')
assert markdown

assert "<h3>h3</h3>" == markdown("### h3")

def test_can_load_module_without_dot():

yaml = load_python_object('yaml')

abc = yaml.load("""
d: efg
l: mno
""")

assert abc['d'] == 'efg'
assert abc['l'] == 'mno'


@raises(HydeException)
def test_exception_raised_for_invalid_module():
load_python_object("junk.junk.junk")
assert False

@raises(HydeException)
def test_exception_raised_for_invalid_object():
load_python_object("markdown.junk")
assert False

+ 0
- 19
hyde/tests/test_util.py View File

@@ -1,19 +0,0 @@
# -*- coding: utf-8 -*-
"""
Use nose
`$ pip install nose`
`$ nosetests`
"""

from hyde.util import load_python_object
import os

def test_can_load_locals():

file_class = load_python_object('hyde.fs.File')
assert file_class

f = file_class(__file__)
assert f

assert f.name == os.path.basename(__file__)

+ 0
- 14
hyde/util.py View File

@@ -1,14 +0,0 @@
"""
Hyde utilities
"""
import sys


def load_python_object(name):
"""
Loads a python module from string
"""
(module_name, _ , object_name) = name.rpartition(".")
__import__(module_name)
module = sys.modules[module_name]
return getattr(module, object_name)

Loading…
Cancel
Save