signature:
def embed_data ( request ) :
    """Create a temporary directory with input data for the test .
 The directory contents is copied from a directory with the same name as the module located in the same directory of
 the test module ."""

implementation:
result = _EmbedDataFixture ( request )
result . delete_data_dir ( )
result . create_data_dir ( )
yield result
result . delete_data_dir ( )


signature:
def get_filename ( self , * parts ) :
    '''Returns an absolute filename in the data - directory ( standardized by StandardizePath ) .
 @ params parts : list ( unicode )
 Path parts . Each part is joined to form a path .
 : rtype : unicode
 : returns :
 The full path prefixed with the data - directory .
 @ remarks :
 This method triggers the data - directory creation .'''

implementation:
from zerotk . easyfs import StandardizePath
result = [ self . _data_dir ] + list ( parts )
result = '/' . join ( result )
return StandardizePath ( result )


signature:
def assert_equal_files ( self , obtained_fn , expected_fn , fix_callback = lambda x : x , binary = False , encoding = None ) :
    '''Compare two files contents . If the files differ , show the diff and write a nice HTML
 diff file into the data directory .
 Searches for the filenames both inside and outside the data directory ( in that order ) .
 : param unicode obtained _ fn : basename to obtained file into the data directory , or full path .
 : param unicode expected _ fn : basename to expected file into the data directory , or full path .
 : param bool binary :
 Thread both files as binary files .
 : param unicode encoding :
 File ' s encoding . If not None , contents obtained from file will be decoded using this
 ` encoding ` .
 : param callable fix _ callback :
 A callback to " fix " the contents of the obtained ( first ) file .
 This callback receives a list of strings ( lines ) and must also return a list of lines ,
 changed as needed .
 The resulting lines will be used to compare with the contents of expected _ fn .
 : param bool binary :
 . . seealso : : zerotk . easyfs . GetFileContents'''

implementation:
import os
from zerotk . easyfs import GetFileContents , GetFileLines
__tracebackhide__ = True
import io
def FindFile ( filename ) : # See if this path exists in the data dir
    data_filename = self . get_filename ( filename )
    if os . path . isfile ( data_filename ) :
        return data_filename
    # If not , we might have already received a full path
    if os . path . isfile ( filename ) :
        return filename
    # If we didn ' t find anything , raise an error
    from . _exceptions import MultipleFilesNotFound
    raise MultipleFilesNotFound ( [ filename , data_filename ] )
obtained_fn = FindFile ( obtained_fn )
expected_fn = FindFile ( expected_fn )
if binary :
    obtained_lines = GetFileContents ( obtained_fn , binary = True )
    expected_lines = GetFileContents ( expected_fn , binary = True )
    assert obtained_lines == expected_lines
else :
    obtained_lines = fix_callback ( GetFileLines ( obtained_fn , encoding = encoding ) )
    expected_lines = GetFileLines ( expected_fn , encoding = encoding )
    if obtained_lines != expected_lines :
        html_fn = os . path . splitext ( obtained_fn ) [ 0 ] + '.diff.html'
        html_diff = self . _generate_html_diff ( expected_fn , expected_lines , obtained_fn , obtained_lines )
        with io . open ( html_fn , 'w' ) as f :
            f . write ( html_diff )
        import difflib
        diff = [ 'FILES DIFFER:' , obtained_fn , expected_fn ]
        diff += [ 'HTML DIFF: %s' % html_fn ]
        diff += difflib . context_diff ( obtained_lines , expected_lines )
        raise AssertionError ( '\n' . join ( diff ) + '\n' )


signature:
def _generate_html_diff ( self , expected_fn , expected_lines , obtained_fn , obtained_lines ) :
    """Returns a nice side - by - side diff of the given files , as a string ."""

implementation:
import difflib
differ = difflib . HtmlDiff ( )
return differ . make_file ( fromlines = expected_lines , fromdesc = expected_fn , tolines = obtained_lines , todesc = obtained_fn , )


signature:
def add_peer ( self , peer ) :
    """Add a peer or multiple peers to the PEERS variable , takes a single string or a list .
 : param peer ( list or string )"""

implementation:
if type ( peer ) == list :
    for i in peer :
        check_url ( i )
    self . PEERS . extend ( peer )
elif type ( peer ) == str :
    check_url ( peer )
    self . PEERS . append ( peer )


signature:
def remove_peer ( self , peer ) :
    """remove one or multiple peers from PEERS variable
 : param peer ( list or string ) :"""

implementation:
if type ( peer ) == list :
    for x in peer :
        check_url ( x )
        for i in self . PEERS :
            if x in i :
                self . PEERS . remove ( i )
elif type ( peer ) == str :
    check_url ( peer )
    for i in self . PEERS :
        if peer == i :
            self . PEERS . remove ( i )
else :
    raise ValueError ( 'peer paramater did not pass url validation' )


signature:
def status ( self ) :
    """check the status of the network and the peers
 : return : network _ height , peer _ status"""

implementation:
peer = random . choice ( self . PEERS )
formatted_peer = 'http://{}:4001' . format ( peer )
peerdata = requests . get ( url = formatted_peer + '/api/peers/' ) . json ( ) [ 'peers' ]
peers_status = { }
networkheight = max ( [ x [ 'height' ] for x in peerdata ] )
for i in peerdata :
    if 'http://{}:4001' . format ( i [ 'ip' ] ) in self . PEERS :
        peers_status . update ( { i [ 'ip' ] : { 'height' : i [ 'height' ] , 'status' : i [ 'status' ] , 'version' : i [ 'version' ] , 'delay' : i [ 'delay' ] , } } )
return { 'network_height' : networkheight , 'peer_status' : peers_status }


signature:
def broadcast_tx ( self , address , amount , secret , secondsecret = None , vendorfield = '' ) :
    """broadcasts a transaction to the peerslist using ark - js library"""

implementation:
peer = random . choice ( self . PEERS )
park = Park ( peer , 4001 , constants . ARK_NETHASH , '1.1.1' )
return park . transactions ( ) . create ( address , str ( amount ) , vendorfield , secret , secondsecret )


signature:
def register ( self , service , name = '' ) :
    """Exposes a given service to this API ."""

implementation:
try :
    is_model = issubclass ( service , orb . Model )
except StandardError :
    is_model = False
# expose an ORB table dynamically as a service
if is_model :
    self . services [ service . schema ( ) . dbname ( ) ] = ( ModelService , service )
else :
    super ( OrbApiFactory , self ) . register ( service , name = name )


signature:
def main ( ) :
    """Register your own mode and handle method here ."""

implementation:
plugin = Register ( )
if plugin . args . option == 'sql' :
    plugin . sql_handle ( )
else :
    plugin . unknown ( "Unknown actions." )


signature:
def prepare ( self , input_files , bundle ) :
    """: type input _ files : list [ static _ bundle . files . StaticFileResult ]
 : type bundle : static _ bundle . bundles . AbstractBundle
 : rtype : list"""

implementation:
out = [ ]
for input_file in input_files :
    if input_file . extension == "less" and os . path . isfile ( input_file . abs_path ) :
        output_file = self . get_compile_file ( input_file , bundle )
        self . compile ( input_file , output_file )
        out . append ( output_file )
    else :
        out . append ( input_file )
return out


signature:
def main ( ) :
    """Main entry point , expects doctopt arg dict as argd ."""

implementation:
global DEBUG
argd = docopt ( USAGESTR , version = VERSIONSTR , script = SCRIPT )
DEBUG = argd [ '--debug' ]
width = parse_int ( argd [ '--width' ] or DEFAULT_WIDTH ) or 1
indent = parse_int ( argd [ '--indent' ] or ( argd [ '--INDENT' ] or 0 ) )
prepend = ' ' * ( indent * 4 )
if prepend and argd [ '--indent' ] : # Smart indent , change max width based on indention .
    width -= len ( prepend )
userprepend = argd [ '--prepend' ] or ( argd [ '--PREPEND' ] or '' )
prepend = '' . join ( ( prepend , userprepend ) )
if argd [ '--prepend' ] : # Smart indent , change max width based on prepended text .
    width -= len ( userprepend )
userappend = argd [ '--append' ] or ( argd [ '--APPEND' ] or '' )
if argd [ '--append' ] :
    width -= len ( userappend )
if argd [ 'WORDS' ] : # Try each argument as a file name .
    argd [ 'WORDS' ] = ( ( try_read_file ( w ) if len ( w ) < 256 else w ) for w in argd [ 'WORDS' ] )
    words = ' ' . join ( ( w for w in argd [ 'WORDS' ] if w ) )
else : # No text / filenames provided , use stdin for input .
    words = read_stdin ( )
block = FormatBlock ( words ) . iter_format_block ( chars = argd [ '--chars' ] , fill = argd [ '--fill' ] , prepend = prepend , strip_first = argd [ '--stripfirst' ] , append = userappend , strip_last = argd [ '--striplast' ] , width = width , newlines = argd [ '--newlines' ] , lstrip = argd [ '--lstrip' ] , )
for i , line in enumerate ( block ) :
    if argd [ '--enumerate' ] : # Current line number format supports up to 999 lines before
    # messing up . Who would format 1000 lines like this anyway ?
        print ( '{: >3}: {}' . format ( i + 1 , line ) )
    else :
        print ( line )
return 0


signature:
def debug ( * args , ** kwargs ) :
    """Print a message only if DEBUG is truthy ."""

implementation:
if not ( DEBUG and args ) :
    return None
# Include parent class name when given .
parent = kwargs . get ( 'parent' , None )
with suppress ( KeyError ) :
    kwargs . pop ( 'parent' )
# Go back more than once when given .
backlevel = kwargs . get ( 'back' , 1 )
with suppress ( KeyError ) :
    kwargs . pop ( 'back' )
frame = inspect . currentframe ( )
# Go back a number of frames ( usually 1 ) .
while backlevel > 0 :
    frame = frame . f_back
    backlevel -= 1
fname = os . path . split ( frame . f_code . co_filename ) [ - 1 ]
lineno = frame . f_lineno
if parent :
    func = '{}.{}' . format ( parent . __class__ . __name__ , frame . f_code . co_name )
else :
    func = frame . f_code . co_name
lineinfo = '{}:{} {}: ' . format ( C ( fname , 'yellow' ) , C ( str ( lineno ) . ljust ( 4 ) , 'blue' ) , C ( ) . join ( C ( func , 'magenta' ) , '()' ) . ljust ( 20 ) )
# Patch args to stay compatible with print ( ) .
pargs = list ( C ( a , 'green' ) . str ( ) for a in args )
pargs [ 0 ] = '' . join ( ( lineinfo , pargs [ 0 ] ) )
print_err ( * pargs , ** kwargs )


signature:
def parse_int ( s ) :
    """Parse a string as an integer .
 Exit with a message on failure ."""

implementation:
try :
    val = int ( s )
except ValueError :
    print_err ( '\nInvalid integer: {}' . format ( s ) )
    sys . exit ( 1 )
return val


signature:
def try_read_file ( s ) :
    """If ` s ` is a file name , read the file and return it ' s content .
 Otherwise , return the original string .
 Returns None if the file was opened , but errored during reading ."""

implementation:
try :
    with open ( s , 'r' ) as f :
        data = f . read ( )
except FileNotFoundError : # Not a file name .
    return s
except EnvironmentError as ex :
    print_err ( '\nFailed to read file: {}\n  {}' . format ( s , ex ) )
    return None
return data


signature:
def close ( self ) :
    """Disconnect and close * Vim * ."""

implementation:
self . _tempfile . close ( )
self . _process . terminate ( )
if self . _process . is_alive ( ) :
    self . _process . kill ( )


signature:
def send_keys ( self , keys , wait = True ) :
    """Send a raw key sequence to * Vim * .
 . . note : : * Vim * style key sequence notation ( like ` ` < Esc > ` ` )
 is not recognized .
 Use escaped characters ( like ` ` ' \033 ' ` ` ) instead .
 Example :
 > > > import headlessvim
 > > > with headlessvim . open ( ) as vim :
 . . . vim . send _ keys ( ' ispam \033 ' )
 . . . str ( vim . display _ lines ( ) [ 0 ] . strip ( ) )
 ' spam '
 : param strgin keys : key sequence to send
 : param boolean wait : whether if wait a response"""

