Restructured files to avoid circular imports
This commit is contained in:
@@ -1,29 +1,6 @@
|
||||
import pymongo
|
||||
|
||||
import fields
|
||||
|
||||
class DocumentMetaclass(type):
|
||||
"""Metaclass for all documents.
|
||||
"""
|
||||
|
||||
def __new__(cls, name, bases, attrs):
|
||||
doc_fields = {}
|
||||
|
||||
# Include all fields present in superclasses
|
||||
for base in bases:
|
||||
if hasattr(base, '_fields'):
|
||||
doc_fields.update(base._fields)
|
||||
|
||||
# Add the document's fields to the _fields attribute
|
||||
for attr_name, attr_val in attrs.items():
|
||||
if issubclass(attr_val.__class__, fields.Field):
|
||||
if not attr_val.name:
|
||||
attr_val.name = attr_name
|
||||
doc_fields[attr_name] = attr_val
|
||||
attrs['_fields'] = doc_fields
|
||||
|
||||
return type.__new__(cls, name, bases, attrs)
|
||||
from base import DocumentMetaclass, BaseDocument
|
||||
|
||||
#import pymongo
|
||||
|
||||
class TopLevelDocumentMetaclass(DocumentMetaclass):
|
||||
"""Metaclass for top-level documents (i.e. documents that have their own
|
||||
@@ -48,21 +25,12 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
||||
return DocumentMetaclass.__new__(cls, name, bases, attrs)
|
||||
|
||||
|
||||
class Document(object):
|
||||
class EmbeddedDocument(BaseDocument):
|
||||
|
||||
__metaclass__ = DocumentMetaclass
|
||||
|
||||
|
||||
class Document(BaseDocument):
|
||||
|
||||
__metaclass__ = TopLevelDocumentMetaclass
|
||||
|
||||
def __init__(self, **values):
|
||||
self._data = {}
|
||||
# Assign initial values to instance
|
||||
for attr_name, attr_value in self._fields.items():
|
||||
if attr_name in values:
|
||||
setattr(self, attr_name, values.pop(attr_name))
|
||||
else:
|
||||
# Use default value
|
||||
setattr(self, attr_name, getattr(self, attr_name))
|
||||
|
||||
def __iter__(self):
|
||||
# Use _data rather than _fields as iterator only looks at names so
|
||||
# values don't need to be converted to Python types
|
||||
return iter(self._data)
|
||||
|
||||
Reference in New Issue
Block a user