/usr/share/pyshared/couchdb/mapping.py is in python-couchdb 0.8-0ubuntu2.
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 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2007-2009 Christopher Lenz
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
"""Mapping from raw JSON data structures to Python objects and vice versa.
>>> from couchdb import Server
>>> server = Server()
>>> db = server.create('python-tests')
To define a document mapping, you declare a Python class inherited from
`Document`, and add any number of `Field` attributes:
>>> from couchdb.mapping import TextField, IntegerField, DateField
>>> class Person(Document):
... name = TextField()
... age = IntegerField()
... added = DateTimeField(default=datetime.now)
>>> person = Person(name='John Doe', age=42)
>>> person.store(db) #doctest: +ELLIPSIS
<Person ...>
>>> person.age
42
You can then load the data from the CouchDB server through your `Document`
subclass, and conveniently access all attributes:
>>> person = Person.load(db, person.id)
>>> old_rev = person.rev
>>> person.name
u'John Doe'
>>> person.age
42
>>> person.added #doctest: +ELLIPSIS
datetime.datetime(...)
To update a document, simply set the attributes, and then call the ``store()``
method:
>>> person.name = 'John R. Doe'
>>> person.store(db) #doctest: +ELLIPSIS
<Person ...>
If you retrieve the document from the server again, you should be getting the
updated data:
>>> person = Person.load(db, person.id)
>>> person.name
u'John R. Doe'
>>> person.rev != old_rev
True
>>> del server['python-tests']
"""
import copy
from calendar import timegm
from datetime import date, datetime, time
from decimal import Decimal
from time import strptime, struct_time
from couchdb.design import ViewDefinition
__all__ = ['Mapping', 'Document', 'Field', 'TextField', 'FloatField',
'IntegerField', 'LongField', 'BooleanField', 'DecimalField',
'DateField', 'DateTimeField', 'TimeField', 'DictField', 'ListField',
'ViewField']
__docformat__ = 'restructuredtext en'
DEFAULT = object()
class Field(object):
"""Basic unit for mapping a piece of data between Python and JSON.
Instances of this class can be added to subclasses of `Document` to describe
the mapping of a document.
"""
def __init__(self, name=None, default=None):
self.name = name
self.default = default
def __get__(self, instance, owner):
if instance is None:
return self
value = instance._data.get(self.name)
if value is not None:
value = self._to_python(value)
elif self.default is not None:
default = self.default
if callable(default):
default = default()
value = default
return value
def __set__(self, instance, value):
if value is not None:
value = self._to_json(value)
instance._data[self.name] = value
def _to_python(self, value):
return unicode(value)
def _to_json(self, value):
return self._to_python(value)
class MappingMeta(type):
def __new__(cls, name, bases, d):
fields = {}
for base in bases:
if hasattr(base, '_fields'):
fields.update(base._fields)
for attrname, attrval in d.items():
if isinstance(attrval, Field):
if not attrval.name:
attrval.name = attrname
fields[attrname] = attrval
d['_fields'] = fields
return type.__new__(cls, name, bases, d)
class Mapping(object):
__metaclass__ = MappingMeta
def __init__(self, **values):
self._data = {}
for attrname, field in self._fields.items():
if attrname in values:
setattr(self, attrname, values.pop(attrname))
else:
setattr(self, attrname, getattr(self, attrname))
def __iter__(self):
return iter(self._data)
def __len__(self):
return len(self._data or ())
def __delitem__(self, name):
del self._data[name]
def __getitem__(self, name):
return self._data[name]
def __setitem__(self, name, value):
self._data[name] = value
def get(self, name, default):
return self._data.get(name, default)
def setdefault(self, name, default):
return self._data.setdefault(name, default)
def unwrap(self):
return self._data
@classmethod
def build(cls, **d):
fields = {}
for attrname, attrval in d.items():
if not attrval.name:
attrval.name = attrname
fields[attrname] = attrval
d['_fields'] = fields
return type('AnonymousStruct', (cls,), d)
@classmethod
def wrap(cls, data):
instance = cls()
instance._data = data
return instance
def _to_python(self, value):
return self.wrap(value)
def _to_json(self, value):
return self.unwrap()
class ViewField(object):
r"""Descriptor that can be used to bind a view definition to a property of
a `Document` class.
>>> class Person(Document):
... name = TextField()
... age = IntegerField()
... by_name = ViewField('people', '''\
... function(doc) {
... emit(doc.name, doc);
... }''')
>>> Person.by_name
<ViewDefinition '_design/people/_view/by_name'>
>>> print Person.by_name.map_fun
function(doc) {
emit(doc.name, doc);
}
That property can be used as a function, which will execute the view.
>>> from couchdb import Database
>>> db = Database('python-tests')
>>> Person.by_name(db, count=3)
<ViewResults <PermanentView '_design/people/_view/by_name'> {'count': 3}>
The results produced by the view are automatically wrapped in the
`Document` subclass the descriptor is bound to. In this example, it would
return instances of the `Person` class. But please note that this requires
the values of the view results to be dictionaries that can be mapped to the
mapping defined by the containing `Document` class. Alternatively, the
``include_docs`` query option can be used to inline the actual documents in
the view results, which will then be used instead of the values.
If you use Python view functions, this class can also be used as a
decorator:
>>> class Person(Document):
... name = TextField()
... age = IntegerField()
...
... @ViewField.define('people')
... def by_name(doc):
... yield doc['name'], doc
>>> Person.by_name
<ViewDefinition '_design/people/_view/by_name'>
>>> print Person.by_name.map_fun
def by_name(doc):
yield doc['name'], doc
"""
def __init__(self, design, map_fun, reduce_fun=None, name=None,
language='javascript', wrapper=DEFAULT, **defaults):
"""Initialize the view descriptor.
:param design: the name of the design document
:param map_fun: the map function code
:param reduce_fun: the reduce function code (optional)
:param name: the actual name of the view in the design document, if
it differs from the name the descriptor is assigned to
:param language: the name of the language used
:param wrapper: an optional callable that should be used to wrap the
result rows
:param defaults: default query string parameters to apply
"""
self.design = design
self.name = name
self.map_fun = map_fun
self.reduce_fun = reduce_fun
self.language = language
self.wrapper = wrapper
self.defaults = defaults
@classmethod
def define(cls, design, name=None, language='python', wrapper=DEFAULT,
**defaults):
"""Factory method for use as a decorator (only suitable for Python
view code).
"""
def view_wrapped(fun):
return cls(design, fun, language=language, wrapper=wrapper,
**defaults)
return view_wrapped
def __get__(self, instance, cls=None):
if self.wrapper is DEFAULT:
wrapper = cls._wrap_row
else:
wrapper = self.wrapper
return ViewDefinition(self.design, self.name, self.map_fun,
self.reduce_fun, language=self.language,
wrapper=wrapper, **self.defaults)
class DocumentMeta(MappingMeta):
def __new__(cls, name, bases, d):
for attrname, attrval in d.items():
if isinstance(attrval, ViewField):
if not attrval.name:
attrval.name = attrname
return MappingMeta.__new__(cls, name, bases, d)
class Document(Mapping):
__metaclass__ = DocumentMeta
def __init__(self, id=None, **values):
Mapping.__init__(self, **values)
if id is not None:
self.id = id
def __repr__(self):
return '<%s %r@%r %r>' % (type(self).__name__, self.id, self.rev,
dict([(k, v) for k, v in self._data.items()
if k not in ('_id', '_rev')]))
def _get_id(self):
if hasattr(self._data, 'id'): # When data is client.Document
return self._data.id
return self._data.get('_id')
def _set_id(self, value):
if self.id is not None:
raise AttributeError('id can only be set on new documents')
self._data['_id'] = value
id = property(_get_id, _set_id, doc='The document ID')
@property
def rev(self):
"""The document revision.
:rtype: basestring
"""
if hasattr(self._data, 'rev'): # When data is client.Document
return self._data.rev
return self._data.get('_rev')
def items(self):
"""Return the fields as a list of ``(name, value)`` tuples.
This method is provided to enable easy conversion to native dictionary
objects, for example to allow use of `mapping.Document` instances with
`client.Database.update`.
>>> class Post(Document):
... title = TextField()
... author = TextField()
>>> post = Post(id='foo-bar', title='Foo bar', author='Joe')
>>> sorted(post.items())
[('_id', 'foo-bar'), ('author', u'Joe'), ('title', u'Foo bar')]
:return: a list of ``(name, value)`` tuples
"""
retval = []
if self.id is not None:
retval.append(('_id', self.id))
if self.rev is not None:
retval.append(('_rev', self.rev))
for name, value in self._data.items():
if name not in ('_id', '_rev'):
retval.append((name, value))
return retval
@classmethod
def load(cls, db, id):
"""Load a specific document from the given database.
:param db: the `Database` object to retrieve the document from
:param id: the document ID
:return: the `Document` instance, or `None` if no document with the
given ID was found
"""
doc = db.get(id)
if doc is None:
return None
return cls.wrap(doc)
def store(self, db):
"""Store the document in the given database."""
db.save(self._data)
return self
@classmethod
def query(cls, db, map_fun, reduce_fun, language='javascript', **options):
"""Execute a CouchDB temporary view and map the result values back to
objects of this mapping.
Note that by default, any properties of the document that are not
included in the values of the view will be treated as if they were
missing from the document. If you want to load the full document for
every row, set the ``include_docs`` option to ``True``.
"""
return db.query(map_fun, reduce_fun=reduce_fun, language=language,
wrapper=cls._wrap_row, **options)
@classmethod
def view(cls, db, viewname, **options):
"""Execute a CouchDB named view and map the result values back to
objects of this mapping.
Note that by default, any properties of the document that are not
included in the values of the view will be treated as if they were
missing from the document. If you want to load the full document for
every row, set the ``include_docs`` option to ``True``.
"""
return db.view(viewname, wrapper=cls._wrap_row, **options)
@classmethod
def _wrap_row(cls, row):
doc = row.get('doc')
if doc is not None:
return cls.wrap(doc)
data = row['value']
data['_id'] = row['id']
return cls.wrap(data)
class TextField(Field):
"""Mapping field for string values."""
_to_python = unicode
class FloatField(Field):
"""Mapping field for float values."""
_to_python = float
class IntegerField(Field):
"""Mapping field for integer values."""
_to_python = int
class LongField(Field):
"""Mapping field for long integer values."""
_to_python = long
class BooleanField(Field):
"""Mapping field for boolean values."""
_to_python = bool
class DecimalField(Field):
"""Mapping field for decimal values."""
def _to_python(self, value):
return Decimal(value)
def _to_json(self, value):
return unicode(value)
class DateField(Field):
"""Mapping field for storing dates.
>>> field = DateField()
>>> field._to_python('2007-04-01')
datetime.date(2007, 4, 1)
>>> field._to_json(date(2007, 4, 1))
'2007-04-01'
>>> field._to_json(datetime(2007, 4, 1, 15, 30))
'2007-04-01'
"""
def _to_python(self, value):
if isinstance(value, basestring):
try:
value = date(*strptime(value, '%Y-%m-%d')[:3])
except ValueError:
raise ValueError('Invalid ISO date %r' % value)
return value
def _to_json(self, value):
if isinstance(value, datetime):
value = value.date()
return value.isoformat()
class DateTimeField(Field):
"""Mapping field for storing date/time values.
>>> field = DateTimeField()
>>> field._to_python('2007-04-01T15:30:00Z')
datetime.datetime(2007, 4, 1, 15, 30)
>>> field._to_json(datetime(2007, 4, 1, 15, 30, 0, 9876))
'2007-04-01T15:30:00Z'
>>> field._to_json(date(2007, 4, 1))
'2007-04-01T00:00:00Z'
"""
def _to_python(self, value):
if isinstance(value, basestring):
try:
value = value.split('.', 1)[0] # strip out microseconds
value = value.rstrip('Z') # remove timezone separator
value = datetime(*strptime(value, '%Y-%m-%dT%H:%M:%S')[:6])
except ValueError:
raise ValueError('Invalid ISO date/time %r' % value)
return value
def _to_json(self, value):
if isinstance(value, struct_time):
value = datetime.utcfromtimestamp(timegm(value))
elif not isinstance(value, datetime):
value = datetime.combine(value, time(0))
return value.replace(microsecond=0).isoformat() + 'Z'
class TimeField(Field):
"""Mapping field for storing times.
>>> field = TimeField()
>>> field._to_python('15:30:00')
datetime.time(15, 30)
>>> field._to_json(time(15, 30))
'15:30:00'
>>> field._to_json(datetime(2007, 4, 1, 15, 30))
'15:30:00'
"""
def _to_python(self, value):
if isinstance(value, basestring):
try:
value = value.split('.', 1)[0] # strip out microseconds
value = time(*strptime(value, '%H:%M:%S')[3:6])
except ValueError:
raise ValueError('Invalid ISO time %r' % value)
return value
def _to_json(self, value):
if isinstance(value, datetime):
value = value.time()
return value.replace(microsecond=0).isoformat()
class DictField(Field):
"""Field type for nested dictionaries.
>>> from couchdb import Server
>>> server = Server()
>>> db = server.create('python-tests')
>>> class Post(Document):
... title = TextField()
... content = TextField()
... author = DictField(Mapping.build(
... name = TextField(),
... email = TextField()
... ))
... extra = DictField()
>>> post = Post(
... title='Foo bar',
... author=dict(name='John Doe',
... email='john@doe.com'),
... extra=dict(foo='bar'),
... )
>>> post.store(db) #doctest: +ELLIPSIS
<Post ...>
>>> post = Post.load(db, post.id)
>>> post.author.name
u'John Doe'
>>> post.author.email
u'john@doe.com'
>>> post.extra
{'foo': 'bar'}
>>> del server['python-tests']
"""
def __init__(self, mapping=None, name=None, default=None):
default = default or {}
Field.__init__(self, name=name, default=lambda: default.copy())
self.mapping = mapping
def _to_python(self, value):
if self.mapping is None:
return value
else:
return self.mapping.wrap(value)
def _to_json(self, value):
if self.mapping is None:
return value
if not isinstance(value, Mapping):
value = self.mapping(**value)
return value.unwrap()
class ListField(Field):
"""Field type for sequences of other fields.
>>> from couchdb import Server
>>> server = Server()
>>> db = server.create('python-tests')
>>> class Post(Document):
... title = TextField()
... content = TextField()
... pubdate = DateTimeField(default=datetime.now)
... comments = ListField(DictField(Mapping.build(
... author = TextField(),
... content = TextField(),
... time = DateTimeField()
... )))
>>> post = Post(title='Foo bar')
>>> post.comments.append(author='myself', content='Bla bla',
... time=datetime.now())
>>> len(post.comments)
1
>>> post.store(db) #doctest: +ELLIPSIS
<Post ...>
>>> post = Post.load(db, post.id)
>>> comment = post.comments[0]
>>> comment['author']
'myself'
>>> comment['content']
'Bla bla'
>>> comment['time'] #doctest: +ELLIPSIS
'...T...Z'
>>> del server['python-tests']
"""
def __init__(self, field, name=None, default=None):
default = default or []
Field.__init__(self, name=name, default=lambda: copy.copy(default))
if type(field) is type:
if issubclass(field, Field):
field = field()
elif issubclass(field, Mapping):
field = DictField(field)
self.field = field
def _to_python(self, value):
return self.Proxy(value, self.field)
def _to_json(self, value):
return [self.field._to_json(item) for item in value]
class Proxy(list):
def __init__(self, list, field):
self.list = list
self.field = field
def __lt__(self, other):
return self.list < other
def __le__(self, other):
return self.list <= other
def __eq__(self, other):
return self.list == other
def __ne__(self, other):
return self.list != other
def __gt__(self, other):
return self.list > other
def __ge__(self, other):
return self.list >= other
def __repr__(self):
return repr(self.list)
def __str__(self):
return str(self.list)
def __unicode__(self):
return unicode(self.list)
def __delitem__(self, index):
del self.list[index]
def __getitem__(self, index):
return self.field._to_python(self.list[index])
def __setitem__(self, index, value):
self.list[index] = self.field._to_json(value)
def __delslice__(self, i, j):
del self.list[i:j]
def __getslice__(self, i, j):
return ListField.Proxy(self.list[i:j], self.field)
def __setslice__(self, i, j, seq):
self.list[i:j] = (self.field._to_json(v) for v in seq)
def __contains__(self, value):
for item in self.list:
if self.field._to_python(item) == value:
return True
return False
def __iter__(self):
for index in range(len(self)):
yield self[index]
def __len__(self):
return len(self.list)
def __nonzero__(self):
return bool(self.list)
def append(self, *args, **kwargs):
if args or not isinstance(self.field, DictField):
if len(args) != 1:
raise TypeError('append() takes exactly one argument '
'(%s given)' % len(args))
value = args[0]
else:
value = kwargs
self.list.append(self.field._to_json(value))
def count(self, value):
return [i for i in self].count(value)
def extend(self, list):
for item in list:
self.append(item)
def index(self, value):
return self.list.index(self.field._to_json(value))
def insert(self, idx, *args, **kwargs):
if args or not isinstance(self.field, DictField):
if len(args) != 1:
raise TypeError('insert() takes exactly 2 arguments '
'(%s given)' % len(args))
value = args[0]
else:
value = kwargs
self.list.insert(idx, self.field._to_json(value))
def remove(self, value):
return self.list.remove(self.field._to_json(value))
def pop(self, *args):
return self.field._to_python(self.list.pop(*args))
|