implementation:
self . _process . stdin . write ( bytearray ( keys , self . _encoding ) )
self . _process . stdin . flush ( )
if wait :
    self . wait ( )


signature:
def wait ( self , timeout = None ) :
    """Wait for response until timeout .
 If timeout is specified to None , ` ` self . timeout ` ` is used .
 : param float timeout : seconds to wait I / O"""

implementation:
if timeout is None :
    timeout = self . _timeout
while self . _process . check_readable ( timeout ) :
    self . _flush ( )


signature:
def install_plugin ( self , dir , entry_script = None ) :
    """Install * Vim * plugin .
 : param string dir : the root directory contains * Vim * script
 : param string entry _ script : path to the initializing script"""

implementation:
self . runtimepath . append ( dir )
if entry_script is not None :
    self . command ( 'runtime! {0}' . format ( entry_script ) , False )


signature:
def command ( self , command , capture = True ) :
    """Execute command on * Vim * .
 . . warning : : Do not use ` ` redir ` ` command if ` ` capture ` ` is ` ` True ` ` .
 It ' s already enabled for internal use .
 If ` ` capture ` ` argument is set ` ` False ` ` ,
 the command execution becomes slightly faster .
 Example :
 > > > import headlessvim
 > > > with headlessvim . open ( ) as vim :
 . . . vim . command ( ' echo 0 ' )
 > > > with headlessvim . open ( ) as vim :
 . . . vim . command ( ' let g : spam = " ham " ' , False )
 . . . vim . echo ( ' g : spam ' )
 ' ham '
 : param string command : a command to execute
 : param boolean capture : ` ` True ` ` if command ' s output needs to be
 captured , else ` ` False ` `
 : return : the output of the given command
 : rtype : string"""

implementation:
if capture :
    self . command ( 'redir! >> {0}' . format ( self . _tempfile . name ) , False )
self . set_mode ( 'command' )
self . send_keys ( '{0}\n' . format ( command ) )
if capture :
    self . command ( 'redir END' , False )
    return self . _tempfile . read ( ) . strip ( '\n' )


signature:
def set_mode ( self , mode ) :
    """Set * Vim * mode to ` ` mode ` ` .
 Supported modes :
 * ` ` normal ` `
 * ` ` insert ` `
 * ` ` command ` `
 * ` ` visual ` `
 * ` ` visual - block ` `
 This method behave as setter - only property .
 Example :
 > > > import headlessvim
 > > > with headlessvim . open ( ) as vim :
 . . . vim . set _ mode ( ' insert ' )
 . . . vim . mode = ' normal ' # also accessible as property
 : param string mode : * Vim * mode to set
 : raises ValueError : if ` ` mode ` ` is not supported"""

implementation:
keys = '\033\033'
if mode == 'normal' :
    pass
elif mode == 'insert' :
    keys += 'i'
elif mode == 'command' :
    keys += ':'
elif mode == 'visual' :
    keys += 'v'
elif mode == 'visual-block' :
    keys += 'V'
else :
    raise ValueError ( 'mode {0} is not supported' . format ( mode ) )
self . send_keys ( keys )


signature:
def screen_size ( self , size ) :
    """: param size : ( lines , columns ) tuple of a screen connected to * Vim * .
 : type size : ( int , int )"""

implementation:
if self . screen_size != size :
    self . _screen . resize ( * self . _swap ( size ) )


signature:
def runtimepath ( self ) :
    """: return : runtime path of * Vim *
 : rtype : runtimepath . RuntimePath"""

implementation:
if self . _runtimepath is None :
    self . _runtimepath = runtimepath . RuntimePath ( self )
return self . _runtimepath


signature:
def make_seekable ( fileobj ) :
    """If the file - object is not seekable , return ArchiveTemp of the fileobject ,
 otherwise return the file - object itself"""

implementation:
if sys . version_info < ( 3 , 0 ) and isinstance ( fileobj , file ) :
    filename = fileobj . name
    fileobj = io . FileIO ( fileobj . fileno ( ) , closefd = False )
    fileobj . name = filename
assert isinstance ( fileobj , io . IOBase ) , "fileobj must be an instance of io.IOBase or a file, got %s" % type ( fileobj )
return fileobj if fileobj . seekable ( ) else ArchiveTemp ( fileobj )


signature:
def init_app ( self , app ) :
    """Setup before _ request , after _ request handlers for tracing ."""

implementation:
app . config . setdefault ( "TRACY_REQUIRE_CLIENT" , False )
if not hasattr ( app , 'extensions' ) :
    app . extensions = { }
app . extensions [ 'restpoints' ] = self
app . before_request ( self . _before )
app . after_request ( self . _after )


signature:
def _before ( self ) :
    """Records the starting time of this reqeust ."""

implementation:
# Don ' t trace excluded routes .
if request . path in self . excluded_routes :
    request . _tracy_exclude = True
    return
request . _tracy_start_time = monotonic ( )
client = request . headers . get ( trace_header_client , None )
require_client = current_app . config . get ( "TRACY_REQUIRE_CLIENT" , False )
if client is None and require_client :
    abort ( 400 , "Missing %s header" % trace_header_client )
request . _tracy_client = client
request . _tracy_id = request . headers . get ( trace_header_id , new_id ( ) )


signature:
def _after ( self , response ) :
    """Calculates the request duration , and adds a transaction
 ID to the header ."""

implementation:
# Ignore excluded routes .
if getattr ( request , '_tracy_exclude' , False ) :
    return response
duration = None
if getattr ( request , '_tracy_start_time' , None ) :
    duration = monotonic ( ) - request . _tracy_start_time
# Add Trace _ ID header .
trace_id = None
if getattr ( request , '_tracy_id' , None ) :
    trace_id = request . _tracy_id
    response . headers [ trace_header_id ] = trace_id
# Get the invoking client .
trace_client = None
if getattr ( request , '_tracy_client' , None ) :
    trace_client = request . _tracy_client
# Extra log kwargs .
d = { 'status_code' : response . status_code , 'url' : request . base_url , 'client_ip' : request . remote_addr , 'trace_name' : trace_client , 'trace_id' : trace_id , 'trace_duration' : duration }
logger . info ( None , extra = d )
return response


signature:
def close ( self ) :
    """Close the http / https connect ."""

implementation:
try :
    self . response . close ( )
    self . logger . debug ( "close connect succeed." )
except Exception as e :
    self . unknown ( "close connect error: %s" % e )


signature:
def _stat ( self , path ) :
    '''IMPORTANT : expects ` path ` ' s parent to already be deref ( ) ' erenced .'''

implementation:
if path not in self . entries :
    return OverlayStat ( * self . originals [ 'os:stat' ] ( path ) [ : 10 ] , st_overlay = 0 )
st = self . entries [ path ] . stat
if stat . S_ISLNK ( st . st_mode ) :
    return self . _stat ( self . deref ( path ) )
return st


signature:
def _lstat ( self , path ) :
    '''IMPORTANT : expects ` path ` ' s parent to already be deref ( ) ' erenced .'''

implementation:
if path not in self . entries :
    return OverlayStat ( * self . originals [ 'os:lstat' ] ( path ) [ : 10 ] , st_overlay = 0 )
return self . entries [ path ] . stat


signature:
def _exists ( self , path ) :
    '''IMPORTANT : expects ` path ` to already be deref ( ) ' erenced .'''

implementation:
try :
    return bool ( self . _stat ( path ) )
except os . error :
    return False


signature:
def _lexists ( self , path ) :
    '''IMPORTANT : expects ` path ` to already be deref ( ) ' erenced .'''

implementation:
try :
    return bool ( self . _lstat ( path ) )
except os . error :
    return False


signature:
def fso_exists ( self , path ) :
    'overlays os . path . exists ( )'

implementation:
try :
    return self . _exists ( self . deref ( path ) )
except os . error :
    return False


signature:
def fso_lexists ( self , path ) :
    'overlays os . path . lexists ( )'

implementation:
try :
    return self . _lexists ( self . deref ( path , to_parent = True ) )
except os . error :
    return False


signature:
def fso_listdir ( self , path ) :
    'overlays os . listdir ( )'

implementation:
path = self . deref ( path )
if not stat . S_ISDIR ( self . _stat ( path ) . st_mode ) :
    raise OSError ( 20 , 'Not a directory' , path )
try :
    ret = self . originals [ 'os:listdir' ] ( path )
except Exception : # assuming that ` path ` was created within this FSO . . .
    ret = [ ]
for entry in self . entries . values ( ) :
    if not entry . path . startswith ( path + '/' ) :
        continue
    subpath = entry . path [ len ( path ) + 1 : ]
    if '/' in subpath :
        continue
    if entry . mode is None :
        if subpath in ret :
            ret . remove ( subpath )
    else :
        if subpath not in ret :
            ret . append ( subpath )
return ret


signature:
def fso_mkdir ( self , path , mode = None ) :
    'overlays os . mkdir ( )'

implementation:
path = self . deref ( path , to_parent = True )
if self . _lexists ( path ) :
    raise OSError ( 17 , 'File exists' , path )
self . _addentry ( OverlayEntry ( self , path , stat . S_IFDIR ) )


signature:
def fso_makedirs ( self , path , mode = None ) :
    'overlays os . makedirs ( )'

implementation:
path = self . abs ( path )
cur = '/'
segments = path . split ( '/' )
for idx , seg in enumerate ( segments ) :
    cur = os . path . join ( cur , seg )
    try :
        st = self . fso_stat ( cur )
    except OSError :
        st = None
    if st is None :
        self . fso_mkdir ( cur )
        continue
    if idx + 1 == len ( segments ) :
        raise OSError ( 17 , 'File exists' , path )
    if not stat . S_ISDIR ( st . st_mode ) :
        raise OSError ( 20 , 'Not a directory' , path )


signature:
def fso_rmdir ( self , path ) :
    'overlays os . rmdir ( )'

implementation:
st = self . fso_lstat ( path )
if not stat . S_ISDIR ( st . st_mode ) :
    raise OSError ( 20 , 'Not a directory' , path )
if len ( self . fso_listdir ( path ) ) > 0 :
    raise OSError ( 39 , 'Directory not empty' , path )
self . _addentry ( OverlayEntry ( self , path , None ) )


signature:
def fso_readlink ( self , path ) :
    'overlays os . readlink ( )'

implementation:
path = self . deref ( path , to_parent = True )
st = self . fso_lstat ( path )
if not stat . S_ISLNK ( st . st_mode ) :
    raise OSError ( 22 , 'Invalid argument' , path )
if st . st_overlay :
    return self . entries [ path ] . content
return self . originals [ 'os:readlink' ] ( path )


signature:
def fso_symlink ( self , source , link_name ) :
    'overlays os . symlink ( )'

implementation:
path = self . deref ( link_name , to_parent = True )
if self . _exists ( path ) :
    raise OSError ( 17 , 'File exists' )
self . _addentry ( OverlayEntry ( self , path , stat . S_IFLNK , source ) )


signature:
def fso_unlink ( self , path ) :
    'overlays os . unlink ( )'

implementation:
path = self . deref ( path , to_parent = True )
if not self . _lexists ( path ) :
    raise OSError ( 2 , 'No such file or directory' , path )
self . _addentry ( OverlayEntry ( self , path , None ) )


signature:
def fso_islink ( self , path ) :
    'overlays os . path . islink ( )'

implementation:
try :
    return stat . S_ISLNK ( self . fso_lstat ( path ) . st_mode )
except OSError :
    return False


signature:
def fso_rmtree ( self , path , ignore_errors = False , onerror = None ) :
    'overlays shutil . rmtree ( )'

implementation:
if ignore_errors :
    def onerror ( * args ) :
        pass
elif onerror is None :
    def onerror ( * args ) :
        raise
try :
    if self . fso_islink ( path ) : # symlinks to directories are forbidden , see shutil bug # 1669
        raise OSError ( 'Cannot call rmtree on a symbolic link' )
except OSError :
    onerror ( os . path . islink , path , sys . exc_info ( ) )
    # can ' t continue even if onerror hook returns
    return
names = [ ]
try :
    names = self . fso_listdir ( path )
except os . error , err :
    onerror ( os . listdir , path , sys . exc_info ( ) )
