Merge pull request #768 from wking/path-handling-cleanup

Cleanup path handling (os.path.join and more)
This commit is contained in:
Justin Mayer 2013-03-21 09:58:12 -07:00
commit 90822de962
12 changed files with 67 additions and 48 deletions

View file

@ -2,7 +2,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import sys, os import sys, os
sys.path.append(os.path.abspath('..')) sys.path.append(os.path.abspath(os.pardir))
from pelican import __version__, __major__ from pelican import __version__, __major__

View file

@ -52,7 +52,7 @@ class Pelican(object):
signals.initialized.send(self) signals.initialized.send(self)
def init_path(self): def init_path(self):
if not any(p in sys.path for p in ['', '.']): if not any(p in sys.path for p in ['', os.curdir]):
logger.debug("Adding current directory to system path") logger.debug("Adding current directory to system path")
sys.path.insert(0, '') sys.path.insert(0, '')

View file

@ -17,7 +17,7 @@ from pelican import signals
from pelican.settings import _DEFAULT_CONFIG from pelican.settings import _DEFAULT_CONFIG
from pelican.utils import (slugify, truncate_html_words, memoized, strftime, from pelican.utils import (slugify, truncate_html_words, memoized, strftime,
python_2_unicode_compatible, deprecated_attribute, python_2_unicode_compatible, deprecated_attribute,
split_all) path_to_url)
# Import these so that they're avalaible when you import from pelican.contents. # Import these so that they're avalaible when you import from pelican.contents.
from pelican.urlwrappers import (URLWrapper, Author, Category, Tag) # NOQA from pelican.urlwrappers import (URLWrapper, Author, Category, Tag) # NOQA
@ -139,7 +139,7 @@ class Content(object):
metadata = copy.copy(self.metadata) metadata = copy.copy(self.metadata)
path = self.metadata.get('path', self.get_relative_source_path()) path = self.metadata.get('path', self.get_relative_source_path())
metadata.update({ metadata.update({
'path': '/'.join(split_all(path)), 'path': path_to_url(path),
'slug': getattr(self, 'slug', ''), 'slug': getattr(self, 'slug', ''),
'lang': getattr(self, 'lang', 'en'), 'lang': getattr(self, 'lang', 'en'),
'date': getattr(self, 'date', datetime.now()), 'date': getattr(self, 'date', datetime.now()),

View file

@ -347,7 +347,7 @@ class ArticlesGenerator(Generator):
def generate_drafts(self, write): def generate_drafts(self, write):
"""Generate drafts pages.""" """Generate drafts pages."""
for article in self.drafts: for article in self.drafts:
write('drafts/%s.html' % article.slug, write(os.path.join('drafts', '%s.html' % article.slug),
self.get_template(article.template), self.context, self.get_template(article.template), self.context,
article=article, category=article.category, article=article, category=article.category,
all_articles=self.articles) all_articles=self.articles)
@ -565,7 +565,7 @@ class StaticGenerator(Generator):
f_rel = os.path.relpath(f, self.path) f_rel = os.path.relpath(f, self.path)
# TODO remove this hardcoded 'static' subdirectory # TODO remove this hardcoded 'static' subdirectory
dest = os.path.join('static', f_rel) dest = os.path.join('static', f_rel)
url = '/'.join(pelican.utils.split_all(dest)) url = pelican.utils.path_to_url(dest)
sc = Static( sc = Static(
content=None, content=None,
metadata={ metadata={
@ -588,7 +588,7 @@ class StaticGenerator(Generator):
def generate_output(self, writer): def generate_output(self, writer):
self._copy_paths(self.settings['THEME_STATIC_PATHS'], self.theme, self._copy_paths(self.settings['THEME_STATIC_PATHS'], self.theme,
'theme', self.output_path, '.') 'theme', self.output_path, os.curdir)
# copy all Static files # copy all Static files
for sc in self.staticfiles: for sc in self.staticfiles:
source_path = os.path.join(self.path, sc.source_path) source_path = os.path.join(self.path, sc.source_path)

View file

@ -15,21 +15,22 @@ from os.path import isabs
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
DEFAULT_THEME = os.sep.join([os.path.dirname(os.path.abspath(__file__)), DEFAULT_THEME = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"themes/notmyidea"]) 'themes', 'notmyidea')
_DEFAULT_CONFIG = {'PATH': '.', _DEFAULT_CONFIG = {'PATH': os.curdir,
'ARTICLE_DIR': '', 'ARTICLE_DIR': '',
'ARTICLE_EXCLUDES': ('pages',), 'ARTICLE_EXCLUDES': ('pages',),
'PAGE_DIR': 'pages', 'PAGE_DIR': 'pages',
'PAGE_EXCLUDES': (), 'PAGE_EXCLUDES': (),
'THEME': DEFAULT_THEME, 'THEME': DEFAULT_THEME,
'OUTPUT_PATH': 'output/', 'OUTPUT_PATH': 'output',
'MARKUP': ('rst', 'md'), 'MARKUP': ('rst', 'md'),
'STATIC_PATHS': ['images', ], 'STATIC_PATHS': ['images', ],
'THEME_STATIC_PATHS': ['static', ], 'THEME_STATIC_PATHS': ['static', ],
'FEED_ALL_ATOM': 'feeds/all.atom.xml', 'FEED_ALL_ATOM': os.path.join('feeds', 'all.atom.xml'),
'CATEGORY_FEED_ATOM': 'feeds/%s.atom.xml', 'CATEGORY_FEED_ATOM': os.path.join('feeds', '%s.atom.xml'),
'TRANSLATION_FEED_ATOM': 'feeds/all-%s.atom.xml', 'TRANSLATION_FEED_ATOM': os.path.join(
'feeds', 'all-%s.atom.xml'),
'FEED_MAX_ITEMS': '', 'FEED_MAX_ITEMS': '',
'SITEURL': '', 'SITEURL': '',
'SITENAME': 'A Pelican Blog', 'SITENAME': 'A Pelican Blog',
@ -49,17 +50,18 @@ _DEFAULT_CONFIG = {'PATH': '.',
'ARTICLE_LANG_URL': '{slug}-{lang}.html', 'ARTICLE_LANG_URL': '{slug}-{lang}.html',
'ARTICLE_LANG_SAVE_AS': '{slug}-{lang}.html', 'ARTICLE_LANG_SAVE_AS': '{slug}-{lang}.html',
'PAGE_URL': 'pages/{slug}.html', 'PAGE_URL': 'pages/{slug}.html',
'PAGE_SAVE_AS': 'pages/{slug}.html', 'PAGE_SAVE_AS': os.path.join('pages', '{slug}.html'),
'PAGE_LANG_URL': 'pages/{slug}-{lang}.html', 'PAGE_LANG_URL': 'pages/{slug}-{lang}.html',
'PAGE_LANG_SAVE_AS': 'pages/{slug}-{lang}.html', 'PAGE_LANG_SAVE_AS': os.path.join(
'pages', '{slug}-{lang}.html'),
'STATIC_URL': '{path}', 'STATIC_URL': '{path}',
'STATIC_SAVE_AS': '{path}', 'STATIC_SAVE_AS': '{path}',
'CATEGORY_URL': 'category/{slug}.html', 'CATEGORY_URL': 'category/{slug}.html',
'CATEGORY_SAVE_AS': 'category/{slug}.html', 'CATEGORY_SAVE_AS': os.path.join('category', '{slug}.html'),
'TAG_URL': 'tag/{slug}.html', 'TAG_URL': 'tag/{slug}.html',
'TAG_SAVE_AS': 'tag/{slug}.html', 'TAG_SAVE_AS': os.path.join('tag', '{slug}.html'),
'AUTHOR_URL': 'author/{slug}.html', 'AUTHOR_URL': 'author/{slug}.html',
'AUTHOR_SAVE_AS': 'author/{slug}.html', 'AUTHOR_SAVE_AS': os.path.join('author', '{slug}.html'),
'YEAR_ARCHIVE_SAVE_AS': False, 'YEAR_ARCHIVE_SAVE_AS': False,
'MONTH_ARCHIVE_SAVE_AS': False, 'MONTH_ARCHIVE_SAVE_AS': False,
'DAY_ARCHIVE_SAVE_AS': False, 'DAY_ARCHIVE_SAVE_AS': False,
@ -125,7 +127,7 @@ def get_settings_from_module(module=None, default_settings=_DEFAULT_CONFIG):
def get_settings_from_file(path, default_settings=_DEFAULT_CONFIG): def get_settings_from_file(path, default_settings=_DEFAULT_CONFIG):
"""Loads settings from a file path, returning a dict.""" """Loads settings from a file path, returning a dict."""
name = os.path.basename(path).rpartition('.')[0] name, ext = os.path.splitext(os.path.basename(path))
module = imp.load_source(name, path) module = imp.load_source(name, path)
return get_settings_from_module(module, default_settings=default_settings) return get_settings_from_module(module, default_settings=default_settings)
@ -141,8 +143,10 @@ def configure_settings(settings):
# lookup the theme in "pelican/themes" if the given one doesn't exist # lookup the theme in "pelican/themes" if the given one doesn't exist
if not os.path.isdir(settings['THEME']): if not os.path.isdir(settings['THEME']):
theme_path = os.sep.join([os.path.dirname( theme_path = os.path.join(
os.path.abspath(__file__)), "themes/%s" % settings['THEME']]) os.path.dirname(os.path.abspath(__file__)),
'themes',
settings['THEME'])
if os.path.exists(theme_path): if os.path.exists(theme_path):
settings['THEME'] = theme_path settings['THEME'] = theme_path
else: else:

View file

@ -13,9 +13,9 @@ from pelican.settings import read_settings
from pelican.tests.support import LoggedTestCase from pelican.tests.support import LoggedTestCase
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
SAMPLES_PATH = os.path.abspath(os.sep.join((CURRENT_DIR, "..", "..", SAMPLES_PATH = os.path.abspath(os.path.join(
"samples"))) CURRENT_DIR, os.pardir, os.pardir, 'samples'))
OUTPUT_PATH = os.path.abspath(os.sep.join((CURRENT_DIR, "output"))) OUTPUT_PATH = os.path.abspath(os.path.join(CURRENT_DIR, 'output'))
INPUT_PATH = os.path.join(SAMPLES_PATH, "content") INPUT_PATH = os.path.join(SAMPLES_PATH, "content")
SAMPLE_CONFIG = os.path.join(SAMPLES_PATH, "pelican.conf.py") SAMPLE_CONFIG = os.path.join(SAMPLES_PATH, "pelican.conf.py")
@ -23,11 +23,11 @@ SAMPLE_CONFIG = os.path.join(SAMPLES_PATH, "pelican.conf.py")
def recursiveDiff(dcmp): def recursiveDiff(dcmp):
diff = { diff = {
'diff_files': [os.sep.join((dcmp.right, f)) 'diff_files': [os.path.join(dcmp.right, f)
for f in dcmp.diff_files], for f in dcmp.diff_files],
'left_only': [os.sep.join((dcmp.right, f)) 'left_only': [os.path.join(dcmp.right, f)
for f in dcmp.left_only], for f in dcmp.left_only],
'right_only': [os.sep.join((dcmp.right, f)) 'right_only': [os.path.join(dcmp.right, f)
for f in dcmp.right_only], for f in dcmp.right_only],
} }
for sub_dcmp in dcmp.subdirs.values(): for sub_dcmp in dcmp.subdirs.values():
@ -74,7 +74,7 @@ class TestPelican(LoggedTestCase):
}) })
pelican = Pelican(settings=settings) pelican = Pelican(settings=settings)
pelican.run() pelican.run()
dcmp = dircmp(self.temp_path, os.sep.join((OUTPUT_PATH, "basic"))) dcmp = dircmp(self.temp_path, os.path.join(OUTPUT_PATH, 'basic'))
self.assertFilesEqual(recursiveDiff(dcmp)) self.assertFilesEqual(recursiveDiff(dcmp))
self.assertLogCountEqual( self.assertLogCountEqual(
count=10, count=10,
@ -90,5 +90,5 @@ class TestPelican(LoggedTestCase):
}) })
pelican = Pelican(settings=settings) pelican = Pelican(settings=settings)
pelican.run() pelican.run()
dcmp = dircmp(self.temp_path, os.sep.join((OUTPUT_PATH, "custom"))) dcmp = dircmp(self.temp_path, os.path.join(OUTPUT_PATH, 'custom'))
self.assertFilesEqual(recursiveDiff(dcmp)) self.assertFilesEqual(recursiveDiff(dcmp))

View file

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function from __future__ import unicode_literals, print_function
import copy import copy
import os
from os.path import dirname, abspath, join from os.path import dirname, abspath, join
from pelican.settings import (read_settings, configure_settings, from pelican.settings import (read_settings, configure_settings,
@ -62,7 +63,7 @@ class TestSettingsConfiguration(unittest.TestCase):
settings = { settings = {
'SITEURL': 'http://blog.notmyidea.org/', 'SITEURL': 'http://blog.notmyidea.org/',
'LOCALE': '', 'LOCALE': '',
'PATH': '.', 'PATH': os.curdir,
'THEME': DEFAULT_THEME, 'THEME': DEFAULT_THEME,
} }
configure_settings(settings) configure_settings(settings)

View file

@ -71,9 +71,10 @@ class TestUtils(LoggedTestCase):
def test_get_relative_path(self): def test_get_relative_path(self):
samples = (('test/test.html', '..'), samples = ((os.path.join('test', 'test.html'), os.pardir),
('test/test/test.html', '../..'), (os.path.join('test', 'test', 'test.html'),
('test.html', '.')) os.path.join(os.pardir, os.pardir)),
('test.html', os.curdir))
for value, expected in samples: for value, expected in samples:
self.assertEquals(utils.get_relative_path(value), expected) self.assertEquals(utils.get_relative_path(value), expected)

View file

@ -18,7 +18,7 @@ _TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
CONF = { CONF = {
'pelican': 'pelican', 'pelican': 'pelican',
'pelicanopts': '', 'pelicanopts': '',
'basedir': '.', 'basedir': os.curdir,
'ftp_host': 'localhost', 'ftp_host': 'localhost',
'ftp_user': 'anonymous', 'ftp_user': 'anonymous',
'ftp_target_dir': '/', 'ftp_target_dir': '/',
@ -146,7 +146,7 @@ def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="A kickstarter for Pelican", description="A kickstarter for Pelican",
formatter_class=argparse.ArgumentDefaultsHelpFormatter) formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-p', '--path', default=".", parser.add_argument('-p', '--path', default=os.curdir,
help="The path to generate the blog into") help="The path to generate the blog into")
parser.add_argument('-t', '--title', metavar="title", parser.add_argument('-t', '--title', metavar="title",
help='Set the title of the website') help='Set the title of the website')
@ -166,7 +166,8 @@ needed by Pelican.
'''.format(v=__version__)) '''.format(v=__version__))
project = os.path.join(os.environ.get('VIRTUAL_ENV', '.'), '.project') project = os.path.join(
os.environ.get('VIRTUAL_ENV', os.curdir), '.project')
if os.path.isfile(project): if os.path.isfile(project):
CONF['basedir'] = open(project, 'r').read().rstrip("\n") CONF['basedir'] = open(project, 'r').read().rstrip("\n")
print('Using project associated with current virtual environment.' print('Using project associated with current virtual environment.'

3
pelican/tools/templates/publishconf.py.in Normal file → Executable file
View file

@ -4,8 +4,9 @@
# This file is only used if you use `make publish` or # This file is only used if you use `make publish` or
# explicitly specify it as your config file. # explicitly specify it as your config file.
import os
import sys import sys
sys.path.append('.') sys.path.append(os.curdir)
from pelicanconf import * from pelicanconf import *
SITEURL = '$siteurl' SITEURL = '$siteurl'

View file

@ -304,11 +304,20 @@ def clean_output_dir(path):
def get_relative_path(path): def get_relative_path(path):
"""Return the relative path from the given path to the root path.""" """Return the relative path from the given path to the root path."""
nslashes = path.count('/') components = split_all(path)
if nslashes == 0: if len(components) <= 1:
return '.' return os.curdir
else: else:
return '/'.join(['..'] * nslashes) parents = [os.pardir] * (len(components) - 1)
return os.path.join(*parents)
def path_to_url(path):
"""Return the URL corresponding to a given path."""
if os.sep == '/':
return path
else:
'/'.join(split_all(path))
def truncate_html_words(s, num, end_text='...'): def truncate_html_words(s, num, end_text='...'):
@ -429,7 +438,7 @@ def files_changed(path, extensions, ignores=[]):
def file_times(path): def file_times(path):
"""Return the last time files have been modified""" """Return the last time files have been modified"""
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
dirs[:] = [x for x in dirs if x[0] != '.'] dirs[:] = [x for x in dirs if x[0] != os.curdir]
for f in files: for f in files:
if any(f.endswith(ext) for ext in extensions) \ if any(f.endswith(ext) for ext in extensions) \
and not any(fnmatch.fnmatch(f, ignore) and not any(fnmatch.fnmatch(f, ignore)

View file

@ -10,7 +10,7 @@ from codecs import open
from feedgenerator import Atom1Feed, Rss201rev2Feed from feedgenerator import Atom1Feed, Rss201rev2Feed
from jinja2 import Markup from jinja2 import Markup
from pelican.paginator import Paginator from pelican.paginator import Paginator
from pelican.utils import get_relative_path, set_date_tzinfo from pelican.utils import get_relative_path, path_to_url, set_date_tzinfo
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -60,7 +60,9 @@ class Writer(object):
old_locale = locale.setlocale(locale.LC_ALL) old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C')) locale.setlocale(locale.LC_ALL, str('C'))
try: try:
self.site_url = context.get('SITEURL', get_relative_path(path)) self.site_url = context.get(
'SITEURL', path_to_url(get_relative_path(path)))
self.feed_domain = context.get('FEED_DOMAIN') self.feed_domain = context.get('FEED_DOMAIN')
self.feed_url = '{}/{}'.format(self.feed_domain, path) self.feed_url = '{}/{}'.format(self.feed_domain, path)
@ -125,9 +127,9 @@ class Writer(object):
localcontext = context.copy() localcontext = context.copy()
if relative_urls: if relative_urls:
relative_path = get_relative_path(name) relative_url = path_to_url(get_relative_path(name))
context['localsiteurl'] = relative_path context['localsiteurl'] = relative_url
localcontext['SITEURL'] = relative_path localcontext['SITEURL'] = relative_url
localcontext['output_file'] = name localcontext['output_file'] = name
localcontext.update(kwargs) localcontext.update(kwargs)