paul@104 | 1 | #!/usr/bin/env python |
paul@104 | 2 | |
paul@104 | 3 | """ |
paul@104 | 4 | Directory context functionality. |
paul@104 | 5 | |
paul@104 | 6 | Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@104 | 7 | |
paul@104 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@104 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@104 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@104 | 11 | version. |
paul@104 | 12 | |
paul@104 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@104 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@104 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@104 | 16 | details. |
paul@104 | 17 | |
paul@104 | 18 | You should have received a copy of the GNU General Public License along with |
paul@104 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@104 | 20 | """ |
paul@104 | 21 | |
paul@136 | 22 | from os import makedirs, rename, walk |
paul@136 | 23 | from os.path import abspath, commonprefix, exists, isdir, isfile, join |
paul@130 | 24 | import fnmatch |
paul@104 | 25 | |
paul@104 | 26 | # Get the directory with trailing path separator when assessing path prefixes |
paul@104 | 27 | # in order to prevent sibling directory confusion. |
paul@104 | 28 | |
paul@104 | 29 | def inside(filename, dirname): |
paul@104 | 30 | |
paul@104 | 31 | "Return whether 'filename' is inside 'dirname'." |
paul@104 | 32 | |
paul@104 | 33 | dirname = join(dirname, "") |
paul@104 | 34 | return commonprefix((filename, dirname)) == dirname |
paul@104 | 35 | |
paul@104 | 36 | def within(filename, dirname): |
paul@104 | 37 | |
paul@104 | 38 | "Return the part of 'filename' found within 'dirname'." |
paul@104 | 39 | |
paul@104 | 40 | dirname = join(dirname, "") |
paul@104 | 41 | prefix = commonprefix((filename, dirname)) |
paul@104 | 42 | |
paul@104 | 43 | if prefix == dirname: |
paul@104 | 44 | return filename[len(prefix):] |
paul@104 | 45 | else: |
paul@104 | 46 | return None |
paul@104 | 47 | |
paul@104 | 48 | |
paul@104 | 49 | |
paul@104 | 50 | class Directory: |
paul@104 | 51 | |
paul@104 | 52 | "A directory abstraction." |
paul@104 | 53 | |
paul@104 | 54 | def __init__(self, filename): |
paul@104 | 55 | |
paul@104 | 56 | "Initialise the abstraction with the given 'filename'." |
paul@104 | 57 | |
paul@104 | 58 | self.filename = abspath(filename) |
paul@104 | 59 | |
paul@136 | 60 | def ensure(self): |
paul@104 | 61 | |
paul@136 | 62 | "Ensure that this directory exists." |
paul@104 | 63 | |
paul@136 | 64 | if not exists(self.filename): |
paul@136 | 65 | makedirs(self.filename) |
paul@104 | 66 | |
paul@104 | 67 | def get_filename(self, filename): |
paul@104 | 68 | |
paul@104 | 69 | """ |
paul@104 | 70 | Return the full path of a file with the given 'filename' found within |
paul@104 | 71 | the directory. The full path is an absolute path. |
paul@104 | 72 | """ |
paul@104 | 73 | |
paul@104 | 74 | # Get the absolute path for the combination of directory and filename. |
paul@104 | 75 | |
paul@104 | 76 | pathname = abspath(join(self.filename, filename)) |
paul@104 | 77 | |
paul@104 | 78 | if inside(pathname, self.filename): |
paul@104 | 79 | return pathname |
paul@104 | 80 | else: |
paul@104 | 81 | raise ValueError, filename |
paul@104 | 82 | |
paul@136 | 83 | # File operations acting on relative filenames. |
paul@136 | 84 | |
paul@136 | 85 | def _apply(self, fn, filename): |
paul@136 | 86 | |
paul@136 | 87 | "Apply 'fn' to the relative 'filename'." |
paul@136 | 88 | |
paul@136 | 89 | return fn(self.get_filename(filename)) |
paul@136 | 90 | |
paul@136 | 91 | def exists(self, filename): |
paul@136 | 92 | |
paul@136 | 93 | "Return whether the relative 'filename' exists within the directory." |
paul@136 | 94 | |
paul@136 | 95 | return self._apply(exists, filename) |
paul@136 | 96 | |
paul@136 | 97 | def isdir(self, filename): |
paul@136 | 98 | |
paul@136 | 99 | "Return whether the relative 'filename' is a directory." |
paul@136 | 100 | |
paul@136 | 101 | return self._apply(isdir, filename) |
paul@136 | 102 | |
paul@136 | 103 | def isfile(self, filename): |
paul@136 | 104 | |
paul@136 | 105 | "Return whether the relative 'filename' is a file." |
paul@136 | 106 | |
paul@136 | 107 | return self._apply(isfile, filename) |
paul@136 | 108 | |
paul@130 | 109 | def makedirs(self, filename): |
paul@130 | 110 | |
paul@130 | 111 | """ |
paul@130 | 112 | Ensure that a directory having the given 'filename' exists by creating |
paul@130 | 113 | it and any directories needed for it to be created. This filename is |
paul@130 | 114 | relative to the directory. |
paul@130 | 115 | """ |
paul@130 | 116 | |
paul@130 | 117 | pathname = self.get_filename(filename) |
paul@130 | 118 | |
paul@130 | 119 | if not exists(pathname): |
paul@130 | 120 | makedirs(pathname) |
paul@130 | 121 | |
paul@136 | 122 | def rename(self, old, new): |
paul@136 | 123 | |
paul@136 | 124 | """ |
paul@136 | 125 | Rename the file with the 'old' relative filename to the 'new' relative |
paul@136 | 126 | filename. |
paul@136 | 127 | """ |
paul@136 | 128 | |
paul@136 | 129 | rename(self.get_filename(old), self.get_filename(new)) |
paul@136 | 130 | |
paul@104 | 131 | def select_files(self, pattern): |
paul@104 | 132 | |
paul@104 | 133 | """ |
paul@104 | 134 | Return a list of filenames found within the directory matching |
paul@104 | 135 | 'pattern'. These filenames are relative to the directory. |
paul@104 | 136 | """ |
paul@104 | 137 | |
paul@130 | 138 | selected = [] |
paul@130 | 139 | |
paul@130 | 140 | # Obtain pathnames, directory names and filenames within the directory. |
paul@130 | 141 | |
paul@130 | 142 | for dirpath, dirnames, filenames in walk(self.filename): |
paul@130 | 143 | for filename in filenames: |
paul@104 | 144 | |
paul@130 | 145 | # Qualify filenames with the directory path. |
paul@130 | 146 | |
paul@130 | 147 | pathname = join(dirpath, filename) |
paul@130 | 148 | |
paul@130 | 149 | # Obtain the local filename within the directory. |
paul@130 | 150 | |
paul@130 | 151 | local_filename = self.within(pathname) |
paul@104 | 152 | |
paul@130 | 153 | # Match filenames supporting the pattern. |
paul@130 | 154 | |
paul@130 | 155 | if local_filename and fnmatch.fnmatch(local_filename, pattern): |
paul@130 | 156 | selected.append(local_filename) |
paul@130 | 157 | |
paul@130 | 158 | return selected |
paul@104 | 159 | |
paul@136 | 160 | # File operations involving complete filenames. |
paul@136 | 161 | |
paul@130 | 162 | def within(self, filename): |
paul@130 | 163 | |
paul@130 | 164 | """ |
paul@136 | 165 | Return the given complete 'filename' translated to be relative to this |
paul@136 | 166 | directory, or return None if the filename describes a location outside |
paul@136 | 167 | the directory. |
paul@130 | 168 | """ |
paul@130 | 169 | |
paul@130 | 170 | return within(filename, self.filename) |
paul@104 | 171 | |
paul@104 | 172 | # vim: tabstop=4 expandtab shiftwidth=4 |