Mercurial > piecrust2
annotate 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 |
rev | line source |
---|---|
60
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os.path |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import codecs |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import logging |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import yaml |
62
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
5 from piecrust.pathutil import SiteNotFoundError, multi_fnmatch_filter |
60
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 logger = logging.getLogger(__name__) |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 class Importer(object): |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 def __init__(self): |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 self.name = None |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 self.description = None |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 def setupParser(self, parser, app): |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 raise NotImplementedError() |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 def importWebsite(self, app, args): |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 raise NotImplementedError() |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 def checkedImportWebsite(self, ctx): |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 if ctx.app.root_dir is None: |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 raise SiteNotFoundError() |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 self.importWebsite(ctx.app, ctx.args) |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 return 0 |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 |
62
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
29 class FileWalkingImporter(Importer): |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
30 def setupParser(self, parser, app): |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
31 parser.add_argument('--exclude', nargs='+', |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
32 help=("Patterns of files and directories to exclude " |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
33 "from the import (always includes `.git*`, " |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
34 "`.hg*`, `.svn`, `.bzr`).")) |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
35 |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
36 def _startWalk(self, root_dir, exclude, *args, **kwargs): |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
37 if exclude is None: |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
38 exclude = [] |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
39 exclude += ['.git*', '.hg*', '.svn', '.bzr'] |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
40 |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
41 for dirpath, dirnames, filenames in os.walk(root_dir): |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
42 rel_dirpath = os.path.relpath(dirpath, root_dir) |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
43 if rel_dirpath == '.': |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
44 rel_dirpath = '' |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
45 |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
46 dirnames[:] = multi_fnmatch_filter( |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
47 dirnames, exclude, |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
48 modifier=lambda d: os.path.join(rel_dirpath, d), |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
49 inverse=True) |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
50 filenames = multi_fnmatch_filter( |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
51 filenames, exclude, |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
52 modifier=lambda f: os.path.join(rel_dirpath, f), |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
53 inverse=True) |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
54 |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
55 for fn in filenames: |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
56 full_fn = os.path.join(dirpath, fn) |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
57 rel_fn = os.path.join(rel_dirpath, fn) |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
58 self._importFile(full_fn, rel_fn, *args, **kwargs) |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
59 |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
60 |
60
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 def create_page(app, endpoint_dir, slug, metadata, content): |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 path = os.path.join(app.root_dir, endpoint_dir, slug) |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 logging.debug("Creating page: %s" % os.path.relpath(path, app.root_dir)) |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 header = yaml.dump(metadata) |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 os.makedirs(os.path.dirname(path), 0o755, True) |
62
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
60
diff
changeset
|
66 with codecs.open(path, 'w', encoding='utf8') as fp: |
60
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 fp.write("---\n") |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 fp.write(header) |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 fp.write("---\n") |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 fp.write(content) |
6e60e0fef2be
Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 |