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