1.1 --- a/moinformat/output/directory.py Sun Aug 05 19:07:27 2018 +0200
1.2 +++ b/moinformat/output/directory.py Sun Aug 05 19:07:53 2018 +0200
1.3 @@ -21,10 +21,9 @@
1.4
1.5 from moinformat.output.common import Output
1.6 from moinformat.utils.directory import Directory
1.7 -from os import makedirs, rename
1.8 -from os.path import exists, isdir, isfile, join, split
1.9 +from os.path import join, split
1.10
1.11 -class DirectoryOutput(Output, Directory):
1.12 +class DirectoryOutput(Output):
1.13
1.14 "A directory output context."
1.15
1.16 @@ -38,12 +37,20 @@
1.17 raise ValueError, parameters
1.18
1.19 Output.__init__(self, parameters)
1.20 - Directory.__init__(self, parameters["filename"])
1.21 + self.dir = Directory(parameters["filename"])
1.22 + self.dir.ensure()
1.23 + self.index_name = parameters and parameters.get("index_name") or "index"
1.24 +
1.25 + # Convenience methods.
1.26
1.27 - if not exists(self.filename):
1.28 - makedirs(self.filename)
1.29 + def get_filename(self, filename):
1.30
1.31 - self.index_name = parameters and parameters.get("index_name") or "index"
1.32 + """
1.33 + Return the full path of a file with the given 'filename' found within
1.34 + the directory. The full path is an absolute path.
1.35 + """
1.36 +
1.37 + return self.dir.get_filename(filename)
1.38
1.39 # Name translation methods.
1.40
1.41 @@ -76,7 +83,7 @@
1.42 optional 'encoding' is specified, override the general encoding.
1.43 """
1.44
1.45 - return self.writepath(text, self.get_filename(filename), encoding)
1.46 + return self.writepath(text, self.dir.get_filename(filename), encoding)
1.47
1.48 def writepage(self, text, pagename, encoding=None):
1.49
1.50 @@ -86,38 +93,35 @@
1.51 the page is a parent of other pages.
1.52 """
1.53
1.54 + dir = self.dir
1.55 +
1.56 filename = self.to_filename(pagename)
1.57 parent = split(filename)[0]
1.58
1.59 # The page may have a parent.
1.60
1.61 if parent and parent != filename:
1.62 - dirpath = self.get_filename(parent)
1.63
1.64 # Relocate any file for the parent to an index file within a page
1.65 # directory.
1.66
1.67 - if isfile(dirpath):
1.68 - parent_tmp = "%s.tmp" % dirpath
1.69 - rename(dirpath, parent_tmp)
1.70 - makedirs(dirpath)
1.71 - rename(parent_tmp, join(dirpath, self.index_name))
1.72 + if dir.isfile(parent):
1.73 + parent_tmp = "%s.tmp" % parent
1.74 + dir.rename(parent, parent_tmp)
1.75 + dir.makedirs(parent)
1.76 + dir.rename(parent_tmp, join(parent, self.index_name))
1.77
1.78 # Or make a directory for the parent.
1.79
1.80 - elif not exists(dirpath):
1.81 - makedirs(dirpath)
1.82 -
1.83 - # Obtain a complete filename for the content.
1.84 -
1.85 - pathname = self.get_filename(filename)
1.86 + elif not dir.exists(parent):
1.87 + dir.makedirs(parent)
1.88
1.89 # Write to an index filename within any existing directory.
1.90
1.91 - if isdir(pathname):
1.92 - pathname = join(pathname, self.index_name)
1.93 + if dir.isdir(filename):
1.94 + filename = join(filename, self.index_name)
1.95
1.96 - self.writepath(text, pathname, encoding)
1.97 + self.writefile(text, filename, encoding)
1.98
1.99 output = DirectoryOutput
1.100