diff piecrust/importing/base.py @ 62:52e4d9a1f917

Simple importer for PieCrust 1 websites.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 27 Aug 2014 17:14:44 -0700
parents 6e60e0fef2be
children 28958565a17b
line wrap: on
line diff
--- a/piecrust/importing/base.py	Wed Aug 27 10:23:32 2014 -0700
+++ b/piecrust/importing/base.py	Wed Aug 27 17:14:44 2014 -0700
@@ -2,7 +2,7 @@
 import codecs
 import logging
 import yaml
-from piecrust.pathutil import SiteNotFoundError
+from piecrust.pathutil import SiteNotFoundError, multi_fnmatch_filter
 
 
 logger = logging.getLogger(__name__)
@@ -26,12 +26,44 @@
         return 0
 
 
+class FileWalkingImporter(Importer):
+    def setupParser(self, parser, app):
+        parser.add_argument('--exclude', nargs='+',
+                help=("Patterns of files and directories to exclude "
+                      "from the import (always includes `.git*`, "
+                      "`.hg*`, `.svn`, `.bzr`)."))
+
+    def _startWalk(self, root_dir, exclude, *args, **kwargs):
+        if exclude is None:
+            exclude = []
+        exclude += ['.git*', '.hg*', '.svn', '.bzr']
+
+        for dirpath, dirnames, filenames in os.walk(root_dir):
+            rel_dirpath = os.path.relpath(dirpath, root_dir)
+            if rel_dirpath == '.':
+                rel_dirpath = ''
+
+            dirnames[:] = multi_fnmatch_filter(
+                    dirnames, exclude,
+                    modifier=lambda d: os.path.join(rel_dirpath, d),
+                    inverse=True)
+            filenames = multi_fnmatch_filter(
+                    filenames, exclude,
+                    modifier=lambda f: os.path.join(rel_dirpath, f),
+                    inverse=True)
+
+            for fn in filenames:
+                full_fn = os.path.join(dirpath, fn)
+                rel_fn = os.path.join(rel_dirpath, fn)
+                self._importFile(full_fn, rel_fn, *args, **kwargs)
+
+
 def create_page(app, endpoint_dir, slug, metadata, content):
     path = os.path.join(app.root_dir, endpoint_dir, slug)
     logging.debug("Creating page: %s" % os.path.relpath(path, app.root_dir))
     header = yaml.dump(metadata)
     os.makedirs(os.path.dirname(path), 0o755, True)
-    with codecs.open(path, 'w', 'utf8') as fp:
+    with codecs.open(path, 'w', encoding='utf8') as fp:
         fp.write("---\n")
         fp.write(header)
         fp.write("---\n")