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@137 | 24 | from os.path import join, split |
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@104 | 32 | def __init__(self, parameters=None): |
paul@100 | 33 | |
paul@104 | 34 | "Initialise the context with the given 'parameters'." |
paul@100 | 35 | |
paul@104 | 36 | if not parameters or not parameters.has_key("filename"): |
paul@104 | 37 | raise ValueError, parameters |
paul@100 | 38 | |
paul@104 | 39 | Output.__init__(self, parameters) |
paul@137 | 40 | self.dir = Directory(parameters["filename"]) |
paul@137 | 41 | self.dir.ensure() |
paul@137 | 42 | self.index_name = parameters and parameters.get("index_name") or "index" |
paul@137 | 43 | |
paul@137 | 44 | # Convenience methods. |
paul@100 | 45 | |
paul@137 | 46 | def get_filename(self, filename): |
paul@131 | 47 | |
paul@137 | 48 | """ |
paul@137 | 49 | Return the full path of a file with the given 'filename' found within |
paul@137 | 50 | the directory. The full path is an absolute path. |
paul@137 | 51 | """ |
paul@137 | 52 | |
paul@137 | 53 | return self.dir.get_filename(filename) |
paul@131 | 54 | |
paul@131 | 55 | # Name translation methods. |
paul@131 | 56 | |
paul@131 | 57 | def to_filename(self, pagename): |
paul@131 | 58 | |
paul@131 | 59 | "Return the filename corresponding to 'pagename'." |
paul@131 | 60 | |
paul@131 | 61 | return pagename |
paul@131 | 62 | |
paul@131 | 63 | def to_pagename(self, filename): |
paul@131 | 64 | |
paul@131 | 65 | "Return the pagename corresponding to 'filename'." |
paul@131 | 66 | |
paul@131 | 67 | # Take the leafname as the pagename from an arbitrary filename. |
paul@131 | 68 | |
paul@131 | 69 | return split(filename)[-1] |
paul@131 | 70 | |
paul@131 | 71 | # Serialisation methods. |
paul@131 | 72 | |
paul@131 | 73 | def can_write(self): |
paul@131 | 74 | |
paul@131 | 75 | "Return whether this context supports page writing." |
paul@131 | 76 | |
paul@131 | 77 | return True |
paul@131 | 78 | |
paul@131 | 79 | def writefile(self, text, filename, encoding=None): |
paul@131 | 80 | |
paul@131 | 81 | """ |
paul@131 | 82 | Write 'text' to the file having the given 'filename'. If the |
paul@131 | 83 | optional 'encoding' is specified, override the general encoding. |
paul@131 | 84 | """ |
paul@131 | 85 | |
paul@137 | 86 | return self.writepath(text, self.dir.get_filename(filename), encoding) |
paul@131 | 87 | |
paul@131 | 88 | def writepage(self, text, pagename, encoding=None): |
paul@131 | 89 | |
paul@131 | 90 | """ |
paul@131 | 91 | Write 'text' to the file having the given 'pagename' and optional |
paul@131 | 92 | 'encoding'. If 'parent' is specified and a true value, it indicates that |
paul@131 | 93 | the page is a parent of other pages. |
paul@131 | 94 | """ |
paul@131 | 95 | |
paul@137 | 96 | dir = self.dir |
paul@137 | 97 | |
paul@131 | 98 | filename = self.to_filename(pagename) |
paul@131 | 99 | parent = split(filename)[0] |
paul@131 | 100 | |
paul@131 | 101 | # The page may have a parent. |
paul@131 | 102 | |
paul@131 | 103 | if parent and parent != filename: |
paul@131 | 104 | |
paul@131 | 105 | # Relocate any file for the parent to an index file within a page |
paul@131 | 106 | # directory. |
paul@131 | 107 | |
paul@137 | 108 | if dir.isfile(parent): |
paul@137 | 109 | parent_tmp = "%s.tmp" % parent |
paul@137 | 110 | dir.rename(parent, parent_tmp) |
paul@137 | 111 | dir.makedirs(parent) |
paul@137 | 112 | dir.rename(parent_tmp, join(parent, self.index_name)) |
paul@131 | 113 | |
paul@131 | 114 | # Or make a directory for the parent. |
paul@131 | 115 | |
paul@137 | 116 | elif not dir.exists(parent): |
paul@137 | 117 | dir.makedirs(parent) |
paul@131 | 118 | |
paul@131 | 119 | # Write to an index filename within any existing directory. |
paul@131 | 120 | |
paul@137 | 121 | if dir.isdir(filename): |
paul@137 | 122 | filename = join(filename, self.index_name) |
paul@131 | 123 | |
paul@137 | 124 | self.writefile(text, filename, encoding) |
paul@131 | 125 | |
paul@104 | 126 | output = DirectoryOutput |
paul@100 | 127 | |
paul@100 | 128 | # vim: tabstop=4 expandtab shiftwidth=4 |