1 #!/usr/bin/env python 2 3 """ 4 Directory output context. 5 6 Copyright (C) 2018, 2019 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 # Use any document index setting as the default for the index filename. 44 45 document_index = metadata.get("document_index", "index.html") 46 47 self.index_name = metadata.get("index_name", document_index) 48 self.page_suffix = metadata.get("page_suffix", "%shtml" % extsep) 49 self.root_pagename = metadata.get("root_pagename") 50 self.attachments_dir = metadata.get("attachments") 51 52 # Convenience methods. 53 54 def ensure(self, pagename): 55 56 "Ensure that the given 'pagename' exists." 57 58 if not pagename: 59 return None 60 61 self.dir.ensure(self.to_filename(pagename)) 62 63 def ensure_attachments(self, pagename): 64 65 "Ensure that attachment storage for the given 'pagename' exists." 66 67 if not pagename: 68 return None 69 70 self.dir.ensure(join(self.to_filename(pagename), self.attachments_dir)) 71 72 def get_attachment_filename(self, pagename, filename): 73 74 """ 75 Return the full path of an attachment file for the given 'pagename' 76 having the given 'filename'. 77 """ 78 79 if not pagename: 80 return None 81 82 return self.dir.get_filename(join(self.to_filename(pagename), 83 self.attachments_dir, filename)) 84 85 def get_filename(self, filename): 86 87 """ 88 Return the full path of a file with the given 'filename' found within 89 the directory. The full path is an absolute path. 90 """ 91 92 return self.dir.get_filename(filename) 93 94 # Name translation methods. 95 96 def to_filename(self, pagename): 97 98 "Return the filename corresponding to 'pagename'." 99 100 # For the root page, use the top-level directory. 101 102 if pagename == self.root_pagename: 103 return "" 104 else: 105 return pagename 106 107 def to_pagename(self, filename): 108 109 "Return the pagename corresponding to 'filename'." 110 111 return self.within(filename) 112 113 # Serialisation methods. 114 115 def can_write(self): 116 117 "Return whether this context supports page writing." 118 119 return True 120 121 def writefile(self, text, filename, encoding=None): 122 123 """ 124 Write 'text' to the file having the given 'filename'. If the 125 optional 'encoding' is specified, override the general encoding. 126 """ 127 128 return self.writepath(text, self.dir.get_filename(filename), encoding) 129 130 def writepage(self, text, pagename, encoding=None): 131 132 """ 133 Write 'text' to the file having the given 'pagename' and optional 134 'encoding'. If 'parent' is specified and a true value, it indicates that 135 the page is a parent of other pages. 136 """ 137 138 filename = self.to_filename(pagename) 139 140 # Make a directory for the page. 141 142 if not self.dir.exists(filename): 143 self.dir.makedirs(filename) 144 145 # Write to an index filename within any existing directory. 146 147 filename = join(filename, self.index_name) 148 self.writefile(text, filename, encoding) 149 150 output = DirectoryOutput 151 152 # vim: tabstop=4 expandtab shiftwidth=4