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