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@144 | 24 | from os.path import extsep, join |
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@165 | 43 | self.index_name = metadata.get("index_name", "index.html") |
paul@165 | 44 | self.page_suffix = metadata.get("page_suffix", "%shtml" % extsep) |
paul@165 | 45 | self.root_pagename = metadata.get("root_pagename", "FrontPage") |
paul@219 | 46 | self.attachments_dir = metadata.get("attachments", "attachments") |
paul@137 | 47 | |
paul@137 | 48 | # Convenience methods. |
paul@100 | 49 | |
paul@144 | 50 | def ensure(self, pagename): |
paul@144 | 51 | |
paul@144 | 52 | "Ensure that the given 'pagename' exists." |
paul@144 | 53 | |
paul@144 | 54 | if not pagename: |
paul@144 | 55 | return None |
paul@144 | 56 | |
paul@144 | 57 | self.dir.ensure(self.to_filename(pagename)) |
paul@144 | 58 | |
paul@144 | 59 | def ensure_attachments(self, pagename): |
paul@144 | 60 | |
paul@144 | 61 | "Ensure that attachment storage for the given 'pagename' exists." |
paul@144 | 62 | |
paul@144 | 63 | if not pagename: |
paul@144 | 64 | return None |
paul@144 | 65 | |
paul@219 | 66 | self.dir.ensure(join(self.to_filename(pagename), self.attachments_dir)) |
paul@144 | 67 | |
paul@144 | 68 | def get_attachment_filename(self, pagename, filename): |
paul@144 | 69 | |
paul@144 | 70 | """ |
paul@144 | 71 | Return the full path of an attachment file for the given 'pagename' |
paul@144 | 72 | having the given 'filename'. |
paul@144 | 73 | """ |
paul@144 | 74 | |
paul@144 | 75 | if not pagename: |
paul@144 | 76 | return None |
paul@144 | 77 | |
paul@219 | 78 | return self.dir.get_filename(join(self.to_filename(pagename), self.attachments_dir, filename)) |
paul@144 | 79 | |
paul@137 | 80 | def get_filename(self, filename): |
paul@131 | 81 | |
paul@137 | 82 | """ |
paul@137 | 83 | Return the full path of a file with the given 'filename' found within |
paul@137 | 84 | the directory. The full path is an absolute path. |
paul@137 | 85 | """ |
paul@137 | 86 | |
paul@137 | 87 | return self.dir.get_filename(filename) |
paul@131 | 88 | |
paul@131 | 89 | # Name translation methods. |
paul@131 | 90 | |
paul@131 | 91 | def to_filename(self, pagename): |
paul@131 | 92 | |
paul@131 | 93 | "Return the filename corresponding to 'pagename'." |
paul@131 | 94 | |
paul@144 | 95 | # For the root page, use the top-level directory. |
paul@144 | 96 | |
paul@144 | 97 | if pagename == self.root_pagename: |
paul@144 | 98 | return "" |
paul@144 | 99 | else: |
paul@144 | 100 | return pagename |
paul@131 | 101 | |
paul@131 | 102 | def to_pagename(self, filename): |
paul@131 | 103 | |
paul@131 | 104 | "Return the pagename corresponding to 'filename'." |
paul@131 | 105 | |
paul@144 | 106 | return self.within(filename) |
paul@131 | 107 | |
paul@131 | 108 | # Serialisation methods. |
paul@131 | 109 | |
paul@131 | 110 | def can_write(self): |
paul@131 | 111 | |
paul@131 | 112 | "Return whether this context supports page writing." |
paul@131 | 113 | |
paul@131 | 114 | return True |
paul@131 | 115 | |
paul@131 | 116 | def writefile(self, text, filename, encoding=None): |
paul@131 | 117 | |
paul@131 | 118 | """ |
paul@131 | 119 | Write 'text' to the file having the given 'filename'. If the |
paul@131 | 120 | optional 'encoding' is specified, override the general encoding. |
paul@131 | 121 | """ |
paul@131 | 122 | |
paul@137 | 123 | return self.writepath(text, self.dir.get_filename(filename), encoding) |
paul@131 | 124 | |
paul@131 | 125 | def writepage(self, text, pagename, encoding=None): |
paul@131 | 126 | |
paul@131 | 127 | """ |
paul@131 | 128 | Write 'text' to the file having the given 'pagename' and optional |
paul@131 | 129 | 'encoding'. If 'parent' is specified and a true value, it indicates that |
paul@131 | 130 | the page is a parent of other pages. |
paul@131 | 131 | """ |
paul@131 | 132 | |
paul@144 | 133 | filename = self.to_filename(pagename) |
paul@131 | 134 | |
paul@144 | 135 | # Make a directory for the page. |
paul@131 | 136 | |
paul@144 | 137 | if not self.dir.exists(filename): |
paul@144 | 138 | self.dir.makedirs(filename) |
paul@131 | 139 | |
paul@131 | 140 | # Write to an index filename within any existing directory. |
paul@131 | 141 | |
paul@144 | 142 | filename = join(filename, self.index_name) |
paul@137 | 143 | self.writefile(text, filename, encoding) |
paul@131 | 144 | |
paul@104 | 145 | output = DirectoryOutput |
paul@100 | 146 | |
paul@100 | 147 | # vim: tabstop=4 expandtab shiftwidth=4 |