idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
51,300
def coffee_compile ( source ) : with open ( COFFEE_COMPILER , 'rb' ) as coffeescript_js : return evaljs ( ( coffeescript_js . read ( ) . decode ( 'utf-8' ) , 'CoffeeScript.compile(dukpy.coffeecode)' ) , coffeecode = source )
Compiles the given source from CoffeeScript to JavaScript
51,301
def register_path ( self , path ) : self . _paths . insert ( 0 , os . path . abspath ( path ) )
Registers a directory where to look for modules .
51,302
def lookup ( self , module_name ) : for search_path in self . _paths : module_path = os . path . join ( search_path , module_name ) new_module_name , module_file = self . _lookup ( module_path , module_name ) if module_file : return new_module_name , module_file return None , None
Searches for a file providing given module .
51,303
def load ( self , module_name ) : module_name , path = self . lookup ( module_name ) if path : with open ( path , 'rb' ) as f : return module_name , f . read ( ) . decode ( 'utf-8' ) return None , None
Returns source code and normalized module id of the given module .
51,304
def less_compile ( source , options = None ) : options = options or { } res = NodeLikeInterpreter ( ) . evaljs ( ( 'var result = null;' 'var less = require("less/less-node");' , 'less.render(dukpy.lesscode, dukpy.lessoptions, function(error, output) {' ' result = {"error": error, "output": output};' '});' 'result;' ) ...
Compiles the given source from LESS to CSS
51,305
def install_jspackage ( package_name , version , modulesdir ) : if not version : version = '' requirements = _resolve_dependencies ( package_name , version ) print ( 'Packages going to be installed: {0}' . format ( ', ' . join ( '{0}->{1}' . format ( * i ) for i in requirements ) ) ) downloads = { } for dependency_name...
Installs a JavaScript package downloaded from npmjs . org .
51,306
def evaljs ( self , code , ** kwargs ) : jsvars = json . dumps ( kwargs ) jscode = self . _adapt_code ( code ) if not isinstance ( jscode , bytes ) : jscode = jscode . encode ( 'utf-8' ) if not isinstance ( jsvars , bytes ) : jsvars = jsvars . encode ( 'utf-8' ) res = _dukpy . eval_string ( self , jscode , jsvars ) if ...
Runs JavaScript code in the context of the interpreter .
51,307
def typescript_compile ( source ) : with open ( TS_COMPILER , 'r' ) as tsservices_js : return evaljs ( ( tsservices_js . read ( ) , 'ts.transpile(dukpy.tscode, {options});' . format ( options = TSC_OPTIONS ) ) , tscode = source )
Compiles the given source from TypeScript to ES5 using TypescriptServices . js
51,308
def get_private_file ( self ) : return PrivateFile ( request = self . request , storage = self . get_storage ( ) , relative_name = self . get_path ( ) )
Return all relevant data in a single object so this is easy to extend and server implementations can pick what they need .
51,309
def get ( self , request , * args , ** kwargs ) : private_file = self . get_private_file ( ) if not self . can_access_file ( private_file ) : return HttpResponseForbidden ( 'Private storage access denied' ) if not private_file . exists ( ) : return self . serve_file_not_found ( private_file ) else : return self . serve...
Handle incoming GET requests
51,310
def serve_file ( self , private_file ) : response = self . server_class ( ) . serve ( private_file ) if self . content_disposition : filename = self . get_content_disposition_filename ( private_file ) response [ 'Content-Disposition' ] = b'; ' . join ( [ self . content_disposition . encode ( ) , self . _encode_filename...
Serve the file that was retrieved from the storage . The relative path can be found with private_file . relative_name .
51,311
def get_content_disposition_filename ( self , private_file ) : return self . content_disposition_filename or os . path . basename ( private_file . relative_name )
Return the filename in the download header .
51,312
def _encode_filename_header ( self , filename ) : user_agent = self . request . META . get ( 'HTTP_USER_AGENT' , None ) if 'WebKit' in user_agent : return u'filename={}' . format ( filename ) . encode ( "utf-8" ) elif 'MSIE' in user_agent : url_encoded = quote ( filename . encode ( "utf-8" ) ) . replace ( 'attachment' ...
The filename encoded to use in a Content - Disposition header .
51,313
def add_no_cache_headers ( func ) : @ wraps ( func ) def _dec ( * args , ** kwargs ) : response = func ( * args , ** kwargs ) response [ 'Expires' ] = 'Thu, 01 Jan 1970 00:00:00 GMT' response [ 'Cache-Control' ] = 'max-age=0, no-cache, must-revalidate, proxy-revalidate' return response return _dec
Makes sure the retrieved file is not cached on disk or cached by proxy servers in between . This would circumvent any checking whether the user may even access the file .
51,314
def readTrainingData ( file_locations , GROUP_LABEL ) : class Mock ( object ) : pass mock_module = Mock ( ) mock_module . PARENT_LABEL = GROUP_LABEL for location in file_locations : with open ( location ) as f : tree = etree . parse ( f ) xml = tree . getroot ( ) for each in data_prep_utils . TrainingData ( xml , mock_...
Used in downstream tests
51,315
def device ( value ) : browser = None for regex , name in BROWSERS : if regex . search ( value ) : browser = name break device = None for regex , name in DEVICES : if regex . search ( value ) : device = name break if browser and device : return _ ( '%(browser)s on %(device)s' ) % { 'browser' : browser , 'device' : devi...
Transform a User Agent into human readable text .
51,316
def location ( value ) : try : location = geoip ( ) and geoip ( ) . city ( value ) except Exception : try : location = geoip ( ) and geoip ( ) . country ( value ) except Exception as e : warnings . warn ( str ( e ) ) location = None if location and location [ 'country_name' ] : if 'city' in location and location [ 'cit...
Transform an IP address into an approximate location .
51,317
def before ( func ) : class BeforeDecorator ( LambdaDecorator ) : def before ( self , event , context ) : return func ( event , context ) return BeforeDecorator
Run a function before the handler is invoked is passed the event & context and must return an event & context too .
51,318
def after ( func ) : class AfterDecorator ( LambdaDecorator ) : def after ( self , retval ) : return func ( retval ) return AfterDecorator
Run a function after the handler is invoked is passed the response and must return an response too .
51,319
def on_exception ( func ) : class OnExceptionDecorator ( LambdaDecorator ) : def on_exception ( self , exception ) : return func ( exception ) return OnExceptionDecorator
Run a function when a handler thows an exception . It s return value is returned to AWS .
51,320
def async_handler ( handler ) : @ wraps ( handler ) def wrapper ( event , context ) : context . loop = asyncio . get_event_loop ( ) return context . loop . run_until_complete ( handler ( event , context ) ) return wrapper
This decorator allows for use of async handlers by automatically running them in an event loop . The loop is added to the context object for if the handler needs it .
51,321
def dump_json_body ( handler ) : @ wraps ( handler ) def wrapper ( event , context ) : response = handler ( event , context ) if 'body' in response : try : response [ 'body' ] = json . dumps ( response [ 'body' ] ) except Exception as exception : return { 'statusCode' : 500 , 'body' : str ( exception ) } return respons...
Automatically serialize response bodies with json . dumps .
51,322
def json_http_resp ( handler ) : @ wraps ( handler ) def wrapper ( event , context ) : response = handler ( event , context ) try : body = json . dumps ( response ) except Exception as exception : return { 'statusCode' : 500 , 'body' : str ( exception ) } return { 'statusCode' : 200 , 'body' : body } return wrapper
Automatically serialize return value to the body of a successfull HTTP response .
51,323
def load_json_body ( handler ) : @ wraps ( handler ) def wrapper ( event , context ) : if isinstance ( event . get ( 'body' ) , str ) : try : event [ 'body' ] = json . loads ( event [ 'body' ] ) except : return { 'statusCode' : 400 , 'body' : 'BAD REQUEST' } return handler ( event , context ) return wrapper
Automatically deserialize event bodies with json . loads .
51,324
def json_schema_validator ( request_schema = None , response_schema = None ) : def wrapper_wrapper ( handler ) : @ wraps ( handler ) def wrapper ( event , context ) : if request_schema is not None : if jsonschema is None : logger . error ( 'jsonschema is not installed, skipping request validation' ) else : try : jsonsc...
Validate your request & response payloads against a JSONSchema .
51,325
def no_retry_on_failure ( handler ) : seen_request_ids = set ( ) @ wraps ( handler ) def wrapper ( event , context ) : if context . aws_request_id in seen_request_ids : logger . critical ( 'Retry attempt on request id %s detected.' , context . aws_request_id ) return { 'statusCode' : 200 } seen_request_ids . add ( cont...
AWS Lambda retries scheduled lambdas that don t execute succesfully .
51,326
def _wrap_thing ( self , thing , kind ) : thing [ 'created' ] = self . _epoch_utc_to_local ( thing [ 'created_utc' ] ) thing [ 'd_' ] = copy . deepcopy ( thing ) ThingType = namedtuple ( kind , thing . keys ( ) ) thing = ThingType ( ** thing ) return thing
Mimic praw . Submission and praw . Comment API
51,327
def _add_nec_args ( self , payload ) : if self . _limited ( payload ) : return if 'limit' not in payload : payload [ 'limit' ] = self . max_results_per_request if 'sort' not in payload : payload [ 'sort' ] = 'desc' if 'filter' in payload : if not isinstance ( payload [ 'filter' ] , list ) : if isinstance ( payload [ 'f...
Adds limit and created_utc arguments to the payload as necessary .
51,328
def pretty_path ( path ) : path = fmt . to_utf8 ( path ) home_dir = os . path . expanduser ( "~" ) if path . startswith ( home_dir ) : path = "~" + path [ len ( home_dir ) : ] return '"%s"' % ( path , )
Prettify path for logging .
51,329
def guarded ( self , call , * args ) : self . LOG . debug ( '%s(%s)' % ( call . __name__ , ', ' . join ( [ pretty_path ( i ) for i in args ] ) , ) ) if not self . options . dry_run : try : call ( * args ) except ( EnvironmentError , UnicodeError ) as exc : self . fatal ( '%s(%s) failed [%s]' % ( call . __name__ , ', ' ...
Catch exceptions thrown by filesystem calls and don t really execute them in dry - run mode .
51,330
def run ( ) : logging . basicConfig ( level = logging . DEBUG ) load_config . ConfigLoader ( ) . load ( ) config . debug = True print ( repr ( config . engine . item ( sys . argv [ 1 ] ) ) )
Module level test .
51,331
def _make_it_so ( self , command , calls , * args , ** kwargs ) : observer = kwargs . pop ( 'observer' , False ) args = ( self . _fields [ "hash" ] , ) + args try : for call in calls : self . _engine . LOG . debug ( "%s%s torrent #%s (%s)" % ( command [ 0 ] . upper ( ) , command [ 1 : ] , self . _fields [ "hash" ] , ca...
Perform some error - checked XMLRPC calls .
51,332
def fetch ( self , name , engine_name = None ) : try : return self . _fields [ name ] except KeyError : if isinstance ( name , ( int , long ) ) : name = "custom_%d" % name if name == "done" : val = float ( self . fetch ( "completed_chunks" ) ) / self . fetch ( "size_chunks" ) elif name == "files" : val = self . _get_fi...
Get a field on demand .
51,333
def datapath ( self ) : path = self . _fields [ 'path' ] if not path : path = self . fetch ( 'directory' ) if path and not self . _fields [ 'is_multi_file' ] : path = os . path . join ( path , self . _fields [ 'name' ] ) return os . path . expanduser ( fmt . to_unicode ( path ) )
Get an item s data path .
51,334
def announce_urls ( self , default = [ ] ) : try : response = self . _engine . _rpc . t . multicall ( self . _fields [ "hash" ] , 0 , "t.url=" , "t.is_enabled=" ) except xmlrpc . ERRORS as exc : raise error . EngineError ( "While getting announce URLs for #%s: %s" % ( self . _fields [ "hash" ] , exc ) ) if response : r...
Get a list of all announce URLs . Returns default if no trackers are found at all .
51,335
def tag ( self , tags ) : tags = tags . lower ( ) previous = self . tagged tagset = previous . copy ( ) for tag in tags . replace ( ',' , ' ' ) . split ( ) : if tag . startswith ( '-' ) : tagset . discard ( tag [ 1 : ] ) elif tag . startswith ( '+' ) : tagset . add ( tag [ 1 : ] ) else : tagset . add ( tag ) tagset . d...
Add or remove tags .
51,336
def set_throttle ( self , name ) : if name . lower ( ) == "null" : name = "NULL" if name . lower ( ) == "none" : name = '' if name not in self . _engine . known_throttle_names : if self . _engine . _rpc . throttle . up . max ( xmlrpc . NOHASH , name ) == - 1 : if self . _engine . _rpc . throttle . down . max ( xmlrpc ....
Assign to throttle group .
51,337
def purge ( self ) : def partial_file ( item ) : "Filter out partial files" return item . completed_chunks < item . size_chunks self . cull ( file_filter = partial_file , attrs = [ "get_completed_chunks" , "get_size_chunks" ] )
Delete PARTIAL data files and remove torrent from client .
51,338
def load_config ( self , namespace = None , rcfile = None ) : if namespace is None : namespace = config if namespace . scgi_url : return if not rcfile : rcfile = getattr ( config , "rtorrent_rc" , None ) if not rcfile : raise error . UserError ( "No 'rtorrent_rc' path defined in configuration!" ) if not os . path . isf...
Load file given in rcfile .
51,339
def _resolve_viewname ( self , viewname ) : if viewname == "-" : try : viewname = self . open ( ) . ui . current_view ( ) except xmlrpc . ERRORS as exc : raise error . EngineError ( "Can't get name of current view: %s" % ( exc ) ) return viewname
Check for special view names and return existing rTorrent one .
51,340
def open ( self ) : if self . _rpc is not None : return self . _rpc self . load_config ( ) if not config . scgi_url : raise error . UserError ( "You need to configure a XMLRPC connection, read" " https://pyrocore.readthedocs.io/en/latest/setup.html" ) self . _rpc = xmlrpc . RTorrentProxy ( config . scgi_url ) self . ve...
Open connection .
51,341
def multicall ( self , viewname , fields ) : commands = tuple ( 'd.{}=' . format ( x ) for x in fields ) result_type = namedtuple ( 'DownloadItem' , [ x . replace ( '.' , '_' ) for x in fields ] ) items = self . open ( ) . d . multicall ( viewname , * commands ) return [ result_type ( * x ) for x in items ]
Query the given fields of items in the given view .
51,342
def item ( self , infohash , prefetch = None , cache = False ) : return next ( self . items ( infohash , prefetch , cache ) )
Fetch a single item by its info hash .
51,343
def _load_rules ( self ) : for ruleset in self . active_rulesets : section_name = 'sweep_rules_' + ruleset . lower ( ) try : ruledefs = getattr ( self . config , section_name ) except AttributeError : raise error . UserError ( "There is no [{}] section in your configuration" . format ( section_name . upper ( ) ) ) for ...
Load rule definitions from config .
51,344
def _parse_schedule ( self , schedule ) : result = { } for param in shlex . split ( str ( schedule ) ) : try : key , val = param . split ( '=' , 1 ) except ( TypeError , ValueError ) : self . fatal ( "Bad param '%s' in job schedule '%s'" % ( param , schedule ) ) else : result [ key ] = val return result
Parse a job schedule .
51,345
def _validate_config ( self ) : groups = dict ( job = defaultdict ( Bunch ) , httpd = defaultdict ( Bunch ) , ) for key , val in config . torque . items ( ) : if val . isdigit ( ) : config . torque [ key ] = val = int ( val ) elif val . lower ( ) in ( matching . TRUE | matching . FALSE ) : val = matching . truth ( str ...
Handle and check configuration .
51,346
def _add_jobs ( self ) : for name , params in self . jobs . items ( ) : if params . active : params . handler = params . handler ( params ) self . sched . add_cron_job ( params . handler . run , ** params . schedule )
Add configured jobs .
51,347
def _init_wsgi_server ( self ) : self . wsgi_server = None if self . httpd . active : from waitress . server import WSGIServer from pyrocore . daemon import webapp wsgi_app = webapp . make_app ( self . httpd ) self . LOG . debug ( "Waitress config: %r" % self . httpd . waitress ) self . wsgi_server = WSGIServer ( wsgi_...
Set up WSGI HTTP server .
51,348
def _run_forever ( self ) : while True : try : tick = time . time ( ) asyncore . loop ( timeout = self . POLL_TIMEOUT , use_poll = True ) tick += self . POLL_TIMEOUT - time . time ( ) if tick > 0 : time . sleep ( min ( tick , self . POLL_TIMEOUT ) ) except KeyboardInterrupt as exc : self . LOG . info ( "Termination req...
Run configured jobs until termination request .
51,349
def read_blob ( arg ) : result = None if arg == '@-' : result = sys . stdin . read ( ) elif any ( arg . startswith ( '@{}://' . format ( x ) ) for x in { 'http' , 'https' , 'ftp' , 'file' } ) : if not requests : raise error . UserError ( "You must 'pip install requests' to support @URL arguments." ) try : response = re...
Read a BLOB from given
51,350
def open ( self ) : if not self . proxy : if not config . scgi_url : config . engine . load_config ( ) if not config . scgi_url : self . LOG . error ( "You need to configure a XMLRPC connection, read" " https://pyrocore.readthedocs.io/en/latest/setup.html" ) self . proxy = xmlrpc . RTorrentProxy ( config . scgi_url ) s...
Open connection and return proxy .
51,351
def execute ( self , proxy , method , args ) : try : result = getattr ( proxy , method ) ( raw_xml = self . options . xml , * tuple ( args ) ) except xmlrpc . ERRORS as exc : self . LOG . error ( "While calling %s(%s): %s" % ( method , ", " . join ( repr ( i ) for i in args ) , exc ) ) self . return_code = error . EX_N...
Execute given XMLRPC call .
51,352
def do_repl ( self ) : from prompt_toolkit import prompt from prompt_toolkit . history import FileHistory from prompt_toolkit . auto_suggest import AutoSuggestFromHistory from prompt_toolkit . contrib . completers import WordCompleter self . options . quiet = False proxy = self . open ( ) ps1 = proxy . session . name (...
REPL for rTorrent XMLRPC commands .
51,353
def do_import ( self ) : tmp_import = None try : if self . args [ 0 ] . startswith ( '@' ) and self . args [ 0 ] != '@-' : import_file = os . path . expanduser ( self . args [ 0 ] [ 1 : ] ) if not os . path . isfile ( import_file ) : self . parser . error ( "File not found (or not a file): {}" . format ( import_file ) ...
Handle import files or streams passed with - i .
51,354
def do_command ( self ) : method = self . args [ 0 ] raw_args = self . args [ 1 : ] if '=' in method : if raw_args : self . parser . error ( "Please don't mix rTorrent and shell argument styles!" ) method , raw_args = method . split ( '=' , 1 ) raw_args = raw_args . split ( ',' ) self . execute ( self . open ( ) , meth...
Call a single command with arguments .
51,355
def download_resource ( self , download_url , target , guard ) : download_url = download_url . strip ( ) if not os . path . isabs ( target ) : target = os . path . join ( config . config_dir , target ) if os . path . exists ( os . path . join ( target , guard ) ) : self . LOG . info ( "Already have '%s' in '%s'..." % (...
Helper to download and install external resources .
51,356
def fmt_duration ( secs ) : return ' ' . join ( fmt . human_duration ( secs , 0 , precision = 2 , short = True ) . strip ( ) . split ( ) )
Format a duration in seconds .
51,357
def disk_free ( path ) : stats = os . statvfs ( path ) return stats . f_bavail * stats . f_frsize
Return free bytes on partition holding path .
51,358
def truth ( val , context ) : try : 0 + val except TypeError : lower_val = val . lower ( ) if lower_val in TRUE : return True elif lower_val in FALSE : return False else : raise FilterError ( "Bad boolean value %r in %r (expected one of '%s', or '%s')" % ( val , context , "' '" . join ( TRUE ) , "' '" . join ( FALSE ) ...
Convert truth value in val to a boolean .
51,359
def _time_ym_delta ( timestamp , delta , months ) : timestamp = list ( time . localtime ( timestamp ) ) timestamp [ int ( months ) ] += delta return time . mktime ( timestamp )
Helper to add a year or month delta to a timestamp .
51,360
def unquote_pre_filter ( pre_filter , _regex = re . compile ( r'[\\]+' ) ) : if pre_filter . startswith ( '"' ) and pre_filter . endswith ( '"' ) : pre_filter = pre_filter [ 1 : - 1 ] pre_filter = _regex . sub ( lambda x : x . group ( 0 ) [ : len ( x . group ( 0 ) ) // 2 ] , pre_filter ) return pre_filter
Unquote a pre - filter condition .
51,361
def _create_filter ( self , condition ) : comparison = re . match ( r"^(%s)(<[>=]?|>=?|!=|~)(.*)$" % self . ident_re , condition ) if comparison : name , comparison , values = comparison . groups ( ) if values and values [ 0 ] in "+-" : raise FilterError ( "Comparison operator cannot be followed by '%s' in '%s'" % ( va...
Create a filter object from a textual condition .
51,362
def parse ( self , conditions ) : conditions_text = conditions try : conditions = shlex . split ( fmt . to_utf8 ( conditions ) ) except AttributeError : conditions_text = self . _tree2str ( conditions ) if not conditions : raise FilterError ( "No conditions given at all!" ) negate = conditions [ : 1 ] == [ "NOT" ] if n...
Parse filter conditions .
51,363
def _flux_engine_data ( engine ) : data = stats . engine_data ( engine ) data [ "up_rate" ] = data [ "upload" ] [ 0 ] data [ "up_limit" ] = data [ "upload" ] [ 1 ] data [ "down_rate" ] = data [ "download" ] [ 0 ] data [ "down_limit" ] = data [ "download" ] [ 1 ] data [ "version" ] = data [ "versions" ] [ 0 ] views = da...
Return rTorrent data set for pushing to InfluxDB .
51,364
def run ( self ) : try : proxy = config_ini . engine . open ( ) self . LOG . info ( "Stats for %s - up %s, %s" % ( config_ini . engine . engine_id , fmt . human_duration ( proxy . system . time ( ) - config_ini . engine . startup , 0 , 2 , True ) . strip ( ) , proxy ) ) except ( error . LoggableError , xmlrpc . ERRORS ...
Statistics logger job callback .
51,365
def _influxdb_url ( self ) : url = "{0}/db/{1}/series" . format ( self . influxdb . url . rstrip ( '/' ) , self . config . dbname ) if self . influxdb . user and self . influxdb . password : url += "?u={0}&p={1}" . format ( self . influxdb . user , self . influxdb . password ) return url
Return REST API URL to access time series .
51,366
def _push_data ( self ) : if not ( self . config . series or self . config . series_host ) : self . LOG . info ( "Misconfigured InfluxDB job, neither 'series' nor 'series_host' is set!" ) return fluxdata = [ ] if self . config . series : try : config_ini . engine . open ( ) data , views = _flux_engine_data ( config_ini...
Push stats data to InfluxDB .
51,367
def run ( self ) : from pyrocore import config try : config . engine . open ( ) items = [ ] self . run_filter ( items ) except ( error . LoggableError , xmlrpc . ERRORS ) as exc : self . LOG . warn ( str ( exc ) )
Filter job callback .
51,368
def replace_fields ( meta , patterns ) : for pattern in patterns : try : field , regex , subst , _ = pattern . split ( pattern [ - 1 ] ) namespace = meta keypath = [ i . replace ( '\0' , '.' ) for i in field . replace ( '..' , '\0' ) . split ( '.' ) ] for key in keypath [ : - 1 ] : namespace = namespace [ key ] namespa...
Replace patterns in fields .
51,369
def connect ( config_dir = None , optional_config_files = None , cron_cfg = "cron" ) : from pyrocore . scripts . base import ScriptBase from pyrocore . util import load_config ScriptBase . setup ( cron_cfg = cron_cfg ) load_config . ConfigLoader ( config_dir ) . load ( optional_config_files or [ ] ) from pyrocore impor...
Initialize everything for interactive use .
51,370
def setup ( cls , cron_cfg = "cron" ) : random . seed ( ) logging_cfg = cls . LOGGING_CFG if "%s" in logging_cfg : logging_cfg = logging_cfg % ( cron_cfg if "--cron" in sys . argv [ 1 : ] else "scripts" , ) logging_cfg = os . path . expanduser ( logging_cfg ) if os . path . exists ( logging_cfg ) : logging . HERE = os ...
Set up the runtime environment .
51,371
def _get_pkg_meta ( self ) : logger = logging . getLogger ( 'pyrocore.scripts.base.version_info' ) pkg_info = None warnings = [ ] for info_ext , info_name in ( ( '.dist-info' , 'METADATA' ) , ( '.egg-info' , 'PKG-INFO' ) ) : try : pkg_path = os . path . join ( __file__ . split ( __name__ . replace ( '.' , os . sep ) ) ...
Try to find package metadata .
51,372
def add_bool_option ( self , * args , ** kwargs ) : dest = [ o for o in args if o . startswith ( "--" ) ] [ 0 ] . replace ( "--" , "" ) . replace ( "-" , "_" ) self . parser . add_option ( dest = dest , action = "store_true" , default = False , help = kwargs [ 'help' ] , * args )
Add a boolean option .
51,373
def add_value_option ( self , * args , ** kwargs ) : kwargs [ 'metavar' ] = args [ - 1 ] if 'dest' not in kwargs : kwargs [ 'dest' ] = [ o for o in args if o . startswith ( "--" ) ] [ 0 ] . replace ( "--" , "" ) . replace ( "-" , "_" ) if 'default' in kwargs and kwargs [ 'default' ] : kwargs [ 'help' ] += " [%s]" % kwa...
Add a value option .
51,374
def handle_completion ( self ) : if len ( sys . argv ) > 1 and sys . argv [ 1 ] . startswith ( "--help-completion-" ) : handler = getattr ( self , sys . argv [ 1 ] [ 2 : ] . replace ( '-' , '_' ) , None ) if handler : print '\n' . join ( sorted ( handler ( ) ) ) self . STD_LOG_LEVEL = logging . DEBUG sys . exit ( error...
Handle shell completion stuff .
51,375
def help_completion_options ( self ) : for opt in self . parser . option_list : for lopt in opt . _long_opts : yield lopt
Return options of this command .
51,376
def fatal ( self , msg , exc = None ) : if exc is not None : self . LOG . fatal ( "%s (%s)" % ( msg , exc ) ) if self . options . debug : return else : self . LOG . fatal ( msg ) sys . exit ( error . EX_SOFTWARE )
Exit on a fatal error .
51,377
def run ( self ) : log_total = True try : try : self . get_options ( ) self . mainloop ( ) except error . LoggableError , exc : if self . options . debug : raise try : msg = str ( exc ) except UnicodeError : msg = unicode ( exc , "UTF-8" ) self . LOG . error ( msg ) sys . exit ( error . EX_SOFTWARE ) except KeyboardInt...
The main program skeleton .
51,378
def add_options ( self ) : super ( ScriptBaseWithConfig , self ) . add_options ( ) self . add_value_option ( "--config-dir" , "DIR" , help = "configuration directory [{}]" . format ( os . environ . get ( 'PYRO_CONFIG_DIR' , self . CONFIG_DIR_DEFAULT ) ) ) self . add_value_option ( "--config-file" , "PATH" , action = "a...
Add configuration options .
51,379
def check_for_connection ( self ) : for idx , arg in enumerate ( self . args ) : if arg . startswith ( '@' ) : if arg [ 1 : ] not in config . connections : self . parser . error ( "Undefined connection '{}'!" . format ( arg [ 1 : ] ) ) config . scgi_url = config . connections [ arg [ 1 : ] ] self . LOG . debug ( "Switc...
Scan arguments for a
51,380
def quit ( self ) : self . script . LOG . warn ( "Abort due to user choice!" ) sys . exit ( self . QUIT_RC )
Exit the program due to user s choices .
51,381
def redirect ( req , _log = pymagic . get_lazy_logger ( "redirect" ) ) : log = req . environ . get ( "wsgilog.logger" , _log ) target = req . relative_url ( req . urlvars . to ) log . info ( "Redirecting '%s' to '%s'" % ( req . url , target ) ) return exc . HTTPMovedPermanently ( location = target )
Redirect controller to emit a HTTP 301 .
51,382
def make_app ( httpd_config ) : htdocs_paths = [ os . path . realpath ( os . path . join ( config . config_dir , "htdocs" ) ) , os . path . join ( os . path . dirname ( config . __file__ ) , "data" , "htdocs" ) , ] return ( Router ( ) . add_route ( "/" , controller = redirect , to = "/static/index.html" ) . add_route (...
Factory for the monitoring webapp .
51,383
def guarded ( self , func , * args , ** kwargs ) : try : return func ( * args , ** kwargs ) except ( EnvironmentError , error . LoggableError , xmlrpc . ERRORS ) as g_exc : if func . __name__ not in self . ERRORS_LOGGED : self . LOG . warn ( "While calling '%s': %s" % ( func . __name__ , g_exc ) ) self . ERRORS_LOGGED ...
Call a function return None on errors .
51,384
def json_engine ( self , req ) : try : return stats . engine_data ( config . engine ) except ( error . LoggableError , xmlrpc . ERRORS ) as torrent_exc : raise exc . HTTPInternalServerError ( str ( torrent_exc ) )
Return torrent engine data .
51,385
def json_charts ( self , req ) : disk_used , disk_total , disk_detail = 0 , 0 , [ ] for disk_usage_path in self . cfg . disk_usage_path . split ( os . pathsep ) : disk_usage = self . guarded ( psutil . disk_usage , os . path . expanduser ( disk_usage_path . strip ( ) ) ) if disk_usage : disk_used += disk_usage . used d...
Return charting data .
51,386
def parse_route ( cls , template ) : regex = '' last_pos = 0 for match in cls . ROUTES_RE . finditer ( template ) : regex += re . escape ( template [ last_pos : match . start ( ) ] ) var_name = match . group ( 1 ) expr = match . group ( 2 ) or '[^/]+' expr = '(?P<%s>%s)' % ( var_name , expr ) regex += expr last_pos = m...
Parse a route definition and return the compiled regex that matches it .
51,387
def add_route ( self , template , controller , ** kwargs ) : if isinstance ( controller , basestring ) : controller = pymagic . import_name ( controller ) self . routes . append ( ( self . parse_route ( template ) , controller , kwargs ) ) return self
Add a route definition
51,388
def _duration ( start , end ) : if start and end : if start > end : return None else : return end - start elif start : return time . time ( ) - start else : return None
Return time delta .
51,389
def _fmt_files ( filelist ) : depth = max ( i . path . count ( '/' ) for i in filelist ) pad = [ '\uFFFE' ] * depth base_indent = ' ' * 38 indent = 0 result = [ ] prev_path = pad sorted_files = sorted ( ( i . path . split ( '/' ) [ : - 1 ] + pad , i . path . rsplit ( '/' , 1 ) [ - 1 ] , i ) for i in filelist ) for path...
Produce a file listing .
51,390
def detect_traits ( item ) : return traits . detect_traits ( name = item . name , alias = item . alias , filetype = ( list ( item . fetch ( "kind_51" ) ) or [ None ] ) . pop ( ) , )
Build traits list from attributes of the passed item . Currently kind_51 name and alias are considered .
51,391
def add_manifold_attribute ( cls , name ) : if name . startswith ( "custom_" ) : try : return FieldDefinition . FIELDS [ name ] except KeyError : field = OnDemandField ( fmt . to_unicode , name , "custom attribute %r" % name . split ( '_' , 1 ) [ 1 ] , matcher = matching . PatternFilter ) setattr ( cls , name , field )...
Register a manifold engine attribute .
51,392
def add_custom_fields ( cls , * args , ** kw ) : for factory in config . custom_field_factories : for field in factory ( ) : setattr ( cls , field . name , field )
Add any custom fields defined in the configuration .
51,393
def _fetch_items ( self ) : if self . _items is None : self . _items = list ( self . engine . items ( self ) ) return self . _items
Fetch to attribute .
51,394
def _check_hash_view ( self ) : infohash = None if self . viewname . startswith ( '#' ) : infohash = self . viewname [ 1 : ] elif len ( self . viewname ) == 40 : try : int ( self . viewname , 16 ) except ( TypeError , ValueError ) : pass else : infohash = self . viewname return infohash
Return infohash if view name refers to a single item else None .
51,395
def size ( self ) : if self . _check_hash_view ( ) : return 1 else : return self . engine . open ( ) . view . size ( xmlrpc . NOHASH , self . viewname )
Total unfiltered size of view .
51,396
def group_by ( self , fields , items = None ) : result = defaultdict ( list ) if items is None : items = self . items ( ) try : key = operator . attrgetter ( fields + '' ) except TypeError : def key ( obj , names = tuple ( fields ) ) : 'Helper to return group key tuple' return tuple ( getattr ( obj , x ) for x in names...
Returns a dict of lists of items grouped by the given fields .
51,397
def _set_mappings ( self ) : try : self . _versions = ( self . system . client_version ( ) , self . system . library_version ( ) , ) self . _version_info = tuple ( int ( i ) for i in self . _versions [ 0 ] . split ( '.' ) ) self . _use_deprecated = self . _version_info < ( 0 , 8 , 7 ) self . _mapping = self . _mapping ...
Set command mappings according to rTorrent version .
51,398
def _fix_mappings ( self ) : self . _mapping . update ( ( key + '=' , val + '=' ) for key , val in self . _mapping . items ( ) if not key . endswith ( '=' ) ) if config . debug : self . LOG . debug ( "CMD MAPPINGS ARE: %r" % ( self . _mapping , ) )
Add computed stuff to mappings .
51,399
def _map_call ( self , cmd ) : if config . debug and cmd != self . _mapping . get ( cmd , cmd ) : self . LOG . debug ( "MAP %s ==> %s" % ( cmd , self . _mapping [ cmd ] ) ) cmd = self . _mapping . get ( cmd , cmd ) if not self . _use_deprecated and any ( cmd . startswith ( i ) for i in ( "d.get_" , "f.get_" , "p.get_" ...
Map old to new command names .