for name in names :
    fullname = os . path . join ( path , name )
    try :
        mode = self . fso_lstat ( fullname ) . st_mode
    except os . error :
        mode = 0
    if stat . S_ISDIR ( mode ) :
        self . fso_rmtree ( fullname , ignore_errors , onerror )
    else :
        try :
            self . fso_remove ( fullname )
        except OSError as err :
            onerror ( os . remove , fullname , sys . exc_info ( ) )
try :
    self . fso_rmdir ( path )
except os . error :
    onerror ( os . rmdir , path , sys . exc_info ( ) )


signature:
def MWAPIWrapper ( func ) :
    """MWAPIWrapper 控制API请求异常的装饰器
 根据requests库定义的异常来控制请求返回的意外情况"""

implementation:
@ wraps ( func )
def wrapper ( * args , ** kwargs ) :
    self = args [ 0 ]
    try :
        result = func ( * args , ** kwargs )
        return result
    except ConnectionError :
        err_title = '连接错误'
        err_message = '[{name}] 连接错误，网络状况异常' . format ( name = func . __name__ , host = self . host )
    except HTTPError as e :
        err_title = 'HTTP响应错误'
        err_message = '[{name}] 目标服务器"{host}" HTTP响应错误({detail})' . format ( name = func . __name__ , host = self . host , detail = e . message )
    except Timeout :
        err_title = '请求超时'
        err_message = '[{name}] 目标服务器"{host}" 请求超时' . format ( name = func . __name__ , host = self . host )
    except TooManyRedirects :
        err_title = '过多重定向'
        err_message = '[{name}] 目标服务器"{host}" 过多重定向' . format ( name = func . __name__ , host = self . host )
    except ValueError as e :
        if e . message . find ( 'JSON' ) >= 0 :
            err_title = 'API JSON返回值异常'
            err_message = '[{name}] 目标服务器"{host}" API JSON返回值异常' . format ( name = func . __name__ , host = self . host )
        else :
            err_title = '值错误'
            err_message = '[{name}] 存在ValueError：{msg}' . format ( name = func . __name__ , msg = e . message )
            self . log . error ( e , exc_info = True )
    except KeyError as e :
        err_title = '键错误'
        err_message = '[{name}] 存在KeyError，错误键为{key}' . format ( name = func . __name__ , key = e . message )
        self . log . error ( e , exc_info = True )
    except MWAPIException as e :
        err_title = 'Mediawiki API 异常'
        err_message = e . message
    self . log . error ( '%s:%s' , err_title , err_message )
    return { 'success' : False , 'errtitle' : err_title , 'errmsg' : err_message }
return wrapper


signature:
def expand_words ( self , line , width = 60 ) :
    """Insert spaces between words until it is wide enough for ` width ` ."""

implementation:
if not line . strip ( ) :
    return line
# Word index , which word to insert on ( cycles between 1 - > len ( words ) )
wordi = 1
while len ( strip_codes ( line ) ) < width :
    wordendi = self . find_word_end ( line , wordi )
    if wordendi < 0 : # Reached the end ? , try starting at the front again .
        wordi = 1
        wordendi = self . find_word_end ( line , wordi )
    if wordendi < 0 : # There are no spaces to expand , just prepend one .
        line = '' . join ( ( ' ' , line ) )
    else :
        line = ' ' . join ( ( line [ : wordendi ] , line [ wordendi : ] ) )
        wordi += 1
# Don ' t push a single word all the way to the right .
if ' ' not in strip_codes ( line ) . strip ( ) :
    return line . replace ( ' ' , '' )
return line


signature:
def find_word_end ( text , count = 1 ) :
    """This is a helper method for self . expand _ words ( ) .
 Finds the index of word endings ( default is first word ) .
 The last word doesn ' t count .
 If there are no words , or there are no spaces in the word , it
 returns - 1.
 This method ignores escape codes .
 Example :
 s = ' this is a test '
 i = find _ word _ end ( s , count = 1)
 print ( ' - ' . join ( ( s [ : i ] , s [ i : ] ) ) )
 # ' this - is a test '
 i = find _ word _ end ( s , count = 2)
 print ( ' - ' . join ( ( s [ : i ] , s [ i : ] ) ) )
 # ' this is - a test '"""

implementation:
if not text :
    return - 1
elif ' ' not in text :
    return 0
elif not text . strip ( ) :
    return - 1
count = count or 1
found = 0
foundindex = - 1
inword = False
indices = get_indices ( str ( text ) )
sortedindices = sorted ( indices )
for i in sortedindices :
    c = indices [ i ]
    if inword and c . isspace ( ) : # Found space .
        inword = False
        foundindex = i
        found += 1
        # Was there an escape code before this space ?
        testindex = i
        while testindex > 0 :
            testindex -= 1
            s = indices . get ( testindex , None )
            if s is None : # Must be in the middle of an escape code .
                continue
            if len ( s ) == 1 : # Test index was a char .
                foundindex = testindex + 1
                break
        if found == count :
            return foundindex
    elif not c . isspace ( ) :
        inword = True
# We ended in a word / escape - code , or there were no words .
lastindex = sortedindices [ - 1 ]
if len ( indices [ lastindex ] ) > 1 : # Last word included an escape code . Rewind a bit .
    while lastindex > 0 :
        lastindex -= 1
        s = indices . get ( lastindex , None )
        if s is None : # Must be in the middle of an escape code .
            continue
        if len ( s ) == 1 : # Found last char .
            return lastindex + 1
return - 1 if inword else foundindex


signature:
def format ( self , text = None , width = 60 , chars = False , fill = False , newlines = False , prepend = None , append = None , strip_first = False , strip_last = False , lstrip = False ) :
    """Format a long string into a block of newline seperated text .
 Arguments :
 See iter _ format _ block ( ) ."""

implementation:
# Basic usage of iter _ format _ block ( ) , for convenience .
return '\n' . join ( self . iter_format_block ( ( self . text if text is None else text ) or '' , prepend = prepend , append = append , strip_first = strip_first , strip_last = strip_last , width = width , chars = chars , fill = fill , newlines = newlines , lstrip = lstrip ) )


signature:
def iter_add_text ( self , lines , prepend = None , append = None ) :
    """Prepend or append text to lines . Yields each line ."""

implementation:
if ( prepend is None ) and ( append is None ) :
    yield from lines
else : # Build up a format string , with optional { prepend } / { append }
    fmtpcs = [ '{prepend}' ] if prepend else [ ]
    fmtpcs . append ( '{line}' )
    if append :
        fmtpcs . append ( '{append}' )
    fmtstr = '' . join ( fmtpcs )
    yield from ( fmtstr . format ( prepend = prepend , line = line , append = append ) for line in lines )


signature:
def iter_block ( self , text = None , width = 60 , chars = False , newlines = False , lstrip = False ) :
    """Iterator that turns a long string into lines no greater than
 ' width ' in length .
 It can wrap on spaces or characters . It only does basic blocks .
 For prepending see ` iter _ format _ block ( ) ` .
 Arguments :
 text : String to format .
 width : Maximum width for each line .
 Default : 60
 chars : Wrap on characters if true , otherwise on spaces .
 Default : False
 newlines : Preserve newlines when True .
 Default : False
 lstrip : Whether to remove leading spaces from each line .
 Default : False"""

implementation:
text = ( self . text if text is None else text ) or ''
if width < 1 :
    width = 1
fmtline = str . lstrip if lstrip else str
if chars and ( not newlines ) : # Simple block by chars , newlines are treated as a space .
    yield from self . iter_char_block ( text , width = width , fmtfunc = fmtline )
elif newlines : # Preserve newlines
    for line in text . split ( '\n' ) :
        yield from self . iter_block ( line , width = width , chars = chars , lstrip = lstrip , newlines = False , )
else : # Wrap on spaces ( ignores newlines ) . .
    yield from self . iter_space_block ( text , width = width , fmtfunc = fmtline , )


signature:
def iter_char_block ( self , text = None , width = 60 , fmtfunc = str ) :
    """Format block by splitting on individual characters ."""

implementation:
if width < 1 :
    width = 1
text = ( self . text if text is None else text ) or ''
text = ' ' . join ( text . split ( '\n' ) )
escapecodes = get_codes ( text )
if not escapecodes : # No escape codes , use simple method .
    yield from ( fmtfunc ( text [ i : i + width ] ) for i in range ( 0 , len ( text ) , width ) )
else : # Ignore escape codes when counting .
    blockwidth = 0
    block = [ ]
    for i , s in enumerate ( get_indices_list ( text ) ) :
        block . append ( s )
        if len ( s ) == 1 : # Normal char .
            blockwidth += 1
        if blockwidth == width :
            yield '' . join ( block )
            block = [ ]
            blockwidth = 0
    if block :
        yield '' . join ( block )


signature:
def iter_format_block ( self , text = None , width = 60 , chars = False , fill = False , newlines = False , append = None , prepend = None , strip_first = False , strip_last = False , lstrip = False ) :
    """Iterate over lines in a formatted block of text .
 This iterator allows you to prepend to each line .
 For basic blocks see iter _ block ( ) .
 Arguments :
 text : String to format .
 width : Maximum width for each line . The prepend string
 is not included in this calculation .
 Default : 60
 chars : Whether to wrap on characters instead of spaces .
 Default : False
 fill : Insert spaces between words so that each line is
 the same width . This overrides ` chars ` .
 Default : False
 newlines : Whether to preserve newlines in the original
 string .
 Default : False
 append : String to append after each line .
 prepend : String to prepend before each line .
 strip _ first : Whether to omit the prepend string for the first
 line .
 Default : False
 Example ( when using prepend = ' $ ' ) :
 Without strip _ first - > ' $ this ' , ' $ that '
 With strip _ first - > ' this ' , ' $ that '
 strip _ last : Whether to omit the append string for the last
 line ( like strip _ first does for prepend ) .
 Default : False
 lstrip : Whether to remove leading spaces from each line .
 This doesn ' t include any spaces in ` prepend ` .
 Default : False"""

implementation:
if fill :
    chars = False
iterlines = self . iter_block ( ( self . text if text is None else text ) or '' , width = width , chars = chars , newlines = newlines , lstrip = lstrip , )
if not ( prepend or append ) : # Shortcut some of the logic below when not prepending / appending .
    if fill :
        yield from ( self . expand_words ( l , width = width ) for l in iterlines )
    else :
        yield from iterlines
else : # Prepend , append , or both prepend / append to each line .
    if prepend :
        prependlen = len ( prepend )
    else : # No prepend , stripping not necessary and shouldn ' t be tried .
        strip_first = False
        prependlen = 0
    if append : # Unfortunately appending mean exhausting the generator .
    # I don ' t know where the last line is if I don ' t .
        lines = list ( iterlines )
        lasti = len ( lines ) - 1
        iterlines = ( l for l in lines )
        appendlen = len ( append )
    else : # No append , stripping not necessary and shouldn ' t be tried .
        strip_last = False
        appendlen = 0
        lasti = - 1
    for i , l in enumerate ( self . iter_add_text ( iterlines , prepend = prepend , append = append ) ) :
        if strip_first and ( i == 0 ) : # Strip the prepend that iter _ add _ text ( ) added .
            l = l [ prependlen : ]
        elif strip_last and ( i == lasti ) : # Strip the append that iter _ add _ text ( ) added .
            l = l [ : - appendlen ]
        if fill :
            yield self . expand_words ( l , width = width )
        else :
            yield l


signature:
def iter_space_block ( self , text = None , width = 60 , fmtfunc = str ) :
    """Format block by wrapping on spaces ."""

implementation:
if width < 1 :
    width = 1
curline = ''
text = ( self . text if text is None else text ) or ''
for word in text . split ( ) :
    possibleline = ' ' . join ( ( curline , word ) ) if curline else word
    # Ignore escape codes .
    codelen = sum ( len ( s ) for s in get_codes ( possibleline ) )
    reallen = len ( possibleline ) - codelen
    if reallen > width : # This word would exceed the limit , start a new line with
    # it .
        yield fmtfunc ( curline )
        curline = word
    else :
        curline = possibleline
# yield the last line .
if curline :
    yield fmtfunc ( curline )


signature:
def squeeze_words ( line , width = 60 ) :
    """Remove spaces in between words until it is small enough for
 ` width ` .
 This will always leave at least one space between words ,
 so it may not be able to get below ` width ` characters ."""

