Mercurial > piecrust2
comparison 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 |
comparison
equal
deleted
inserted
replaced
61:64f37c4cce68 | 62:52e4d9a1f917 |
---|---|
1 import os.path | 1 import os.path |
2 import codecs | 2 import codecs |
3 import logging | 3 import logging |
4 import yaml | 4 import yaml |
5 from piecrust.pathutil import SiteNotFoundError | 5 from piecrust.pathutil import SiteNotFoundError, multi_fnmatch_filter |
6 | 6 |
7 | 7 |
8 logger = logging.getLogger(__name__) | 8 logger = logging.getLogger(__name__) |
9 | 9 |
10 | 10 |
24 raise SiteNotFoundError() | 24 raise SiteNotFoundError() |
25 self.importWebsite(ctx.app, ctx.args) | 25 self.importWebsite(ctx.app, ctx.args) |
26 return 0 | 26 return 0 |
27 | 27 |
28 | 28 |
29 class FileWalkingImporter(Importer): | |
30 def setupParser(self, parser, app): | |
31 parser.add_argument('--exclude', nargs='+', | |
32 help=("Patterns of files and directories to exclude " | |
33 "from the import (always includes `.git*`, " | |
34 "`.hg*`, `.svn`, `.bzr`).")) | |
35 | |
36 def _startWalk(self, root_dir, exclude, *args, **kwargs): | |
37 if exclude is None: | |
38 exclude = [] | |
39 exclude += ['.git*', '.hg*', '.svn', '.bzr'] | |
40 | |
41 for dirpath, dirnames, filenames in os.walk(root_dir): | |
42 rel_dirpath = os.path.relpath(dirpath, root_dir) | |
43 if rel_dirpath == '.': | |
44 rel_dirpath = '' | |
45 | |
46 dirnames[:] = multi_fnmatch_filter( | |
47 dirnames, exclude, | |
48 modifier=lambda d: os.path.join(rel_dirpath, d), | |
49 inverse=True) | |
50 filenames = multi_fnmatch_filter( | |
51 filenames, exclude, | |
52 modifier=lambda f: os.path.join(rel_dirpath, f), | |
53 inverse=True) | |
54 | |
55 for fn in filenames: | |
56 full_fn = os.path.join(dirpath, fn) | |
57 rel_fn = os.path.join(rel_dirpath, fn) | |
58 self._importFile(full_fn, rel_fn, *args, **kwargs) | |
59 | |
60 | |
29 def create_page(app, endpoint_dir, slug, metadata, content): | 61 def create_page(app, endpoint_dir, slug, metadata, content): |
30 path = os.path.join(app.root_dir, endpoint_dir, slug) | 62 path = os.path.join(app.root_dir, endpoint_dir, slug) |
31 logging.debug("Creating page: %s" % os.path.relpath(path, app.root_dir)) | 63 logging.debug("Creating page: %s" % os.path.relpath(path, app.root_dir)) |
32 header = yaml.dump(metadata) | 64 header = yaml.dump(metadata) |
33 os.makedirs(os.path.dirname(path), 0o755, True) | 65 os.makedirs(os.path.dirname(path), 0o755, True) |
34 with codecs.open(path, 'w', 'utf8') as fp: | 66 with codecs.open(path, 'w', encoding='utf8') as fp: |
35 fp.write("---\n") | 67 fp.write("---\n") |
36 fp.write(header) | 68 fp.write(header) |
37 fp.write("---\n") | 69 fp.write("---\n") |
38 fp.write(content) | 70 fp.write(content) |
39 | 71 |