idx
int64
0
251k
question
stringlengths
53
3.53k
target
stringlengths
5
1.23k
len_question
int64
20
893
len_target
int64
3
238
238,800
def query_all ( self ) : return self . query_model ( self . model , self . condition , order_by = self . order_by , group_by = self . group_by , having = self . having )
Query all records without limit and offset .
49
8
238,801
def copy ( self ) : missing = object ( ) result = object . __new__ ( self . __class__ ) for name in self . __slots__ : val = getattr ( self , name , missing ) if val is not missing : setattr ( result , name , val ) return result
Create a flat copy of the dict .
63
8
238,802
def list ( self ) : before , after = self . filename_template . split ( '%s' , 1 ) filename_re = re . compile ( r'%s(.{5,})%s$' % ( re . escape ( before ) , re . escape ( after ) ) ) result = [ ] for filename in os . listdir ( self . path ) : #: this is a session that is still being saved. if filename . endswith ( _fs_transaction_suffix ) : continue match = filename_re . match ( filename ) if match is not None : result . append ( match . group ( 1 ) ) return result
Lists all sessions in the store .
140
8
238,803
def to_timezone ( dt , tzinfo = None ) : if not dt : return dt tz = pick_timezone ( tzinfo , __timezone__ ) if not tz : return dt dttz = getattr ( dt , 'tzinfo' , None ) if not dttz : return dt . replace ( tzinfo = tz ) else : return dt . astimezone ( tz )
Convert a datetime to timezone
98
8
238,804
def to_date ( dt , tzinfo = None , format = None ) : d = to_datetime ( dt , tzinfo , format ) if not d : return d return date ( d . year , d . month , d . day )
Convert a datetime to date with tzinfo
56
11
238,805
def to_time ( dt , tzinfo = None , format = None ) : d = to_datetime ( dt , tzinfo , format ) if not d : return d return time_ ( d . hour , d . minute , d . second , d . microsecond , tzinfo = d . tzinfo )
Convert a datetime to time with tzinfo
72
11
238,806
def to_datetime ( dt , tzinfo = None , format = None ) : if not dt : return dt tz = pick_timezone ( tzinfo , __timezone__ ) if isinstance ( dt , ( str , unicode ) ) : if not format : formats = DEFAULT_DATETIME_INPUT_FORMATS else : formats = list ( format ) d = None for fmt in formats : try : d = datetime . strptime ( dt , fmt ) except ValueError : continue if not d : return None d = d . replace ( tzinfo = tz ) else : d = datetime ( getattr ( dt , 'year' , 1970 ) , getattr ( dt , 'month' , 1 ) , getattr ( dt , 'day' , 1 ) , getattr ( dt , 'hour' , 0 ) , getattr ( dt , 'minute' , 0 ) , getattr ( dt , 'second' , 0 ) , getattr ( dt , 'microsecond' , 0 ) ) if not getattr ( dt , 'tzinfo' , None ) : d = d . replace ( tzinfo = tz ) else : d = d . replace ( tzinfo = dt . tzinfo ) return to_timezone ( d , tzinfo )
Convert a date or time to datetime with tzinfo
294
13
238,807
def parse_time ( t ) : if isinstance ( t , ( str , unicode ) ) : b = re_time . match ( t ) if b : v , unit = int ( b . group ( 1 ) ) , b . group ( 2 ) if unit == 's' : return v * 1000 elif unit == 'm' : return v * 60 * 1000 elif unit == 'h' : return v * 60 * 60 * 1000 else : return v else : raise TimeFormatError ( t ) elif isinstance ( t , ( int , long ) ) : return t else : raise TimeFormatError ( t )
Parse string time format to microsecond
134
8
238,808
def process_exception ( self , request , e ) : if isinstance ( e , RedirectException ) : response = e . get_response ( ) self . process_response ( request , response )
Still process session data when specially Exception
43
7
238,809
def jsonp ( data , * * json_kwargs ) : from uliweb import request if 'jsonp' in json_kwargs : cb = json_kwargs . pop ( 'jsonp' ) else : cb = 'callback' begin = str ( request . GET . get ( cb ) ) if not begin : raise BadRequest ( "Can't found %s parameter in request's query_string" % cb ) if not r_callback . match ( begin ) : raise BadRequest ( "The callback name is not right, it can be alphabetic, number and underscore only" ) if callable ( data ) : @ wraps ( data ) def f ( * arg , * * kwargs ) : ret = data ( * arg , * * kwargs ) return Response ( begin + '(' + json_dumps ( ret ) + ');' , * * json_kwargs ) return f else : return Response ( begin + '(' + json_dumps ( data ) + ');' , * * json_kwargs )
jsonp is callback key name
226
6
238,810
def get_url_adapter ( _domain_name ) : from werkzeug . _compat import wsgi_decoding_dance domain = application . domains . get ( _domain_name , { } ) server_name = None if domain . get ( 'domain' , '' ) : server_name = domain [ 'domain' ] try : env = { } environ = request . environ env [ 'url_scheme' ] = environ [ 'wsgi.url_scheme' ] env [ 'default_method' ] = environ [ 'REQUEST_METHOD' ] def _get_wsgi_string ( name ) : val = environ . get ( name ) if val is not None : return wsgi_decoding_dance ( val , "utf-8" ) env [ 'script_name' ] = _get_wsgi_string ( 'SCRIPT_NAME' ) env [ 'path_info' ] = _get_wsgi_string ( 'PATH_INFO' ) env [ 'query_args' ] = _get_wsgi_string ( 'QUERY_STRING' ) except : env = { } adapter = url_map . bind ( server_name , * * env ) else : try : env = request . environ except : #this env if for testing only env = { 'HTTP_ACCEPT' : 'text/html,application/xhtml+xml,application/xml;' 'q=0.9,*/*;q=0.8' , 'HTTP_ACCEPT_CHARSET' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3' , 'HTTP_ACCEPT_ENCODING' : 'gzip,deflate,sdch' , 'HTTP_ACCEPT_LANGUAGE' : 'uk,en-US;q=0.8,en;q=0.6' , 'HTTP_CACHE_CONTROL' : 'max-age=0' , 'HTTP_CONNECTION' : 'keep-alive' , # 'HTTP_HOST': 'localhost:8080', 'HTTP_USER_AGENT' : 'Mozilla/5.0 (X11; Linux i686)' , # 'PATH_INFO': '/', # 'QUERY_STRING': '', 'REMOTE_ADDR' : '127.0.0.1' , 'REQUEST_METHOD' : 'GET' , 'REQUEST_URI' : '/' , 'SCRIPT_NAME' : '' , 'SERVER_NAME' : 'localhost' , 'SERVER_PORT' : '8080' , 'SERVER_PROTOCOL' : 'HTTP/1.1' , 'wsgi.errors' : None , 'wsgi.file_wrapper' : None , # 'wsgi.input': BytesIO(ntob('', 'utf-8')), 'wsgi.multiprocess' : False , 'wsgi.multithread' : False , 'wsgi.run_once' : False , 'wsgi.url_scheme' : 'http' , 'wsgi.version' : ( 1 , 0 ) , } adapter = url_map . bind_to_environ ( env ) return adapter
Fetch a domain url_adapter object and bind it to according domain
729
15
238,811
def get_app_dir ( app ) : path = __app_dirs__ . get ( app ) if path is not None : return path else : p = app . split ( '.' ) try : path = pkg . resource_filename ( p [ 0 ] , '' ) except ImportError as e : log . error ( "Can't import app %s" % app ) log . exception ( e ) path = '' if len ( p ) > 1 : path = os . path . join ( path , * p [ 1 : ] ) __app_dirs__ [ app ] = path return path
Get an app s directory
128
5
238,812
def get_file ( self , filename , dir = 'static' ) : if os . path . exists ( filename ) : return filename dirs = self . apps if dir : fname = os . path . join ( dir , filename ) else : fname = filename for d in reversed ( dirs ) : path = pkg . resource_filename ( d , fname ) if os . path . exists ( path ) : return path return None
get_file will search from apps directory
93
8
238,813
def get_template_dirs ( self ) : def if_not_empty ( dir ) : if not os . path . exists ( dir ) : return for root , dirs , files in os . walk ( dir ) : if dirs : return True for f in files : if f != 'readme.txt' : return True template_dirs = [ os . path . join ( self . project_dir , x ) for x in settings . GLOBAL . TEMPLATE_DIRS or [ ] ] taglibs_dirs = [ ] for p in reversed ( self . apps ) : app_path = get_app_dir ( p ) path = os . path . join ( app_path , 'templates' ) if if_not_empty ( path ) : template_dirs . append ( path ) path = os . path . join ( app_path , 'taglibs' ) if if_not_empty ( path ) : taglibs_dirs . append ( path ) Dispatcher . template_dirs = template_dirs Dispatcher . taglibs_dirs = taglibs_dirs
Get templates directory from apps but in reversed order so the same named template file will be overrided by latter defined app
247
23
238,814
def get_lock ( key , value = None , expiry_time = 60 ) : from uliweb . utils . common import get_uuid redis = get_redis ( ) value = value or get_uuid ( ) return redis . set ( key , value , ex = expiry_time , nx = True )
Get a distribute lock
74
4
238,815
def set_lock ( key , value = None , expiry_time = 60 ) : from uliweb . utils . common import get_uuid redis = get_redis ( ) value = value or get_uuid ( ) return redis . set ( key , value , ex = expiry_time , xx = True )
Force to set a distribute lock
73
6
238,816
def after_init_apps ( sender ) : from uliweb import settings from uliweb . utils . common import log check = settings . get_var ( 'REDIS/check_version' ) if check : client = get_redis ( ) try : info = client . info ( ) except Exception as e : log . exception ( e ) log . error ( 'Redis is not started!' ) return redis_version = info [ 'redis_version' ] version = tuple ( map ( int , redis_version . split ( '.' ) ) ) op = re_compare_op . search ( check ) if op : _op = op . group ( ) _v = check [ op . end ( ) + 1 : ] . strip ( ) else : _op = '=' _v = check nv = tuple ( map ( int , _v . split ( '.' ) ) ) if _op == '=' : flag = version [ : len ( nv ) ] == nv elif _op == '>=' : flag = version >= nv elif _op == '>' : flag = version > nv elif _op == '<=' : flag = version <= nv elif _op == '<' : flag = version < nv else : log . error ( "Can't support operator %s when check redis version" % _op ) if not flag : log . error ( "Redis version %s is not matched what you want %s" % ( redis_version , _v ) )
Check redis version
332
4
238,817
def _make_text_block ( name , content , content_type = None ) : if content_type == 'xhtml' : return u'<%s type="xhtml"><div xmlns="%s">%s</div></%s>\n' % ( name , XHTML_NAMESPACE , content , name ) if not content_type : return u'<%s>%s</%s>\n' % ( name , escape ( content ) , name ) return u'<%s type="%s">%s</%s>\n' % ( name , content_type , escape ( content ) , name )
Helper function for the builder that creates an XML text block .
142
12
238,818
def _style_range ( self , cell , cell_range , border = None , fill = None , font = None , alignment = None ) : from openpyxl . styles import Border , Side top = left = right = bottom = Side ( border_style = 'thin' , color = self . border_color ) def border_add ( border , top = None , right = None , left = None , bottom = None ) : top = top or border . top left = left or border . left right = right or border . right bottom = bottom or border . bottom return Border ( top = top , left = left , right = right , bottom = bottom ) cell . alignment = alignment cell . fill = fill rows = list ( self . sheet [ cell_range ] ) for cell in rows [ 0 ] : cell . border = border_add ( cell . border , top = top ) for cell in rows [ - 1 ] : cell . border = border_add ( cell . border , bottom = bottom ) for row in rows : l = row [ 0 ] r = row [ - 1 ] l . border = border_add ( l . border , left = left ) r . border = border_add ( r . border , right = right )
Apply styles to a range of cells as if they were a single cell .
261
15
238,819
def url_unquote ( string , charset = 'utf-8' , errors = 'replace' , unsafe = '' ) : rv = _unquote_to_bytes ( string , unsafe ) if charset is not None : rv = rv . decode ( charset , errors ) return rv
URL decode a single string with a given encoding . If the charset is set to None no unicode decoding is performed and raw bytes are returned .
66
30
238,820
def decode_netloc ( self ) : rv = _decode_idna ( self . host or '' ) if ':' in rv : rv = '[%s]' % rv port = self . port if port is not None : rv = '%s:%d' % ( rv , port ) auth = ':' . join ( filter ( None , [ _url_unquote_legacy ( self . raw_username or '' , '/:%@' ) , _url_unquote_legacy ( self . raw_password or '' , '/:%@' ) , ] ) ) if auth : rv = '%s@%s' % ( auth , rv ) return rv
Decodes the netloc part into a string .
156
10
238,821
def decode ( self , charset = 'utf-8' , errors = 'replace' ) : return URL ( self . scheme . decode ( 'ascii' ) , self . decode_netloc ( ) , self . path . decode ( charset , errors ) , self . query . decode ( charset , errors ) , self . fragment . decode ( charset , errors ) )
Decodes the URL to a tuple made out of strings . The charset is only being used for the path query and fragment .
82
26
238,822
def _mixed_join ( iterable , sentinel ) : iterator = iter ( iterable ) first_item = next ( iterator , sentinel ) if isinstance ( first_item , bytes ) : return first_item + b'' . join ( iterator ) return first_item + u'' . join ( iterator )
concatenate any string type in an intelligent way .
67
12
238,823
def _buf_append ( self , string ) : if not self . _buf : self . _buf = string else : self . _buf += string
Replace string directly without appending to an empty string avoiding type issues .
32
15
238,824
def quote_etag ( etag , weak = False ) : if '"' in etag : raise ValueError ( 'invalid etag' ) etag = '"%s"' % etag if weak : etag = 'w/' + etag return etag
Quote an etag .
59
5
238,825
def parse_etags ( value ) : if not value : return ETags ( ) strong = [ ] weak = [ ] end = len ( value ) pos = 0 while pos < end : match = _etag_re . match ( value , pos ) if match is None : break is_weak , quoted , raw = match . groups ( ) if raw == '*' : return ETags ( star_tag = True ) elif quoted : raw = quoted if is_weak : weak . append ( raw ) else : strong . append ( raw ) pos = match . end ( ) return ETags ( strong , weak )
Parse an etag header .
131
7
238,826
def wait_pid ( pid , timeout = None , callback = None ) : def check_timeout ( delay ) : if timeout is not None : if time . time ( ) >= stop_at : if callback : callback ( pid ) else : raise TimeoutExpired time . sleep ( delay ) return min ( delay * 2 , 0.04 ) if timeout is not None : waitcall = lambda : os . waitpid ( pid , os . WNOHANG ) stop_at = time . time ( ) + timeout else : waitcall = lambda : os . waitpid ( pid , 0 ) delay = 0.0001 while 1 : try : retpid , status = waitcall ( ) except OSError as err : if err . errno == errno . EINTR : delay = check_timeout ( delay ) continue elif err . errno == errno . ECHILD : # This has two meanings: # - pid is not a child of os.getpid() in which case # we keep polling until it's gone # - pid never existed in the first place # In both cases we'll eventually return None as we # can't determine its exit status code. while 1 : if pid_exists ( pid ) : delay = check_timeout ( delay ) else : return else : raise else : if retpid == 0 : # WNOHANG was used, pid is still running delay = check_timeout ( delay ) continue # process exited due to a signal; return the integer of # that signal if os . WIFSIGNALED ( status ) : return os . WTERMSIG ( status ) # process exited using exit(2) system call; return the # integer exit(2) system call has been called with elif os . WIFEXITED ( status ) : return os . WEXITSTATUS ( status ) else : # should never happen raise RuntimeError ( "unknown process exit status" )
Wait for process with pid pid to terminate and return its exit status code as an integer .
403
18
238,827
def get_filename ( self , filename , filesystem = False , convert = False , subpath = '' ) : from uliweb . utils . common import safe_unicode #make sure the filename is unicode s = settings . GLOBAL if convert : _p , _f = os . path . split ( filename ) _filename = os . path . join ( _p , self . filename_convert ( _f ) ) else : _filename = filename nfile = safe_unicode ( _filename , s . HTMLPAGE_ENCODING ) if subpath : paths = [ application_path ( self . to_path ) , subpath , nfile ] else : paths = [ application_path ( self . to_path ) , nfile ] f = os . path . normpath ( os . path . join ( * paths ) ) . replace ( '\\' , '/' ) if filesystem : return files . encode_filename ( f , to_encoding = s . FILESYSTEM_ENCODING ) return f
Get the filename according to self . to_path and if filesystem is False then return unicode filename otherwise return filesystem encoded filename
221
25
238,828
def download ( self , filename , action = 'download' , x_filename = '' , x_sendfile = None , real_filename = '' ) : from uliweb import request from uliweb . utils . common import safe_str from uliweb . utils . filedown import filedown s = settings . GLOBAL action = request . GET . get ( 'action' , action ) if not real_filename : real_filename = self . get_filename ( filename , True , convert = False ) else : real_filename = files . encode_filename ( real_filename , to_encoding = s . FILESYSTEM_ENCODING ) if not x_filename : x_filename = safe_str ( filename , s . FILESYSTEM_ENCODING ) if self . x_file_prefix : x_filename = os . path . normpath ( os . path . join ( self . x_file_prefix , x_filename ) ) . replace ( '\\' , '/' ) xsend_flag = bool ( self . x_sendfile ) if x_sendfile is None else x_sendfile return filedown ( request . environ , filename , action = action , x_sendfile = xsend_flag , x_header_name = self . x_header_name , x_filename = x_filename , real_filename = real_filename )
action will be download inline and if the request . GET has action then the action will be replaced by it .
299
22
238,829
def logout ( ) : from uliweb import request delete_user_session ( ) request . session . delete ( ) request . user = None return True
Remove the authenticated user s ID from the request .
33
10
238,830
def get ( self , key ) : if isinstance ( key , unicode ) : key = key . encode ( 'utf-8' ) v = self . client . get ( key ) if v is None : raise KeyError ( "Cache key [%s] not found" % key ) else : return v
because memcached does not provide a function to check if a key is existed so here is a heck way if the value is None then raise Exception
66
30
238,831
def get_commands ( mod ) : import inspect import types commands = { } def check ( c ) : return ( inspect . isclass ( c ) and issubclass ( c , Command ) and c is not Command and not issubclass ( c , CommandManager ) ) for name in dir ( mod ) : c = getattr ( mod , name ) if check ( c ) : commands [ c . name ] = c return commands
Find commands from a module
91
5
238,832
def usage ( self , subcommand ) : if len ( self . option_list ) > 0 : usage = '%%prog %s [options] %s' % ( subcommand , self . args ) else : usage = '%%prog %s %s' % ( subcommand , self . args ) if self . help : return '%s\n\n%s' % ( usage , self . help ) else : return usage
Return a brief description of how to use this command by default from the attribute self . help .
94
19
238,833
def show_table ( name , table , i , total ) : return '[%d/%d, %s] %s' % ( i + 1 , total , table . __appname__ , name )
Display table info name is tablename table is table object i is current Index total is total of tables
45
21
238,834
def get_template ( self , name ) : filename = path . join ( self . search_path , * [ p for p in name . split ( '/' ) if p and p [ 0 ] != '.' ] ) if not path . exists ( filename ) : raise TemplateNotFound ( name ) return Template . from_file ( filename , self . encoding )
Get a template from a given name .
76
8
238,835
def render_to_string ( self , * args , * * kwargs ) : try : template_name , args = args [ 0 ] , args [ 1 : ] except IndexError : raise TypeError ( 'name of template required' ) return self . get_template ( template_name ) . render ( * args , * * kwargs )
Load and render a template into a unicode string .
75
11
238,836
def get_template ( self , template_name ) : try : return self . loader . load ( template_name , encoding = self . encoding ) except self . not_found_exception , e : # catch the exception raised by Genshi, convert it into a werkzeug # exception (for the sake of consistency) raise TemplateNotFound ( template_name )
Get the template which is at the given name
79
9
238,837
def render_to_string ( self , template_name , context = None ) : # create an empty context if no context was specified context = context or { } tmpl = self . get_template ( template_name ) # render the template into a unicode string (None means unicode) return tmpl . generate ( * * context ) . render ( self . output_type , encoding = None )
Load and render a template into an unicode string
87
10
238,838
def process_permission_roles ( perm , v ) : if isinstance ( v , ( tuple , list ) ) : roles = v else : roles = [ v ] for r in roles : if isinstance ( r , ( tuple , list ) ) : role_name , role_props = r else : role_name , role_props = r , '' role = Role . get ( Role . c . name == role_name ) if not role : raise Exception , 'Role [%s] not found.' % r rel = Rel . get ( ( Rel . c . role == role . id ) & ( Rel . c . permission == perm . id ) ) if not rel : rel = Rel ( role = role , permission = perm , props = role_props ) msg = 'Add Relation(Permision=%s, Role=%s)...' % ( name , role_name ) else : rel . update ( props = role_props ) msg = 'Update Relation(Permision=%s, Role=%s)...' % ( name , role_name ) flag = rel . save ( ) if flag : print msg
v is roles
247
3
238,839
def generate_adapter ( adapter , name = 'url_for' , map_name = 'url_map' ) : values = { u'server_name' : dumps ( adapter . server_name ) , u'script_name' : dumps ( adapter . script_name ) , u'subdomain' : dumps ( adapter . subdomain ) , u'url_scheme' : dumps ( adapter . url_scheme ) , u'name' : name , u'map_name' : map_name } return u'''\ var %(name)s = %(map_name)s( %(server_name)s, %(script_name)s, %(subdomain)s, %(url_scheme)s );''' % values
Generates the url building function for a map .
168
10
238,840
def js_to_url_function ( converter ) : if hasattr ( converter , 'js_to_url_function' ) : data = converter . js_to_url_function ( ) else : for cls in getmro ( type ( converter ) ) : if cls in js_to_url_functions : data = js_to_url_functions [ cls ] ( converter ) break else : return 'encodeURIComponent' return '(function(value) { %s })' % data
Get the JavaScript converter function from a rule .
113
9
238,841
def _warn_if_string ( iterable ) : if isinstance ( iterable , string_types ) : from warnings import warn warn ( Warning ( 'response iterable was set to a string. This appears ' 'to work but means that the server will send the ' 'data to the client char, by char. This is almost ' 'never intended behavior, use response.data to assign ' 'strings to the response object.' ) , stacklevel = 2 )
Helper for the response objects to check if the iterable returned to the WSGI server is not a string .
97
22
238,842
def _get_file_stream ( self , total_content_length , content_type , filename = None , content_length = None ) : return default_stream_factory ( total_content_length , content_type , filename , content_length )
Called to get a stream for the file upload .
55
11
238,843
def close ( self ) : files = self . __dict__ . get ( 'files' ) for key , value in iter_multi_items ( files or ( ) ) : value . close ( )
Closes associated resources of this request object . This closes all file handles explicitly . You can also use the request object in a with statement with will automatically close it .
42
33
238,844
def get_data ( self , as_text = False ) : self . _ensure_sequence ( ) rv = b'' . join ( self . iter_encoded ( ) ) if as_text : rv = rv . decode ( self . charset ) return rv
The string representation of the request body . Whenever you call this property the request iterable is encoded and flattened . This can lead to unwanted behavior if you stream big data .
61
34
238,845
def _ensure_sequence ( self , mutable = False ) : if self . is_sequence : # if we need a mutable object, we ensure it's a list. if mutable and not isinstance ( self . response , list ) : self . response = list ( self . response ) return if self . direct_passthrough : raise RuntimeError ( 'Attempted implicit sequence conversion ' 'but the response object is in direct ' 'passthrough mode.' ) if not self . implicit_sequence_conversion : raise RuntimeError ( 'The response object required the iterable ' 'to be a sequence, but the implicit ' 'conversion was disabled. Call ' 'make_sequence() yourself.' ) self . make_sequence ( )
This method can be called by methods that need a sequence . If mutable is true it will also ensure that the response sequence is a standard Python list .
157
31
238,846
def delete_cookie ( self , key , path = '/' , domain = None ) : self . set_cookie ( key , expires = 0 , max_age = 0 , path = path , domain = domain )
Delete a cookie . Fails silently if key doesn t exist .
45
13
238,847
def freeze ( self ) : # we explicitly set the length to a list of the *encoded* response # iterator. Even if the implicit sequence conversion is disabled. self . response = list ( self . iter_encoded ( ) ) self . headers [ 'Content-Length' ] = str ( sum ( map ( len , self . response ) ) )
Call this method if you want to make your response object ready for being pickled . This buffers the generator if there is one . It will also set the Content - Length header to the length of the body .
74
42
238,848
def get_app_iter ( self , environ ) : status = self . status_code if environ [ 'REQUEST_METHOD' ] == 'HEAD' or 100 <= status < 200 or status in ( 204 , 304 ) : iterable = ( ) elif self . direct_passthrough : if __debug__ : _warn_if_string ( self . response ) return self . response else : iterable = self . iter_encoded ( ) return ClosingIterator ( iterable , self . close )
Returns the application iterator for the given environ . Depending on the request method and the current status code the return value might be an empty response rather than the one from the response .
109
36
238,849
def www_authenticate ( self ) : def on_update ( www_auth ) : if not www_auth and 'www-authenticate' in self . headers : del self . headers [ 'www-authenticate' ] elif www_auth : self . headers [ 'WWW-Authenticate' ] = www_auth . to_header ( ) header = self . headers . get ( 'www-authenticate' ) return parse_www_authenticate_header ( header , on_update )
The WWW - Authenticate header in a parsed form .
107
12
238,850
def make_alias_redirect_url ( self , path , endpoint , values , method , query_args ) : url = self . build ( endpoint , values , method , append_unknown = False , force_external = True ) if query_args : url += '?' + self . encode_query_args ( query_args ) assert url != path , 'detected invalid alias setting. No canonical ' 'URL found' return url
Internally called to make an alias redirect URL .
92
10
238,851
def from_file ( cls , file , charset = 'utf-8' , errors = 'strict' , unicode_mode = True ) : close = False f = file if isinstance ( file , basestring ) : f = open ( file , 'r' ) close = True try : data = _decode_unicode ( f . read ( ) , charset , errors ) finally : if close : f . close ( ) return cls ( data , getattr ( f , 'name' , '<template>' ) , charset , errors , unicode_mode )
Load a template from a file .
128
7
238,852
def safe_str_cmp ( a , b ) : if _builtin_safe_str_cmp is not None : return _builtin_safe_str_cmp ( a , b ) if len ( a ) != len ( b ) : return False rv = 0 if isinstance ( a , bytes ) and isinstance ( b , bytes ) and not PY2 : for x , y in izip ( a , b ) : rv |= x ^ y else : for x , y in izip ( a , b ) : rv |= ord ( x ) ^ ord ( y ) return rv == 0
This function compares strings in somewhat constant time . This requires that the length of at least one string is known in advance .
135
24
238,853
def gen_salt ( length ) : if length <= 0 : raise ValueError ( 'requested salt of length <= 0' ) return '' . join ( _sys_rng . choice ( SALT_CHARS ) for _ in range_type ( length ) )
Generate a random string of SALT_CHARS with specified length .
57
15
238,854
def html ( self , data = '' , py = True ) : if py : value = self . to_html ( data ) else : value = data if self . static : return str ( '<span class="value">%s</span>' % safe_str ( value ) ) else : if self . hidden : build = Hidden else : build = self . build self . _get_http_attrs ( ) return str ( build ( name = self . name , value = value , id = self . id , * * self . html_attrs ) )
Convert data to html value format .
120
8
238,855
def validate ( self , data , all_data = None ) : all_data = all_data or { } if hasattr ( data , 'stream' ) : data . file = data . stream if hasattr ( data , 'file' ) : if data . file : v = data . filename else : raise Exception , 'Unsupport type %s' % type ( data ) else : v = data # if v is None: msg = TEST_NOT_EMPTY ( ) ( v ) if self . required : if msg : return False , msg else : if msg : return True , self . default try : if isinstance ( data , list ) : v = [ ] for i in data : v . append ( self . to_python ( i ) ) data = v else : data = self . to_python ( data ) except : return False , unicode ( ERR_CONVERT ) % ( data , self . __class__ . __name__ ) for v in self . get_validators ( ) : msg = v ( data , all_data ) if msg : return False , msg return True , data
if rule in kwargs then validate extra rules
236
10
238,856
def cache_property ( key , empty , type ) : return property ( lambda x : x . _get_cache_value ( key , empty , type ) , lambda x , v : x . _set_cache_value ( key , v , type ) , lambda x : x . _del_cache_value ( key ) , 'accessor for %r' % key )
Return a new property object for a cache header . Useful if you want to add support for a cache extension in a subclass .
80
25
238,857
def set ( self , start , stop , length = None , units = 'bytes' ) : assert is_byte_range_valid ( start , stop , length ) , 'Bad range provided' self . _units = units self . _start = start self . _stop = stop self . _length = length if self . on_update is not None : self . on_update ( self )
Simple method to update the ranges .
83
7
238,858
def qop ( self ) : def on_update ( header_set ) : if not header_set and 'qop' in self : del self [ 'qop' ] elif header_set : self [ 'qop' ] = header_set . to_header ( ) return parse_set_header ( self . get ( 'qop' ) , on_update )
Indicates what quality of protection the client has applied to the message for HTTP digest auth .
82
18
238,859
def set_basic ( self , realm = 'authentication required' ) : dict . clear ( self ) dict . update ( self , { '__auth_type__' : 'basic' , 'realm' : realm } ) if self . on_update : self . on_update ( self )
Clear the auth info and enable basic auth .
64
9
238,860
def get_connection ( connection = '' , engine_name = None , connection_type = 'long' , * * args ) : engine_name = engine_name or __default_engine__ if '://' in connection : d = { 'connection_string' : connection , 'connection_args' : args , 'connection_type' : connection_type , } return engine_manager . add ( engine_name , d ) . engine else : connection = connection or __default_engine__ if connection in engine_manager : return engine_manager [ connection ] . engine else : raise Error ( "Can't find engine %s" % connection )
Creating an NamedEngine or just return existed engine instance
136
10
238,861
def get_metadata ( engine_name = None ) : dispatch . get ( None , 'load_models' ) engine = engine_manager [ engine_name ] for tablename , m in engine . models . items ( ) : get_model ( tablename , engine_name , signal = False ) if hasattr ( m , '__dynamic__' ) and getattr ( m , '__dynamic__' ) : m . table . __mapping_only__ = True return engine . metadata
get metadata according used for alembic It ll import all tables
108
13
238,862
def get_session ( ec = None , create = True ) : ec = ec or __default_engine__ if isinstance ( ec , ( str , unicode ) ) : session = engine_manager [ ec ] . session ( create = True ) elif isinstance ( ec , Session ) : session = ec else : raise Error ( "Connection %r should be existed engine name or Session object" % ec ) return session
ec - engine_name or connection
88
7
238,863
def rawsql ( query , ec = None ) : if isinstance ( query , Result ) : query = query . get_query ( ) ec = ec or __default_engine__ if isinstance ( ec , ( str , unicode ) ) : engine = engine_manager [ ec ] dialect = engine . engine . dialect else : dialect = ec . dialect if isinstance ( query , ( str , unicode ) ) : return query # comp = query.compile(dialect=dialect) compiler = query . _compiler ( dialect ) class LiteralCompiler ( compiler . __class__ ) : def visit_bindparam ( self , bindparam , within_columns_clause = False , literal_binds = False , * * kwargs ) : return super ( LiteralCompiler , self ) . render_literal_bindparam ( bindparam , within_columns_clause = within_columns_clause , literal_binds = literal_binds , * * kwargs ) def render_literal_value ( self , value , type_ ) : """Render the value of a bind parameter as a quoted literal. This is used for statement sections that do not accept bind paramters on the target driver/database. This should be implemented by subclasses using the quoting services of the DBAPI. """ return repr_value ( value ) compiler = LiteralCompiler ( dialect , query ) return str ( compiler . process ( query ) ) . replace ( '\n' , '' )
ec could be engine name or engine instance
320
8
238,864
def get_engine_name ( ec = None ) : ec = ec or __default_engine__ if isinstance ( ec , ( str , unicode ) ) : return ec elif isinstance ( ec , Session ) : return ec . engine_name else : raise Error ( "Parameter ec should be an engine_name or Session object, but %r found" % ec )
Get the name of a engine or session
79
8
238,865
def CommitAll ( close = None ) : if close : warnings . simplefilter ( 'default' ) warnings . warn ( "close parameter will not need at all." , DeprecationWarning ) for k , v in engine_manager . items ( ) : session = v . session ( create = False ) if session : session . commit ( )
Commit all transactions according Local . conn
71
8
238,866
def RollbackAll ( close = None ) : if close : warnings . simplefilter ( 'default' ) warnings . warn ( "close parameter will not need at all." , DeprecationWarning ) for k , v in engine_manager . items ( ) : session = v . session ( create = False ) if session : session . rollback ( )
Rollback all transactions according Local . conn
73
8
238,867
def set_model ( model , tablename = None , created = None , appname = None , model_path = None ) : if isinstance ( model , type ) and issubclass ( model , Model ) : #use alias first tablename = model . _alias or model . tablename tablename = tablename . lower ( ) #set global __models__ d = __models__ . setdefault ( tablename , { } ) engines = d . get ( 'config' , { } ) . pop ( 'engines' , [ 'default' ] ) if isinstance ( engines , ( str , unicode ) ) : engines = [ engines ] d [ 'engines' ] = engines item = { } if created is not None : item [ 'created' ] = created else : item [ 'created' ] = None if isinstance ( model , ( str , unicode ) ) : if model_path is None : model_path = model else : model_path = model_path if not appname : appname = model . rsplit ( '.' , 2 ) [ 0 ] #for example 'uliweb.contrib.auth.models.User' model = None else : appname = model . __module__ . rsplit ( '.' , 1 ) [ 0 ] if model_path is None : model_path = model . __module__ + '.' + model . __name__ else : model_path = '' #for example 'uliweb.contrib.auth.models' model . __engines__ = engines item [ 'model' ] = model item [ 'model_path' ] = model_path item [ 'appname' ] = appname d [ 'model_path' ] = model_path d [ 'appname' ] = appname for name in engines : if not isinstance ( name , ( str , unicode ) ) : raise BadValueError ( 'Engine name should be string type, but %r found' % name ) engine_manager [ name ] . models [ tablename ] = item . copy ( )
Register an model and tablename to a global variable . model could be a string format i . e . uliweb . contrib . auth . models . User
444
34
238,868
def create_model ( modelname , fields , indexes = None , basemodel = None , * * props ) : assert not props or isinstance ( props , dict ) assert not indexes or isinstance ( indexes , list ) props = SortedDict ( props or { } ) props [ '__dynamic__' ] = True props [ '__config__' ] = False for p in fields : kwargs = p . copy ( ) name = kwargs . pop ( 'name' ) _type = kwargs . pop ( 'type' ) #if the key is start with '_', then remove it for k in kwargs . keys ( ) : if k . startswith ( '_' ) : kwargs . pop ( k , None ) field_type = get_field_type ( _type ) prop = field_type ( * * kwargs ) props [ name ] = prop if basemodel : model = import_attr ( basemodel ) # model.clear_relation() else : model = Model # try: # old = get_model(modelname, signal=False) # old.clear_relation() # except ModelNotFound as e: # pass cls = type ( str ( modelname . title ( ) ) , ( model , ) , props ) tablename = props . get ( '__tablename__' , modelname ) set_model ( cls , tablename , appname = __name__ , model_path = '' ) get_model ( modelname , signal = False , reload = True ) indexes = indexes or [ ] for x in indexes : kwargs = x . copy ( ) name = kwargs . pop ( 'name' ) fields = kwargs . pop ( 'fields' ) #if the key is start with '_', then remove it for k in kwargs . keys ( ) : if k . startswith ( '_' ) : kwargs . pop ( k , None ) if not isinstance ( fields , ( list , tuple ) ) : raise ValueError ( "Index value format is not right, the value is %r" % indexes ) props = [ ] for y in fields : props . append ( cls . c [ y ] ) Index ( name , * props , * * kwargs ) return cls
Create model dynamically
502
3
238,869
def reflect_table_model ( table , mapping = None , without_id = False , engine_name = 'default' ) : table = reflect_table ( table , engine_name ) mapping = mapping or { } meta = reflect_table_data ( table ) code = [ 'class {}(Model):' . format ( table . name . title ( ) ) ] code . append ( ''' """ Description: """ __tablename__ = '{}\'''' . format ( table . name ) ) if sa_version >= '1.2' and table . comment : code . append ( ' __verbose_name__ = {}\n' . format ( dumps ( table . comment , bool_int = False ) ) ) #process id if 'id' not in meta [ 'columns' ] and without_id : code . append ( ' __without_id__ = True\n' ) # if _primary_key: # code.append(' _primary_field = {}'.format(_primary_key)) #output columns text for k , v in meta [ 'columns' ] . items ( ) : kw = v [ 1 ] . items ( ) x_v = mapping . get ( v [ 0 ] ) kwargs = ', ' . join ( [ v [ 0 ] ] + [ '{0}={1}' . format ( x , dumps ( y , bool_int = False ) ) for x , y in kw ] ) if x_v : type_class = ' ,type_class={}' . format ( x_v ) else : type_class = '' txt = " " * 4 + "{0} = Field({1}{2})" . format ( k , kwargs , type_class ) code . append ( txt ) #output index text if meta [ 'indexes' ] : code . append ( """ @classmethod def OnInit(cls):""" ) for index in meta [ 'indexes' ] : buf = [ ] buf . append ( index [ 'name' ] ) for c in index [ 'columns' ] : buf . append ( 'cls.c.{}' . format ( c ) ) if index [ 'unique' ] : buf . append ( 'unique=True' ) code . append ( ' ' * 8 + 'Index({})' . format ( ', ' . join ( buf ) ) ) return '\n' . join ( code )
Write table to Model class
521
5
238,870
def SelfReferenceProperty ( label = None , collection_name = None , * * attrs ) : if 'reference_class' in attrs : raise ConfigurationError ( 'Do not provide reference_class to self-reference.' ) return ReferenceProperty ( _SELF_REFERENCE , label , collection_name , * * attrs )
Create a self reference .
71
5
238,871
def session ( self , create = True ) : if hasattr ( self . local , 'session' ) : return self . local . session else : if create : s = Session ( self . name ) self . local . session = s return s
Used to created default session
51
5
238,872
def get_parameters ( self ) : d = { } for k in [ 'label' , 'verbose_name' , 'required' , 'hint' , 'placeholder' , 'choices' , 'default' , 'validators' , 'max_length' ] : d [ k ] = getattr ( self , k ) return d
Get common attributes and it ll used for Model . relationship clone process
77
13
238,873
def validate ( self , value ) : if value == '' : if self . kwargs . get ( 'nullable' , __nullable__ ) : value = None else : value = 0 if not isinstance ( value , Model ) : return super ( ReferenceProperty , self ) . validate ( value ) if not value . is_saved ( ) : raise BadValueError ( '%s instance must be saved before it can be stored as a ' 'reference' % self . reference_class . __class__ . __name__ ) if not isinstance ( value , self . reference_class ) : raise KindError ( 'Property %s must be an instance of %s' % ( self . name , self . reference_class . __class__ . __name__ ) ) return value
Validate reference .
166
4
238,874
def get_fields ( self ) : columns = self . columns model = self . model fields = [ ] for col in columns : if isinstance ( col , ( str , unicode ) ) : v = col . split ( '.' ) if len ( v ) > 1 : field = get_model ( v [ 0 ] , engine_name = self . model . get_engine_name ( ) , signal = False ) . properties ( v [ 1 ] ) else : field = model . properties [ col ] elif isinstance ( col , Column ) : field = get_model ( col . table . name , engine_name = self . model . get_engine_name ( ) , signal = False ) . properties [ col . name ] else : field = col fields . append ( field ) return fields
get property instance according self . columns
169
7
238,875
def count ( self ) : if self . _group_by or self . _join or self . distinct_field : return self . do_ ( self . get_query ( ) . limit ( None ) . order_by ( None ) . offset ( None ) . alias ( ) . count ( ) ) . scalar ( ) else : return self . do_ ( self . get_query ( ) . with_only_columns ( [ func . count ( ) ] ) . limit ( None ) . order_by ( None ) . offset ( None ) ) . scalar ( )
If result is True then the count will process result set if result if False then only use condition to count
123
21
238,876
def update ( self , * * kwargs ) : if self . condition is not None : self . result = self . do_ ( self . model . table . update ( ) . where ( self . condition ) . values ( * * kwargs ) ) else : self . result = self . do_ ( self . model . table . update ( ) . values ( * * kwargs ) ) return self . result
Execute update table set field = field + 1 like statement
89
12
238,877
def save_file ( self , filename , encoding = 'utf8' , headers = None , convertors = None , display = True , * * kwargs ) : global save_file convertors = convertors or { } headers = headers or [ ] fields = self . get_fields ( ) _header = [ ] for i , column in enumerate ( fields ) : if column . name not in convertors : if display : def f ( value , data ) : return column . get_display_value ( value ) convertors [ column . name ] = f flag = False for j in headers : if not isinstance ( j , dict ) : raise ValueError ( "Header should be a list of dict, but {} type found" . format ( type ( j ) ) ) if j [ 'name' ] == column . name : _header . append ( j ) flag = True break if not flag : d = { 'name' : column . name } if display : d [ 'title' ] = column . verbose_name or column . name else : d [ 'title' ] = column . name _header . append ( d ) return save_file ( self . run ( ) , filename , encoding = encoding , headers = _header , convertors = convertors , * * kwargs )
save result to a csv file . display = True will convert value according choices value
275
17
238,878
def all ( self , cache = False ) : if cache : return [ get_object ( self . modelb , obj_id , cache = True , use_local = True ) for obj_id in self . keys ( True ) ] else : return self
can use cache to return objects
54
6
238,879
def update ( self , * objs ) : keys = self . keys ( ) new_keys = get_objs_columns ( objs , self . realfieldb ) modified = False for v in new_keys : if v in keys : #the id has been existed, so don't insert new record keys . remove ( v ) else : d = { self . fielda : self . valuea , self . fieldb : v } if self . before_save : self . before_save ( d ) if self . through_model : obj = self . through_model ( * * d ) obj . save ( ) else : self . do_ ( self . table . insert ( ) . values ( * * d ) ) modified = True if keys : #if there are still keys, so delete them self . clear ( * keys ) modified = True #cache [] to _STORED_attr_name setattr ( self . instance , self . store_key , new_keys ) return modified
Update the third relationship table but not the ModelA or ModelB
211
13
238,880
def with_relation ( self , relation_name = None ) : if not relation_name : relation_name = 'relation' if hasattr ( self . modelb , relation_name ) : raise Error ( "The attribute name %s has already existed in Model %s!" % ( relation_name , self . modelb . __name__ ) ) if not self . through_model : raise Error ( "Only with through style in ManyToMany supports with_relation function of Model %s!" % self . modelb . __name__ ) self . with_relation_name = relation_name return self
if relation is not None when fetch manytomany result also fetch relation record and saved them to manytomany object and named them as relation . If relation_name is not given then default value is relation
127
41
238,881
def in_ ( self , * objs ) : if not objs : return self . table . c [ self . fielda ] != self . table . c [ self . fielda ] else : keys = get_objs_columns ( objs , self . reference_fieldname ) sub_query = select ( [ self . table . c [ self . fielda ] ] , ( self . table . c [ self . fieldb ] == self . reference_class . c [ self . reference_fieldname ] ) & ( self . table . c [ self . fieldb ] . in_ ( keys ) ) ) condition = self . model_class . c [ self . reversed_fieldname ] . in_ ( sub_query ) return condition
Create a condition
159
3
238,882
def join_in ( self , * objs ) : if not objs : return self . table . c [ self . fielda ] != self . table . c [ self . fielda ] else : keys = get_objs_columns ( objs , self . reference_fieldname ) return ( self . table . c [ self . fielda ] == self . model_class . c [ self . reversed_fieldname ] ) & ( self . table . c [ self . fieldb ] . in_ ( keys ) )
Create a join condition connect A and C
113
8
238,883
def join_right_in ( self , * objs ) : if not objs : return self . table . c [ self . fielda ] != self . table . c [ self . fielda ] else : keys = get_objs_columns ( objs , self . reference_fieldname ) return ( self . table . c [ self . fieldb ] == self . reference_class . c [ self . reference_fieldname ] ) & ( self . table . c [ self . fielda ] . in_ ( keys ) )
Create a join condition connect B and C
115
8
238,884
def _get_data ( self , fields = None , compare = True ) : fields = fields or [ ] if self . _key is None or self . _key == '' or self . _key == 0 : d = { } for k , v in self . properties . items ( ) : #test fields if fields and k not in fields : continue # if not isinstance(v, ManyToMany): if v . property_type == 'compound' : continue if v . sequence : continue if not isinstance ( v , ManyToMany ) : x = v . get_value_for_datastore ( self ) if isinstance ( x , Model ) : x = x . _key elif x is None or ( k == self . _primary_field and not x ) : if isinstance ( v , DateTimeProperty ) and v . auto_now_add : x = v . now ( ) elif ( v . auto_add or ( not v . auto and not v . auto_add ) ) : x = v . default_value ( ) else : x = v . get_value_for_datastore ( self , cached = True ) if x is not None and not x is Lazy : d [ k ] = x else : d = { } d [ self . _primary_field ] = self . _key for k , v in self . properties . items ( ) : if fields and k not in fields : continue if v . property_type == 'compound' : continue t = self . _old_values . get ( k , None ) if not isinstance ( v , ManyToMany ) : x = v . get_value_for_datastore ( self ) if isinstance ( x , Model ) : x = x . _key else : x = v . get_value_for_datastore ( self , cached = True ) if not x is Lazy : if ( compare and t != self . field_str ( x ) ) or not compare : d [ k ] = x return d
Get the changed property it ll be used to save the object If compare is False then it ll include all data not only changed property
436
26
238,885
def create_sql ( self , insert = False , version = False , version_fieldname = None , fields = None , ec = None , compare = False ) : version_fieldname = version_fieldname or 'version' #fix when d is empty, orm will not insert record bug 2013/04/07 if not self . _key or insert : d = self . _get_data ( fields , compare = compare ) if d : return rawsql ( self . table . insert ( ) . values ( * * d ) , ec or self . get_engine_name ( ) ) + ';' else : d = self . _get_data ( fields , compare = compare ) _key = d . pop ( self . _primary_field ) if d : _cond = self . table . c [ self . _primary_field ] == self . _key if version : version_field = self . table . c . get ( version_fieldname ) if version_field is None : raise KindError ( "version_fieldname %s is not existed in Model %s" % ( version_fieldname , self . __class__ . __name__ ) ) _version_value = getattr ( self , version_fieldname , 0 ) # setattr(self, version_fieldname, _version_value+1) d [ version_fieldname ] = _version_value + 1 _cond = ( version_field == _version_value ) & _cond return rawsql ( self . table . update ( _cond ) . values ( * * d ) , ec or self . get_engine_name ( ) ) + ';' return ''
Create sql statement do not process manytomany
352
9
238,886
def get_collection_name ( cls , from_class_name , collection_name = None , prefix = None ) : if not collection_name : collection_name = prefix + '_set' if hasattr ( cls , collection_name ) : #if the xxx_set is already existed, then automatically #create unique collection_set id collection_name = prefix + '_set_' + str ( cls . _collection_set_id ) cls . _collection_set_id += 1 else : if collection_name in cls . _collection_names : if cls . _collection_names . get ( collection_name ) != from_class_name : raise DuplicatePropertyError ( "Model %s already has collection property %s" % ( cls . __name__ , collection_name ) ) #add property check if collection_name in cls . properties : raise DuplicatePropertyError ( "Model %s already has property %s" % ( cls . __name__ , collection_name ) ) return collection_name
Get reference collection_name if the collection_name is None then make sure the collection_name is not conflict but if the collection_name is not None then check if the collection_name is already exists if existed then raise Exception .
226
47
238,887
def _use ( cls , ec ) : # class ConnectModel(cls): # pass ConnectModel = type ( cls . __name__ , ( cls , ) , { } ) ConnectModel . tablename = cls . tablename ConnectModel . _base_class = cls if isinstance ( ec , ( str , unicode ) ) : ConnectModel . _engine_name = ec elif isinstance ( ec , Session ) : ConnectModel . _engine_name = ec . engine_name ConnectModel . _connection = ec return ConnectModel
underly implement of use
120
5
238,888
def use ( cls , ec ) : if isinstance ( ec , ( str , unicode ) ) : m = get_model ( cls . _alias , ec , signal = False ) else : m = cls . _use ( ec ) return m
use will duplicate a new Model class and bind ec ec is Engine name or Sesstion object
55
20
238,889
def get_tree ( cls , * condition , * * kwargs ) : parent_field = kwargs . pop ( 'parent_field' , 'parent' ) parent = kwargs . pop ( 'parent' , None ) parent_order_by = kwargs . pop ( 'parent_order_by' , None ) current = kwargs . pop ( 'current' , None ) order_by = kwargs . pop ( 'order_by' , None ) id_field = kwargs . pop ( 'id_field' , 'id' ) mode = kwargs . pop ( 'mode' , 'wide' ) if mode not in ( 'wide' , 'deep' ) : raise Exception ( "mode parameter should be 'wide' or 'deep', but '{}' found." . format ( mode ) ) def _f ( parent ) : query = cls . filter ( cls . c [ parent_field ] == parent , * condition ) if order_by is not None : query . order_by ( order_by ) for row in query : if mode == 'wide' : yield row for _row in _f ( getattr ( row , id_field ) ) : yield _row if mode == 'deep' : yield row if current : query = cls . filter ( cls . c [ id_field ] == current ) else : if is_condition ( parent ) : query = cls . filter ( parent ) else : query = cls . filter ( cls . c [ parent_field ] == parent ) if parent_order_by is not None : query . order_by ( parent_order_by ) for row in query : if mode == 'wide' : yield row for r in _f ( getattr ( row , id_field ) ) : yield r if mode == 'deep' : yield row
parent is root parent value default is None current is current value condition is extra condition for select root records mode is search method value is wide or deep
401
29
238,890
def refresh ( self , fields = None , * * kwargs ) : cond = self . c [ self . _primary_field ] == self . _key query = self . filter ( cond , * * kwargs ) if not fields : fields = list ( self . table . c ) v = query . values_one ( * fields ) if not v : raise NotFound ( 'Instance <{0}:{1}> can not be found' . format ( self . tablename , self . _key ) ) d = self . _data_prepare ( v . items ( ) ) self . update ( * * d ) self . set_saved ( )
Re get the instance of current id
143
7
238,891
def dump ( self , fields = None , exclude = None ) : exclude = exclude or [ ] d = { } if fields and self . _primary_field not in fields : fields = list ( fields ) fields . append ( self . _primary_field ) for k , v in self . properties . items ( ) : if ( ( not fields ) or ( k in fields ) ) and ( not exclude or ( k not in exclude ) ) : if not isinstance ( v , ManyToMany ) : t = v . get_value_for_datastore ( self ) if t is Lazy : self . refresh ( ) t = v . get_value_for_datastore ( self ) if isinstance ( t , Model ) : t = t . _key d [ k ] = v . to_str ( t ) else : if fields : d [ k ] = ',' . join ( [ str ( x ) for x in getattr ( self , v . _lazy_value ( ) , [ ] ) ] ) if self . _primary_field and d and self . _primary_field not in d : d [ self . _primary_field ] = str ( self . _key ) return d
Dump current object to dict but the value is string for manytomany fields will not automatically be dumpped only when they are given in fields parameter
258
31
238,892
def clear_relation ( cls ) : for k , v in cls . properties . items ( ) : if isinstance ( v , ReferenceProperty ) : if hasattr ( v , 'collection_name' ) and hasattr ( v . reference_class , v . collection_name ) : delattr ( v . reference_class , v . collection_name ) if isinstance ( v , OneToOne ) : #append to reference_class._onetoone del v . reference_class . _onetoone [ v . collection_name ]
Clear relation properties for reference Model such as OneToOne Reference ManyToMany
116
15
238,893
def put ( self , _name , * * values ) : try : sql = self . sqles [ _name ] data = sql [ 'data' ] if sql [ 'positional' ] : d = [ values [ k ] for k , v in sql [ 'fields' ] . items ( ) ] else : d = { v : values [ k ] for k , v in sql [ 'fields' ] . items ( ) } data . append ( d ) if self . size and len ( data ) >= self . size : do_ ( sql [ 'raw_sql' ] , args = data ) sql [ 'data' ] = [ ] except : if self . transcation : Rollback ( self . engine ) raise
Put data to cach if reached size value it ll execute at once .
153
15
238,894
def get_key ( keyfile = None ) : keyfile = keyfile or application_path ( settings . SECRETKEY . SECRET_FILE ) with file ( keyfile , 'rb' ) as f : return f . read ( )
Read the key content from secret_file
51
8
238,895
def get_cipher_key ( keyfile = None ) : _key = get_key ( keyfile ) _k = md5 ( _key ) . hexdigest ( ) key = xor ( _k [ : 8 ] , _k [ 8 : 16 ] , _k [ 16 : 24 ] , _k [ 24 : ] ) return key
Create key which will be used in des because des need 8bytes chars
76
14
238,896
def set_many ( self , mapping , timeout = None ) : for key , value in _items ( mapping ) : self . set ( key , value , timeout )
Sets multiple keys and values from a mapping .
35
10
238,897
def dec ( self , key , delta = 1 ) : self . set ( key , ( self . get ( key ) or 0 ) - delta )
Decrements the value of a key by delta . If the key does not yet exist it is initialized with - delta .
31
24
238,898
def import_preferred_memcache_lib ( self , servers ) : try : import pylibmc except ImportError : pass else : return pylibmc . Client ( servers ) try : from google . appengine . api import memcache except ImportError : pass else : return memcache . Client ( ) try : import memcache except ImportError : pass else : return memcache . Client ( servers )
Returns an initialized memcache client . Used by the constructor .
86
12
238,899
def dump_object ( self , value ) : t = type ( value ) if t is int or t is long : return str ( value ) return '!' + pickle . dumps ( value )
Dumps an object into a string for redis . By default it serializes integers as regular string and pickle dumps everything else .
41
27