/usr/share/pyshared/storm/variables.py is in python-storm 0.19-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | #
# Copyright (c) 2006, 2007 Canonical
#
# Written by Gustavo Niemeyer <gustavo@niemeyer.net>
#
# This file is part of Storm Object Relational Mapper.
#
# Storm is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# Storm 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from datetime import datetime, date, time, timedelta
from decimal import Decimal
import cPickle as pickle
import re
try:
import uuid
except ImportError:
uuid = None
from storm.compat import json
from storm.exceptions import NoneError
from storm import Undef, has_cextensions
__all__ = [
"VariableFactory",
"Variable",
"LazyValue",
"BoolVariable",
"IntVariable",
"FloatVariable",
"DecimalVariable",
"RawStrVariable",
"UnicodeVariable",
"DateTimeVariable",
"DateVariable",
"TimeVariable",
"TimeDeltaVariable",
"EnumVariable",
"UUIDVariable",
"PickleVariable",
"JSONVariable",
"ListVariable",
]
class LazyValue(object):
"""Marker to be used as a base class on lazily evaluated values."""
__slots__ = ()
def raise_none_error(column):
if not column:
raise NoneError("None isn't acceptable as a value")
else:
from storm.expr import compile, CompileError
name = column.name
if column.table is not Undef:
try:
table = compile(column.table)
name = "%s.%s" % (table, name)
except CompileError:
pass
raise NoneError("None isn't acceptable as a value for %s" % name)
def VariableFactory(cls, **old_kwargs):
"""Build cls with kwargs of constructor updated by kwargs of call.
This is really an implementation of partial/curry functions, and
is replaced by 'partial' when 2.5+ is in use.
"""
def variable_factory(**new_kwargs):
kwargs = old_kwargs.copy()
kwargs.update(new_kwargs)
return cls(**kwargs)
return variable_factory
try:
from functools import partial as VariableFactory
except ImportError:
pass
class Variable(object):
"""Basic representation of a database value in Python.
@type column: L{storm.expr.Column}
@ivar column: The column this variable represents.
@type event: L{storm.event.EventSystem}
@ivar event: The event system on which to broadcast events. If
None, no events will be emitted.
"""
_value = Undef
_lazy_value = Undef
_checkpoint_state = Undef
_allow_none = True
_validator = None
_validator_object_factory = None
_validator_attribute = None
column = None
event = None
def __init__(self, value=Undef, value_factory=Undef, from_db=False,
allow_none=True, column=None, event=None, validator=None,
validator_object_factory=None, validator_attribute=None):
"""
@param value: The initial value of this variable. The default
behavior is for the value to stay undefined until it is
set with L{set}.
@param value_factory: If specified, this will immediately be
called to get the initial value.
@param from_db: A boolean value indicating where the initial
value comes from, if C{value} or C{value_factory} are
specified.
@param allow_none: A boolean indicating whether None should be
allowed to be set as the value of this variable.
@param validator: Validation function called whenever trying to
set the variable to a non-db value. The function should
look like validator(object, attr, value), where the first and
second arguments are the result of validator_object_factory()
(or None, if this parameter isn't provided) and the value of
validator_attribute, respectively. When called, the function
should raise an error if the value is unacceptable, or return
the value to be used in place of the original value otherwise.
@type column: L{storm.expr.Column}
@param column: The column that this variable represents. It's
used for reporting better error messages.
@type event: L{EventSystem}
@param event: The event system to broadcast messages with. If
not specified, then no events will be broadcast.
"""
if not allow_none:
self._allow_none = False
if value is not Undef:
self.set(value, from_db)
elif value_factory is not Undef:
self.set(value_factory(), from_db)
if validator is not None:
self._validator = validator
self._validator_object_factory = validator_object_factory
self._validator_attribute = validator_attribute
self.column = column
self.event = event
def get_lazy(self, default=None):
"""Get the current L{LazyValue} without resolving its value.
@param default: If no L{LazyValue} was previously specified,
return this value. Defaults to None.
"""
if self._lazy_value is Undef:
return default
return self._lazy_value
def get(self, default=None, to_db=False):
"""Get the value, resolving it from a L{LazyValue} if necessary.
If the current value is an instance of L{LazyValue}, then the
C{resolve-lazy-value} event will be emitted, to give third
parties the chance to resolve the lazy value to a real value.
@param default: Returned if no value has been set.
@param to_db: A boolean flag indicating whether this value is
destined for the database.
"""
if self._lazy_value is not Undef and self.event is not None:
self.event.emit("resolve-lazy-value", self, self._lazy_value)
value = self._value
if value is Undef:
return default
if value is None:
return None
return self.parse_get(value, to_db)
def set(self, value, from_db=False):
"""Set a new value.
Generally this will be called when an attribute was set in
Python, or data is being loaded from the database.
If the value is different from the previous value (or it is a
L{LazyValue}), then the C{changed} event will be emitted.
@param value: The value to set. If this is an instance of
L{LazyValue}, then later calls to L{get} will try to
resolve the value.
@param from_db: A boolean indicating whether this value has
come from the database.
"""
# FASTPATH This method is part of the fast path. Be careful when
# changing it (try to profile any changes).
if isinstance(value, LazyValue):
self._lazy_value = value
self._checkpoint_state = new_value = Undef
else:
if not from_db and self._validator is not None:
# We use a factory rather than the object itself to prevent
# the cycle object => obj_info => variable => object
value = self._validator(self._validator_object_factory and
self._validator_object_factory(),
self._validator_attribute, value)
self._lazy_value = Undef
if value is None:
if self._allow_none is False:
raise_none_error(self.column)
new_value = None
else:
new_value = self.parse_set(value, from_db)
if from_db:
# Prepare it for being used by the hook below.
value = self.parse_get(new_value, False)
old_value = self._value
self._value = new_value
if (self.event is not None and
(self._lazy_value is not Undef or new_value != old_value)):
if old_value is not None and old_value is not Undef:
old_value = self.parse_get(old_value, False)
self.event.emit("changed", self, old_value, value, from_db)
def delete(self):
"""Delete the internal value.
If there was a value set, then emit the C{changed} event.
"""
old_value = self._value
if old_value is not Undef:
self._value = Undef
if self.event is not None:
if old_value is not None and old_value is not Undef:
old_value = self.parse_get(old_value, False)
self.event.emit("changed", self, old_value, Undef, False)
def is_defined(self):
"""Check whether there is currently a value.
@return: boolean indicating whether there is currently a value
for this variable. Note that if a L{LazyValue} was
previously set, this returns False; it only returns True if
there is currently a real value set.
"""
return self._value is not Undef
def has_changed(self):
"""Check whether the value has changed.
@return: boolean indicating whether the value has changed
since the last call to L{checkpoint}.
"""
return (self._lazy_value is not Undef or
self.get_state() != self._checkpoint_state)
def get_state(self):
"""Get the internal state of this object.
@return: A value which can later be passed to L{set_state}.
"""
return (self._lazy_value, self._value)
def set_state(self, state):
"""Set the internal state of this object.
@param state: A result from a previous call to
L{get_state}. The internal state of this variable will be set
to the state of the variable which get_state was called on.
"""
self._lazy_value, self._value = state
def checkpoint(self):
""""Checkpoint" the internal state.
See L{has_changed}.
"""
self._checkpoint_state = self.get_state()
def copy(self):
"""Make a new copy of this Variable with the same internal state."""
variable = self.__class__.__new__(self.__class__)
variable.set_state(self.get_state())
return variable
def parse_get(self, value, to_db):
"""Convert the internal value to an external value.
Get a representation of this value either for Python or for
the database. This method is only intended to be overridden
in subclasses, not called from external code.
@param value: The value to be converted.
@param to_db: Whether or not this value is destined for the
database.
"""
return value
def parse_set(self, value, from_db):
"""Convert an external value to an internal value.
A value is being set either from Python code or from the
database. Parse it into its internal representation. This
method is only intended to be overridden in subclasses, not
called from external code.
@param value: The value, either from Python code setting an
attribute or from a column in a database.
@param from_db: A boolean flag indicating whether this value
is from the database.
"""
return value
if has_cextensions:
from storm.cextensions import Variable
class BoolVariable(Variable):
__slots__ = ()
def parse_set(self, value, from_db):
if not isinstance(value, (int, long, float, Decimal)):
raise TypeError("Expected bool, found %r: %r"
% (type(value), value))
return bool(value)
class IntVariable(Variable):
__slots__ = ()
def parse_set(self, value, from_db):
if not isinstance(value, (int, long, float, Decimal)):
raise TypeError("Expected int, found %r: %r"
% (type(value), value))
return int(value)
class FloatVariable(Variable):
__slots__ = ()
def parse_set(self, value, from_db):
if not isinstance(value, (int, long, float, Decimal)):
raise TypeError("Expected float, found %r: %r"
% (type(value), value))
return float(value)
class DecimalVariable(Variable):
__slots__ = ()
@staticmethod
def parse_set(value, from_db):
if (from_db and isinstance(value, basestring) or
isinstance(value, (int, long))):
value = Decimal(value)
elif not isinstance(value, Decimal):
raise TypeError("Expected Decimal, found %r: %r"
% (type(value), value))
return value
@staticmethod
def parse_get(value, to_db):
if to_db:
return unicode(value)
return value
class RawStrVariable(Variable):
__slots__ = ()
def parse_set(self, value, from_db):
if isinstance(value, buffer):
value = str(value)
elif not isinstance(value, str):
raise TypeError("Expected str, found %r: %r"
% (type(value), value))
return value
class UnicodeVariable(Variable):
__slots__ = ()
def parse_set(self, value, from_db):
if not isinstance(value, unicode):
raise TypeError("Expected unicode, found %r: %r"
% (type(value), value))
return value
class DateTimeVariable(Variable):
__slots__ = ("_tzinfo",)
def __init__(self, *args, **kwargs):
self._tzinfo = kwargs.pop("tzinfo", None)
super(DateTimeVariable, self).__init__(*args, **kwargs)
def parse_set(self, value, from_db):
if from_db:
if isinstance(value, datetime):
pass
elif isinstance(value, (str, unicode)):
if " " not in value:
raise ValueError("Unknown date/time format: %r" % value)
date_str, time_str = value.split(" ")
value = datetime(*(_parse_date(date_str) +
_parse_time(time_str)))
else:
raise TypeError("Expected datetime, found %s" % repr(value))
if self._tzinfo is not None:
if value.tzinfo is None:
value = value.replace(tzinfo=self._tzinfo)
else:
value = value.astimezone(self._tzinfo)
else:
if type(value) in (int, long, float):
value = datetime.utcfromtimestamp(value)
elif not isinstance(value, datetime):
raise TypeError("Expected datetime, found %s" % repr(value))
if self._tzinfo is not None:
value = value.astimezone(self._tzinfo)
return value
class DateVariable(Variable):
__slots__ = ()
def parse_set(self, value, from_db):
if from_db:
if value is None:
return None
if isinstance(value, datetime):
return value.date()
if isinstance(value, date):
return value
if not isinstance(value, (str, unicode)):
raise TypeError("Expected date, found %s" % repr(value))
if " " in value:
value, time_str = value.split(" ")
return date(*_parse_date(value))
else:
if isinstance(value, datetime):
return value.date()
if not isinstance(value, date):
raise TypeError("Expected date, found %s" % repr(value))
return value
class TimeVariable(Variable):
__slots__ = ()
def parse_set(self, value, from_db):
if from_db:
# XXX Can None ever get here, considering that set() checks for it?
if value is None:
return None
if isinstance(value, time):
return value
if not isinstance(value, (str, unicode)):
raise TypeError("Expected time, found %s" % repr(value))
if " " in value:
date_str, value = value.split(" ")
return time(*_parse_time(value))
else:
if isinstance(value, datetime):
return value.time()
if not isinstance(value, time):
raise TypeError("Expected time, found %s" % repr(value))
return value
class TimeDeltaVariable(Variable):
__slots__ = ()
def parse_set(self, value, from_db):
if from_db:
# XXX Can None ever get here, considering that set() checks for it?
if value is None:
return None
if isinstance(value, timedelta):
return value
if not isinstance(value, (str, unicode)):
raise TypeError("Expected timedelta, found %s" % repr(value))
return _parse_interval(value)
else:
if not isinstance(value, timedelta):
raise TypeError("Expected timedelta, found %s" % repr(value))
return value
class UUIDVariable(Variable):
__slots__ = ()
def parse_set(self, value, from_db):
assert uuid is not None, "The uuid module was not found."
if from_db and isinstance(value, basestring):
value = uuid.UUID(value)
elif not isinstance(value, uuid.UUID):
raise TypeError("Expected UUID, found %r: %r"
% (type(value), value))
return value
def parse_get(self, value, to_db):
if to_db:
return unicode(value)
return value
class EnumVariable(Variable):
__slots__ = ("_get_map", "_set_map")
def __init__(self, get_map, set_map, *args, **kwargs):
self._get_map = get_map
self._set_map = set_map
Variable.__init__(self, *args, **kwargs)
def parse_set(self, value, from_db):
if from_db:
return value
try:
return self._set_map[value]
except KeyError:
raise ValueError("Invalid enum value: %s" % repr(value))
def parse_get(self, value, to_db):
if to_db:
return value
try:
return self._get_map[value]
except KeyError:
raise ValueError("Invalid enum value: %s" % repr(value))
class MutableValueVariable(Variable):
"""
A variable which contains a reference to mutable content. For this kind
of variable, we can't simply detect when a modification has been made, so
we have to synchronize the content of the variable when the store is
flushing current objects, to check if the state has changed.
"""
__slots__ = ("_event_system")
def __init__(self, *args, **kwargs):
self._event_system = None
Variable.__init__(self, *args, **kwargs)
if self.event is not None:
self.event.hook("start-tracking-changes", self._start_tracking)
self.event.hook("object-deleted", self._detect_changes_and_stop)
def _start_tracking(self, obj_info, event_system):
self._event_system = event_system
self.event.hook("stop-tracking-changes", self._stop_tracking)
def _stop_tracking(self, obj_info, event_system):
event_system.unhook("flush", self._detect_changes)
self._event_system = None
def _detect_changes(self, obj_info):
if (self._checkpoint_state is not Undef and
self.get_state() != self._checkpoint_state):
self.event.emit("changed", self, None, self._value, False)
def _detect_changes_and_stop(self, obj_info):
self._detect_changes(obj_info)
if self._event_system is not None:
self._stop_tracking(obj_info, self._event_system)
def get(self, default=None, to_db=False):
if self._event_system is not None:
self._event_system.hook("flush", self._detect_changes)
return super(MutableValueVariable, self).get(default, to_db)
def set(self, value, from_db=False):
if self._event_system is not None:
if isinstance(value, LazyValue):
self._event_system.unhook("flush", self._detect_changes)
else:
self._event_system.hook("flush", self._detect_changes)
super(MutableValueVariable, self).set(value, from_db)
class EncodedValueVariable(MutableValueVariable):
__slots__ = ()
def parse_set(self, value, from_db):
if from_db:
if isinstance(value, buffer):
value = str(value)
return self._loads(value)
else:
return value
def parse_get(self, value, to_db):
if to_db:
return self._dumps(value)
else:
return value
def get_state(self):
return (self._lazy_value, self._dumps(self._value))
def set_state(self, state):
self._lazy_value = state[0]
self._value = self._loads(state[1])
class PickleVariable(EncodedValueVariable):
def _loads(self, value):
return pickle.loads(value)
def _dumps(self, value):
return pickle.dumps(value, -1)
class JSONVariable(EncodedValueVariable):
__slots__ = ()
def __init__(self, *args, **kwargs):
assert json is not None, (
"Neither the json nor the simplejson module was found.")
super(JSONVariable, self).__init__(*args, **kwargs)
def _loads(self, value):
if not isinstance(value, unicode):
raise TypeError(
"Cannot safely assume encoding of byte string %r." % value)
return json.loads(value)
def _dumps(self, value):
# http://www.ietf.org/rfc/rfc4627.txt states that JSON is text-based
# and so we treat it as such here. In other words, this method returns
# unicode and never str.
dump = json.dumps(value, ensure_ascii=False)
if not isinstance(dump, unicode):
# json.dumps() does not always return unicode. See
# http://code.google.com/p/simplejson/issues/detail?id=40 for one
# of many discussions of str/unicode handling in simplejson.
dump = dump.decode("utf-8")
return dump
class ListVariable(MutableValueVariable):
__slots__ = ("_item_factory",)
def __init__(self, item_factory, *args, **kwargs):
self._item_factory = item_factory
MutableValueVariable.__init__(self, *args, **kwargs)
def parse_set(self, value, from_db):
if from_db:
item_factory = self._item_factory
return [item_factory(value=val, from_db=from_db).get()
for val in value]
else:
return value
def parse_get(self, value, to_db):
if to_db:
item_factory = self._item_factory
return [item_factory(value=val, from_db=False) for val in value]
else:
return value
def get_state(self):
return (self._lazy_value, pickle.dumps(self._value, -1))
def set_state(self, state):
self._lazy_value = state[0]
self._value = pickle.loads(state[1])
def _parse_time(time_str):
# TODO Add support for timezones.
colons = time_str.count(":")
if not 1 <= colons <= 2:
raise ValueError("Unknown time format: %r" % time_str)
if colons == 2:
hour, minute, second = time_str.split(":")
else:
hour, minute = time_str.split(":")
second = "0"
if "." in second:
second, microsecond = second.split(".")
second = int(second)
microsecond = int(int(microsecond) * 10 ** (6 - len(microsecond)))
return int(hour), int(minute), second, microsecond
return int(hour), int(minute), int(second), 0
def _parse_date(date_str):
if "-" not in date_str:
raise ValueError("Unknown date format: %r" % date_str)
year, month, day = date_str.split("-")
return int(year), int(month), int(day)
def _parse_interval_table():
table = {}
for units, delta in (
("d day days", timedelta),
("h hour hours", lambda x: timedelta(hours=x)),
("m min minute minutes", lambda x: timedelta(minutes=x)),
("s sec second seconds", lambda x: timedelta(seconds=x)),
("ms millisecond milliseconds", lambda x: timedelta(milliseconds=x)),
("microsecond microseconds", lambda x: timedelta(microseconds=x))
):
for unit in units.split():
table[unit] = delta
return table
_parse_interval_table = _parse_interval_table()
_parse_interval_re = re.compile(r"[\s,]*"
r"([-+]?(?:\d\d?:\d\d?(?::\d\d?)?(?:\.\d+)?"
r"|\d+(?:\.\d+)?))"
r"[\s,]*")
def _parse_interval(interval):
result = timedelta(0)
value = None
for token in _parse_interval_re.split(interval):
if not token:
pass
elif ":" in token:
if value is not None:
result += timedelta(days=value)
value = None
h, m, s, ms = _parse_time(token)
result += timedelta(hours=h, minutes=m, seconds=s, microseconds=ms)
elif value is None:
try:
value = float(token)
except ValueError:
raise ValueError("Expected an interval value rather than "
"%r in interval %r" % (token, interval))
else:
unit = _parse_interval_table.get(token)
if unit is None:
raise ValueError("Unsupported interval unit %r in interval %r"
% (token, interval))
result += unit(value)
value = None
if value is not None:
result += timedelta(seconds=value)
return result
|