Mercurial > piecrust2
comparison piecrust/importing/base.py @ 300:2daa05a21026
import: Add an XML-based Wordpress importer.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 11 Mar 2015 23:48:35 -0700 |
parents | 2823ea40cfac |
children | 9093618aea08 |
comparison
equal
deleted
inserted
replaced
299:88bffd469b04 | 300:2daa05a21026 |
---|---|
1 import os.path | 1 import os.path |
2 import shutil | |
2 import codecs | 3 import codecs |
3 import logging | 4 import logging |
4 import yaml | 5 import yaml |
6 from urllib.parse import urlparse | |
7 from urllib.request import urlopen | |
5 from piecrust.pathutil import SiteNotFoundError, multi_fnmatch_filter | 8 from piecrust.pathutil import SiteNotFoundError, multi_fnmatch_filter |
6 | 9 |
7 | 10 |
8 logger = logging.getLogger(__name__) | 11 logger = logging.getLogger(__name__) |
9 | 12 |
55 full_fn = os.path.join(dirpath, fn) | 58 full_fn = os.path.join(dirpath, fn) |
56 rel_fn = os.path.join(rel_dirpath, fn) | 59 rel_fn = os.path.join(rel_dirpath, fn) |
57 self._importFile(full_fn, rel_fn, *args, **kwargs) | 60 self._importFile(full_fn, rel_fn, *args, **kwargs) |
58 | 61 |
59 | 62 |
60 def create_page(app, endpoint_dir, slug, metadata, content): | 63 def create_page(app, rel_path, metadata, content): |
61 path = os.path.join(app.root_dir, endpoint_dir, slug) | 64 path = os.path.join(app.root_dir, rel_path) |
62 logging.debug("Creating page: %s" % os.path.relpath(path, app.root_dir)) | 65 logging.info("Creating page: %s" % rel_path) |
63 header = yaml.dump(metadata) | 66 header = yaml.dump(metadata) |
64 os.makedirs(os.path.dirname(path), 0o755, True) | 67 os.makedirs(os.path.dirname(path), 0o755, True) |
65 with codecs.open(path, 'w', encoding='utf8') as fp: | 68 with codecs.open(path, 'w', encoding='utf8') as fp: |
66 fp.write("---\n") | 69 fp.write("---\n") |
67 fp.write(header) | 70 fp.write(header) |
68 fp.write("---\n") | 71 fp.write("---\n") |
69 fp.write(content) | 72 fp.write(content) |
70 | 73 |
74 | |
75 def download_asset(app, url, rel_path=None, skip_if_exists=True): | |
76 if rel_path is None: | |
77 parsed_url = urlparse(url) | |
78 rel_path = 'assets/' + parsed_url.path.lstrip('/') | |
79 path = os.path.join(app.root_dir, rel_path) | |
80 if skip_if_exists and os.path.exists(path): | |
81 return | |
82 logger.info("Downloading %s" % rel_path) | |
83 os.makedirs(os.path.dirname(path), 0o755, True) | |
84 with urlopen(url) as resp, open(path, 'wb') as fp: | |
85 shutil.copyfileobj(resp, fp) | |
86 |