1.1 --- a/moinformat/input/directory.py Sun Aug 05 19:07:27 2018 +0200
1.2 +++ b/moinformat/input/directory.py Sun Aug 05 19:07:53 2018 +0200
1.3 @@ -23,7 +23,7 @@
1.4 from moinformat.utils.directory import Directory
1.5 from os.path import sep
1.6
1.7 -class DirectoryInput(Input, Directory):
1.8 +class DirectoryInput(Input):
1.9
1.10 "A directory output context."
1.11
1.12 @@ -37,7 +37,7 @@
1.13 raise ValueError, parameters
1.14
1.15 Input.__init__(self, parameters)
1.16 - Directory.__init__(self, parameters["filename"])
1.17 + self.dir = Directory(parameters["filename"])
1.18
1.19 # Support an encoding of the level separator for the filesystem.
1.20 # Where it is the same as the directory separator, documents are stored
1.21 @@ -49,7 +49,7 @@
1.22
1.23 "Return all pages in the context."
1.24
1.25 - return map(self.to_pagename, self.select_files("*"))
1.26 + return map(self.to_pagename, self.dir.select_files("*"))
1.27
1.28 # Page characteristics.
1.29
1.30 @@ -58,7 +58,7 @@
1.31 "Return the subpage filenames of 'pagename'."
1.32
1.33 pattern = self.to_filename("%s/*" % pagename)
1.34 - return self.select_files(pattern)
1.35 + return self.dir.select_files(pattern)
1.36
1.37 def subpages(self, pagename):
1.38
1.39 @@ -75,7 +75,7 @@
1.40 'encoding'.
1.41 """
1.42
1.43 - return self.readpath(self.get_filename(filename), encoding)
1.44 + return self.readpath(self.dir.get_filename(filename), encoding)
1.45
1.46 # NOTE: Translation methods should encode filenames appropriately.
1.47
2.1 --- a/moinformat/output/directory.py Sun Aug 05 19:07:27 2018 +0200
2.2 +++ b/moinformat/output/directory.py Sun Aug 05 19:07:53 2018 +0200
2.3 @@ -21,10 +21,9 @@
2.4
2.5 from moinformat.output.common import Output
2.6 from moinformat.utils.directory import Directory
2.7 -from os import makedirs, rename
2.8 -from os.path import exists, isdir, isfile, join, split
2.9 +from os.path import join, split
2.10
2.11 -class DirectoryOutput(Output, Directory):
2.12 +class DirectoryOutput(Output):
2.13
2.14 "A directory output context."
2.15
2.16 @@ -38,12 +37,20 @@
2.17 raise ValueError, parameters
2.18
2.19 Output.__init__(self, parameters)
2.20 - Directory.__init__(self, parameters["filename"])
2.21 + self.dir = Directory(parameters["filename"])
2.22 + self.dir.ensure()
2.23 + self.index_name = parameters and parameters.get("index_name") or "index"
2.24 +
2.25 + # Convenience methods.
2.26
2.27 - if not exists(self.filename):
2.28 - makedirs(self.filename)
2.29 + def get_filename(self, filename):
2.30
2.31 - self.index_name = parameters and parameters.get("index_name") or "index"
2.32 + """
2.33 + Return the full path of a file with the given 'filename' found within
2.34 + the directory. The full path is an absolute path.
2.35 + """
2.36 +
2.37 + return self.dir.get_filename(filename)
2.38
2.39 # Name translation methods.
2.40
2.41 @@ -76,7 +83,7 @@
2.42 optional 'encoding' is specified, override the general encoding.
2.43 """
2.44
2.45 - return self.writepath(text, self.get_filename(filename), encoding)
2.46 + return self.writepath(text, self.dir.get_filename(filename), encoding)
2.47
2.48 def writepage(self, text, pagename, encoding=None):
2.49
2.50 @@ -86,38 +93,35 @@
2.51 the page is a parent of other pages.
2.52 """
2.53
2.54 + dir = self.dir
2.55 +
2.56 filename = self.to_filename(pagename)
2.57 parent = split(filename)[0]
2.58
2.59 # The page may have a parent.
2.60
2.61 if parent and parent != filename:
2.62 - dirpath = self.get_filename(parent)
2.63
2.64 # Relocate any file for the parent to an index file within a page
2.65 # directory.
2.66
2.67 - if isfile(dirpath):
2.68 - parent_tmp = "%s.tmp" % dirpath
2.69 - rename(dirpath, parent_tmp)
2.70 - makedirs(dirpath)
2.71 - rename(parent_tmp, join(dirpath, self.index_name))
2.72 + if dir.isfile(parent):
2.73 + parent_tmp = "%s.tmp" % parent
2.74 + dir.rename(parent, parent_tmp)
2.75 + dir.makedirs(parent)
2.76 + dir.rename(parent_tmp, join(parent, self.index_name))
2.77
2.78 # Or make a directory for the parent.
2.79
2.80 - elif not exists(dirpath):
2.81 - makedirs(dirpath)
2.82 -
2.83 - # Obtain a complete filename for the content.
2.84 -
2.85 - pathname = self.get_filename(filename)
2.86 + elif not dir.exists(parent):
2.87 + dir.makedirs(parent)
2.88
2.89 # Write to an index filename within any existing directory.
2.90
2.91 - if isdir(pathname):
2.92 - pathname = join(pathname, self.index_name)
2.93 + if dir.isdir(filename):
2.94 + filename = join(filename, self.index_name)
2.95
2.96 - self.writepath(text, pathname, encoding)
2.97 + self.writefile(text, filename, encoding)
2.98
2.99 output = DirectoryOutput
2.100