implementation:
# Start removing spaces to " squeeze " the text , leaving at least one .
while ( '  ' in line ) and ( len ( line ) > width ) : # Remove two spaces from the end , replace with one .
    head , _ , tail = line . rpartition ( '  ' )
    line = ' ' . join ( ( head , tail ) )
return line


signature:
def check_ip ( self , ip ) :
    """Check IP trough the httpBL API
 : param ip : ipv4 ip address
 : return : httpBL results or None if any error is occurred"""

implementation:
self . _last_result = None
if is_valid_ipv4 ( ip ) :
    key = None
    if self . _use_cache :
        key = self . _make_cache_key ( ip )
        self . _last_result = self . _cache . get ( key , version = self . _cache_version )
    if self . _last_result is None : # request httpBL API
        error , age , threat , type = self . _request_httpbl ( ip )
        if error == 127 or error == 0 :
            self . _last_result = { 'error' : error , 'age' : age , 'threat' : threat , 'type' : type }
            if self . _use_cache :
                self . _cache . set ( key , self . _last_result , timeout = self . _api_timeout , version = self . _cache_version )
    if self . _last_result is not None and settings . CACHED_HTTPBL_USE_LOGGING :
        logger . info ( 'httpBL check ip: {0}; ' 'httpBL result: error: {1}, age: {2}, threat: {3}, type: {4}' . format ( ip , self . _last_result [ 'error' ] , self . _last_result [ 'age' ] , self . _last_result [ 'threat' ] , self . _last_result [ 'type' ] ) )
return self . _last_result


signature:
def is_threat ( self , result = None , harmless_age = None , threat_score = None , threat_type = None ) :
    """Check if IP is a threat
 : param result : httpBL results ; if None , then results from last check _ ip ( ) used ( optional )
 : param harmless _ age : harmless age for check if httpBL age is older ( optional )
 : param threat _ score : threat score for check if httpBL threat is lower ( optional )
 : param threat _ type : threat type , if not equal httpBL score type , then return False ( optional )
 : return : True or False"""

implementation:
harmless_age = harmless_age if harmless_age is not None else settings . CACHED_HTTPBL_HARMLESS_AGE
threat_score = threat_score if threat_score is not None else settings . CACHED_HTTPBL_THREAT_SCORE
threat_type = threat_type if threat_type is not None else - 1
result = result if result is not None else self . _last_result
threat = False
if result is not None :
    if result [ 'age' ] < harmless_age and result [ 'threat' ] > threat_score :
        threat = True
    if threat_type > - 1 :
        if result [ 'type' ] & threat_type :
            threat = True
        else :
            threat = False
return threat


signature:
def is_suspicious ( self , result = None ) :
    """Check if IP is suspicious
 : param result : httpBL results ; if None , then results from last check _ ip ( ) used ( optional )
 : return : True or False"""

implementation:
result = result if result is not None else self . _last_result
suspicious = False
if result is not None :
    suspicious = True if result [ 'type' ] > 0 else False
return suspicious


signature:
def invalidate_ip ( self , ip ) :
    """Invalidate httpBL cache for IP address
 : param ip : ipv4 IP address"""

implementation:
if self . _use_cache :
    key = self . _make_cache_key ( ip )
    self . _cache . delete ( key , version = self . _cache_version )


signature:
def invalidate_cache ( self ) :
    """Invalidate httpBL cache"""

implementation:
if self . _use_cache :
    self . _cache_version += 1
    self . _cache . increment ( 'cached_httpbl_{0}_version' . format ( self . _api_key ) )


signature:
def run ( self ) :
    """Runs the consumer ."""

implementation:
self . log . debug ( 'consumer is running...' )
self . running = True
while self . running :
    self . upload ( )
self . log . debug ( 'consumer exited.' )


signature:
def upload ( self ) :
    """Upload the next batch of items , return whether successful ."""

implementation:
success = False
batch = self . next ( )
if len ( batch ) == 0 :
    return False
try :
    self . request ( batch )
    success = True
except Exception as e :
    self . log . error ( 'error uploading: %s' , e )
    success = False
    if self . on_error :
        self . on_error ( e , batch )
finally : # cleanup
    for item in batch :
        self . queue . task_done ( )
    return success


signature:
def next ( self ) :
    """Return the next batch of items to upload ."""

implementation:
queue = self . queue
items = [ ]
item = self . next_item ( )
if item is None :
    return items
items . append ( item )
while len ( items ) < self . upload_size and not queue . empty ( ) :
    item = self . next_item ( )
    if item :
        items . append ( item )
return items


signature:
def next_item ( self ) :
    """Get a single item from the queue ."""

implementation:
queue = self . queue
try :
    item = queue . get ( block = True , timeout = 5 )
    return item
except Exception :
    return None


signature:
def request ( self , batch , attempt = 0 ) :
    """Attempt to upload the batch and retry before raising an error"""

implementation:
try :
    q = self . api . new_queue ( )
    for msg in batch :
        q . add ( msg [ 'event' ] , msg [ 'value' ] , source = msg [ 'source' ] )
    q . submit ( )
except :
    if attempt > self . retries :
        raise
    self . request ( batch , attempt + 1 )


signature:
def _camelcase_to_underscore ( url ) :
    """Translate camelCase into underscore format .
 > > > _ camelcase _ to _ underscore ( ' minutesBetweenSummaries ' )
 ' minutes _ between _ summaries '"""

implementation:
def upper2underscore ( text ) :
    for char in text :
        if char . islower ( ) :
            yield char
        else :
            yield '_'
            if char . isalpha ( ) :
                yield char . lower ( )
return '' . join ( upper2underscore ( url ) )


signature:
def create_tree ( endpoints ) :
    """Creates the Trello endpoint tree .
 > > > r = { ' 1 ' : { ' actions ' : { ' METHODS ' : { ' GET ' } } , ' boards ' : { ' members ' : { ' METHODS ' : { ' DELETE ' } } } } }
 > > > r = = create _ tree ( [ ' GET / 1 / actions / [ idAction ] ' , ' DELETE / 1 / boards / [ board _ id ] / members / [ idMember ] ' ] )
 True"""

implementation:
tree = { }
for method , url , doc in endpoints :
    path = [ p for p in url . strip ( '/' ) . split ( '/' ) ]
    here = tree
    # First element ( API Version ) .
    version = path [ 0 ]
    here . setdefault ( version , { } )
    here = here [ version ]
    # The rest of elements of the URL .
    for p in path [ 1 : ] :
        part = _camelcase_to_underscore ( p )
        here . setdefault ( part , { } )
        here = here [ part ]
    # Allowed HTTP methods .
    if not 'METHODS' in here :
        here [ 'METHODS' ] = [ [ method , doc ] ]
    else :
        if not method in here [ 'METHODS' ] :
            here [ 'METHODS' ] . append ( [ method , doc ] )
return tree


signature:
def main ( ) :
    """Prints the complete YAML ."""

implementation:
ep = requests . get ( TRELLO_API_DOC ) . content
root = html . fromstring ( ep )
links = root . xpath ( '//a[contains(@class, "reference internal")]/@href' )
pages = [ requests . get ( TRELLO_API_DOC + u ) for u in links if u . endswith ( 'index.html' ) ]
endpoints = [ ]
for page in pages :
    root = html . fromstring ( page . content )
    sections = root . xpath ( '//div[@class="section"]/h2/..' )
    for sec in sections :
        ep_html = etree . tostring ( sec ) . decode ( 'utf-8' )
        ep_text = html2text ( ep_html ) . splitlines ( )
        match = EP_DESC_REGEX . match ( ep_text [ 0 ] )
        if not match :
            continue
        ep_method , ep_url = match . groups ( )
        ep_text [ 0 ] = ' ' . join ( [ ep_method , ep_url ] )
        ep_doc = b64encode ( gzip . compress ( '\n' . join ( ep_text ) . encode ( 'utf-8' ) ) )
        endpoints . append ( ( ep_method , ep_url , ep_doc ) )
print ( yaml . dump ( create_tree ( endpoints ) ) )


signature:
def _parser_exit ( parser : argparse . ArgumentParser , proc : "DirectoryListProcessor" , _ = 0 , message : Optional [ str ] = None ) -> None :
    """Override the default exit in the parser .
 : param parser :
 : param _ : exit code . Unused because we don ' t exit
 : param message : Optional message"""

implementation:
if message :
    parser . _print_message ( message , sys . stderr )
proc . successful_parse = False


signature:
def decode_file_args ( self , argv : List [ str ] ) -> List [ str ] :
    """Preprocess any arguments that begin with the fromfile prefix char ( s ) .
 This replaces the one in Argparse because it
 a ) doesn ' t process " - x y " correctly and
 b ) ignores bad files
 : param argv : raw options list
 : return : options list with file references replaced"""

implementation:
for arg in [ arg for arg in argv if arg [ 0 ] in self . fromfile_prefix_chars ] :
    argv . remove ( arg )
    with open ( arg [ 1 : ] ) as config_file :
        argv += shlex . split ( config_file . read ( ) )
        return self . decode_file_args ( argv )
return argv


signature:
def _proc_error ( ifn : str , e : Exception ) -> None :
    """Report an error
 : param ifn : Input file name
 : param e : Exception to report"""

implementation:
type_ , value_ , traceback_ = sys . exc_info ( )
traceback . print_tb ( traceback_ , file = sys . stderr )
print ( file = sys . stderr )
print ( "***** ERROR: %s" % ifn , file = sys . stderr )
print ( str ( e ) , file = sys . stderr )


signature:
def _call_proc ( self , proc : Callable [ [ Optional [ str ] , Optional [ str ] , argparse . Namespace ] , bool ] , ifn : Optional [ str ] , ofn : Optional [ str ] ) -> bool :
    """Call the actual processor and intercept anything that goes wrong
 : param proc : Process to call
 : param ifn : Input file name to process . If absent , typical use is stdin
 : param ofn : Output file name . If absent , typical use is stdout
 : return : true means process was successful"""

implementation:
rslt = False
try :
    rslt = proc ( ifn , ofn , self . opts )
except Exception as e :
    self . _proc_error ( ifn , e )
return True if rslt or rslt is None else False


signature:
def run ( self , proc : Callable [ [ Optional [ str ] , Optional [ str ] , argparse . Namespace ] , Optional [ bool ] ] , file_filter : Optional [ Callable [ [ str ] , bool ] ] = None , file_filter_2 : Optional [ Callable [ [ Optional [ str ] , str , argparse . Namespace ] , bool ] ] = None ) -> Tuple [ int , int ] :
    """Run the directory list processor calling a function per file .
 : param proc : Process to invoke . Args : input _ file _ name , output _ file _ name , argparse options . Return pass or fail .
 No return also means pass
 : param file _ filter : Additional filter for testing file names , types , etc .
 : param file _ filter _ 2 : File filter that includes directory , filename and opts
 ( separate for backwards compatibility )
 : return : tuple - ( number of files passed to proc : int , number of files that passed proc )"""

implementation:
nfiles = 0
nsuccess = 0
# List of one or more input and output files
if self . opts . infile :
    for file_idx in range ( len ( self . opts . infile ) ) :
        in_f = self . opts . infile [ file_idx ]
        if self . _check_filter ( in_f , self . opts . indir , file_filter , file_filter_2 ) :
            fn = os . path . join ( self . opts . indir , in_f ) if self . opts . indir else in_f
            nfiles += 1
            if self . _call_proc ( proc , fn , self . _outfile_name ( '' , fn , outfile_idx = file_idx ) ) :
                nsuccess += 1
            elif self . opts . stoponerror :
                return nfiles , nsuccess
# Single input from the command line
elif not self . opts . indir :
    if self . _check_filter ( None , None , file_filter , file_filter_2 ) :
        nfiles += 1
        if self . _call_proc ( proc , None , self . _outfile_name ( '' , '' ) ) :
            nsuccess += 1
# Input directory that needs to be navigated
else :
    for dirpath , _ , filenames in os . walk ( self . opts . indir ) :
        for fn in filenames :
            if self . _check_filter ( fn , dirpath , file_filter , file_filter_2 ) :
                nfiles += 1
                if self . _call_proc ( proc , os . path . join ( dirpath , fn ) , self . _outfile_name ( dirpath , fn ) ) :
                    nsuccess += 1
                elif self . opts . stoponerror :
                    return nfiles , nsuccess
