comparison piecrust/importing/piecrust.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
children 28958565a17b
comparison
equal deleted inserted replaced
61:64f37c4cce68 62:52e4d9a1f917
1 import os
2 import os.path
3 import re
4 import shutil
5 import logging
6 from piecrust.importing.base import FileWalkingImporter
7
8
9 logger = logging.getLogger(__name__)
10
11
12 class PieCrust1Importer(FileWalkingImporter):
13 def __init__(self):
14 super(PieCrust1Importer, self).__init__()
15 self.name = 'piecrust1'
16 self.description = "Imports content from a PieCrust 1 website."
17
18 def setupParser(self, parser, app):
19 super(PieCrust1Importer, self).setupParser(parser, app)
20 parser.add_argument('root_dir',
21 help="The root directory of the PieCrust 1 website.")
22
23 def importWebsite(self, app, args):
24 logger.debug("Importing PieCrust 1 site from: %s" % args.root_dir)
25 exclude = args.exclude or []
26 exclude += ['_cache', '_counter']
27 self._startWalk(args.root_dir, exclude, app)
28 logger.info("The PieCrust website was successfully imported.")
29
30 def _importFile(self, full_fn, rel_fn, app):
31 logger.debug("- %s" % rel_fn)
32 dest_path = rel_fn
33 convert_func = None
34 if rel_fn.replace('\\', '/') == '_content/config.yml':
35 dest_path = 'config.yml'
36 convert_func = self.convertConfig
37 elif rel_fn.startswith('_content'):
38 dest_path = rel_fn[len('_content/'):]
39 fn_dirname = os.path.dirname(rel_fn)
40 if not fn_dirname.endswith('-assets'):
41 convert_func = self.convertPage
42 else:
43 dest_path = 'assets/' + rel_fn
44
45 logger.debug(" %s -> %s" % (rel_fn, dest_path))
46 full_dest_path = os.path.join(app.root_dir, dest_path)
47 os.makedirs(os.path.dirname(full_dest_path), 0o755, True)
48 if convert_func is None:
49 shutil.copy2(full_fn, full_dest_path)
50 else:
51 with open(full_fn, 'r', encoding='utf8') as fp:
52 content = fp.read()
53 converted_content = convert_func(content)
54 with open(full_dest_path, 'w', encoding='utf8') as fp:
55 fp.write(converted_content)
56 if converted_content != content:
57 logger.warning("'%s' has been modified. The original version "
58 "has been kept for reference." % rel_fn)
59 shutil.copy2(full_fn, full_dest_path + '.orig')
60
61 def convertConfig(self, content):
62 return content
63
64 def convertPage(self, content):
65 return content
66