<s> """<STR_LIT>""" <EOL> from pprint import pformat <EOL> from collections import MutableMapping <EOL> from abc import ABCMeta <EOL> import six <EOL> from scrapy . utils . trackref import object_ref <EOL> class BaseItem ( object_ref ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Field ( dict ) : <EOL> """<STR_LIT>""" <EOL> class ItemMeta ( ABCMeta ) : <EOL> def __new__ ( mcs , class_name , bases , attrs ) : <EOL> new_bases = tuple ( base . _class for base in bases if hasattr ( base , '<STR_LIT>' ) ) <EOL> _class = super ( ItemMeta , mcs ) . __new__ ( mcs , '<STR_LIT>' + class_name , new_bases , attrs ) <EOL> fields = getattr ( _class , '<STR_LIT>' , { } ) <EOL> new_attrs = { } <EOL> for n in dir ( _class ) : <EOL> v = getattr ( _class , n ) <EOL> if isinstance ( v , Field ) : <EOL> fields [ n ] = v <EOL> elif n in attrs : <EOL> new_attrs [ n ] = attrs [ n ] <EOL> new_attrs [ '<STR_LIT>' ] = fields <EOL> new_attrs [ '<STR_LIT>' ] = _class <EOL> return super ( ItemMeta , mcs ) . __new__ ( mcs , class_name , bases , new_attrs ) <EOL> class DictItem ( MutableMapping , BaseItem ) : <EOL> fields = { } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _values = { } <EOL> if args or kwargs : <EOL> for k , v in six . iteritems ( dict ( * args , ** kwargs ) ) : <EOL> self [ k ] = v <EOL> def __getitem__ ( self , key ) : <EOL> return self . _values [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> if key in self . fields : <EOL> self . _values [ key ] = value <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" % <EOL> ( self . __class__ . __name__ , key ) ) <EOL> def __delitem__ ( self , key ) : <EOL> del self . _values [ key ] <EOL> def __getattr__ ( self , name ) : <EOL> if name in self . fields : <EOL> raise AttributeError ( "<STR_LIT>" % name ) <EOL> raise AttributeError ( name ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if not name . startswith ( '<STR_LIT:_>' ) : <EOL> raise AttributeError ( "<STR_LIT>" % <EOL> ( name , value ) ) <EOL> super ( DictItem , self ) . __setattr__ ( name , value ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _values ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _values ) <EOL> __hash__ = BaseItem . __hash__ <EOL> def keys ( self ) : <EOL> return self . _values . keys ( ) <EOL> def __repr__ ( self ) : <EOL> return pformat ( dict ( self ) ) <EOL> def copy ( self ) : <EOL> return self . __class__ ( self ) <EOL> @ six . add_metaclass ( ItemMeta ) <EOL> class Item ( DictItem ) : <EOL> pass </s>