return nfiles , nsuccess


signature:
def _outfile_name ( self , dirpath : str , infile : str , outfile_idx : int = 0 ) -> Optional [ str ] :
    """Construct the output file name from the input file . If a single output file was named and there isn ' t a
 directory , return the output file .
 : param dirpath : Directory path to infile
 : param infile : Name of input file
 : param outfile _ idx : Index into output file list ( for multiple input / output files )
 : return : Full name of output file or None if output is not otherwise supplied"""

implementation:
if not self . opts . outfile and not self . opts . outdir : # Up to the process itself to decide what do do with it
    return None
if self . opts . outfile : # Output file specified - either one aggregate file or a 1 to 1 list
    outfile_element = self . opts . outfile [ 0 ] if len ( self . opts . outfile ) == 1 else self . opts . outfile [ outfile_idx ]
elif self . opts . infile : # Input file name ( s ) have been supplied
    if '://' in infile : # Input file is a URL - - generate an output file of the form " _ url [ n ] "
        outfile_element = "_url{}" . format ( outfile_idx + 1 )
    else :
        outfile_element = os . path . basename ( infile ) . rsplit ( '.' , 1 ) [ 0 ]
else : # Doing an input directory to an output directory
    relpath = dirpath [ len ( self . opts . indir ) + 1 : ] if not self . opts . flatten and self . opts . indir else ''
    outfile_element = os . path . join ( relpath , os . path . split ( infile ) [ 1 ] [ : - len ( self . infile_suffix ) ] )
return ( os . path . join ( self . opts . outdir , outfile_element ) if self . opts . outdir else outfile_element ) + ( self . outfile_suffix if not self . opts . outfile and self . outfile_suffix else '' )


signature:
def quit ( self ) :
    """Close and exit the connection ."""

implementation:
try :
    self . ftp . quit ( )
    self . logger . debug ( "quit connect succeed." )
except ftplib . Error as e :
    self . unknown ( "quit connect error: %s" % e )


signature:
def query ( self , wql ) :
    """Connect by wmi and run wql ."""

implementation:
try :
    self . __wql = [ 'wmic' , '-U' , self . args . domain + '\\' + self . args . user + '%' + self . args . password , '//' + self . args . host , '--namespace' , self . args . namespace , '--delimiter' , self . args . delimiter , wql ]
    self . logger . debug ( "wql: {}" . format ( self . __wql ) )
    self . __output = subprocess . check_output ( self . __wql )
    self . logger . debug ( "output: {}" . format ( self . __output ) )
    self . logger . debug ( "wmi connect succeed." )
    self . __wmi_output = self . __output . splitlines ( ) [ 1 : ]
    self . logger . debug ( "wmi_output: {}" . format ( self . __wmi_output ) )
    self . __csv_header = csv . DictReader ( self . __wmi_output , delimiter = '|' )
    self . logger . debug ( "csv_header: {}" . format ( self . __csv_header ) )
    return list ( self . __csv_header )
except subprocess . CalledProcessError as e :
    self . unknown ( "Connect by wmi and run wql error: %s" % e )


signature:
def set_value ( self , value , timeout ) :
    """Changes the cached value and updates creation time .
 Args :
 value : the new cached value .
 timeout : time to live for the object in milliseconds
 Returns : None"""

implementation:
self . value = value
self . expiration = time . clock ( ) * 1000 + timeout


signature:
def log ( self , url = None , credentials = None , do_verify_certificate = True ) :
    """Wrapper for the other log methods , decide which one based on the
 URL parameter ."""

implementation:
if url is None :
    url = self . url
if re . match ( "file://" , url ) :
    self . log_file ( url )
elif re . match ( "https://" , url ) or re . match ( "http://" , url ) :
    self . log_post ( url , credentials , do_verify_certificate )
else :
    self . log_stdout ( )


signature:
def log_file ( self , url = None ) :
    """Write to a local log file"""

implementation:
if url is None :
    url = self . url
f = re . sub ( "file://" , "" , url )
try :
    with open ( f , "a" ) as of :
        of . write ( str ( self . store . get_json_tuples ( True ) ) )
except IOError as e :
    print ( e )
    print ( "Could not write the content to the file.." )


signature:
def log_post ( self , url = None , credentials = None , do_verify_certificate = True ) :
    """Write to a remote host via HTTP POST"""

implementation:
if url is None :
    url = self . url
if credentials is None :
    credentials = self . credentials
if do_verify_certificate is None :
    do_verify_certificate = self . do_verify_certificate
if credentials and "base64" in credentials :
    headers = { "Content-Type" : "application/json" , 'Authorization' : 'Basic %s' % credentials [ "base64" ] }
else :
    headers = { "Content-Type" : "application/json" }
try :
    request = requests . post ( url , headers = headers , data = self . store . get_json ( ) , verify = do_verify_certificate )
except httplib . IncompleteRead as e :
    request = e . partial


signature:
def register_credentials ( self , credentials = None , user = None , user_file = None , password = None , password_file = None ) :
    """Helper method to store username and password"""

implementation:
# lets store all kind of credential data into this dict
if credentials is not None :
    self . credentials = credentials
else :
    self . credentials = { }
    # set the user from CLI or file
    if user :
        self . credentials [ "user" ] = user
    elif user_file :
        with open ( user_file , "r" ) as of : # what would the file entry look like ?
            pattern = re . compile ( "^user: " )
            for l in of :
                if re . match ( pattern , l ) : # strip away the newline
                    l = l [ 0 : - 1 ]
                    self . credentials [ "user" ] = re . sub ( pattern , "" , l )
        # remove any surrounding quotes
        if self . credentials [ "user" ] [ 0 : 1 ] == '"' and self . credentials [ "user" ] [ - 1 : ] == '"' :
            self . credentials [ "user" ] = self . credentials [ "user" ] [ 1 : - 1 ]
    # set the password from CLI or file
    if password :
        self . credentials [ "password" ] = password
    elif password_file :
        with open ( password_file , "r" ) as of : # what would the file entry look like ?
            pattern = re . compile ( "^password: " )
            for l in of :
                if re . match ( pattern , l ) : # strip away the newline
                    l = l [ 0 : - 1 ]
                    self . credentials [ "password" ] = re . sub ( pattern , "" , l )
        # remove any surrounding quotes
        if self . credentials [ "password" ] [ 0 : 1 ] == '"' and self . credentials [ "password" ] [ - 1 : ] == '"' :
            self . credentials [ "password" ] = self . credentials [ "password" ] [ 1 : - 1 ]
    # if both user and password is set ,
    # 1 . encode to base 64 for basic auth
    if "user" in self . credentials and "password" in self . credentials :
        c = self . credentials [ "user" ] + ":" + self . credentials [ "password" ]
        self . credentials [ "base64" ] = b64encode ( c . encode ( ) ) . decode ( "ascii" )


signature:
def generator_to_list ( function ) :
    """Wrap a generator function so that it returns a list when called .
 For example :
 # Define a generator
 > > > def mygen ( n ) :
 . . . i = 0
 . . . while i < n :
 . . . yield i
 . . . i + = 1
 # This is how it might work
 > > > generator = mygen ( 5)
 > > > generator . next ( )
 > > > generator . next ( )
 # Wrap it in generator _ to _ list , and it will behave differently .
 > > > mygen = generator _ to _ list ( mygen )
 > > > mygen ( 5)
 [0 , 1 , 2 , 3 , 4]"""

implementation:
def wrapper ( * args , ** kwargs ) :
    return list ( function ( * args , ** kwargs ) )
wrapper . __name__ = function . __name__
wrapper . __doc__ = function . __doc__
return wrapper


signature:
def logrotate ( filename ) :
    """Return the next available filename for a particular filename prefix .
 For example :
 > > > import os
 # Make three ( empty ) files in a directory
 > > > fp0 = open ( ' file . 0 ' , ' w ' )
 > > > fp1 = open ( ' file . 1 ' , ' w ' )
 > > > fp2 = open ( ' file . 2 ' , ' w ' )
 > > > fp0 . close ( ) , fp1 . close ( ) , fp2 . close ( )
 ( None , None , None )
 # Use logrotate to get the next available filename .
 > > > logrotate ( ' file ' )
 ' file . 3'
 > > > logrotate ( ' file . 2 ' )
 ' file . 3'
 > > > logrotate ( ' file . 1 ' )
 ' file . 3'
 This can be used to get the next available filename for logging , allowing
 you to rotate log files , without using Python ' s ` ` logging ` ` module ."""

implementation:
match = re . match ( r'(.*)' + re . escape ( os . path . extsep ) + r'(\d+)' , filename )
if os . path . exists ( filename ) :
    if match :
        prefix , number = match . groups ( )
        number = int ( number )
        while os . path . exists ( os . path . extsep . join ( ( prefix , str ( number ) ) ) ) :
            number += 1
        return os . path . extsep . join ( ( prefix , str ( number ) ) )
elif match :
    return filename
return logrotate ( os . path . extsep . join ( ( filename , '0' ) ) )


signature:
def set_connection ( host = None , database = None , user = None , password = None ) :
    """Set connection parameters . Call set _ connection with no arguments to clear ."""

implementation:
c . CONNECTION [ 'HOST' ] = host
c . CONNECTION [ 'DATABASE' ] = database
c . CONNECTION [ 'USER' ] = user
c . CONNECTION [ 'PASSWORD' ] = password


signature:
def set_delegate ( address = None , pubkey = None , secret = None ) :
    """Set delegate parameters . Call set _ delegate with no arguments to clear ."""

implementation:
c . DELEGATE [ 'ADDRESS' ] = address
c . DELEGATE [ 'PUBKEY' ] = pubkey
c . DELEGATE [ 'PASSPHRASE' ] = secret


signature:
def get_transactionlist ( delegate_pubkey ) :
    """returns a list of named tuples of all transactions relevant to a specific delegates voters .
 Flow : finds all voters and unvoters , SELECTs all transactions of those voters , names all transactions according to
 the scheme : ' transaction ' , ' id amount timestamp recipientId senderId rawasset type fee blockId '"""

implementation:
res = DbCursor ( ) . execute_and_fetchall ( """
        SELECT transactions."id", transactions."amount",
               blocks."timestamp", transactions."recipientId",
               transactions."senderId", transactions."rawasset",
               transactions."type", transactions."fee", transactions."blockId"
        FROM transactions
        INNER JOIN blocks
          ON transactions."blockId" = blocks.id
        WHERE transactions."senderId" IN
          (SELECT transactions."recipientId"
           FROM transactions, votes
           WHERE transactions."id" = votes."transactionId"
           AND votes."votes" = '+{0}')
        OR transactions."recipientId" IN
          (SELECT transactions."recipientId"
           FROM transactions, votes
           WHERE transactions."id" = votes."transactionId"
           AND votes."votes" = '+{0}')
        ORDER BY blocks."timestamp" ASC;""" . format ( delegate_pubkey ) )
Transaction = namedtuple ( 'transaction' , 'id amount timestamp recipientId senderId rawasset type fee' )
named_transactions = [ ]
for i in res :
    tx_id = Transaction ( id = i [ 0 ] , amount = i [ 1 ] , timestamp = i [ 2 ] , recipientId = i [ 3 ] , senderId = i [ 4 ] , rawasset = i [ 5 ] , type = i [ 6 ] , fee = i [ 7 ] , )
    named_transactions . append ( tx_id )
return named_transactions


signature:
def get_events ( delegate_pubkey ) :
    """returns a list of named tuples of all transactions relevant to a specific delegates voters .
 Flow : finds all voters and unvoters , SELECTs all transactions of those voters , names all transactions according to
 the scheme : ' transaction ' , ' id amount timestamp recipientId senderId rawasset type fee blockId '"""

