paul@303 | 1 | #!/usr/bin/env python |
paul@303 | 2 | |
paul@303 | 3 | """ |
paul@303 | 4 | MoinMoin directory input context. |
paul@303 | 5 | |
paul@303 | 6 | Copyright (C) 2018, 2019, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@303 | 7 | |
paul@303 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@303 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@303 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@303 | 11 | version. |
paul@303 | 12 | |
paul@303 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@303 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@303 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@303 | 16 | details. |
paul@303 | 17 | |
paul@303 | 18 | You should have received a copy of the GNU General Public License along with |
paul@303 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@303 | 20 | """ |
paul@303 | 21 | |
paul@303 | 22 | from moinformat.input.directory import DirectoryInput |
paul@303 | 23 | from moinformat.utils.directory import Directory |
paul@303 | 24 | from os.path import exists, join |
paul@303 | 25 | import re |
paul@303 | 26 | |
paul@303 | 27 | class MoinDirectoryInput(DirectoryInput): |
paul@303 | 28 | |
paul@303 | 29 | "A directory input context." |
paul@303 | 30 | |
paul@303 | 31 | name = "moindirectory" |
paul@303 | 32 | |
paul@303 | 33 | # Pagename and filename translation. |
paul@303 | 34 | |
paul@303 | 35 | unsafe_pagename_characters = re.compile(r"([^A-Za-z0-9_]+)") |
paul@303 | 36 | encoded_pagename_groups = re.compile(r"(\([A-Fa-f0-9]+\))") |
paul@303 | 37 | |
paul@303 | 38 | def __init__(self, metadata): |
paul@303 | 39 | |
paul@303 | 40 | "Initialise the context with the given 'metadata'." |
paul@303 | 41 | |
paul@303 | 42 | if not metadata.has_key("input_filename"): |
paul@303 | 43 | raise ValueError, metadata |
paul@303 | 44 | |
paul@303 | 45 | DirectoryInput.__init__(self, metadata) |
paul@303 | 46 | |
paul@303 | 47 | # Do not search recursively in nested directories for pages. |
paul@303 | 48 | # This overrides the common directory input behaviour. |
paul@303 | 49 | |
paul@303 | 50 | self.nested = False |
paul@303 | 51 | |
paul@303 | 52 | def _get_attachments_dir(self, pagename): |
paul@303 | 53 | |
paul@303 | 54 | "Return the attachments directory for 'pagename'." |
paul@303 | 55 | |
paul@303 | 56 | return self.dir.get_filename(join(self.to_filename(pagename), "attachments")) |
paul@303 | 57 | |
paul@303 | 58 | def _get_current_revision(self, filename): |
paul@303 | 59 | |
paul@303 | 60 | filename = self.dir.get_filename(join(filename, "current")) |
paul@303 | 61 | |
paul@303 | 62 | try: |
paul@303 | 63 | current = open(filename) |
paul@303 | 64 | except IOError: |
paul@303 | 65 | return None |
paul@303 | 66 | |
paul@303 | 67 | try: |
paul@303 | 68 | return current.read().strip() |
paul@303 | 69 | finally: |
paul@303 | 70 | current.close() |
paul@303 | 71 | |
paul@303 | 72 | def all(self): |
paul@303 | 73 | |
paul@303 | 74 | "Return all pages in the context." |
paul@303 | 75 | |
paul@303 | 76 | # Ignore dotfiles. Do not search recursively, and obtain directory names |
paul@303 | 77 | # instead of filenames to identify pages. |
paul@303 | 78 | |
paul@303 | 79 | pages = [] |
paul@303 | 80 | |
paul@303 | 81 | for filename in self.dir.select_files("[!.]*", self.nested, |
paul@303 | 82 | directories=True): |
paul@303 | 83 | |
paul@303 | 84 | revision = self._get_current_revision(filename) |
paul@303 | 85 | |
paul@303 | 86 | if revision and exists(self.dir.get_filename(join(filename, "revisions", revision))): |
paul@303 | 87 | pages.append(self.to_pagename(filename)) |
paul@303 | 88 | |
paul@303 | 89 | return pages |
paul@303 | 90 | |
paul@303 | 91 | def all_attachments(self): |
paul@303 | 92 | |
paul@303 | 93 | "Return all attachment filenames in the context." |
paul@303 | 94 | |
paul@303 | 95 | return self.dir.select_files(join("*", "attachments", "*"), True) |
paul@303 | 96 | |
paul@303 | 97 | def get_attachments(self, pagename): |
paul@303 | 98 | |
paul@303 | 99 | """ |
paul@303 | 100 | Return all attachment filenames for the given 'pagename'. Each filename |
paul@303 | 101 | is relative to the appropriate attachment directory. |
paul@303 | 102 | """ |
paul@303 | 103 | |
paul@303 | 104 | return Directory(self._get_attachments_dir(pagename)).select_files("*") |
paul@303 | 105 | |
paul@303 | 106 | # Page characteristics. |
paul@303 | 107 | |
paul@303 | 108 | def subpage_filenames(self, pagename): |
paul@303 | 109 | |
paul@303 | 110 | "Return the subpage filenames of 'pagename'." |
paul@303 | 111 | |
paul@303 | 112 | pattern = self.to_filename("%s%s" % (pagename, self.level_sep)) |
paul@303 | 113 | return self.dir.select_files("%s*" % pattern, self.nested, directories=True) |
paul@303 | 114 | |
paul@303 | 115 | # Page access methods. |
paul@303 | 116 | |
paul@303 | 117 | def readfile(self, filename, encoding=None): |
paul@303 | 118 | |
paul@303 | 119 | """ |
paul@303 | 120 | Return the contents of the file having the given 'filename' and optional |
paul@303 | 121 | 'encoding'. |
paul@303 | 122 | """ |
paul@303 | 123 | |
paul@303 | 124 | # Moin employs a file to indicate the current revision and a directory |
paul@303 | 125 | # containing the revisions. |
paul@303 | 126 | |
paul@303 | 127 | revision = self._get_current_revision(filename) |
paul@303 | 128 | |
paul@303 | 129 | return self.readpath(self.dir.get_filename(join(filename, "revisions", |
paul@303 | 130 | revision)), encoding) |
paul@303 | 131 | |
paul@303 | 132 | # Convenience methods. |
paul@303 | 133 | |
paul@303 | 134 | def get_attachment_filename(self, pagename, filename): |
paul@303 | 135 | |
paul@303 | 136 | """ |
paul@303 | 137 | Return the full path of an attachment file for the given 'pagename' |
paul@303 | 138 | having the given 'filename'. |
paul@303 | 139 | """ |
paul@303 | 140 | |
paul@303 | 141 | if not pagename: |
paul@303 | 142 | return None |
paul@303 | 143 | |
paul@303 | 144 | return self.dir.get_filename(join(self.to_filename(pagename), |
paul@303 | 145 | "attachments", |
paul@303 | 146 | filename)) |
paul@303 | 147 | |
paul@303 | 148 | # NOTE: Translation methods should encode filenames appropriately. |
paul@303 | 149 | |
paul@303 | 150 | def to_filename(self, pagename): |
paul@303 | 151 | |
paul@303 | 152 | "Return the filename corresponding to 'pagename'." |
paul@303 | 153 | |
paul@303 | 154 | # Transform "special" characters into the Moin "(xx)" representation. |
paul@303 | 155 | # Groups of characters are enclosed within each instance. |
paul@303 | 156 | |
paul@303 | 157 | encoded = [] |
paul@303 | 158 | append = encoded.append |
paul@303 | 159 | safe = True |
paul@303 | 160 | |
paul@303 | 161 | for group in self.unsafe_pagename_characters.split(pagename): |
paul@303 | 162 | if safe: |
paul@303 | 163 | append(group) |
paul@303 | 164 | else: |
paul@303 | 165 | append("(") |
paul@303 | 166 | for ch in group: |
paul@303 | 167 | append("%02x" % ord(ch)) |
paul@303 | 168 | append(")") |
paul@303 | 169 | |
paul@303 | 170 | safe = not safe |
paul@303 | 171 | |
paul@303 | 172 | return DirectoryInput.to_filename(self, "".join(encoded)) |
paul@303 | 173 | |
paul@303 | 174 | def to_pagename(self, filename): |
paul@303 | 175 | |
paul@303 | 176 | "Return the pagename corresponding to 'filename'." |
paul@303 | 177 | |
paul@303 | 178 | # Transform "special" characters from the Moin "(xx)" representation. |
paul@303 | 179 | |
paul@303 | 180 | decoded = [] |
paul@303 | 181 | append = decoded.append |
paul@303 | 182 | safe = True |
paul@303 | 183 | |
paul@303 | 184 | for group in self.encoded_pagename_groups.split(filename): |
paul@303 | 185 | if safe: |
paul@303 | 186 | append(group) |
paul@303 | 187 | else: |
paul@303 | 188 | num = group[1:-1] |
paul@303 | 189 | append(chr(int(num, 16))) |
paul@303 | 190 | |
paul@303 | 191 | safe = not safe |
paul@303 | 192 | |
paul@303 | 193 | return DirectoryInput.to_pagename(self, "".join(decoded)) |
paul@303 | 194 | |
paul@303 | 195 | input = MoinDirectoryInput |
paul@303 | 196 | |
paul@303 | 197 | # vim: tabstop=4 expandtab shiftwidth=4 |