content: Convert Path.filename to .source_path

Making everything consistent is a bit awkward, since this is a
commonly used attribute, but I've done my best.

Reasons for not consolidating on `filename`:

* It is often used for the "basename" (last component in the path).
  Using `source_path` makes it clear that this attribute can contain
  multiple components.

Reasons for not consolidating on `filepath`:

* It is barely used in the Pelican source, and therefore easy to
  change.
* `path` is more Pythonic.  The only place `filepath` ever show up in
  the documentation for `os`, `os.path`, and `shutil` is in the
  `os.path.relpath` documentation [1].

Reasons for not consolidating on `path`:

* The Page elements have both a source (this attribute) and a
  destination (.save_as).  To avoid confusion for developers not aware
  of this, make it painfully obvious that this attribute is for the
  source.  Explicit is better than implicit ;).

Where I was touching the line, I also updated the string formatting in
StaticGenerator.generate_output to use the forward compatible
'{}'.format() syntax.

[1]: http://docs.python.org/2/library/os.path.html#os.path.relpath
This commit is contained in:
W. Trevor King 2013-01-04 10:50:09 -05:00
commit 004adfa5cc
6 changed files with 81 additions and 79 deletions

View file

@ -32,7 +32,7 @@ class Page(object):
default_template = 'page'
def __init__(self, content, metadata=None, settings=None,
filename=None, context=None):
source_path=None, context=None):
# init parameters
if not metadata:
metadata = {}
@ -75,8 +75,8 @@ class Page(object):
if not hasattr(self, 'slug') and hasattr(self, 'title'):
self.slug = slugify(self.title)
if filename:
self.filename = filename
if source_path:
self.source_path = source_path
# manage the date format
if not hasattr(self, 'date_format'):
@ -160,8 +160,8 @@ class Page(object):
if value.startswith('/'):
value = value[1:]
else:
# relative to the filename of this content
value = self.get_relative_filename(
# relative to the source path of this content
value = self.get_relative_source_path(
os.path.join(self.relative_dir, value)
)
@ -215,24 +215,25 @@ class Page(object):
else:
return self.default_template
def get_relative_filename(self, filename=None):
def get_relative_source_path(self, source_path=None):
"""Return the relative path (from the content path) to the given
filename.
source_path.
If no filename is specified, use the filename of this content object.
If no source path is specified, use the source path of this
content object.
"""
if not filename:
filename = self.filename
if not source_path:
source_path = self.source_path
return os.path.relpath(
os.path.abspath(os.path.join(self.settings['PATH'], filename)),
os.path.abspath(os.path.join(self.settings['PATH'], source_path)),
os.path.abspath(self.settings['PATH'])
)
@property
def relative_dir(self):
return os.path.dirname(os.path.relpath(
os.path.abspath(self.filename),
os.path.abspath(self.source_path),
os.path.abspath(self.settings['PATH']))
)
@ -305,11 +306,11 @@ class StaticContent(object):
settings = copy.deepcopy(_DEFAULT_CONFIG)
self.src = src
self.url = dst or src
self.filename = os.path.join(settings['PATH'], src)
self.source_path = os.path.join(settings['PATH'], src)
self.save_as = os.path.join(settings['OUTPUT_PATH'], self.url)
def __str__(self):
return self.filename
return self.source_path
def is_valid_content(content, f):