paul@100 | 1 | #!/usr/bin/env python |
paul@100 | 2 | |
paul@100 | 3 | """ |
paul@100 | 4 | Directory output context. |
paul@100 | 5 | |
paul@219 | 6 | Copyright (C) 2018, 2019 Paul Boddie <paul@boddie.org.uk> |
paul@100 | 7 | |
paul@100 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@100 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@100 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@100 | 11 | version. |
paul@100 | 12 | |
paul@100 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@100 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@100 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@100 | 16 | details. |
paul@100 | 17 | |
paul@100 | 18 | You should have received a copy of the GNU General Public License along with |
paul@100 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@100 | 20 | """ |
paul@100 | 21 | |
paul@100 | 22 | from moinformat.output.common import Output |
paul@104 | 23 | from moinformat.utils.directory import Directory |
paul@312 | 24 | from os.path import extsep, join, sep |
paul@100 | 25 | |
paul@137 | 26 | class DirectoryOutput(Output): |
paul@100 | 27 | |
paul@100 | 28 | "A directory output context." |
paul@100 | 29 | |
paul@104 | 30 | name = "directory" |
paul@100 | 31 | |
paul@165 | 32 | def __init__(self, metadata): |
paul@100 | 33 | |
paul@165 | 34 | "Initialise the context with the given 'metadata'." |
paul@100 | 35 | |
paul@165 | 36 | if not metadata.has_key("output_filename"): |
paul@165 | 37 | raise ValueError, metadata |
paul@100 | 38 | |
paul@165 | 39 | Output.__init__(self, metadata) |
paul@165 | 40 | self.dir = Directory(metadata.get("output_filename")) |
paul@137 | 41 | self.dir.ensure() |
paul@140 | 42 | |
paul@312 | 43 | # Support an encoding of the level separator for the filesystem. |
paul@312 | 44 | # Where it is the same as the directory separator, documents are stored |
paul@312 | 45 | # using nested directories, not as a flat list. |
paul@312 | 46 | |
paul@312 | 47 | self.level_sep = metadata.get("output_separator", sep) |
paul@312 | 48 | |
paul@253 | 49 | # Use any document index setting as the default for the index filename. |
paul@253 | 50 | |
paul@253 | 51 | document_index = metadata.get("document_index", "index.html") |
paul@253 | 52 | self.index_name = metadata.get("index_name", document_index) |
paul@253 | 53 | self.root_pagename = metadata.get("root_pagename") |
paul@253 | 54 | self.attachments_dir = metadata.get("attachments") |
paul@137 | 55 | |
paul@312 | 56 | # Support a common attachments directory. |
paul@312 | 57 | |
paul@312 | 58 | self.common_attachments = metadata.get("common_attachments") |
paul@312 | 59 | |
paul@312 | 60 | def _get_attachments_dir(self, pagename): |
paul@312 | 61 | |
paul@312 | 62 | "Return the attachments directory for 'pagename'." |
paul@312 | 63 | |
paul@312 | 64 | if self.common_attachments: |
paul@312 | 65 | return join(self.dir.filename, self.attachments_dir) |
paul@312 | 66 | else: |
paul@312 | 67 | return join(self.dir.filename, self.attachments_dir, pagename) |
paul@312 | 68 | |
paul@137 | 69 | # Convenience methods. |
paul@100 | 70 | |
paul@144 | 71 | def ensure(self, pagename): |
paul@144 | 72 | |
paul@144 | 73 | "Ensure that the given 'pagename' exists." |
paul@144 | 74 | |
paul@144 | 75 | if not pagename: |
paul@144 | 76 | return None |
paul@144 | 77 | |
paul@144 | 78 | self.dir.ensure(self.to_filename(pagename)) |
paul@144 | 79 | |
paul@144 | 80 | def ensure_attachments(self, pagename): |
paul@144 | 81 | |
paul@144 | 82 | "Ensure that attachment storage for the given 'pagename' exists." |
paul@144 | 83 | |
paul@144 | 84 | if not pagename: |
paul@144 | 85 | return None |
paul@144 | 86 | |
paul@312 | 87 | self.dir.ensure(self._get_attachments_dir(pagename)) |
paul@144 | 88 | |
paul@144 | 89 | def get_attachment_filename(self, pagename, filename): |
paul@144 | 90 | |
paul@144 | 91 | """ |
paul@144 | 92 | Return the full path of an attachment file for the given 'pagename' |
paul@144 | 93 | having the given 'filename'. |
paul@144 | 94 | """ |
paul@144 | 95 | |
paul@144 | 96 | if not pagename: |
paul@144 | 97 | return None |
paul@144 | 98 | |
paul@312 | 99 | return self.dir.get_filename(join(self._get_attachments_dir(pagename), filename)) |
paul@144 | 100 | |
paul@137 | 101 | def get_filename(self, filename): |
paul@131 | 102 | |
paul@137 | 103 | """ |
paul@137 | 104 | Return the full path of a file with the given 'filename' found within |
paul@137 | 105 | the directory. The full path is an absolute path. |
paul@137 | 106 | """ |
paul@137 | 107 | |
paul@137 | 108 | return self.dir.get_filename(filename) |
paul@131 | 109 | |
paul@131 | 110 | # Name translation methods. |
paul@131 | 111 | |
paul@131 | 112 | def to_filename(self, pagename): |
paul@131 | 113 | |
paul@131 | 114 | "Return the filename corresponding to 'pagename'." |
paul@131 | 115 | |
paul@312 | 116 | # Encode hierarchical filenames. |
paul@312 | 117 | |
paul@312 | 118 | if self.level_sep == sep: |
paul@312 | 119 | |
paul@312 | 120 | # For the root page, use the top-level directory. |
paul@144 | 121 | |
paul@312 | 122 | if pagename == self.root_pagename: |
paul@312 | 123 | return "" |
paul@312 | 124 | else: |
paul@312 | 125 | return pagename |
paul@312 | 126 | |
paul@312 | 127 | # Encode single-directory filenames. |
paul@312 | 128 | |
paul@144 | 129 | else: |
paul@312 | 130 | return self.level_sep.join(pagename.split("/")) |
paul@131 | 131 | |
paul@131 | 132 | def to_pagename(self, filename): |
paul@131 | 133 | |
paul@131 | 134 | "Return the pagename corresponding to 'filename'." |
paul@131 | 135 | |
paul@312 | 136 | # Encode pagenames from hierarchical filenames. |
paul@312 | 137 | |
paul@312 | 138 | if self.level_sep == sep: |
paul@312 | 139 | return self.within(filename) |
paul@312 | 140 | |
paul@312 | 141 | # Encode pagenames from single-directory filenames. |
paul@312 | 142 | |
paul@312 | 143 | else: |
paul@312 | 144 | return "/".join(filename.split(self.level_sep)) |
paul@131 | 145 | |
paul@131 | 146 | # Serialisation methods. |
paul@131 | 147 | |
paul@131 | 148 | def can_write(self): |
paul@131 | 149 | |
paul@131 | 150 | "Return whether this context supports page writing." |
paul@131 | 151 | |
paul@131 | 152 | return True |
paul@131 | 153 | |
paul@131 | 154 | def writefile(self, text, filename, encoding=None): |
paul@131 | 155 | |
paul@131 | 156 | """ |
paul@131 | 157 | Write 'text' to the file having the given 'filename'. If the |
paul@131 | 158 | optional 'encoding' is specified, override the general encoding. |
paul@131 | 159 | """ |
paul@131 | 160 | |
paul@137 | 161 | return self.writepath(text, self.dir.get_filename(filename), encoding) |
paul@131 | 162 | |
paul@131 | 163 | def writepage(self, text, pagename, encoding=None): |
paul@131 | 164 | |
paul@131 | 165 | """ |
paul@131 | 166 | Write 'text' to the file having the given 'pagename' and optional |
paul@131 | 167 | 'encoding'. If 'parent' is specified and a true value, it indicates that |
paul@131 | 168 | the page is a parent of other pages. |
paul@131 | 169 | """ |
paul@131 | 170 | |
paul@144 | 171 | filename = self.to_filename(pagename) |
paul@131 | 172 | |
paul@312 | 173 | # For hierarchical storage, store the page inside a directory bearing |
paul@312 | 174 | # its name. |
paul@312 | 175 | |
paul@312 | 176 | if self.level_sep == sep: |
paul@312 | 177 | |
paul@312 | 178 | # Make a directory for the page. |
paul@131 | 179 | |
paul@312 | 180 | if not self.dir.exists(filename): |
paul@312 | 181 | self.dir.makedirs(filename) |
paul@131 | 182 | |
paul@312 | 183 | # Write to an index filename within any existing directory. |
paul@131 | 184 | |
paul@312 | 185 | filename = join(filename, self.index_name) |
paul@312 | 186 | |
paul@137 | 187 | self.writefile(text, filename, encoding) |
paul@131 | 188 | |
paul@104 | 189 | output = DirectoryOutput |
paul@100 | 190 | |
paul@100 | 191 | # vim: tabstop=4 expandtab shiftwidth=4 |