1 #!/usr/bin/env python 2 3 """ 4 Directory output context. 5 6 Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from moinformat.output.common import Output 23 from moinformat.utils.directory import Directory 24 from os.path import extsep, join, split, splitext 25 26 class DirectoryOutput(Output): 27 28 "A directory output context." 29 30 name = "directory" 31 32 def __init__(self, parameters=None): 33 34 "Initialise the context with the given 'parameters'." 35 36 if not parameters or not parameters.has_key("filename"): 37 raise ValueError, parameters 38 39 Output.__init__(self, parameters) 40 self.dir = Directory(parameters["filename"]) 41 self.dir.ensure() 42 43 self.index_name = self.parameters.get("index_name") or "index.html" 44 self.page_suffix = self.parameters.get("page_suffix") or "%shtml" % extsep 45 46 # Convenience methods. 47 48 def get_filename(self, filename): 49 50 """ 51 Return the full path of a file with the given 'filename' found within 52 the directory. The full path is an absolute path. 53 """ 54 55 return self.dir.get_filename(filename) 56 57 # Name translation methods. 58 59 def to_filename(self, pagename): 60 61 "Return the filename corresponding to 'pagename'." 62 63 return "%s%s" % (pagename, self.page_suffix) 64 65 def to_pagename(self, filename): 66 67 "Return the pagename corresponding to 'filename'." 68 69 # Take the leafname as the pagename from an arbitrary filename, removing 70 # any file extension. 71 72 return splitext(split(filename)[-1])[0] 73 74 def to_parent_filename(self, pagename): 75 76 "Return the parent page filename corresponding to 'pagename'." 77 78 return pagename 79 80 # Serialisation methods. 81 82 def can_write(self): 83 84 "Return whether this context supports page writing." 85 86 return True 87 88 def writefile(self, text, filename, encoding=None): 89 90 """ 91 Write 'text' to the file having the given 'filename'. If the 92 optional 'encoding' is specified, override the general encoding. 93 """ 94 95 return self.writepath(text, self.dir.get_filename(filename), encoding) 96 97 def writepage(self, text, pagename, encoding=None): 98 99 """ 100 Write 'text' to the file having the given 'pagename' and optional 101 'encoding'. If 'parent' is specified and a true value, it indicates that 102 the page is a parent of other pages. 103 """ 104 105 dir = self.dir 106 parent = self.parent(pagename) 107 108 # The page may have a parent. 109 110 if parent: 111 parentfile = self.to_filename(parent) 112 parentdir = self.to_parent_filename(parent) 113 114 # Relocate any file for the parent to an index file within a page 115 # directory. 116 117 if dir.isfile(parentfile): 118 parent_tmp = "%s.tmp" % parentfile 119 dir.rename(parentfile, parent_tmp) 120 if not dir.exists(parentdir): 121 dir.makedirs(parentdir) 122 dir.rename(parent_tmp, join(parentdir, self.index_name)) 123 124 # Or make a directory for the parent. 125 126 elif not dir.exists(parentdir): 127 dir.makedirs(parentdir) 128 129 # Write to an index filename within any existing directory. 130 131 dirname = self.to_parent_filename(pagename) 132 133 if dir.isdir(dirname): 134 filename = join(dirname, self.index_name) 135 else: 136 filename = self.to_filename(pagename) 137 138 self.writefile(text, filename, encoding) 139 140 output = DirectoryOutput 141 142 # vim: tabstop=4 expandtab shiftwidth=4