implementation:
res = DbCursor ( ) . execute_and_fetchall ( """
    SELECT *
      FROM(
        SELECT transactions."id",
               transactions."amount",
               transactions."fee",
               blocks."timestamp",
               transactions."recipientId",
               transactions."senderId",
               transactions."type",
               transactions."rawasset"
        FROM transactions
        INNER JOIN blocks
          ON transactions."blockId" = blocks.id
        WHERE transactions."senderId" IN
          (SELECT transactions."recipientId"
           FROM transactions, votes
           WHERE transactions."id" = votes."transactionId"
           AND votes."votes" = '+{0}')
        OR transactions."recipientId" IN
          (SELECT transactions."recipientId"
           FROM transactions, votes
           WHERE transactions."id" = votes."transactionId"
           AND votes."votes" = '+{0}')
        UNION
        SELECT blocks."id",
               blocks."reward",
               blocks."totalFee",
               blocks."timestamp",
               mem_accounts."address",
               NULL,
               100,
               blocks."rawtxs"
        FROM blocks
        INNER JOIN mem_accounts
          ON mem_accounts."publicKey" = blocks."generatorPublicKey"
        WHERE mem_accounts."address" IN
          (SELECT transactions."recipientId"
           FROM transactions, votes
           WHERE transactions."id" = votes."transactionId"
           AND votes."votes" = '+{0}')) AS events
        ORDER BY events."timestamp";""" . format ( delegate_pubkey ) )
Event = namedtuple ( 'Event' , 'id amount fee timestamp recipientId senderId type raw' )
named_events = [ ]
for i in res :
    tx_id = Event ( id = i [ 0 ] , amount = i [ 1 ] , fee = i [ 2 ] , timestamp = i [ 3 ] , recipientId = i [ 4 ] , senderId = i [ 5 ] , type = i [ 6 ] , raw = i [ 7 ] )
    named_events . append ( tx_id )
return named_events


signature:
def payout ( address ) :
    """returns all received transactions between the address and registered delegate accounts
 ORDER by timestamp ASC ."""

implementation:
qry = DbCursor ( ) . execute_and_fetchall ( """
                SELECT DISTINCT transactions."id", transactions."amount",
                       transactions."timestamp", transactions."recipientId",
                       transactions."senderId", transactions."rawasset",
                       transactions."type", transactions."fee"
                FROM transactions, delegates
                WHERE transactions."senderId" IN (
                  SELECT transactions."senderId"
                  FROM transactions, delegates
                  WHERE transactions."id" = delegates."transactionId"
                )
                AND transactions."recipientId" = '{}'
                ORDER BY transactions."timestamp" ASC""" . format ( address ) )
Transaction = namedtuple ( 'transaction' , 'id amount timestamp recipientId senderId rawasset type fee' )
named_transactions = [ ]
for i in qry :
    tx_id = Transaction ( id = i [ 0 ] , amount = i [ 1 ] , timestamp = i [ 2 ] , recipientId = i [ 3 ] , senderId = i [ 4 ] , rawasset = i [ 5 ] , type = i [ 6 ] , fee = i [ 7 ] , )
    named_transactions . append ( tx_id )
return named_transactions


signature:
def votes ( address ) :
    """Returns a list of namedtuples all votes made by an address , { ( + / - ) pubkeydelegate : timestamp } , timestamp DESC"""

implementation:
qry = DbCursor ( ) . execute_and_fetchall ( """
           SELECT votes."votes", transactions."timestamp"
           FROM votes, transactions
           WHERE transactions."id" = votes."transactionId"
           AND transactions."senderId" = '{}'
           ORDER BY transactions."timestamp" DESC
        """ . format ( address ) )
Vote = namedtuple ( 'vote' , 'direction delegate timestamp' )
res = [ ]
for i in qry :
    if i [ 0 ] [ 0 ] == '+' :
        direction = True
    elif i [ 0 ] [ 0 ] == '-' :
        direction = False
    else :
        logger . fatal ( 'failed to interpret direction for: {}' . format ( i ) )
        raise ParseError ( 'failed to interpret direction of vote for: {}' . format ( i ) )
    vote = Vote ( direction = direction , delegate = i [ 0 ] [ 1 : ] , timestamp = i [ 1 ] , )
    res . append ( vote )
return res


signature:
def balance ( address ) :
    """Takes a single address and returns the current balance ."""

implementation:
txhistory = Address . transactions ( address )
balance = 0
for i in txhistory :
    if i . recipientId == address :
        balance += i . amount
    if i . senderId == address :
        balance -= ( i . amount + i . fee )
delegates = Delegate . delegates ( )
for i in delegates :
    if address == i . address :
        forged_blocks = Delegate . blocks ( i . pubkey )
        for block in forged_blocks :
            balance += ( block . reward + block . totalFee )
if balance < 0 :
    height = Node . height ( )
    logger . fatal ( 'Negative balance for address {0}, Nodeheight: {1)' . format ( address , height ) )
    raise NegativeBalanceError ( 'Negative balance for address {0}, Nodeheight: {1)' . format ( address , height ) )
return balance


signature:
def balance_over_time ( address ) :
    """returns a list of named tuples , x . timestamp , x . amount including block rewards"""

implementation:
forged_blocks = None
txhistory = Address . transactions ( address )
delegates = Delegate . delegates ( )
for i in delegates :
    if address == i . address :
        forged_blocks = Delegate . blocks ( i . pubkey )
balance_over_time = [ ]
balance = 0
block = 0
Balance = namedtuple ( 'balance' , 'timestamp amount' )
for tx in txhistory :
    if forged_blocks :
        while forged_blocks [ block ] . timestamp <= tx . timestamp :
            balance += ( forged_blocks [ block ] . reward + forged_blocks [ block ] . totalFee )
            balance_over_time . append ( Balance ( timestamp = forged_blocks [ block ] . timestamp , amount = balance ) )
            block += 1
    if tx . senderId == address :
        balance -= ( tx . amount + tx . fee )
        res = Balance ( timestamp = tx . timestamp , amount = balance )
        balance_over_time . append ( res )
    if tx . recipientId == address :
        balance += tx . amount
        res = Balance ( timestamp = tx . timestamp , amount = balance )
        balance_over_time . append ( res )
if forged_blocks and block <= len ( forged_blocks ) - 1 :
    if forged_blocks [ block ] . timestamp > txhistory [ - 1 ] . timestamp :
        for i in forged_blocks [ block : ] :
            balance += ( i . reward + i . totalFee )
            res = Balance ( timestamp = i . timestamp , amount = balance )
            balance_over_time . append ( res )
return balance_over_time


signature:
def delegates ( ) :
    """returns a list of named tuples of all delegates .
 { username : { ' pubkey ' : pubkey , ' timestamp ' : timestamp , ' address ' : address } }"""

implementation:
qry = DbCursor ( ) . execute_and_fetchall ( """
            SELECT delegates."username", delegates."transactionId", transactions."timestamp", transactions."senderId",
            transactions."senderPublicKey"
            FROM transactions
            JOIN delegates ON transactions."id" = delegates."transactionId"
        """ )
Delegate = namedtuple ( 'delegate' , 'username pubkey timestamp address transactionId' )
res = [ ]
for i in qry :
    registration = Delegate ( username = i [ 0 ] , pubkey = binascii . hexlify ( i [ 4 ] ) . decode ( "utf-8" ) , timestamp = i [ 2 ] , address = i [ 3 ] , transactionId = i [ 1 ] )
    res . append ( registration )
return res


signature:
def lastpayout ( delegate_address , blacklist = None ) :
    '''Assumes that all send transactions from a delegate are payouts .
 Use blacklist to remove rewardwallet and other transactions if the
 address is not a voter . blacklist can contain both addresses and transactionIds'''

implementation:
if blacklist and len ( blacklist ) > 1 :
    command_blacklist = 'NOT IN ' + str ( tuple ( blacklist ) )
elif blacklist and len ( blacklist ) == 1 :
    command_blacklist = '!= ' + "'" + blacklist [ 0 ] + "'"
else :
    command_blacklist = "!= 'nothing'"
qry = DbCursor ( ) . execute_and_fetchall ( """
                    SELECT ts."recipientId", ts."id", ts."timestamp"
                    FROM transactions ts,
                      (SELECT MAX(transactions."timestamp") AS max_timestamp, transactions."recipientId"
                       FROM transactions
                       WHERE transactions."senderId" = '{0}'
                       AND transactions."id" {1}
                       GROUP BY transactions."recipientId") maxresults
                    WHERE ts."recipientId" = maxresults."recipientId"
                    AND ts."recipientId" {1}
                    AND ts."timestamp"= maxresults.max_timestamp;

                    """ . format ( delegate_address , command_blacklist ) )
result = [ ]
Payout = namedtuple ( 'payout' , 'address id timestamp' )
for i in qry :
    payout = Payout ( address = i [ 0 ] , id = i [ 1 ] , timestamp = i [ 2 ] )
    result . append ( payout )
return result


signature:
def votes ( delegate_pubkey ) :
    """returns every address that has voted for a delegate .
 Current voters can be obtained using voters . ORDER BY timestamp ASC"""

implementation:
qry = DbCursor ( ) . execute_and_fetchall ( """
                 SELECT transactions."recipientId", transactions."timestamp"
                 FROM transactions, votes
                 WHERE transactions."id" = votes."transactionId"
                 AND votes."votes" = '+{}'
                 ORDER BY transactions."timestamp" ASC;
        """ . format ( delegate_pubkey ) )
Voter = namedtuple ( 'voter' , 'address timestamp' )
voters = [ ]
for i in qry :
    voter = Voter ( address = i [ 0 ] , timestamp = i [ 1 ] )
    voters . append ( voter )
return voters


signature:
def blocks ( delegate_pubkey = None , max_timestamp = None ) :
    """returns a list of named tuples of all blocks forged by a delegate .
 if delegate _ pubkey is not specified , set _ delegate needs to be called in advance .
 max _ timestamp can be configured to retrieve blocks up to a certain timestamp ."""

implementation:
if not delegate_pubkey :
    delegate_pubkey = c . DELEGATE [ 'PUBKEY' ]
if max_timestamp :
    max_timestamp_sql = """ blocks."timestamp" <= {} AND""" . format ( max_timestamp )
else :
    max_timestamp_sql = ''
qry = DbCursor ( ) . execute_and_fetchall ( """
             SELECT blocks."timestamp", blocks."height", blocks."id", blocks."totalFee", blocks."reward"
             FROM blocks
             WHERE {0} blocks."generatorPublicKey" = '\\x{1}'
             ORDER BY blocks."timestamp"
             ASC""" . format ( max_timestamp_sql , delegate_pubkey ) )
Block = namedtuple ( 'block' , 'timestamp height id totalFee reward' )
block_list = [ ]
for block in qry :
    block_value = Block ( timestamp = block [ 0 ] , height = block [ 1 ] , id = block [ 2 ] , totalFee = block [ 3 ] , reward = block [ 4 ] )
    block_list . append ( block_value )
return block_list


signature:
def share ( passphrase = None , last_payout = None , start_block = 0 , del_pubkey = None , del_address = None ) :
    """Calculate the true blockweight payout share for a given delegate ,
 assuming no exceptions were made for a voter . last _ payout is a map of addresses and timestamps :
 { address : timestamp } . If no argument are given , it will start the calculation at the first forged block
 by the delegate , generate a last _ payout from transaction history , and use the set _ delegate info .
 If a passphrase is provided , it is only used to generate the adddress and keys , no transactions are sent .
 ( Still not recommended unless you know what you are doing , version control could store your passphrase for example ;
 very risky )"""

implementation:
logger . info ( 'starting share calculation using settings: {0} {1}' . format ( c . DELEGATE , c . CALCULATION_SETTINGS ) )
delegate_pubkey = c . DELEGATE [ 'PUBKEY' ]
delegate_address = c . DELEGATE [ 'ADDRESS' ]
if del_pubkey and del_address :
    delegate_address = del_address
    delegate_pubkey = del_pubkey
logger . info ( 'Starting share calculation, using address:{0}, pubkey:{1}' . format ( delegate_address , delegate_pubkey ) )
max_timestamp = Node . max_timestamp ( )
logger . info ( 'Share calculation max_timestamp = {}' . format ( max_timestamp ) )
# utils function
transactions = get_transactionlist ( delegate_pubkey = delegate_pubkey )
votes = Delegate . votes ( delegate_pubkey )
# create a map of voters
voter_dict = { }
for voter in votes :
    voter_dict . update ( { voter . address : { 'balance' : 0.0 , 'status' : False , 'last_payout' : voter . timestamp , 'share' : 0.0 , 'vote_timestamp' : voter . timestamp , 'blocks_forged' : [ ] } } )
