Skip to content

Optional items in FILENAME_METADATA expression. - #2117

Merged
justinmayer merged 1 commit into
getpelican:masterfrom
timwienk:fix-none-filename-metadata
Mar 20, 2017
Merged

justinmayer merged 1 commit into
getpelican:masterfrom
timwienk:fix-none-filename-metadata

Conversation

@timwienk

@timwienk timwienk commented Mar 11, 2017 •

Copy link
Copy Markdown
Contributor

To keep my articles somewhat organised, I like to add the date to the filename of the article and extract it using FILENAME_METADATA. However, for pages I don't quite want to do that. I used to use a simple plugin to set the date based on mtime, but since DEFAULT_DATE = 'fs' uses mtime now (since December 2016) instead of ctime, this option is great for this.

However, things go wrong when making the date capture group optional in the FILENAME_METADATA expression. Previously I monkey patched the pelican.readers.parse_path_metadata function, but since the DEFAULT_DATE = 'fs' option changed, I figured it's a good idea to try and get this part of the date configuration sorted within Pelican itself as well.

Case:

files:

  • content/index.md
  • content/articles/2017-03-11.interesting-article.md

pelicanconf.py:

FILENAME_METADATA = '(?:(?P<date>\d{4}-\d{2}-\d{2})\.)?(?P<slug>.*)\.md'

Error:

The regex match result for the "date" group is None for content/index.md. When Pelican attempts to process the date, None.replace() throws an AttributeError: 'NoneType' object has no attribute 'replace'.

Solution:

In addition to checking if k not in metadata when applying the matched filename metadata, also check if v is not None.

Please see the commit in the pull request for the actual change + added tests.

@ingwinlu ingwinlu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment thread pelican/readers.py Outdated
for k, v in match.groupdict().items():
k = k.lower() # metadata must be lowercase
if k not in metadata:
if k not in metadata and v is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that big of a deal but I'd prefer switching the comparison order for performance reasons. Comparison to None would be faster and should short-circuit earlier:

if v is not None and k not in metadata:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, that makes sense. I'll swap the order and replace this commit.

This is relevant when using optional items in the expression. E.g. if an
optional captured group is not matched, the result of
`match.groupdict()` contains the captured group with value `None`.
@timwienk
timwienk force-pushed the fix-none-filename-metadata branch from 97dafde to 4917b86 Compare March 15, 2017 16:09

@avaris avaris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@timwienk timwienk changed the title Optional items in FILE_METADATA expression. Optional items in FILENAME_METADATA expression. Mar 17, 2017
@justinmayer

Copy link
Copy Markdown
Member

Many thanks to @avaris and @ingwinlu for reviewing this pull request.

@justinmayer justinmayer added this to the 3.8.0 milestone Mar 20, 2017
@justinmayer

Copy link
Copy Markdown
Member

You are my new hero, Tim. Why, you ask?

  • clear description of your intended use case, perceived problem, and proposed solution
  • rebased & squashed into a single commit — without having to be asked
  • a single line change accompanied by three dozen lines of tests (!)

Thank you for fulfilling every project maintainer's dream. 😃 👍

@justinmayer
justinmayer merged commit fb07f4d into getpelican:master Mar 20, 2017
@timwienk
timwienk deleted the fix-none-filename-metadata branch March 21, 2017 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants