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