From d9d35d5b2d376d89dbfe6b1d75a8b52c2569b415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramiro=20G=C3=B3mez?= Date: Tue, 21 Apr 2015 23:30:32 +0200 Subject: [PATCH] Added path module and tests for it Moved logya.get_path to path.join. --- logya/core.py | 60 +++++++++++++++++++--------------------------- logya/path.py | 20 ++++++++++++++++ tests/test_path.py | 21 ++++++++++++++++ 3 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 logya/path.py create mode 100644 tests/test_path.py diff --git a/logya/core.py b/logya/core.py index d20d887..cd7de29 100644 --- a/logya/core.py +++ b/logya/core.py @@ -5,6 +5,7 @@ from operator import itemgetter +from logya import path from logya.compat import execfile, is3 from logya.config import Config from logya.docreader import DocReader @@ -41,23 +42,24 @@ def init_env(self): environment and sets object properties. """ - self.dir_content = self.get_path('content', required=True) - self.config = Config(self.get_path('site.yaml', required=True)) + cwd = self.dir_current + self.dir_content = path.join(cwd, 'content', required=True) + self.config = Config(path.join(cwd, 'site.yaml', required=True)) - self.dir_templates = self.get_path('templates', required=True) + self.dir_templates = path.join(cwd, 'templates', required=True) self.template = Template(self) - self.dir_bin = self.get_path('bin') + self.dir_bin = path.join(cwd, 'bin') # make all settings in site section available to templates for key, val in list(self.config.section('site').items()): self.template.vars[key] = val # Optional directory with static files like css, js and images. - self.dir_static = self.get_path('static') + self.dir_static = path.join(cwd, 'static') # Directory is created by the generate command. - self.dir_dst = self.get_path('deploy') + self.dir_dst = path.join(cwd, 'deploy') self.base_url = self.config.get('site', 'base_url') # base_url must be defined in settings @@ -70,17 +72,6 @@ def info(self, msg): if self.verbose: print(msg) - def get_path(self, name, required=False): - """Get path relative to current working directory for given name. - - Raises an exception if resource is required and doesn't exist. - """ - - path = os.path.join(self.dir_current, name) - if required and not os.path.exists(path): - raise Exception('Resource at path {} does not exist.'.format(path)) - return path - def get_doc_template(self, doc): """Get template setting from doc otherwise from configuration.""" @@ -100,11 +91,6 @@ def get_dirs_from_path(self, url): dirs = dirs[:-1] return dirs - def update_index(self, doc, path): - """Add a doc to given path index.""" - - self.indexes[path] = self.indexes.get(path, []) + [doc] - def _update_indexes(self, doc, url=None): """Add a doc to indexes determined from given url.""" @@ -115,7 +101,8 @@ def _update_indexes(self, doc, url=None): last = 0 for d in dirs: last += 1 - self.update_index(doc, '/'.join(dirs[:last])) + fullpath = '/'.join(dirs[:last]) + self.indexes[fullpath] = self.indexes.get(fullpath, []) + [doc] def update_indexes(self, doc): """Add all indexes for doc determined from headers.""" @@ -132,15 +119,18 @@ def update_indexes(self, doc): if idx['var'] in doc: self.update_doc_index(doc, idx['var'], idx['path']) - def update_doc_index(self, doc, var, path): + def update_doc_index(self, doc, var, basepath): """Add the doc to the index defined for the header variable (var).""" for val in doc[var]: - var_path = re.sub(self.re_url_replace, '-', val).lower() - url = '/{}/{}/'.format(path, var_path) + url = '/{}/{}/'.format( + basepath, + re.sub(self.re_url_replace, '-', val).lower()) + links = var + '_links' doc[links] = doc.get(links, []) + [(url, val)] - # must append path after tag string to create subdir + + # Must append file name to url to create subdir. self._update_indexes(doc, url + self.index_filename) def build_indexes(self, mode=None): @@ -192,17 +182,17 @@ def write_rss(self, feed_title, directory, docs): writer = FileWriter() page = self.template.env.get_template('rss2.xml') - fh = writer.file_handle(self.dir_dst, os.path.join(directory, 'rss.xml')) + fh = writer.file_handle(self.dir_dst, path.join(directory, 'rss.xml')) writer.write(fh, page.render(self.template.vars)) def write_index(self, filewriter, directory, template): """Write an auto-generated index.html file.""" - url_path = '/{}'.format(os.path.join(directory, self.index_filename)) + urlpath = '/{}'.format(path.join(directory, self.index_filename)) # make sure there exists no document at the index path - if url_path not in self.docs_parsed: + if urlpath not in self.docs_parsed: # remove the file name part if it's index.html - url = url_path.replace(self.index_filename, '') + url = urlpath.replace(self.index_filename, '') docs = self.indexes[directory] @@ -259,11 +249,11 @@ def write_indexes(self): self.write_rss(feed_title, '', docs) # TODO remove in 4.0 - def get_execs(self, path): + def get_execs(self, dirname): """Generator yielding paths to executable files in given directory.""" - for p in os.listdir(path): - fpath = os.path.join(path, p) + for p in os.listdir(dirname): + fpath = path.join(dirname, p) if os.path.isfile(fpath) and os.access(fpath, os.X_OK): yield fpath @@ -271,7 +261,7 @@ def get_execs(self, path): def exec_bin(self): """Execute binary files in bin dir.""" - dir_bin = self.get_path('bin') + dir_bin = path.join('bin') if os.path.exists(dir_bin): args = {'logya': self} for exe in self.get_execs(dir_bin): diff --git a/logya/path.py b/logya/path.py new file mode 100644 index 0000000..9cf78b9 --- /dev/null +++ b/logya/path.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Wrappers for functions from os.path. +import os + + +class PathResourceError(Exception): + pass + + +def join(basedir, *args, **kwargs): + """Get joined name relative to basedir for file or path name. + + Raises an exception if resource is required and doesn't exist. + """ + + path = os.path.join(basedir, *args) + if kwargs.get('required') and not os.path.exists(path): + raise PathResourceError( + 'Resource at path {} does not exist.'.format(path)) + return path diff --git a/tests/test_path.py b/tests/test_path.py new file mode 100644 index 0000000..5010f70 --- /dev/null +++ b/tests/test_path.py @@ -0,0 +1,21 @@ +import unittest + +from logya import path + + +class TestPath(unittest.TestCase): + + def test_join(self): + self.assertEqual( + 'dir/file.ext', path.join('dir', 'file.ext')) + self.assertEqual( + 'dir/subdir/file.ext', path.join('dir', 'subdir', 'file.ext')) + + def test_join_required_exists(self): + self.assertEqual('tests', path.join('tests', required=True)) + + def test_join_required_not_exists(self): + self.assertRaises(path.PathResourceError, + path.join, + *('dir', 'file.ext'), + required=True)