| 1 |
"""Dejavu is an Object-Relational Mapper. |
|---|
| 2 |
|
|---|
| 3 |
Persisted objects are called "Units", and are served into Sandboxes within |
|---|
| 4 |
an Arena. Each Unit instance has a class, which maintains its schema via |
|---|
| 5 |
Unit Properties. |
|---|
| 6 |
|
|---|
| 7 |
"Dejavu", to quote Flying Circus episode 16, means "that strange feeling |
|---|
| 8 |
we sometimes get that we've lived through something before." What better |
|---|
| 9 |
name for an object server? Our terminology reflects this cognitive bent: |
|---|
| 10 |
sandboxes "memorize", "recall" and "forget" Units. |
|---|
| 11 |
|
|---|
| 12 |
Most Unit lifecycles follow the same pattern: |
|---|
| 13 |
aUnit = sandbox.unit(cls, ID=ID) |
|---|
| 14 |
val = aUnit.propertyName |
|---|
| 15 |
aUnit.propertyName = newValue |
|---|
| 16 |
del aUnit # or otherwise release the reference, e.g. close the scope. |
|---|
| 17 |
|
|---|
| 18 |
When creating new Units, a similar pattern would be: |
|---|
| 19 |
newUnit = unit_class() |
|---|
| 20 |
newUnit.propertyName = newValue |
|---|
| 21 |
sandbox.memorize(newUnit) |
|---|
| 22 |
del newUnit # or otherwise release the reference. |
|---|
| 23 |
|
|---|
| 24 |
Using recall(), you get a list; using xrecall(), you get an iterator: |
|---|
| 25 |
for unit in sandbox.recall(cls, expr): |
|---|
| 26 |
do_something_with(unit) |
|---|
| 27 |
|
|---|
| 28 |
You destroy a Unit via Unit.forget(). |
|---|
| 29 |
|
|---|
| 30 |
Applications only need to call Unit.repress() when they wish to stop |
|---|
| 31 |
caching the object, returning it to storage. This is very rare, and |
|---|
| 32 |
should really only be performed within dejavu code. |
|---|
| 33 |
""" |
|---|
| 34 |
|
|---|
| 35 |
__version__ = "1.4 beta" |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
import datetime |
|---|
| 39 |
|
|---|
| 40 |
from dejavu.analysis import * |
|---|
| 41 |
from dejavu.arenas import * |
|---|
| 42 |
from dejavu.containers import * |
|---|
| 43 |
from dejavu.errors import * |
|---|
| 44 |
from dejavu.schemas import * |
|---|
| 45 |
from dejavu.units import * |
|---|
| 46 |
from dejavu import logic |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
dejavuarena = Arena() |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
def icontains(a, b): |
|---|
| 62 |
"""Case-insensitive test b in a. Note the operand order.""" |
|---|
| 63 |
return icontainedby(b, a) |
|---|
| 64 |
|
|---|
| 65 |
def icontainedby(a, b): |
|---|
| 66 |
"""Case-insensitive test a in b. Note the operand order.""" |
|---|
| 67 |
if isinstance(b, basestring): |
|---|
| 68 |
|
|---|
| 69 |
if a is None or b is None: |
|---|
| 70 |
return False |
|---|
| 71 |
return a.lower() in b.lower() |
|---|
| 72 |
else: |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
if a is None or not b: |
|---|
| 76 |
return False |
|---|
| 77 |
return a.lower() in [x.lower() for x in b] |
|---|
| 78 |
|
|---|
| 79 |
def istartswith(a, b): |
|---|
| 80 |
"""True if a starts with b (case-insensitive), False otherwise.""" |
|---|
| 81 |
if a is None or b is None: |
|---|
| 82 |
return False |
|---|
| 83 |
return a.lower().startswith(b.lower()) |
|---|
| 84 |
|
|---|
| 85 |
def iendswith(a, b): |
|---|
| 86 |
"""True if a ends with b (case-insensitive), False otherwise.""" |
|---|
| 87 |
if a is None or b is None: |
|---|
| 88 |
return False |
|---|
| 89 |
return a.lower().endswith(b.lower()) |
|---|
| 90 |
|
|---|
| 91 |
def ieq(a, b): |
|---|
| 92 |
"""True if a == b (case-insensitive), False otherwise.""" |
|---|
| 93 |
if a is None or b is None: |
|---|
| 94 |
return False |
|---|
| 95 |
return (a.lower() == b.lower()) |
|---|
| 96 |
|
|---|
| 97 |
def year(value): |
|---|
| 98 |
"""The year attribute of a date.""" |
|---|
| 99 |
if isinstance(value, (datetime.date, datetime.datetime)): |
|---|
| 100 |
return value.year |
|---|
| 101 |
else: |
|---|
| 102 |
return None |
|---|
| 103 |
|
|---|
| 104 |
def now(): |
|---|
| 105 |
"""Late-bound datetime.datetime.now(). Taint this when early binding.""" |
|---|
| 106 |
return datetime.datetime.now() |
|---|
| 107 |
now.bind_late = True |
|---|
| 108 |
|
|---|
| 109 |
def today(): |
|---|
| 110 |
"""Late-bound datetime.date.today(). Taint this when early binding.""" |
|---|
| 111 |
return datetime.date.today() |
|---|
| 112 |
today.bind_late = True |
|---|
| 113 |
|
|---|
| 114 |
def iscurrentweek(value): |
|---|
| 115 |
"""If value is in the current week, return True, else False.""" |
|---|
| 116 |
if isinstance(value, (datetime.date, datetime.datetime)): |
|---|
| 117 |
return datetime.date.today().strftime('%W%Y') == value.strftime('%W%Y') |
|---|
| 118 |
else: |
|---|
| 119 |
return False |
|---|
| 120 |
iscurrentweek.bind_late = True |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
class _Empty(object): pass |
|---|
| 124 |
_d = _Empty() |
|---|
| 125 |
for _name in ['icontains', 'icontainedby', 'istartswith', 'iendswith', |
|---|
| 126 |
'ieq', 'year', 'now', 'today', 'iscurrentweek']: |
|---|
| 127 |
setattr(_d, _name, globals()[_name]) |
|---|
| 128 |
logic.dejavu = _d |
|---|