Skip to content

Commit

Permalink
Better support for a whole tree of data files
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Aug 30, 2016
1 parent 6497f53 commit c09787a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,11 @@
New in master
=============

Features
--------

* Better support for a tree of files in ``data/``

New in v7.8.0
=============

Expand Down
14 changes: 7 additions & 7 deletions nikola/nikola.py
Expand Up @@ -32,7 +32,6 @@
from copy import copy
from pkg_resources import resource_filename
import datetime
import glob
import locale
import os
import json
Expand Down Expand Up @@ -1136,12 +1135,13 @@ def _set_global_context_from_config(self):

def _set_global_context_from_data(self):
"""Load files from data/ and put them in the global context."""
self._GLOBAL_CONTEXT['data'] = {}
for fname in glob.glob('data/*'):
data = utils.load_data(fname)
key = os.path.basename(fname)
key = os.path.splitext(key)[0]
self._GLOBAL_CONTEXT['data'][key] = data
self._GLOBAL_CONTEXT['data'] = defaultdict(defaultdict)
for root, dirs, files in os.walk('data', followlinks=True):
for fname in files:
fname = os.path.join(root, fname)
data = utils.load_data(fname)
key = os.path.splitext(fname.split(os.sep, 1)[1])[0]
self._GLOBAL_CONTEXT['data'][key] = data

def _activate_plugins_of_category(self, category):
"""Activate all the plugins of a given category and return them."""
Expand Down
3 changes: 3 additions & 0 deletions nikola/utils.py
Expand Up @@ -1923,6 +1923,7 @@ def prefixed_lines():
def load_data(path):
"""Given path to a file, load data from it."""
ext = os.path.splitext(path)[-1]
loader = None
if ext in {'.yml', '.yaml'}:
loader = yaml
if yaml is None:
Expand All @@ -1935,5 +1936,7 @@ def load_data(path):
req_missing(['toml'], 'use TOML data files')
return {}
loader = toml
if loader is None:
return
with io.open(path, 'r', encoding='utf8') as inf:
return loader.load(inf)

0 comments on commit c09787a

Please sign in to comment.