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 25 26 class DirectoryOutput(Output): 27 28 "A directory output context." 29 30 name = "directory" 31 32 def __init__(self, metadata): 33 34 "Initialise the context with the given 'metadata'." 35 36 if not metadata.has_key("output_filename"): 37 raise ValueError, metadata 38 39 Output.__init__(self, metadata) 40 self.dir = Directory(metadata.get("output_filename")) 41 self.dir.ensure() 42 43 self.index_name = metadata.get("index_name", "index.html") 44 self.page_suffix = metadata.get("page_suffix", "%shtml" % extsep) 45 self.root_pagename = metadata.get("root_pagename", "FrontPage") 46 47 # Convenience methods. 48 49 def ensure(self, pagename): 50 51 "Ensure that the given 'pagename' exists." 52 53 if not pagename: 54 return None 55 56 self.dir.ensure(self.to_filename(pagename)) 57 58 def ensure_attachments(self, pagename): 59 60 "Ensure that attachment storage for the given 'pagename' exists." 61 62 if not pagename: 63 return None 64 65 self.dir.ensure(join(self.to_filename(pagename), "attachments")) 66 67 def get_attachment_filename(self, pagename, filename): 68 69 """ 70 Return the full path of an attachment file for the given 'pagename' 71 having the given 'filename'. 72 """ 73 74 if not pagename: 75 return None 76 77 return self.dir.get_filename(join(self.to_filename(pagename), "attachments", filename)) 78 79 def get_filename(self, filename): 80 81 """ 82 Return the full path of a file with the given 'filename' found within 83 the directory. The full path is an absolute path. 84 """ 85 86 return self.dir.get_filename(filename) 87 88 # Name translation methods. 89 90 def to_filename(self, pagename): 91 92 "Return the filename corresponding to 'pagename'." 93 94 # For the root page, use the top-level directory. 95 96 if pagename == self.root_pagename: 97 return "" 98 else: 99 return pagename 100 101 def to_pagename(self, filename): 102 103 "Return the pagename corresponding to 'filename'." 104 105 return self.within(filename) 106 107 # Serialisation methods. 108 109 def can_write(self): 110 111 "Return whether this context supports page writing." 112 113 return True 114 115 def writefile(self, text, filename, encoding=None): 116 117 """ 118 Write 'text' to the file having the given 'filename'. If the 119 optional 'encoding' is specified, override the general encoding. 120 """ 121 122 return self.writepath(text, self.dir.get_filename(filename), encoding) 123 124 def writepage(self, text, pagename, encoding=None): 125 126 """ 127 Write 'text' to the file having the given 'pagename' and optional 128 'encoding'. If 'parent' is specified and a true value, it indicates that 129 the page is a parent of other pages. 130 """ 131 132 filename = self.to_filename(pagename) 133 134 # Make a directory for the page. 135 136 if not self.dir.exists(filename): 137 self.dir.makedirs(filename) 138 139 # Write to an index filename within any existing directory. 140 141 filename = join(filename, self.index_name) 142 self.writefile(text, filename, encoding) 143 144 output = DirectoryOutput 145 146 # vim: tabstop=4 expandtab shiftwidth=4