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 join, split 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 self.index_name = parameters and parameters.get("index_name") or "index" 43 44 # Convenience methods. 45 46 def get_filename(self, filename): 47 48 """ 49 Return the full path of a file with the given 'filename' found within 50 the directory. The full path is an absolute path. 51 """ 52 53 return self.dir.get_filename(filename) 54 55 # Name translation methods. 56 57 def to_filename(self, pagename): 58 59 "Return the filename corresponding to 'pagename'." 60 61 return pagename 62 63 def to_pagename(self, filename): 64 65 "Return the pagename corresponding to 'filename'." 66 67 # Take the leafname as the pagename from an arbitrary filename. 68 69 return split(filename)[-1] 70 71 # Serialisation methods. 72 73 def can_write(self): 74 75 "Return whether this context supports page writing." 76 77 return True 78 79 def writefile(self, text, filename, encoding=None): 80 81 """ 82 Write 'text' to the file having the given 'filename'. If the 83 optional 'encoding' is specified, override the general encoding. 84 """ 85 86 return self.writepath(text, self.dir.get_filename(filename), encoding) 87 88 def writepage(self, text, pagename, encoding=None): 89 90 """ 91 Write 'text' to the file having the given 'pagename' and optional 92 'encoding'. If 'parent' is specified and a true value, it indicates that 93 the page is a parent of other pages. 94 """ 95 96 dir = self.dir 97 98 filename = self.to_filename(pagename) 99 parent = split(filename)[0] 100 101 # The page may have a parent. 102 103 if parent and parent != filename: 104 105 # Relocate any file for the parent to an index file within a page 106 # directory. 107 108 if dir.isfile(parent): 109 parent_tmp = "%s.tmp" % parent 110 dir.rename(parent, parent_tmp) 111 dir.makedirs(parent) 112 dir.rename(parent_tmp, join(parent, self.index_name)) 113 114 # Or make a directory for the parent. 115 116 elif not dir.exists(parent): 117 dir.makedirs(parent) 118 119 # Write to an index filename within any existing directory. 120 121 if dir.isdir(filename): 122 filename = join(filename, self.index_name) 123 124 self.writefile(text, filename, encoding) 125 126 output = DirectoryOutput 127 128 # vim: tabstop=4 expandtab shiftwidth=4