# check if a voter is / used to be a forging delegate
delegates = Delegate . delegates ( )
for i in delegates :
    if i . address in voter_dict :
        logger . info ( 'A registered delegate is a voter: {0}, {1}, {2}' . format ( i . username , i . address , i . pubkey ) )
        try :
            blocks_by_voter = Delegate . blocks ( i . pubkey )
            voter_dict [ i . address ] [ 'blocks_forged' ] . extend ( Delegate . blocks ( i . pubkey ) )
            logger . info ( 'delegate {0} has forged {1} blocks' . format ( i . username , len ( blocks_by_voter ) ) )
        except Exception :
            logger . info ( 'delegate {} has not forged any blocks' . format ( i ) )
            pass
try :
    for i in c . CALCULATION_SETTINGS [ 'BLACKLIST' ] :
        voter_dict . pop ( i )
        logger . debug ( 'popped {} from calculations' . format ( i ) )
except Exception :
    pass
if not last_payout :
    last_payout = Delegate . lastpayout ( delegate_address )
    for payout in last_payout :
        try :
            voter_dict [ payout . address ] [ 'last_payout' ] = payout . timestamp
        except Exception :
            pass
elif type ( last_payout ) is int :
    for address in voter_dict :
        if address [ 'vote_timestamp' ] < last_payout :
            address [ 'last_payout' ] = last_payout
elif type ( last_payout ) is dict :
    for payout in last_payout :
        try :
            voter_dict [ payout . address ] [ 'last_payout' ] = payout . timestamp
        except Exception :
            pass
else :
    logger . fatal ( 'last_payout object not recognised: {}' . format ( type ( last_payout ) ) )
    raise InputError ( 'last_payout object not recognised: {}' . format ( type ( last_payout ) ) )
# get all forged blocks of delegate :
blocks = Delegate . blocks ( max_timestamp = max_timestamp , delegate_pubkey = delegate_pubkey )
block_nr = start_block
chunk_dict = { }
reuse = False
try :
    for tx in transactions :
        while tx . timestamp > blocks [ block_nr ] . timestamp :
            if reuse :
                block_nr += 1
                for x in chunk_dict :
                    voter_dict [ x ] [ 'share' ] += chunk_dict [ x ]
                continue
            block_nr += 1
            poolbalance = 0
            chunk_dict = { }
            for i in voter_dict :
                balance = voter_dict [ i ] [ 'balance' ]
                try :
                    if voter_dict [ i ] [ 'balance' ] > c . CALCULATION_SETTINGS [ 'MAX' ] :
                        balance = c . CALCULATION_SETTINGS [ 'MAX' ]
                except Exception :
                    pass
                try :
                    if balance > c . CALCULATION_SETTINGS [ 'EXCEPTIONS' ] [ i ] [ 'REPLACE' ] :
                        balance = c . CALCULATION_SETTINGS [ 'EXCEPTIONS' ] [ i ] [ 'REPLACE' ]
                except Exception :
                    pass
                try :
                    for x in voter_dict [ i ] [ 'blocks_forged' ] :
                        if x . timestamp < blocks [ block_nr ] . timestamp :
                            voter_dict [ i ] [ 'balance' ] += ( x . reward + x . totalFee )
                            voter_dict [ i ] [ 'blocks_forged' ] . remove ( x )
                    balance = voter_dict [ i ] [ 'balance' ]
                except Exception :
                    pass
                if voter_dict [ i ] [ 'status' ] :
                    if not voter_dict [ i ] [ 'balance' ] < - 20 * c . ARK :
                        poolbalance += balance
                    else :
                        logger . fatal ( 'balance lower than zero for: {0}' . format ( i ) )
                        raise NegativeBalanceError ( 'balance lower than zero for: {0}' . format ( i ) )
            for i in voter_dict :
                balance = voter_dict [ i ] [ 'balance' ]
                if voter_dict [ i ] [ 'balance' ] > c . CALCULATION_SETTINGS [ 'MAX' ] :
                    balance = c . CALCULATION_SETTINGS [ 'MAX' ]
                try :
                    if balance > c . CALCULATION_SETTINGS [ 'EXCEPTIONS' ] [ i ] [ 'REPLACE' ] :
                        balance = c . CALCULATION_SETTINGS [ 'EXCEPTIONS' ] [ i ] [ 'REPLACE' ]
                except Exception :
                    pass
                if voter_dict [ i ] [ 'status' ] and voter_dict [ i ] [ 'last_payout' ] < blocks [ block_nr ] . timestamp :
                    if c . CALCULATION_SETTINGS [ 'SHARE_FEES' ] :
                        share = ( balance / poolbalance ) * ( blocks [ block_nr ] . reward + blocks [ block_nr ] . totalFee )
                    else :
                        share = ( balance / poolbalance ) * blocks [ block_nr ] . reward
                    voter_dict [ i ] [ 'share' ] += share
                    chunk_dict . update ( { i : share } )
            reuse = True
        # parsing a transaction
        minvote = '{{"votes":["-{0}"]}}' . format ( delegate_pubkey )
        plusvote = '{{"votes":["+{0}"]}}' . format ( delegate_pubkey )
        reuse = False
        if tx . recipientId in voter_dict :
            voter_dict [ tx . recipientId ] [ 'balance' ] += tx . amount
        if tx . senderId in voter_dict :
            voter_dict [ tx . senderId ] [ 'balance' ] -= ( tx . amount + tx . fee )
        if tx . senderId in voter_dict and tx . type == 3 and plusvote in tx . rawasset :
            voter_dict [ tx . senderId ] [ 'status' ] = True
        if tx . senderId in voter_dict and tx . type == 3 and minvote in tx . rawasset :
            voter_dict [ tx . senderId ] [ 'status' ] = False
    remaining_blocks = len ( blocks ) - block_nr - 1
    for i in range ( remaining_blocks ) :
        for x in chunk_dict :
            voter_dict [ x ] [ 'share' ] += chunk_dict [ x ]
# an IndexError occurs if max ( transactions . timestamp ) > max ( blocks . timestamp ) This means we parsed every block
except IndexError :
    pass
for i in voter_dict :
    logger . info ( "{0}  {1}  {2}  {3}  {4}" . format ( i , voter_dict [ i ] [ 'share' ] , voter_dict [ i ] [ 'status' ] , voter_dict [ i ] [ 'last_payout' ] , voter_dict [ i ] [ 'vote_timestamp' ] ) )
return voter_dict , max_timestamp


signature:
def dep_trueshare ( start_block = 0 , del_pubkey = None , del_address = None , blacklist = None , share_fees = False , max_weight = float ( 'inf' ) , raiseError = True ) :
    '''Legacy TBW script ( still pretty performant , but has some quirky behavior when forging delegates are amongst
 your voters )
 : param int start _ block : block from which we start adding to the share ( we calculate balances from block 0 anyways )
 : param str del _ pubkey : delegate public key as is presented in the ark wallet
 : param str del _ address : delegate address
 : param list blacklist : blacklist for addresses to be removed BEFORE calculation . Their share is removed from the pool balance
 : param bool share _ fees : if tx fees should be shared as well .
 : param float max _ weight : max _ balance of a voter'''

implementation:
delegate_pubkey = c . DELEGATE [ 'PUBKEY' ]
delegate_address = c . DELEGATE [ 'ADDRESS' ]
if del_pubkey and del_address :
    delegate_address = del_address
    delegate_pubkey = del_pubkey
max_timestamp = Node . max_timestamp ( )
# utils function
transactions = get_transactionlist ( delegate_pubkey = delegate_pubkey )
votes = Delegate . votes ( delegate_pubkey )
# create a map of voters
voter_dict = { }
for voter in votes :
    voter_dict . update ( { voter . address : { 'balance' : 0.0 , 'status' : False , 'last_payout' : voter . timestamp , 'share' : 0.0 , 'vote_timestamp' : voter . timestamp , 'blocks_forged' : [ ] } } )
try :
    for i in blacklist :
        voter_dict . pop ( i )
except Exception :
    pass
# check if a voter is / used to be a forging delegate
delegates = Delegate . delegates ( )
for i in delegates :
    if i . address in voter_dict :
        try :
            blocks_by_voter = Delegate . blocks ( i . pubkey )
            voter_dict [ i . address ] [ 'blocks_forged' ] . extend ( Delegate . blocks ( i . pubkey ) )
        except Exception :
            pass
last_payout = Delegate . lastpayout ( delegate_address )
for payout in last_payout :
    try :
        voter_dict [ payout . address ] [ 'last_payout' ] = payout . timestamp
    except Exception :
        pass
blocks = Delegate . blocks ( delegate_pubkey )
block_nr = start_block
chunk_dict = { }
reuse = False
try :
    for tx in transactions :
        while tx . timestamp > blocks [ block_nr ] . timestamp :
            if reuse :
                block_nr += 1
                for x in chunk_dict :
                    voter_dict [ x ] [ 'share' ] += chunk_dict [ x ]
                continue
            block_nr += 1
            poolbalance = 0
            chunk_dict = { }
            for i in voter_dict :
                balance = voter_dict [ i ] [ 'balance' ]
                if balance > max_weight :
                    balance = max_weight
                # checks if a delegate that votes for us is has forged blocks in the mean time
                try :
                    for x in voter_dict [ i ] [ 'blocks_forged' ] :
                        if x . timestamp < blocks [ block_nr ] . timestamp :
                            voter_dict [ i ] [ 'balance' ] += ( x . reward + x . totalFee )
                            voter_dict [ i ] [ 'blocks_forged' ] . remove ( x )
                    balance = voter_dict [ i ] [ 'balance' ]
                except Exception :
                    pass
                if voter_dict [ i ] [ 'status' ] :
                    if not voter_dict [ i ] [ 'balance' ] < - 20 * c . ARK :
                        poolbalance += balance
                    else :
                        if raiseError :
                            raise NegativeBalanceError ( 'balance lower than zero for: {0}' . format ( i ) )
                        pass
            for i in voter_dict :
                balance = voter_dict [ i ] [ 'balance' ]
                if voter_dict [ i ] [ 'status' ] and voter_dict [ i ] [ 'last_payout' ] < blocks [ block_nr ] . timestamp :
                    if share_fees :
                        share = ( balance / poolbalance ) * ( blocks [ block_nr ] . reward + blocks [ block_nr ] . totalFee )
                    else :
                        share = ( balance / poolbalance ) * blocks [ block_nr ] . reward
                    voter_dict [ i ] [ 'share' ] += share
                    chunk_dict . update ( { i : share } )
            reuse = True
        # parsing a transaction
        minvote = '{{"votes":["-{0}"]}}' . format ( delegate_pubkey )
        plusvote = '{{"votes":["+{0}"]}}' . format ( delegate_pubkey )
        reuse = False
        if tx . recipientId in voter_dict :
            voter_dict [ tx . recipientId ] [ 'balance' ] += tx . amount
        if tx . senderId in voter_dict :
            voter_dict [ tx . senderId ] [ 'balance' ] -= ( tx . amount + tx . fee )
        if tx . senderId in voter_dict and tx . type == 3 and plusvote in tx . rawasset :
            voter_dict [ tx . senderId ] [ 'status' ] = True
        if tx . senderId in voter_dict and tx . type == 3 and minvote in tx . rawasset :
            voter_dict [ tx . senderId ] [ 'status' ] = False
    remaining_blocks = len ( blocks ) - block_nr - 1
    for i in range ( remaining_blocks ) :
        for x in chunk_dict :
            voter_dict [ x ] [ 'share' ] += chunk_dict [ x ]
except IndexError :
    pass
for i in voter_dict :
    logger . info ( "{0}  {1}  {2}  {3}  {4}" . format ( i , voter_dict [ i ] [ 'share' ] , voter_dict [ i ] [ 'status' ] , voter_dict [ i ] [ 'last_payout' ] , voter_dict [ i ] [ 'vote_timestamp' ] ) )
return voter_dict , max_timestamp


