<s> import json <EOL> from copy import deepcopy <EOL> from collections import namedtuple <EOL> from couchbase . _pyport import long , xrange , ulp , basestring , parse_qs <EOL> from couchbase . exceptions import ArgumentError <EOL> STALE_UPDATE_BEFORE = "<STR_LIT:false>" <EOL> STALE_UPDATE_AFTER = "<STR_LIT>" <EOL> STALE_OK = "<STR_LIT>" <EOL> ONERROR_CONTINUE = "<STR_LIT>" <EOL> ONERROR_STOP = "<STR_LIT>" <EOL> class _Unspec ( object ) : <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> __bool__ = __nonzero__ <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> UNSPEC = _Unspec ( ) <EOL> def _bool_param_handler ( input ) : <EOL> if isinstance ( input , bool ) : <EOL> if input : <EOL> return "<STR_LIT:true>" <EOL> else : <EOL> return "<STR_LIT:false>" <EOL> if isinstance ( input , basestring ) : <EOL> if input not in ( "<STR_LIT:true>" , "<STR_LIT:false>" ) : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" <EOL> "<STR_LIT>" , input ) <EOL> return input <EOL> try : <EOL> input + <NUM_LIT:0> <EOL> if input : <EOL> return "<STR_LIT:true>" <EOL> else : <EOL> return "<STR_LIT:false>" <EOL> except TypeError : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , input ) <EOL> def _num_param_handler ( input ) : <EOL> if isinstance ( input , bool ) : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" , <EOL> input ) <EOL> try : <EOL> return str ( int ( input ) ) <EOL> except Exception as e : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" , input , e ) <EOL> def _string_param_common ( input , do_quote = False ) : <EOL> do_quote = False <EOL> s = None <EOL> if isinstance ( input , basestring ) : <EOL> s = input <EOL> elif isinstance ( input , bool ) : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" , input ) <EOL> elif isinstance ( input , ( int , long , float ) ) : <EOL> s = str ( input ) <EOL> else : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" , <EOL> input ) <EOL> if do_quote : <EOL> s = ulp . quote ( s ) <EOL> return s <EOL> def _string_param_handler ( input ) : <EOL> return _string_param_common ( input , do_quote = True ) <EOL> def _generic_param_handler ( input ) : <EOL> return _string_param_handler ( input , do_quote = False ) <EOL> def _stale_param_handler ( input ) : <EOL> if input in ( STALE_UPDATE_AFTER , STALE_OK , STALE_UPDATE_BEFORE ) : <EOL> return input <EOL> ret = _bool_param_handler ( input ) <EOL> if ret == "<STR_LIT:true>" : <EOL> ret = STALE_OK <EOL> return ret <EOL> def _onerror_param_handler ( input ) : <EOL> if input not in ( ONERROR_CONTINUE , ONERROR_STOP ) : <EOL> raise ArgumentError . pyexc ( <EOL> "<STR_LIT>" , input ) <EOL> return input <EOL> def _jval_param_handler ( input ) : <EOL> try : <EOL> ret = json . dumps ( input ) <EOL> return _string_param_handler ( ret ) <EOL> except Exception as e : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" , input , e ) <EOL> def _jarry_param_handler ( input ) : <EOL> ret = _jval_param_handler ( input ) <EOL> if not ret . startswith ( '<STR_LIT:[>' ) : <EOL> raise ArgumentError . pyexc ( <EOL> "<STR_LIT>" , input ) <EOL> return ret <EOL> class Params ( object ) : <EOL> DESCENDING = "<STR_LIT>" <EOL> STARTKEY = "<STR_LIT>" <EOL> STARTKEY_DOCID = "<STR_LIT>" <EOL> ENDKEY = "<STR_LIT>" <EOL> ENDKEY_DOCID = "<STR_LIT>" <EOL> KEY = "<STR_LIT:key>" <EOL> KEYS = "<STR_LIT>" <EOL> INCLUSIVE_END = "<STR_LIT>" <EOL> GROUP = "<STR_LIT>" <EOL> GROUP_LEVEL = "<STR_LIT>" <EOL> REDUCE = "<STR_LIT>" <EOL> SKIP = "<STR_LIT>" <EOL> LIMIT = "<STR_LIT>" <EOL> ON_ERROR = "<STR_LIT>" <EOL> STALE = "<STR_LIT>" <EOL> DEBUG = "<STR_LIT>" <EOL> CONNECTION_TIMEOUT = "<STR_LIT>" <EOL> FULL_SET = "<STR_LIT>" <EOL> MAPKEY_SINGLE = "<STR_LIT>" <EOL> MAPKEY_MULTI = "<STR_LIT>" <EOL> MAPKEY_RANGE = "<STR_LIT>" <EOL> DOCKEY_RANGE = "<STR_LIT>" <EOL> START_RANGE = "<STR_LIT>" <EOL> END_RANGE = "<STR_LIT>" <EOL> _HANDLER_MAP = { <EOL> Params . DESCENDING : _bool_param_handler , <EOL> Params . STARTKEY : _jval_param_handler , <EOL> Params . STARTKEY_DOCID : _string_param_handler , <EOL> Params . ENDKEY : _jval_param_handler , <EOL> Params . ENDKEY_DOCID : _string_param_handler , <EOL> Params . FULL_SET : _bool_param_handler , <EOL> Params . GROUP : _bool_param_handler , <EOL> Params . GROUP_LEVEL : _num_param_handler , <EOL> Params . INCLUSIVE_END : _bool_param_handler , <EOL> Params . KEY : _jval_param_handler , <EOL> Params . KEYS : _jarry_param_handler , <EOL> Params . ON_ERROR : _onerror_param_handler , <EOL> Params . REDUCE : _bool_param_handler , <EOL> Params . STALE : _stale_param_handler , <EOL> Params . SKIP : _num_param_handler , <EOL> Params . LIMIT : _num_param_handler , <EOL> Params . DEBUG : _bool_param_handler , <EOL> Params . CONNECTION_TIMEOUT : _num_param_handler , <EOL> Params . START_RANGE : _jarry_param_handler , <EOL> Params . END_RANGE : _jarry_param_handler , <EOL> } <EOL> def _gendoc ( param ) : <EOL> for k , v in Params . __dict__ . items ( ) : <EOL> if param == v : <EOL> return "<STR_LIT>" . format ( k ) <EOL> def _rangeprop ( k_sugar , k_start , k_end ) : <EOL> def getter ( self ) : <EOL> return self . _user_options . get ( k_sugar , UNSPEC ) <EOL> def setter ( self , value ) : <EOL> self . _set_range_common ( k_sugar , k_start , k_end , value ) <EOL> return property ( getter , setter , fdel = None , doc = _gendoc ( k_sugar ) ) <EOL> def _genprop ( p ) : <EOL> def getter ( self ) : <EOL> return self . _get_common ( p ) <EOL> def setter ( self , value ) : <EOL> self . _set_common ( p , value ) <EOL> return property ( getter , setter , fdel = None , doc = _gendoc ( p ) ) <EOL> class QueryBase ( object ) : <EOL> def _set_common ( self , param , value , set_user = True ) : <EOL> self . _encoded = None <EOL> if value is UNSPEC : <EOL> self . _real_options . pop ( param , None ) <EOL> if set_user : <EOL> self . _user_options . pop ( param , None ) <EOL> return <EOL> handler = _HANDLER_MAP . get ( param ) <EOL> if not handler : <EOL> if not self . unrecognized_ok : <EOL> raise ArgumentError . pyexc ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not handler : <EOL> self . _extra_options [ param ] = _string_param_handler ( value ) <EOL> return <EOL> if self . passthrough : <EOL> handler = _string_param_handler <EOL> self . _real_options [ param ] = handler ( value ) <EOL> if set_user : <EOL> self . _user_options [ param ] = value <EOL> def _get_common ( self , param ) : <EOL> if param in self . _user_options : <EOL> return self . _user_options [ param ] <EOL> return self . _real_options . get ( param , UNSPEC ) <EOL> def _set_range_common ( self , k_sugar , k_start , k_end , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , ( list , tuple , _Unspec ) ) : <EOL> raise ArgumentError . pyexc ( <EOL> "<STR_LIT>" <EOL> . format ( k_sugar ) ) <EOL> if self . _user_options . get ( k_start , UNSPEC ) is not UNSPEC or ( <EOL> self . _user_options . get ( k_end , UNSPEC ) is not UNSPEC ) : <EOL> raise ArgumentError . pyexc ( <EOL> "<STR_LIT>" <EOL> . format ( k_sugar , k_start , k_end ) ) <EOL> if not value : <EOL> self . _set_common ( k_start , UNSPEC , set_user = False ) <EOL> self . _set_common ( k_end , UNSPEC , set_user = False ) <EOL> self . _user_options [ k_sugar ] = UNSPEC <EOL> return <EOL> if len ( value ) not in ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> value ) <EOL> value = value [ : : ] <EOL> if len ( value ) == <NUM_LIT:1> : <EOL> value . append ( UNSPEC ) <EOL> for p , ix in ( ( k_start , <NUM_LIT:0> ) , ( k_end , <NUM_LIT:1> ) ) : <EOL> self . _set_common ( p , value [ ix ] , set_user = False ) <EOL> self . _user_options [ k_sugar ] = value <EOL> STRING_RANGE_END = json . loads ( '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , passthrough = False , unrecognized_ok = False , ** params ) : <EOL> """<STR_LIT>""" <EOL> self . passthrough = passthrough <EOL> self . unrecognized_ok = unrecognized_ok <EOL> self . _real_options = { } <EOL> self . _user_options = { } <EOL> self . _extra_options = { } <EOL> self . _encoded = None <EOL> self . _base_str = "<STR_LIT>" <EOL> self . update ( ** params ) <EOL> def update ( self , copy = False , ** params ) : <EOL> """<STR_LIT>""" <EOL> if copy : <EOL> self = deepcopy ( self ) <EOL> for k , v in params . items ( ) : <EOL> if not hasattr ( self , k ) : <EOL> if not self . unrecognized_ok : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" , k ) <EOL> self . _set_common ( k , v ) <EOL> else : <EOL> setattr ( self , k , v ) <EOL> return self <EOL> @ classmethod <EOL> def from_any ( cls , params , ** ctor_opts ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( params , cls ) : <EOL> return deepcopy ( params ) <EOL> elif isinstance ( params , dict ) : <EOL> ctor_opts . update ( ** params ) <EOL> if cls is QueryBase : <EOL> if ( '<STR_LIT>' in params or '<STR_LIT>' in params or <EOL> '<STR_LIT>' in params ) : <EOL> return SpatialQuery ( ** ctor_opts ) <EOL> else : <EOL> return ViewQuery ( ** ctor_opts ) <EOL> elif isinstance ( params , basestring ) : <EOL> ret = cls ( ) <EOL> ret . _base_str = params <EOL> return ret <EOL> else : <EOL> raise ArgumentError . pyexc ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def from_string ( cls , qstr ) : <EOL> """<STR_LIT>""" <EOL> return cls . from_any ( qstr ) <EOL> def _encode ( self , omit_keys = False ) : <EOL> res_d = [ ] <EOL> for k , v in self . _real_options . items ( ) : <EOL> if v is UNSPEC : <EOL> continue <EOL> if omit_keys and k == "<STR_LIT>" : <EOL> continue <EOL> if not self . passthrough : <EOL> k = ulp . quote ( k ) <EOL> v = ulp . quote ( v ) <EOL> res_d . append ( "<STR_LIT>" . format ( k , v ) ) <EOL> for k , v in self . _extra_options . items ( ) : <EOL> res_d . append ( "<STR_LIT>" . format ( k , v ) ) <EOL> return '<STR_LIT:&>' . join ( res_d ) <EOL> @ property <EOL> def encoded ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _encoded : <EOL> self . _encoded = self . _encode ( ) <EOL> if self . _base_str : <EOL> return '<STR_LIT:&>' . join ( ( self . _base_str , self . _encoded ) ) <EOL> else : <EOL> return self . _encoded <EOL> @ property <EOL> def _long_query_encoded ( self ) : <EOL> """<STR_LIT>""" <EOL> uristr = self . _encode ( omit_keys = True ) <EOL> kstr = "<STR_LIT:{}>" <EOL> klist = self . _real_options . get ( '<STR_LIT>' , UNSPEC ) <EOL> if klist != UNSPEC : <EOL> kstr = '<STR_LIT>' . format ( klist ) <EOL> return ( uristr , kstr ) <EOL> @ property <EOL> def has_blob ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _base_str or self . unrecognized_ok or self . passthrough <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . encoded ) <EOL> stale = _genprop ( Params . STALE ) <EOL> skip = _genprop ( Params . SKIP ) <EOL> limit = _genprop ( Params . LIMIT ) <EOL> full_set = _genprop ( Params . FULL_SET ) <EOL> connection_timeout = _genprop ( Params . CONNECTION_TIMEOUT ) <EOL> debug = _genprop ( Params . DEBUG ) <EOL> on_error = _genprop ( Params . ON_ERROR ) <EOL> class ViewQuery ( QueryBase ) : <EOL> descending = _genprop ( Params . DESCENDING ) <EOL> startkey = _genprop ( Params . STARTKEY ) <EOL> endkey = _genprop ( Params . ENDKEY ) <EOL> startkey_docid = _genprop ( Params . STARTKEY_DOCID ) <EOL> endkey_docid = _genprop ( Params . ENDKEY_DOCID ) <EOL> keys = _genprop ( Params . KEYS ) <EOL> key = _genprop ( Params . KEY ) <EOL> inclusive_end = _genprop ( Params . INCLUSIVE_END ) <EOL> reduce = _genprop ( Params . REDUCE ) <EOL> group = _genprop ( Params . GROUP ) <EOL> group_level = _genprop ( Params . GROUP_LEVEL ) <EOL> mapkey_single = _genprop ( Params . KEY ) <EOL> mapkey_multi = _genprop ( Params . KEYS ) <EOL> mapkey_range = _rangeprop ( Params . MAPKEY_RANGE , <EOL> Params . STARTKEY , Params . ENDKEY ) <EOL> dockey_range = _rangeprop ( Params . DOCKEY_RANGE , <EOL> Params . STARTKEY_DOCID , <EOL> Params . ENDKEY_DOCID ) <EOL> class SpatialQuery ( QueryBase ) : <EOL> start_range = _genprop ( Params . START_RANGE ) <EOL> end_range = _genprop ( Params . END_RANGE ) <EOL> class Query ( ViewQuery ) : <EOL> pass <EOL> def make_options_string ( input , unrecognized_ok = False , passthrough = False ) : <EOL> if not isinstance ( input , QueryBase ) : <EOL> input = QueryBase . from_any ( input , unrecognized_ok = unrecognized_ok , <EOL> passthrough = passthrough ) <EOL> return input . encoded <EOL> def make_dvpath ( doc , view ) : <EOL> return "<STR_LIT>" . format ( doc , view ) </s>
