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