signature:
def trueshare ( start_block = 0 , del_pubkey = None , del_address = None , blacklist = None , share_fees = False , max_weight = float ( 'inf' ) ) :
    '''Legacy TBW script ( still pretty performant , but has some quirky behavior when forging delegates are amongst
 your voters )
 : param int start _ block : block from which we start adding to the share ( we calculate balances from block 0 anyways )
 : param str del _ pubkey : delegate public key as is presented in the ark wallet
 : param str del _ address : delegate address
 : param list blacklist : blacklist for addresses to be removed BEFORE calculation . Their share is removed from the pool balance
 : param bool share _ fees : if tx fees should be shared as well .
 : param float max _ weight : max _ balance of a voter'''

implementation:
delegate_pubkey = c . DELEGATE [ 'PUBKEY' ]
delegate_address = c . DELEGATE [ 'ADDRESS' ]
if del_pubkey and del_address :
    delegate_address = del_address
    delegate_pubkey = del_pubkey
max_timestamp = Node . max_timestamp ( )
# utils function
events = get_events ( delegate_pubkey = delegate_pubkey )
votes = Delegate . votes ( delegate_pubkey )
# create a map of voters
voter_dict = { }
for voter in votes :
    voter_dict . update ( { voter . address : { 'balance' : 0.0 , 'status' : False , 'last_payout' : voter . timestamp , 'share' : 0.0 , 'vote_timestamp' : voter . timestamp , 'blocks_forged' : [ ] } } )
try :
    for i in blacklist :
        voter_dict . pop ( i )
except Exception :
    pass
last_payout = Delegate . lastpayout ( delegate_address )
for payout in last_payout :
    try :
        voter_dict [ payout . address ] [ 'last_payout' ] = payout . timestamp
    except Exception :
        pass
blocks = Delegate . blocks ( delegate_pubkey )
block_nr = start_block
chunk_dict = { }
reuse = False
try :
    for e in events :
        while e . timestamp > blocks [ block_nr ] . timestamp :
            if reuse :
                block_nr += 1
                for x in chunk_dict :
                    voter_dict [ x ] [ 'share' ] += chunk_dict [ x ]
                continue
            block_nr += 1
            poolbalance = 0
            chunk_dict = { }
            for i in voter_dict :
                balance = voter_dict [ i ] [ 'balance' ]
                if balance > max_weight :
                    balance = max_weight
                if voter_dict [ i ] [ 'status' ] :
                    if voter_dict [ i ] [ 'balance' ] >= 0 :
                        poolbalance += balance
                    else :
                        print ( voter_dict [ i ] )
                        raise NegativeBalanceError ( 'balance lower than zero for: {0}' . format ( i ) )
            for i in voter_dict :
                balance = voter_dict [ i ] [ 'balance' ]
                if voter_dict [ i ] [ 'status' ] and voter_dict [ i ] [ 'last_payout' ] < blocks [ block_nr ] . timestamp :
                    if share_fees :
                        share = ( balance / poolbalance ) * ( blocks [ block_nr ] . reward + blocks [ block_nr ] . totalFee )
                    else :
                        share = ( balance / poolbalance ) * blocks [ block_nr ] . reward
                    voter_dict [ i ] [ 'share' ] += share
                    chunk_dict . update ( { i : share } )
            reuse = True
        # parsing a transaction
        minvote = '{{"votes":["-{0}"]}}' . format ( delegate_pubkey )
        plusvote = '{{"votes":["+{0}"]}}' . format ( delegate_pubkey )
        reuse = False
        # type 100 is a forged block
        if e . type != 100 :
            if e . recipientId in voter_dict :
                voter_dict [ e . recipientId ] [ 'balance' ] += e . amount
            if e . senderId in voter_dict :
                voter_dict [ e . senderId ] [ 'balance' ] -= ( e . amount + e . fee )
            if e . senderId in voter_dict and e . type == 3 and plusvote in e . raw :
                voter_dict [ e . senderId ] [ 'status' ] = True
            if e . senderId in voter_dict and e . type == 3 and minvote in e . raw :
                voter_dict [ e . senderId ] [ 'status' ] = False
        elif e . type == 100 :
            if e . recipientId in voter_dict :
                voter_dict [ e . recipientId ] [ 'balance' ] += e . amount + e . fee
    remaining_blocks = len ( blocks ) - block_nr - 1
    for i in range ( remaining_blocks ) :
        for x in chunk_dict :
            voter_dict [ x ] [ 'share' ] += chunk_dict [ x ]
except IndexError :
    pass
for i in voter_dict :
    logger . info ( "{0}  {1}  {2}  {3}  {4}" . format ( i , voter_dict [ i ] [ 'share' ] , voter_dict [ i ] [ 'status' ] , voter_dict [ i ] [ 'last_payout' ] , voter_dict [ i ] [ 'vote_timestamp' ] ) )
return voter_dict , max_timestamp


signature:
def add_source ( self , source_class , * constructor_args ) :
    """Adds a source to the factory provided it ' s type and constructor arguments
 : param source _ class : The class used to instantiate the source
 : type source _ class : type
 : param constructor _ args : Arguments to be passed into the constructor
 : type constructor _ args : Iterable"""

implementation:
if not IIPSource . implementedBy ( source_class ) :
    raise TypeError ( "source_class {} must implement IIPSource" . format ( source_class ) )
else :
    self . _sources . add ( ( source_class , constructor_args ) )


signature:
def get_sources ( self , limit = sys . maxsize , types_list = None ) :
    """Generates instantiated sources from the factory
 : param limit : the max number of sources to yield
 : type limit : int
 : param types _ list : filter by types so the constructor can be used to accomidate many types
 : type types _ list : class or list of classes
 : return : Yields types added by add _ source
 : rtype : generator"""

implementation:
if types_list and not isinstance ( types_list , ( tuple , list ) ) :
    types_list = [ types_list ]
sources = list ( self . _sources )
random . shuffle ( sources )
for source in sources :
    if not types_list or source [ 0 ] in types_list :
        limit -= 1
        yield source [ 0 ] ( * source [ 1 ] )
    if limit <= 0 :
        break


signature:
def value_to_bool ( config_val , evar ) :
    """Massages the ' true ' and ' false ' strings to bool equivalents .
 : param str config _ val : The env var value .
 : param EnvironmentVariable evar : The EVar object we are validating
 a value for .
 : rtype : bool
 : return : True or False , depending on the value ."""

implementation:
if not config_val :
    return False
if config_val . strip ( ) . lower ( ) == 'true' :
    return True
else :
    return False


signature:
def validate_is_not_none ( config_val , evar ) :
    """If the value is ` ` None ` ` , fail validation .
 : param str config _ val : The env var value .
 : param EnvironmentVariable evar : The EVar object we are validating
 a value for .
 : raises : ValueError if the config value is None ."""

implementation:
if config_val is None :
    raise ValueError ( "Value for environment variable '{evar_name}' can't " "be empty." . format ( evar_name = evar . name ) )
return config_val


signature:
def validate_is_boolean_true ( config_val , evar ) :
    """Make sure the value evaluates to boolean True .
 : param str config _ val : The env var value .
 : param EnvironmentVariable evar : The EVar object we are validating
 a value for .
 : raises : ValueError if the config value evaluates to boolean False ."""


signature:
def value_to_python_log_level ( config_val , evar ) :
    """Convert an evar value into a Python logging level constant .
 : param str config _ val : The env var value .
 : param EnvironmentVariable evar : The EVar object we are validating
 a value for .
 : return : A validated string .
 : raises : ValueError if the log level is invalid ."""

implementation:
if not config_val :
    config_val = evar . default_val
config_val = config_val . upper ( )
# noinspection PyProtectedMember
return logging . _checkLevel ( config_val )


signature:
def register_range_type ( pgrange , pyrange , conn ) :
    """Register a new range type as a PostgreSQL range .
 > > > register _ range _ type ( " int4range " , intrange , conn )
 The above will make sure intrange is regarded as an int4range for queries
 and that int4ranges will be cast into intrange when fetching rows .
 pgrange should be the full name including schema for the custom range type .
 Note that adaption is global , meaning if a range type is passed to a regular
 psycopg2 connection it will adapt it to its proper range type . Parsing of
 rows from the database however is not global and just set on a per connection
 basis ."""

implementation:
register_adapter ( pyrange , partial ( adapt_range , pgrange ) )
register_range_caster ( pgrange , pyrange , * query_range_oids ( pgrange , conn ) , scope = conn )


signature:
def get_api_error ( response ) :
    """Acquires the correct error for a given response .
 : param requests . Response response : HTTP error response
 : returns : the appropriate error for a given response
 : rtype : APIError"""

implementation:
error_class = _status_code_to_class . get ( response . status_code , APIError )
return error_class ( response )


signature:
def get_param_values ( request , model = None ) :
    """Converts the request parameters to Python .
 : param request : < pyramid . request . Request > | | < dict >
 : return : < dict >"""

implementation:
if type ( request ) == dict :
    return request
params = get_payload ( request )
# support in - place editing formatted request
try :
    del params [ 'pk' ]
    params [ params . pop ( 'name' ) ] = params . pop ( 'value' )
except KeyError :
    pass
return { k . rstrip ( '[]' ) : safe_eval ( v ) if not type ( v ) == list else [ safe_eval ( sv ) for sv in v ] for k , v in params . items ( ) }


signature:
def get_context ( request , model = None ) :
    """Extracts ORB context information from the request .
 : param request : < pyramid . request . Request >
 : param model : < orb . Model > | | None
 : return : { < str > key : < variant > value } values , < orb . Context >"""

implementation:
# convert request parameters to python
param_values = get_param_values ( request , model = model )
# extract the full orb context if provided
context = param_values . pop ( 'orb_context' , { } )
if isinstance ( context , ( unicode , str ) ) :
    context = projex . rest . unjsonify ( context )
# otherwise , extract the limit information
has_limit = 'limit' in context or 'limit' in param_values
# create the new orb context
orb_context = orb . Context ( ** context )
# build up context information from the request params
used = set ( )
query_context = { }
for key in orb . Context . Defaults :
    if key in param_values :
        used . add ( key )
        query_context [ key ] = param_values . get ( key )
# generate a simple query object
schema_values = { }
if model : # extract match dict items
    for key , value in request . matchdict . items ( ) :
        if model . schema ( ) . column ( key , raise_ = False ) :
            schema_values [ key ] = value
    # extract payload items
    for key , value in param_values . items ( ) :
        root_key = key . split ( '.' ) [ 0 ]
        schema_object = model . schema ( ) . column ( root_key , raise_ = False ) or model . schema ( ) . collector ( root_key )
        if schema_object :
            value = param_values . pop ( key )
            if isinstance ( schema_object , orb . Collector ) and type ( value ) not in ( tuple , list ) :
                value = [ value ]
            schema_values [ key ] = value
# generate the base context information
query_context [ 'scope' ] = { 'request' : request }
# include any request specific scoping or information from the request
# first , look for default ORB context for all requests
try :
    default_context = request . orb_default_context
# then , look for scope specific information for all requests
except AttributeError :
    try :
        query_context [ 'scope' ] . update ( request . orb_scope )
    except AttributeError :
        pass
# if request specific context defaults exist , then
# merge them with the rest of the query context
else :
    if 'scope' in default_context :
        query_context [ 'scope' ] . update ( default_context . pop ( 'scope' ) )
    # setup defaults based on the request
    for k , v in default_context . items ( ) :
        query_context . setdefault ( k , v )
orb_context . update ( query_context )
return schema_values , orb_context


signature:
def _real_time_thread ( self ) :
    """Handles real - time updates to the order book ."""

implementation:
while self . ws_client . connected ( ) :
    if self . die :
        break
    if self . pause :
        sleep ( 5 )
        continue
    message = self . ws_client . receive ( )
    if message is None :
        break
    message_type = message [ 'type' ]
    if message_type == 'error' :
        continue
    if message [ 'sequence' ] <= self . sequence :
        continue
    if message_type == 'open' :
        self . _handle_open ( message )
    elif message_type == 'match' :
        self . _handle_match ( message )
    elif message_type == 'done' :
        self . _handle_done ( message )
    elif message_type == 'change' :
        self . _handle_change ( message )
    else :
        continue
self . ws_client . disconnect ( )


signature:
def _keep_alive_thread ( self ) :
    """Used exclusively as a thread which keeps the WebSocket alive ."""

implementation:
while True :
    with self . _lock :
        if self . connected ( ) :
            self . _ws . ping ( )
        else :
            self . disconnect ( )
            self . _thread = None
            return
    sleep ( 30 )