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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" Defines common, low-level capabilities needed by the Traits package.
"""
import enum
import os
import sys
from os import getcwd
from os.path import dirname, exists, join
from weakref import ref
from .etsconfig.api import ETSConfig
# Constants
SequenceTypes = (list, tuple)
ComplexTypes = (float, int)
RangeTypes = (int, float)
TypeTypes = (
str,
int,
float,
complex,
list,
tuple,
dict,
bool,
)
TraitNotifier = "__trait_notifier__"
# The standard Traits property cache prefix:
TraitsCache = "_traits_cache_"
# Singleton 'Uninitialized' object:
Uninitialized = None
class _Uninitialized(object):
""" The singleton value of this class represents the uninitialized state
of a trait and is specified as the 'old' value in the trait change
notification that occurs when the value of a trait is read before being
set.
"""
def __new__(cls):
if Uninitialized is not None:
return Uninitialized
else:
self = object.__new__(cls)
return self
def __repr__(self):
return "<uninitialized>"
def __reduce_ex__(self, protocol):
return (_Uninitialized, ())
#: When the first reference to a trait is a 'get' reference, the default value
#: of the trait is implicitly assigned and returned as the value of the trait.
#: Because of this implicit assignment, a trait change notification is
#: generated with the Uninitialized object as the 'old' value of the trait, and
#: the default trait value as the 'new' value. This allows other parts of the
#: traits package to recognize the assignment as the implicit default value
#: assignment, and treat it specially.
Uninitialized = _Uninitialized()
Undefined = None
class _Undefined(object):
""" Singleton 'Undefined' object (used as undefined trait name and/or
value).
"""
def __new__(cls):
if Undefined is not None:
return Undefined
else:
self = object.__new__(cls)
return self
def __repr__(self):
return "<undefined>"
def __reduce_ex__(self, protocol):
return (_Undefined, ())
def __eq__(self, other):
return type(self) is type(other)
def __hash__(self):
return hash(type(self))
def __ne__(self, other):
return type(self) is not type(other)
#: Singleton object that indicates that a trait attribute has not yet had a
#: value set (i.e., its value is undefined). This object is used instead of
#: None, because None often has other meanings, such as that a value is not
#: used. When a trait attribute is first assigned a value, and its associated
#: trait notification handlers are called, Undefined is passed as the *old*
#: parameter, to indicate that the attribute previously had no value.
Undefined = _Undefined()
class Missing(object):
""" Singleton 'Missing' object (used as missing method argument marker).
"""
def __repr__(self):
return "<missing>"
#: Singleton object that indicates that a method argument is missing from a
#: type-checked method signature.
Missing = Missing()
class Self(object):
""" Singleton 'Self' object (used as object reference to current 'object').
"""
def __repr__(self):
return "<self>"
#: Singleton object that references the current 'object'.
Self = Self()
def strx(arg):
""" Wraps the built-in str() function to raise a TypeError if the
argument is not of a type in StringTypes.
"""
if isinstance(arg, StringTypes):
return str(arg)
raise TypeError
# Constants
StringTypes = (str, int, float, complex)
# Default item validator for TraitDict, TraitList and TraitSet.
def _validate_everything(item):
""" Item validator which accepts any item and returns it unaltered.
"""
return item
def safe_contains(value, container):
""" Perform "in" containment check, allowing for TypeErrors.
This is required because in some circumstances ``x in y`` can raise a
TypeError. In these cases we make the (reasonable) assumption that the
value is _not_ contained in the container.
"""
# Do a LBYL check for Enums, to avoid the DeprecationWarning issued
# by Python 3.7. Ref: enthought/traits#853.
if isinstance(container, enum.EnumMeta):
if not isinstance(value, enum.Enum):
return False
try:
return value in container
except TypeError:
return False
def class_of(object):
""" Returns a string containing the class name of an object with the
correct indefinite article ('a' or 'an') preceding it (e.g., 'an Image',
'a PlotValue').
"""
if isinstance(object, str):
return add_article(object)
return add_article(object.__class__.__name__)
def add_article(name):
""" Returns a string containing the correct indefinite article ('a' or
'an') prefixed to the specified string.
"""
if name[:1].lower() in "aeiou":
return "an " + name
return "a " + name
def user_name_for(name):
""" Returns a "user-friendly" version of a string, with the first letter
capitalized and with underscore characters replaced by spaces. For example,
``user_name_for('user_name_for')`` returns ``'User name for'``.
"""
name = name.replace("_", " ")
result = ""
last_lower = False
for c in name:
if c.isupper() and last_lower:
result += " "
last_lower = c.islower()
result += c
return result.capitalize()
_traits_home = None
def traits_home():
""" Gets the path to the Traits home directory.
"""
global _traits_home
if _traits_home is None:
_traits_home = verify_path(join(ETSConfig.application_data, "traits"))
return _traits_home
def verify_path(path):
""" Verify that a specified path exists, and try to create it if it
does not exist.
"""
if not exists(path):
try:
os.mkdir(path)
except:
pass
return path
def get_module_name(level=2):
""" Returns the name of the module that the caller's caller is located in.
"""
return sys._getframe(level).f_globals.get("__name__", "__main__")
def get_resource_path(level=2):
"""Returns a resource path calculated from the caller's stack.
"""
module = sys._getframe(level).f_globals.get("__name__", "__main__")
path = None
if module != "__main__":
# Return the path to the module:
try:
path = dirname(getattr(sys.modules.get(module), "__file__"))
except:
# Apparently 'module' is not a registered module...treat it like
# '__main__':
pass
if path is None:
# '__main__' is not a real module, so we need a work around:
for path in [dirname(sys.argv[0]), getcwd()]:
if exists(path):
break
# Handle application bundlers. Since the python source files may be placed
# in a zip file and therefore won't be directly accessable using standard
# open/read commands, the app bundlers will look for resources (i.e. data
# files, images, etc.) in specific locations. For py2app, this is in the
# [myapp].app/Contents/Resources directory. For py2exe, this is the same
# directory as the [myapp].exe executable file generated by py2exe. For
# pyinstaller, the attribute sys._MEIPASS is set to this directory.
frozen = getattr(sys, "frozen", False)
if frozen:
if hasattr(sys, "_MEIPASS"):
root = sys._MEIPASS
elif frozen == "macosx_app":
root = os.environ["RESOURCEPATH"]
elif frozen in ("dll", "windows_exe", "console_exe"):
root = os.path.dirname(sys.executable)
else:
# Unknown app bundler, but try anyway
root = os.path.dirname(sys.executable)
if ".zip/" in path:
zippath, image_path = path.split(".zip/")
path = os.path.join(root, image_path)
return path
def xgetattr(object, xname, default=Undefined):
""" Returns the value of an extended object attribute name of the form:
name[.name2[.name3...]].
"""
names = xname.split(".")
for name in names[:-1]:
if default is Undefined:
object = getattr(object, name)
else:
object = getattr(object, name, None)
if object is None:
return default
if default is Undefined:
return getattr(object, names[-1])
return getattr(object, names[-1], default)
def xsetattr(object, xname, value):
""" Sets the value of an extended object attribute name of the form:
name[.name2[.name3...]].
"""
names = xname.split(".")
for name in names[:-1]:
object = getattr(object, name)
setattr(object, names[-1], value)
# Helpers for weak references.
def _make_value_freed_callback(object_ref, name):
def _value_freed(value_ref):
object = object_ref()
if object is not None:
object.trait_property_changed(name, Undefined, None)
return _value_freed
class HandleWeakRef(object):
def __init__(self, object, name, value):
object_ref = ref(object)
_value_freed = _make_value_freed_callback(object_ref, name)
self.object = object_ref
self.name = name
self.value = ref(value, _value_freed)
def is_none(value):
return value is None
def not_none(value):
return value is not None
def not_false(value):
return value is not False
def not_event(value):
return value != "event"
def is_str(value):
return isinstance(value, str)
|