1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
"""Generally useful Python code.
But strictly no third party module dependencies.
"""
# Copyright (C) 2011 Stephen Fairchild (s-fairchild@users.sourceforge.net)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in the file entitled COPYING.
# If not, see <http://www.gnu.org/licenses/>.
__all__ = ["Singleton", "PolicedAttributes", "FixedAttributes",
"PathStr", "SlotObject", "string_multireplace"]
import os
import uuid
import re
import glob
import shutil
import threading
from functools import wraps
def cmp(a, b):
""" cmp was removed in python3, so added this simple replacement """
return (a > b) - ( a < b)
class Singleton(type):
"""Enforce the singleton pattern upon the user class."""
def __init__(cls, name, bases, dict_):
super(Singleton, cls).__init__(name, bases, dict_)
cls._instance = None
def __call__(cls, *args, **kwds):
if cls._instance is not None:
# Return an existing instance.
return cls._instance
else:
# No existing instance so instantiate just this once.
cls._instance = super(Singleton, cls).__call__(*args, **kwds)
return cls._instance
def _pa_rlock(func):
"""Policed Attributes helper for thread locking."""
@wraps(func)
def _wrapper(cls, *args, **kwds):
"""Wrapper with locking feature. Performs rlock."""
rlock = type.__getattribute__(cls, "_rlock")
try:
rlock.acquire()
return func(cls, *args, **kwds)
finally:
rlock.release()
return _wrapper
class FixedAttributes(type):
"""Implements a namespace class of constants."""
def __setattr__(cls, name, value):
raise AttributeError("attribute is locked")
def __call__(cls, *args, **kwds):
raise TypeError("%s object is not callable" % cls.__name__)
class PolicedAttributes(FixedAttributes):
"""Polices data access to a namespace class.
Prevents write access to attributes after they have been read.
Envisioned useful for the implementation of "safe" global variables.
"""
def __new__(mcs, name, bases, dict_):
@classmethod
@_pa_rlock
def peek(cls, attr, callback, *args, **kwds):
"""Allow read + write within a callback.
Typical use might be to append to an existing string.
No modification ban is placed or bypassed.
"""
if attr not in type.__getattribute__(cls, "_banned"):
new = callback(
super(PolicedAttributes, cls).__getattribute__(attr),
*args, **kwds)
type.__setattr__(attr, new)
else:
raise AttributeError("attribute is locked")
dict_["peek"] = peek
dict_["_banned"] = set()
dict_["_rlock"] = threading.RLock()
return super(PolicedAttributes, mcs).__new__(mcs, name, bases, dict_)
@_pa_rlock
def __getattribute__(cls, name):
type.__getattribute__(cls, "_banned").add(name)
return type.__getattribute__(cls, name)
@_pa_rlock
def __setattr__(cls, name, value):
if name in type.__getattribute__(cls, "_banned"):
FixedAttributes.__setattr__(cls, name, value)
type.__setattr__(cls, name, value)
class PathStrMeta(type):
"""PathStr() returns None if called with None."""
def __call__(cls, arg):
if arg is None:
return None
else:
return cls.__new__(cls, arg)
class PathStr(str, metaclass=PathStrMeta):
"""A data type to perform path joins using the / operator.
In this case the higher precedence of / is unfortunate.
"""
def __truediv__(self, other):
return PathStr(os.path.join(str(self), other))
def __add__(self, other):
return PathStr(str.__add__(self, other))
def __repr__(self):
return "PathStr('%s')" % self
class SlotObject(object):
"""A mutable object containing an immutable object."""
__slots__ = ['value']
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
def __int__(self):
return int(self.value)
def __float__(self):
return float(self.value)
def __repr__(self):
return "SlotObject(%s)" % repr(self.value)
def __getattr__(self, what):
"""Universal getter for get_ prefix."""
def assign(value):
"""Returned by set_ prefix call. A setter function."""
self.value = value
if what.startswith("get_"):
return lambda : self.value
elif what.startswith("set_"):
return assign
else:
object.__getattribute__(self, what)
def string_multireplace(part, table, encoding='utf-8'):
"""Replace multiple items in a string.
Table is a sequence of 2 tuples of from, to strings.
TOREVIEW: I added scanning the table and parts to ensure
that everything is encoded as (by default) utf-8. This
was to try and keep most of the bytes/string conversion
in one place. It's kinda ugly but works. I'm thinking a
more pythonic solution would be to create a new list/tuple class
that automatically does this.
"""
if not table:
return part
nt = []
for r in table:
nr = []
for c in r:
if type(c) is bytes:
c = c.decode(encoding, 'replace')
nr.append(c)
nt.append(nr)
table = nt
parts = part.split(table[0][0])
t_next = table[1:]
for i, each in enumerate(parts):
if each is type(bytes):
each = each.decode(encoding, 'replace')
if i is type(bytes):
i = i.decode(encoding, 'replace')
parts[i] = string_multireplace(each, t_next)
return table[0][1].join(parts)
class LinkUUIDRegistry(dict, metaclass=Singleton):
"""Manage substitute hard links for data files."""
link_re = re.compile(
r"\{[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}\}")
link_dir = None
def add(self, uuid_, pathname):
if os.path.exists(pathname):
self[uuid_] = pathname
else:
print("LinkUUIDRegistry: pathname does not exist", pathname)
def remove(self, uuid_):
try:
del self[uuid_]
except KeyError:
print("LinkUUIDRegisty: remove -- UUID does not exist: {%s}" % uuid_)
def _purge(self, where):
"""Clean orphaned hard links from the links directory."""
basedir, dirs, files = next(os.walk(where))
for filename in files:
match = self.link_re.match(filename)
try:
if match is None or str(uuid.UUID(match.group(0))) not in self:
os.unlink(os.path.join(basedir, filename))
except EnvironmentError as e:
print("LinkUUIDRegistry: link purge failed: %s" % e)
def _save(self, where, copy):
"""Write new hard links to the links directory.
Existing links are kept as they are. To unlink them could delete the
only copy of the link source.
"""
# Create the links directory as needed.
if not os.path.isdir(where):
try:
os.mkdir(where)
except EnvironmentError as e:
print("LinkUUIDRegistry: link directory creation failed:", e)
return
for uuid_, source in self.items():
ext = os.path.splitext(source)[1]
if copy:
cmd = shutil.copyfile
else:
cmd = os.link
try:
cmd(source, os.path.join(where, "{%s}%s" % (uuid_, ext)))
except EnvironmentError as e:
if e.errno != 17:
print("LinkUUIDRegistry: link failed:", e)
except shutil.Error:
pass
def update(self, where, copy=False):
"""Update the hard links in the links directory."""
self._save(where, copy)
# Purge after save because the link source may just be in the
# links directory itself.
self._purge(where)
self.link_dir = where
def get_link_filename(self, uuid_):
"""Check in the links directory for a specific UUID filename."""
if self.link_dir is not None:
matches = glob.glob(os.path.join(self.link_dir, "{%s}.*" % uuid_))
if len(matches) == 1:
return os.path.basename(matches[0])
# Link does not exist e.g. can't hard-link across filesystems
# or was not made due to policy.
# For a return value of None the caller must substitute the
# pre-existing pathname to preserve functionality.
return None
|