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