mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
added importlib.py with test and moved settings around as well as utils
This commit is contained in:
parent
3dcfe1846c
commit
9506802558
10 changed files with 135 additions and 5 deletions
|
|
@ -62,7 +62,8 @@ class Pelican(object):
|
|||
# if it's a string, then import it
|
||||
if isinstance(plugin, six.string_types):
|
||||
logger.debug("Loading plugin `{0}' ...".format(plugin))
|
||||
plugin = __import__(plugin, globals(), locals(), 'module')
|
||||
# http://stackoverflow.com/questions/1971356/haystack-whoosh-index-generation-error#answer-2683624
|
||||
plugin = __import__(plugin, globals(), locals(), 'module'.encode('ascii'))
|
||||
|
||||
logger.debug("Registering plugin `{0}'".format(plugin.__name__))
|
||||
plugin.register()
|
||||
|
|
@ -247,6 +248,7 @@ def parse_arguments():
|
|||
action='store_true',
|
||||
help="Relaunch pelican each time a modification occurs"
|
||||
" on the content files.")
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
|
|
@ -284,6 +286,7 @@ def main():
|
|||
args = parse_arguments()
|
||||
init(args.verbosity)
|
||||
pelican = get_instance(args)
|
||||
|
||||
|
||||
try:
|
||||
if args.autoreload:
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ from datetime import datetime
|
|||
|
||||
|
||||
from pelican import signals
|
||||
from pelican.settings import _DEFAULT_CONFIG
|
||||
from pelican.settings.base import _DEFAULT_CONFIG
|
||||
from pelican.utils import (slugify, truncate_html_words, memoized, strftime,
|
||||
python_2_unicode_compatible, deprecated_attribute,
|
||||
path_to_url)
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@ from pygments.lexers import get_lexer_by_name, TextLexer
|
|||
import re
|
||||
|
||||
INLINESTYLES = False
|
||||
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES)
|
||||
# import ipdb;ipdb.set_trace()
|
||||
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES, linenos=True)
|
||||
VARIANTS = {
|
||||
'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
|
||||
}
|
||||
|
|
|
|||
6
pelican/settings/__init__.py
Normal file
6
pelican/settings/__init__.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from pelican.settings.base import \
|
||||
read_settings, get_settings_from_module, get_settings_from_file, \
|
||||
configure_settings, DEFAULT_THEME, _DEFAULT_CONFIG
|
||||
|
||||
from pelican.settings.conf import settings as conf
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals, print_function
|
||||
import six
|
||||
|
||||
|
|
@ -11,6 +10,8 @@ import logging
|
|||
|
||||
from os.path import isabs
|
||||
|
||||
from pelican.settings.conf import load_settings_into_global_conf
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -111,7 +112,10 @@ def read_settings(path=None, override=None):
|
|||
if override:
|
||||
local_settings.update(override)
|
||||
|
||||
return configure_settings(local_settings)
|
||||
|
||||
settings = configure_settings(local_settings)
|
||||
load_settings_into_global_conf(settings)
|
||||
return settings
|
||||
|
||||
|
||||
def get_settings_from_module(module=None, default_settings=_DEFAULT_CONFIG):
|
||||
|
|
@ -256,3 +260,4 @@ def configure_settings(settings):
|
|||
settings[PATH_KEY] = _DEFAULT_CONFIG[PATH_KEY]
|
||||
|
||||
return settings
|
||||
|
||||
24
pelican/settings/conf.py
Normal file
24
pelican/settings/conf.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
__all__ = ['load_settings_into_global_conf']
|
||||
|
||||
class settings(object):
|
||||
"""
|
||||
Just a wrapper to the object interface.
|
||||
We're generating a module so we can import from it:
|
||||
|
||||
>>> from pelican.conf import settings
|
||||
>>> settings.PATH
|
||||
"/Users/jbcurtin/workspace/blog/content"
|
||||
>>> from pelican.conf.settings import PATH
|
||||
>>> PATH
|
||||
"/Users/jbcurtin/workspace/blog/content"
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
def load_settings_into_global_conf(new_settings):
|
||||
for key, value in new_settings.iteritems():
|
||||
# for key,value in read_settings(args.settings, override=get_config(args)).iteritems():
|
||||
setattr(settings, key, value)
|
||||
|
||||
return settings
|
||||
49
pelican/tests/test_importlib.py
Normal file
49
pelican/tests/test_importlib.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals, print_function
|
||||
import codecs
|
||||
import logging
|
||||
import shutil
|
||||
import os
|
||||
import datetime
|
||||
import time
|
||||
import unittest
|
||||
import sys
|
||||
import shutil
|
||||
|
||||
from pelican.utils.importlib import import_module
|
||||
|
||||
# runtest with:
|
||||
# cd ~/pelican/pelican/tests && python -m unittest discover . 'test_importlib*' && cd -
|
||||
class TestImportLib(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
if not os.path.exists('/tmp/pytest'):
|
||||
os.makedirs('/tmp/pytest')
|
||||
with codecs.open('/tmp/pytest/__init__.py','wb+', 'utf-8') as stream:
|
||||
stream.write('# -*- coding: utf-8 -*-')
|
||||
|
||||
with codecs.open('/tmp/pytest/injected_module.py', 'wb+', 'utf-8') as stream:
|
||||
stream.write('''# -*- coding: utf-8 -*-
|
||||
class Awesome(object):
|
||||
"""
|
||||
This is pretty awesome.
|
||||
"""
|
||||
def cute(self):
|
||||
return True
|
||||
|
||||
sauce = Awesome()
|
||||
''')
|
||||
sys.path.append('/tmp')
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree('/tmp/pytest')
|
||||
|
||||
def test_import_lib(self):
|
||||
assert os.path.exists('/tmp/pytest/injected_module.py')
|
||||
assert '/tmp' in sys.path
|
||||
|
||||
module = import_module('pytest.injected_module')
|
||||
assert hasattr(module, 'Awesome')
|
||||
assert hasattr(module, 'sauce')
|
||||
assert module.sauce.cute()
|
||||
|
||||
|
|
@ -153,3 +153,6 @@ class TestUtils(LoggedTestCase):
|
|||
f.close()
|
||||
utils.clean_output_dir(test_directory)
|
||||
self.assertTrue(not os.path.exists(test_directory))
|
||||
|
||||
def test_import_lib(self):
|
||||
import ipdb;ipdb.set_trace()
|
||||
|
|
|
|||
39
pelican/utils/importlib.py
Normal file
39
pelican/utils/importlib.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# Taken from Python 2.7 with permission from/by the original author.
|
||||
# Taken from django source:
|
||||
# -*- coding: utf-8 -*-
|
||||
# https://github.com/django/django/blob/a84d79f572fbe7512b999c6b3cd7667cbe3138ff/django/utils/importlib.py
|
||||
import sys
|
||||
|
||||
def _resolve_name(name, package, level):
|
||||
"""Return the absolute name of the module to be imported."""
|
||||
if not hasattr(package, 'rindex'):
|
||||
raise ValueError("'package' not set to a string")
|
||||
dot = len(package)
|
||||
for x in range(level, 1, -1):
|
||||
try:
|
||||
dot = package.rindex('.', 0, dot)
|
||||
except ValueError:
|
||||
raise ValueError("attempted relative import beyond top-level "
|
||||
"package")
|
||||
return "%s.%s" % (package[:dot], name)
|
||||
|
||||
|
||||
def import_module(name, package=None):
|
||||
"""Import a module.
|
||||
|
||||
The 'package' argument is required when performing a relative import. It
|
||||
specifies the package to use as the anchor point from which to resolve the
|
||||
relative import to an absolute import.
|
||||
|
||||
"""
|
||||
if name.startswith('.'):
|
||||
if not package:
|
||||
raise TypeError("relative imports require the 'package' argument")
|
||||
level = 0
|
||||
for character in name:
|
||||
if character != '.':
|
||||
break
|
||||
level += 1
|
||||
name = _resolve_name(name[level:], package, level)
|
||||
__import__(name)
|
||||
return sys.modules[name]
|
||||
Loading…
Add table
Add a link
Reference in a new issue