idx
int64
0
251k
question
stringlengths
53
3.53k
target
stringlengths
5
1.23k
len_question
int64
20
893
len_target
int64
3
238
3,400
def loadJson ( self , filename ) : jsonConfig = { } if os . path . isfile ( filename ) : jsonConfig = json . loads ( ' ' . join ( open ( filename , 'r' ) . readlines ( ) ) ) return jsonConfig
Read parse and return given Json config file
56
9
3,401
def get_readonly_fields ( self , request , obj = None ) : if obj is None : return [ ] return super ( ExportAdmin , self ) . get_readonly_fields ( request , obj )
The model can t be changed once the export is created
45
11
3,402
def response_add ( self , request , obj , post_url_continue = POST_URL_CONTINUE ) : if '_addanother' not in request . POST and '_popup' not in request . POST : request . POST [ '_continue' ] = 1 return super ( ExportAdmin , self ) . response_add ( request , obj , post_url_continue )
If we re adding save must be save and continue editing
83
11
3,403
def above_score_threshold ( new_data , old_data , strict = False , threshold = PYLINT_SCORE_THRESHOLD ) : success = True score = 0 message = '' if strict : for fscore , fname in new_data [ 'scores' ] : if fscore < threshold : success = False score = - 1 message += "File {} score ({}) below threshold {}\n" . format ( fname , fscore , threshold ) return success , score , message else : if new_data [ 'average' ] < threshold : success = False message = "Failed! Average pylint score ({})" " below threshold (9)!" . format ( new_data [ 'average' ] ) score = - 1 return success , score , message
Verifies that the pylint score is above a given threshold .
168
14
3,404
def run ( self , new_pylint_data , old_pylint_data ) : for validator in self . checkers : success , score , message = validator ( new_pylint_data , old_pylint_data ) if not success : return score , message message = self . default_message . format ( new_pylint_data [ 'average' ] ) return self . default_score , message
Run the new pylint data through given all current checkers including comparisons to old pylint data .
95
22
3,405
def _get_document ( self , source ) : scheme_url = source if not source . startswith ( "http" ) : scheme_url = "http://%s" % source text = source try : text = urllib . urlopen ( scheme_url ) . read ( ) except : pass else : return ( text , scheme_url ) try : text = open ( source , "r" ) . read ( ) except : pass else : return ( text , source ) return ( text , None )
helper open a file or url and return the content and identifier
109
13
3,406
def select_random ( ports = None , exclude_ports = None ) : if ports is None : ports = available_good_ports ( ) if exclude_ports is None : exclude_ports = set ( ) ports . difference_update ( set ( exclude_ports ) ) for port in random . sample ( ports , min ( len ( ports ) , 100 ) ) : if not port_is_used ( port ) : return port raise PortForException ( "Can't select a port" )
Returns random unused port number .
103
6
3,407
def good_port_ranges ( ports = None , min_range_len = 20 , border = 3 ) : min_range_len += border * 2 if ports is None : ports = available_ports ( ) ranges = utils . to_ranges ( list ( ports ) ) lenghts = sorted ( [ ( r [ 1 ] - r [ 0 ] , r ) for r in ranges ] , reverse = True ) long_ranges = [ l [ 1 ] for l in lenghts if l [ 0 ] >= min_range_len ] without_borders = [ ( low + border , high - border ) for low , high in long_ranges ] return without_borders
Returns a list of good port ranges . Such ranges are large and don t contain ephemeral or well - known ports . Ranges borders are also excluded .
149
32
3,408
def port_is_used ( port , host = '127.0.0.1' ) : unused = _can_bind ( port , host ) and _refuses_connection ( port , host ) return not unused
Returns if port is used . Port is considered used if the current process can t bind to it or the port doesn t refuse connections .
47
27
3,409
def current_day ( ) : aoc_now = datetime . datetime . now ( tz = AOC_TZ ) if aoc_now . month != 12 : log . warning ( "current_day is only available in December (EST)" ) return 1 day = min ( aoc_now . day , 25 ) return day
Most recent day if it s during the Advent of Code . Happy Holidays! Day 1 is assumed otherwise .
73
22
3,410
def register_linter ( linter ) : if hasattr ( linter , "EXTS" ) and hasattr ( linter , "run" ) : LintFactory . PLUGINS . append ( linter ) else : raise LinterException ( "Linter does not have 'run' method or EXTS variable!" )
Register a Linter class for file verification .
70
9
3,411
def escape_query ( query ) : return query . replace ( "\\" , r"\5C" ) . replace ( "*" , r"\2A" ) . replace ( "(" , r"\28" ) . replace ( ")" , r"\29" )
Escapes certain filter characters from an LDAP query .
62
11
3,412
def _validate_xor_args ( self , p ) : if len ( p [ 1 ] ) != 2 : raise ValueError ( 'Invalid syntax: XOR only accepts 2 arguments, got {0}: {1}' . format ( len ( p [ 1 ] ) , p ) )
Raises ValueError if 2 arguments are not passed to an XOR
63
14
3,413
def _match_value_filter ( self , p , value ) : return self . _VALUE_FILTER_MAP [ p [ 0 ] ] ( value [ p [ 1 ] ] , p [ 2 ] )
Returns True of False if value in the pattern p matches the filter .
45
14
3,414
def get_field_keys ( self , pattern = None ) : # Use own pattern or passed in argument for recursion pattern = pattern or self . pattern # Validate the pattern so we can make assumptions about the data self . _validate ( pattern ) keys = set ( ) # Valid pattern length can only be 2 or 3 # With key filters, field key is second item just like 3 item patterns if len ( pattern ) == 2 and pattern [ 0 ] not in self . _KEY_FILTER_MAP : if pattern [ 0 ] in ( '&' , '|' , '^' ) : # Pass each nested pattern to get_field_keys for filter_item in pattern [ 1 ] : keys = keys . union ( self . get_field_keys ( filter_item ) ) else : # pattern[0] == '!' keys = keys . union ( self . get_field_keys ( pattern [ 1 ] ) ) else : # Pattern length is 3 keys . add ( pattern [ 1 ] ) return keys
Builds a set of all field keys used in the pattern including nested fields .
215
16
3,415
def to_file ( self , path ) : xmp_path = path + '.xmp' # remove any existing XMP file if os . path . exists ( xmp_path ) : os . unlink ( xmp_path ) # attempt to open image/video file for metadata md_path = path md = GExiv2 . Metadata ( ) try : md . open_path ( md_path ) except GLib . GError : # file type does not support metadata so use XMP sidecar md_path = xmp_path # create empty XMP file with open ( md_path , 'w' ) as of : of . write ( '''<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0-Exiv2"> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool=""/> </rdf:RDF> </x:xmpmeta> <?xpacket end="w"?>''' ) md = GExiv2 . Metadata ( ) md . open_path ( md_path ) # add our namespace md . register_xmp_namespace ( 'https://github.com/jim-easterbrook/pyctools' , 'pyctools' ) # copy metadata for tag , value in self . data . items ( ) : if md . get_tag_type ( tag ) in ( 'XmpBag' , 'XmpSeq' ) : md . set_tag_multiple ( tag , value ) else : md . set_tag_string ( tag , value ) if self . comment is not None : md . set_comment ( self . comment ) # save file md . save_file ( md_path )
Write metadata to an image video or XMP sidecar file .
471
13
3,416
def image_size ( self ) : xlen = None ylen = None for tag in ( 'Xmp.pyctools.xlen' , 'Exif.Photo.PixelXDimension' , 'Exif.Image.ImageWidth' , 'Xmp.tiff.ImageWidth' ) : if tag in self . data : xlen = int ( self . data [ tag ] ) break for tag in ( 'Xmp.pyctools.ylen' , 'Exif.Photo.PixelYDimension' , 'Exif.Image.ImageLength' , 'Xmp.tiff.ImageLength' ) : if tag in self . data : ylen = int ( self . data [ tag ] ) break if xlen and ylen : return xlen , ylen raise RuntimeError ( 'Metadata does not have image dimensions' )
Get image dimensions from metadata .
183
6
3,417
def get ( self , tag , default = None ) : full_tag = 'Xmp.pyctools.' + tag if full_tag in self . data : return self . data [ full_tag ] return default
Get a metadata value .
46
5
3,418
def set ( self , tag , value ) : full_tag = 'Xmp.pyctools.' + tag self . data [ full_tag ] = value
Set a metadata value .
34
5
3,419
def get_connection ( self , is_read_only = False ) -> redis . StrictRedis : if self . connection is not None : return self . connection if self . is_sentinel : kwargs = dict ( ) if self . password : kwargs [ "password" ] = self . password sentinel = Sentinel ( [ ( self . host , self . port ) ] , * * kwargs ) if is_read_only : connection = sentinel . slave_for ( self . sentinel_service , decode_responses = True ) else : connection = sentinel . master_for ( self . sentinel_service , decode_responses = True ) else : connection = redis . StrictRedis ( host = self . host , port = self . port , decode_responses = True , password = self . password ) self . connection = connection return connection
Gets a StrictRedis connection for normal redis or for redis sentinel based upon redis mode in configuration .
190
26
3,420
def calculate_hash_for_file ( name ) : longlongformat = 'q' # long long bytesize = struct . calcsize ( longlongformat ) f = open ( name , "rb" ) filesize = os . path . getsize ( name ) hash = filesize minimum_size = 65536 * 2 assert filesize >= minimum_size , 'Movie {name} must have at least {min} bytes' . format ( min = minimum_size , name = name ) for x in range ( 65536 // bytesize ) : buffer = f . read ( bytesize ) ( l_value , ) = struct . unpack ( longlongformat , buffer ) hash += l_value hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number f . seek ( max ( 0 , filesize - 65536 ) , 0 ) for x in range ( 65536 // bytesize ) : buffer = f . read ( bytesize ) ( l_value , ) = struct . unpack ( longlongformat , buffer ) hash += l_value hash = hash & 0xFFFFFFFFFFFFFFFF f . close ( ) returnedhash = "%016x" % hash return returnedhash
Calculates the hash for the given filename .
253
10
3,421
def set_config ( self , config ) : # put copy of config on queue for running component self . _configmixin_queue . append ( copy . deepcopy ( config ) ) # notify component, using thread safe method self . new_config ( )
Update the component s configuration .
54
6
3,422
def GaussianFilterCore ( x_sigma = 0.0 , y_sigma = 0.0 ) : def filter_1D ( sigma ) : alpha = 1.0 / ( 2.0 * ( max ( sigma , 0.0001 ) ** 2.0 ) ) coefs = [ ] coef = 1.0 while coef > 0.0001 : coefs . append ( coef ) coef = math . exp ( - ( alpha * ( float ( len ( coefs ) ** 2 ) ) ) ) fil_dim = len ( coefs ) - 1 result = numpy . zeros ( 1 + ( fil_dim * 2 ) , dtype = numpy . float32 ) for n , coef in enumerate ( coefs ) : result [ fil_dim - n ] = coef result [ fil_dim + n ] = coef # normalise result result /= result . sum ( ) return result x_sigma = max ( x_sigma , 0.0 ) y_sigma = max ( y_sigma , 0.0 ) x_fil = filter_1D ( x_sigma ) y_fil = filter_1D ( y_sigma ) result = numpy . empty ( [ y_fil . shape [ 0 ] , x_fil . shape [ 0 ] , 1 ] , dtype = numpy . float32 ) for y in range ( y_fil . shape [ 0 ] ) : for x in range ( x_fil . shape [ 0 ] ) : result [ y , x , 0 ] = x_fil [ x ] * y_fil [ y ] out_frame = Frame ( ) out_frame . data = result out_frame . type = 'fil' audit = out_frame . metadata . get ( 'audit' ) audit += 'data = GaussianFilter()\n' if x_sigma != 0.0 : audit += ' x_sigma: %g\n' % ( x_sigma ) if y_sigma != 0.0 : audit += ' y_sigma: %g\n' % ( y_sigma ) out_frame . metadata . set ( 'audit' , audit ) return out_frame
Gaussian filter generator core .
485
6
3,423
def queue_command ( self , command ) : if self . _running : # queue event normally QtCore . QCoreApplication . postEvent ( self , ActionEvent ( command ) , QtCore . Qt . LowEventPriority ) else : # save event until we are started self . _incoming . append ( command )
Put a command on the queue to be called in the component s thread .
67
15
3,424
def join ( self , timeout = 3600 ) : start = time . time ( ) while self . _running : now = time . time ( ) maxtime = timeout + start - now if maxtime <= 0 : return QCoreApplication . processEvents ( QEventLoop . AllEvents , int ( maxtime * 1000 ) )
Wait until the event loop terminates or timeout is reached .
71
12
3,425
def IntraField ( config = { } ) : return Compound ( config = config , deint = SimpleDeinterlace ( ) , interp = Resize ( ) , filgen = FilterGenerator ( yaperture = 8 , ycut = 50 ) , gain = Arithmetic ( func = 'data * pt_float(2)' ) , linkages = { ( 'self' , 'input' ) : [ ( 'deint' , 'input' ) ] , ( 'deint' , 'output' ) : [ ( 'interp' , 'input' ) ] , ( 'interp' , 'output' ) : [ ( 'self' , 'output' ) ] , ( 'filgen' , 'output' ) : [ ( 'gain' , 'input' ) ] , ( 'gain' , 'output' ) : [ ( 'interp' , 'filter' ) ] , } )
Intra field interlace to sequential converter .
197
9
3,426
def create ( self , period : int , limit : int ) : self . period = period self . limit = limit
Creates a rate limiting rule with rate limiting period and attempt limit
24
13
3,427
def is_rate_limited ( self , namespace : str ) -> bool : return not self . __can_attempt ( namespace = namespace , add_attempt = False )
Checks if a namespace is already rate limited or not without making any additional attempts
37
16
3,428
def main ( ) : credentials = get_credentials ( ) http = credentials . authorize ( httplib2 . Http ( ) ) service = discovery . build ( 'calendar' , 'v3' , http = http ) now = datetime . datetime . utcnow ( ) . isoformat ( ) + 'Z' # 'Z' indicates UTC time print ( 'Getting the upcoming 10 events' ) eventsResult = service . events ( ) . list ( calendarId = 'primary' , timeMin = now , maxResults = 10 , singleEvents = True , orderBy = 'startTime' ) . execute ( ) events = eventsResult . get ( 'items' , [ ] ) if not events : print ( 'No upcoming events found.' ) for event in events : start = event [ 'start' ] . get ( 'dateTime' , event [ 'start' ] . get ( 'date' ) ) print ( start , event [ 'summary' ] )
Shows basic usage of the Google Calendar API .
207
10
3,429
def fix_list_arguments ( self ) : either = [ list ( c . children ) for c in self . either . children ] for case in either : case = [ c for c in case if case . count ( c ) > 1 ] for a in [ e for e in case if type ( e ) == Argument ] : a . value = [ ] return self
Find arguments that should accumulate values and fix them .
78
10
3,430
def either ( self ) : # Currently the pattern will not be equivalent, but more "narrow", # although good enough to reason about list arguments. if not hasattr ( self , 'children' ) : return Either ( Required ( self ) ) else : ret = [ ] groups = [ [ self ] ] while groups : children = groups . pop ( 0 ) types = [ type ( c ) for c in children ] if Either in types : either = [ c for c in children if type ( c ) is Either ] [ 0 ] children . pop ( children . index ( either ) ) for c in either . children : groups . append ( [ c ] + children ) elif Required in types : required = [ c for c in children if type ( c ) is Required ] [ 0 ] children . pop ( children . index ( required ) ) groups . append ( list ( required . children ) + children ) elif Optional in types : optional = [ c for c in children if type ( c ) is Optional ] [ 0 ] children . pop ( children . index ( optional ) ) groups . append ( list ( optional . children ) + children ) elif OneOrMore in types : oneormore = [ c for c in children if type ( c ) is OneOrMore ] [ 0 ] children . pop ( children . index ( oneormore ) ) groups . append ( list ( oneormore . children ) * 2 + children ) else : ret . append ( children ) return Either ( * [ Required ( * e ) for e in ret ] )
Transform pattern into an equivalent with only top - level Either .
322
12
3,431
def syncImage ( img , current , session ) : def _img_str ( i ) : return "%s - %s" % ( i . type , i . description ) for db_img in current . images : img_info = ( img . type , img . md5 , img . size ) db_img_info = ( db_img . type , db_img . md5 , db_img . size ) if db_img_info == img_info : img = None break elif ( db_img . type == img . type and db_img . description == img . description ) : if img . md5 != db_img . md5 : # Update image current . images . remove ( db_img ) current . images . append ( img ) session . add ( current ) pout ( Fg . green ( "Updating image" ) + ": " + _img_str ( img ) ) img = None break if img : # Add image current . images . append ( img ) session . add ( current ) pout ( Fg . green ( "Adding image" ) + ": " + _img_str ( img ) )
Add or updated the Image .
246
6
3,432
def add_fields ( self , form , index ) : super ( ColumnFormSet , self ) . add_fields ( form , index ) form . fields [ 'column' ] . choices = self . get_choices ( )
Filter the form s column choices
48
6
3,433
def find_packages ( ) : packages = [ 'pyctools' ] for root , dirs , files in os . walk ( os . path . join ( 'src' , 'pyctools' ) ) : package = '.' . join ( root . split ( os . sep ) [ 1 : ] ) for name in dirs : packages . append ( package + '.' + name ) return packages
Walk source directory tree and convert each sub directory to a package name .
85
14
3,434
def extract_param ( param , args , type = None ) : parser = argparse . ArgumentParser ( ) parser . add_argument ( '--' + param , type = type ) res , unused = parser . parse_known_args ( args ) return getattr ( res , param ) , unused
From a list of args extract the one param if supplied returning the value and unused args .
63
18
3,435
def get_nltk_builder ( languages ) : all_stemmers = [ ] all_stopwords_filters = [ ] all_word_characters = set ( ) for language in languages : if language == "en" : # use Lunr's defaults all_stemmers . append ( lunr . stemmer . stemmer ) all_stopwords_filters . append ( stop_word_filter ) all_word_characters . update ( { r"\w" } ) else : stopwords , word_characters = _get_stopwords_and_word_characters ( language ) all_stemmers . append ( Pipeline . registered_functions [ "stemmer-{}" . format ( language ) ] ) all_stopwords_filters . append ( generate_stop_word_filter ( stopwords , language = language ) ) all_word_characters . update ( word_characters ) builder = Builder ( ) multi_trimmer = generate_trimmer ( "" . join ( sorted ( all_word_characters ) ) ) Pipeline . register_function ( multi_trimmer , "lunr-multi-trimmer-{}" . format ( "-" . join ( languages ) ) ) builder . pipeline . reset ( ) for fn in chain ( [ multi_trimmer ] , all_stopwords_filters , all_stemmers ) : builder . pipeline . add ( fn ) for fn in all_stemmers : builder . search_pipeline . add ( fn ) return builder
Returns a builder with stemmers for all languages added to it .
326
13
3,436
def register_languages ( ) : for language in set ( SUPPORTED_LANGUAGES ) - { "en" } : language_stemmer = partial ( nltk_stemmer , get_language_stemmer ( language ) ) Pipeline . register_function ( language_stemmer , "stemmer-{}" . format ( language ) )
Register all supported languages to ensure compatibility .
75
8
3,437
def ordinal ( value ) : try : value = int ( value ) except ( TypeError , ValueError ) : raise ValueError if value % 100 in ( 11 , 12 , 13 ) : return '%d%s' % ( value , ORDINAL_SUFFIX [ 0 ] ) else : return '%d%s' % ( value , ORDINAL_SUFFIX [ value % 10 ] )
Converts a number to its ordinal representation .
88
10
3,438
def percentage ( value , digits = 2 ) : value = float ( value ) * 100.0 return u'' + '%s %%' % ( _format ( value , digits ) , )
Converts a fraction to a formatted percentage .
40
9
3,439
def word ( value , digits = 2 ) : convention = locale . localeconv ( ) decimal_point = convention [ 'decimal_point' ] decimal_zero = re . compile ( r'%s0+' % re . escape ( decimal_point ) ) prefix = value < 0 and '-' or '' value = abs ( int ( value ) ) if value < 1000 : return u'' . join ( [ prefix , decimal_zero . sub ( '' , _format ( value , digits ) ) , ] ) for base , suffix in enumerate ( LARGE_NUMBER_SUFFIX ) : exp = ( base + 2 ) * 3 power = 10 ** exp if value < power : value = value / float ( 10 ** ( exp - 3 ) ) return '' . join ( [ prefix , decimal_zero . sub ( '' , _format ( value , digits ) ) , ' ' , suffix , ] ) raise OverflowError
Converts a large number to a formatted number containing the textual suffix for that number .
198
17
3,440
def _full_rename ( args ) : return ( args . ns and all ( map ( args . rename . affects , args . ns ) ) )
Return True only if the arguments passed specify exact namespaces and to conduct a rename of every namespace .
32
20
3,441
def apply ( db , op ) : dbname = op [ 'ns' ] . split ( '.' ) [ 0 ] or "admin" opts = bson . CodecOptions ( uuid_representation = bson . binary . STANDARD ) db [ dbname ] . command ( "applyOps" , [ op ] , codec_options = opts )
Apply operation in db
77
4
3,442
def since ( self , ts ) : spec = { 'ts' : { '$gt' : ts } } cursor = self . query ( spec ) while True : # todo: trap InvalidDocument errors: # except bson.errors.InvalidDocument as e: # logging.info(repr(e)) for doc in cursor : yield doc if not cursor . alive : break time . sleep ( 1 )
Query the oplog for items since ts and then return
86
12
3,443
def has_ops_before ( self , ts ) : spec = { 'ts' : { '$lt' : ts } } return bool ( self . coll . find_one ( spec ) )
Determine if there are any ops before ts
42
10
3,444
def since ( self , ts ) : while True : items = super ( TailingOplog , self ) . since ( ts ) for doc in items : yield doc ts = doc [ 'ts' ]
Tail the oplog starting from ts .
43
10
3,445
def dump ( self , stream ) : items = ( ( 'time' , self . time ) , ( 'inc' , self . inc ) , ) # use ordered dict to retain order ts = collections . OrderedDict ( items ) json . dump ( dict ( ts = ts ) , stream )
Serialize self to text stream .
63
7
3,446
def load ( cls , stream ) : data = json . load ( stream ) [ 'ts' ] return cls ( data [ 'time' ] , data [ 'inc' ] )
Load a serialized version of self from text stream .
40
11
3,447
def for_window ( cls , window ) : utcnow = datetime . datetime . utcnow ( ) return cls ( utcnow - window , 0 )
Given a timedelta window return a timestamp representing that time .
38
12
3,448
def save ( self , ts ) : with open ( self , 'w' ) as f : Timestamp . wrap ( ts ) . dump ( f )
Save timestamp to file .
32
5
3,449
def Tokenizer ( obj , metadata = None , separator = SEPARATOR ) : if obj is None : return [ ] metadata = metadata or { } if isinstance ( obj , ( list , tuple ) ) : return [ Token ( as_string ( element ) . lower ( ) , deepcopy ( metadata ) ) for element in obj ] string = str ( obj ) . strip ( ) . lower ( ) length = len ( string ) tokens = [ ] slice_start = 0 for slice_end in range ( length ) : char = string [ slice_end ] slice_length = slice_end - slice_start if separator . match ( char ) or slice_end == length - 1 : if slice_length > 0 : sl = slice ( slice_start , slice_end if slice_end < length - 1 else None ) token_metadata = { } token_metadata [ "position" ] = [ slice_start , slice_length if slice_end < length - 1 else slice_length + 1 , ] token_metadata [ "index" ] = len ( tokens ) token_metadata . update ( metadata ) tokens . append ( Token ( string [ sl ] , token_metadata ) ) slice_start = slice_end + 1 return tokens
Splits a string into tokens ready to be inserted into the search index .
262
15
3,450
def all_collections ( db ) : include_pattern = r'(?!system\.)' return ( db [ name ] for name in db . list_collection_names ( ) if re . match ( include_pattern , name ) )
Yield all non - sytem collections in db .
51
11
3,451
def safe_purge_collection ( coll ) : op = ( drop_collection if coll . options ( ) . get ( 'capped' , False ) else purge_collection ) return op ( coll )
Cannot remove documents from capped collections in later versions of MongoDB so drop the collection instead .
43
19
3,452
def generate_stop_word_filter ( stop_words , language = None ) : def stop_word_filter ( token , i = None , tokens = None ) : if token and str ( token ) not in stop_words : return token # camelCased for for compatibility with lunr.js label = ( "stopWordFilter-{}" . format ( language ) if language is not None else "stopWordFilter" ) Pipeline . register_function ( stop_word_filter , label ) return stop_word_filter
Builds a stopWordFilter function from the provided list of stop words .
110
15
3,453
def pesn ( number , separator = u'' ) : number = re . sub ( r'[\s-]' , '' , meid ( number ) ) serial = hashlib . sha1 ( unhexlify ( number [ : 14 ] ) ) return separator . join ( [ '80' , serial . hexdigest ( ) [ - 6 : ] . upper ( ) ] )
Printable Pseudo Electronic Serial Number .
85
8
3,454
def filesize ( value , format = 'decimal' , digits = 2 ) : if format not in FILESIZE_SUFFIX : raise TypeError base = FILESIZE_BASE [ format ] size = int ( value ) sign = size < 0 and u'-' or '' size = abs ( size ) for i , suffix in enumerate ( FILESIZE_SUFFIX [ format ] ) : unit = base ** ( i + 1 ) if size < unit : result = u'' . join ( [ sign , _format ( base * size / float ( unit ) , digits ) , u' ' , suffix , ] ) if format == 'gnu' : result = result . replace ( ' ' , '' ) return result raise OverflowError
Convert a file size into natural readable format . Multiple formats are supported .
158
15
3,455
def create_dn_in_filter ( filter_class , filter_value , helper ) : in_filter = FilterFilter ( ) in_filter . AddChild ( create_dn_wcard_filter ( filter_class , filter_value ) ) return in_filter
Creates filter object for given class name and DN values .
57
12
3,456
def get_managed_object ( handle , class_id , params , inMo = None , in_heir = False , dump = False ) : return handle . GetManagedObject ( inMo , class_id , params , inHierarchical = in_heir , dumpXml = dump )
Get the specified MO from UCS Manager .
66
8
3,457
def config_managed_object ( p_dn , p_class_id , class_id , mo_config , mo_dn , handle = None , delete = True ) : if handle is None : handle = self . handle try : result = handle . AddManagedObject ( None , classId = class_id , params = mo_config , modifyPresent = True , dumpXml = YesOrNo . FALSE ) return result except UcsException as ex : print ( _ ( "Cisco client exception: %(msg)s" ) , { 'msg' : ex } ) raise exception . UcsOperationError ( 'config_managed_object' , error = ex )
Configure the specified MO in UCS Manager .
144
9
3,458
def project ( * args , * * kwargs ) : projection = dict ( * args , * * kwargs ) return { key : int ( value ) for key , value in six . iteritems ( projection ) }
Build a projection for MongoDB .
47
7
3,459
def upsert_and_fetch ( coll , doc , * * kwargs ) : return coll . find_one_and_update ( doc , { "$setOnInsert" : doc } , upsert = True , return_document = pymongo . ReturnDocument . AFTER , * * kwargs )
Fetch exactly one matching document or upsert the document if not found returning the matching or upserted document .
67
23
3,460
def update ( self , retry = 2 ) -> None : try : _LOGGER . debug ( "Updating device state." ) key = ON_KEY if not self . _flip_on_off else OFF_KEY self . state = self . _device . readCharacteristic ( HANDLE ) == key except ( bluepy . btle . BTLEException , AttributeError ) : if retry < 1 or not self . _connect ( ) : self . available = False _LOGGER . error ( "Failed to update device state." , exc_info = True ) return None return self . update ( retry - 1 ) self . available = True return None
Synchronize state with switch .
143
7
3,461
def combine ( self , other ) : for term in other . metadata . keys ( ) : if term not in self . metadata : self . metadata [ term ] = { } fields = other . metadata [ term ] . keys ( ) for field in fields : if field not in self . metadata [ term ] : self . metadata [ term ] [ field ] = { } keys = other . metadata [ term ] [ field ] . keys ( ) for key in keys : if key not in self . metadata [ term ] [ field ] : self . metadata [ term ] [ field ] [ key ] = other . metadata [ term ] [ field ] [ key ] else : self . metadata [ term ] [ field ] [ key ] . extend ( other . metadata [ term ] [ field ] [ key ] )
An instance of lunr . MatchData will be created for every term that matches a document .
165
19
3,462
def get_power_state ( self ) : rn_array = [ self . helper . service_profile , ManagedObject ( NamingId . LS_POWER ) . MakeRn ( ) ] try : ls_power = ucs_helper . get_managed_object ( self . helper . handle , LsPower . ClassId ( ) , { LsPower . DN : UcsUtils . MakeDn ( rn_array ) } ) if not ls_power : raise exception . UcsOperationError ( "get_power_state" , "Failed to get LsPower MO, configure valid " "service-profile" ) return ls_power [ 0 ] . getattr ( LsPower . STATE ) except UcsException as ex : raise exception . UcsOperationError ( message = ex )
Get current power state of this node
177
7
3,463
def set_power_state ( self , desired_state ) : rn_array = [ self . helper . service_profile , ManagedObject ( NamingId . LS_POWER ) . MakeRn ( ) ] try : ls_power = ucs_helper . get_managed_object ( self . helper . handle , LsPower . ClassId ( ) , { LsPower . DN : UcsUtils . MakeDn ( rn_array ) } ) if not ls_power : raise exception . UcsOperationError ( "set_power_state" , "Failed to get power MO," " configure valid service-profile." ) else : ls_power_set = self . helper . handle . SetManagedObject ( ls_power , LsPower . ClassId ( ) , { LsPower . STATE : desired_state } , dumpXml = YesOrNo . TRUE ) if ls_power_set : power = ls_power_set . pop ( ) return power . getattr ( LsPower . STATE ) else : return states . ERROR except Exception as ex : raise exception . UcsOperationError ( "set_power_state" , "Failed to get power MO," "configure valid servie-profile." )
Set power state of this node
271
6
3,464
def reboot ( self ) : if self . get_power_state ( ) == LsPower . CONST_STATE_DOWN : self . set_power_state ( LsPower . CONST_STATE_UP ) else : self . set_power_state ( LsPower . CONST_STATE_HARD_RESET_IMMEDIATE )
Hard reset the power of this node .
77
8
3,465
def connect ( uri , factory = pymongo . MongoClient ) : warnings . warn ( "do not use. Just call MongoClient directly." , DeprecationWarning ) return factory ( uri )
Use the factory to establish a connection to uri .
43
11
3,466
def connect_gridfs ( uri , db = None ) : return gridfs . GridFS ( db or connect_db ( uri ) , collection = get_collection ( uri ) or 'fs' , )
Construct a GridFS instance for a MongoDB URI .
46
11
3,467
def Compare ( fromMo , toMo , diff ) : from UcsBase import UcsUtils if ( fromMo . classId != toMo . classId ) : return CompareStatus . TypesDifferent for prop in UcsUtils . GetUcsPropertyMetaAttributeList ( str ( fromMo . classId ) ) : propMeta = UcsUtils . IsPropertyInMetaIgnoreCase ( fromMo . classId , prop ) if propMeta != None : if ( ( propMeta . access == UcsPropertyMeta . Internal ) or ( propMeta . access == UcsPropertyMeta . ReadOnly ) or ( prop in toMo . _excludePropList ) ) : continue if ( ( toMo . __dict__ . has_key ( prop ) ) and ( fromMo . getattr ( prop ) != toMo . getattr ( prop ) ) ) : diff . append ( prop ) if ( len ( diff ) > 0 ) : return CompareStatus . PropsDifferent return CompareStatus . Equal
Internal method to support CompareManagedObject functionality .
211
10
3,468
def TranslateManagedObject ( mObj , xlateOrg , xlateMap ) : from UcsBase import UcsUtils , WriteUcsWarning from Mos import OrgOrg xMO = mObj . Clone ( ) xMO . SetHandle ( mObj . GetHandle ( ) ) if ( xlateOrg != None ) : matchObj = re . match ( r'^(org-[\-\.:_a-zA-Z0-9]{1,16}/)*org-[\-\.:_a-zA-Z0-9]{1,16}' , xMO . Dn ) if matchObj : if UcsUtils . WordL ( xMO . classId ) == OrgOrg . ClassId ( ) : orgMoMeta = UcsUtils . GetUcsPropertyMeta ( UcsUtils . WordU ( OrgOrg . ClassId ( ) ) , "Meta" ) if orgMoMeta == None : # TODO: Add Warning/Error messages in Logger. WriteUcsWarning ( '[Warning]: Could not translate [%s]' % ( xMO . Dn ) ) return xMO # Check for naming property matchObj1 = re . findall ( r'(\[[^\]]+\])' , orgMoMeta . rn ) if matchObj1 : UpdateMoDnAlongWithNamingProperties ( xMO , orgMoMeta , xlateOrg ) else : newDn = re . sub ( "%s" % ( matchObj . group ( 0 ) ) , "%s" % ( xlateOrg ) , xMO . Dn ) # print "Translating", xMO.Dn, " => ", newDn xMO . Dn = newDn else : newDn = re . sub ( "^%s/" % ( matchObj . group ( 0 ) ) , "%s/" % ( xlateOrg ) , xMO . Dn ) # print "Translating", xMO.Dn, " => ", newDn xMO . Dn = newDn if ( xlateMap != None ) : originalDn = xMO . Dn if originalDn in xlateMap : xMoMeta = UcsUtils . GetUcsPropertyMeta ( UcsUtils . WordU ( xMO . classId ) , "Meta" ) if xMoMeta == None : # TODO: Add Warning/Error messages in Logger. WriteUcsWarning ( '[Warning]: Could not translate [%s]' % ( originalDn ) ) return xMO # Check for naming property matchObj = re . findall ( r'(\[[^\]]+\])' , xMoMeta . rn ) if matchObj : UpdateMoDnAlongWithNamingProperties ( xMO , xMoMeta , xlateMap [ originalDn ] ) else : # print "Translating", xMO.Dn, " => ", xlateMap[originalDn] xMO . Dn = xlateMap [ originalDn ] else : originalDn = re . sub ( r'[/]*[^/]+$' , '' , originalDn ) while ( originalDn != None or originalDn == "" ) : if ( not ( originalDn in xlateMap ) ) : originalDn = re . sub ( r'[/]*[^/]+$' , '' , originalDn ) continue newDn = re . sub ( "^%s/" % ( originalDn ) , "%s/" % ( xlateMap [ originalDn ] ) , xMO . Dn ) # print "Translating", xMO.Dn, " => ", newDn xMO . Dn = newDn break return xMO
Method used to translate a managedobject . This method is used in CompareManagedObject .
811
18
3,469
def ImportUcsSession ( filePath , key ) : from UcsBase import UcsUtils , WriteUcsWarning , UcsValidationException # from p3 import p3_encrypt, p3_decrypt if filePath is None : raise UcsValidationException ( "filePath parameter is not provided." ) # raise Exception('[Error]: Please provide filePath') if key is None : raise UcsValidationException ( "key parameter is not provided." ) # raise Exception('[Error]: Please provide key') if not os . path . isfile ( filePath ) or not os . path . exists ( filePath ) : raise UcsValidationException ( '[Error]: File <%s> does not exist ' % ( filePath ) ) # raise Exception('[Error]: File <%s> does not exist ' %(filePath)) doc = xml . dom . minidom . parse ( filePath ) topNode = doc . documentElement # print topNode.localName if topNode is None or topNode . localName != UcsLoginXml . UCS_HANDLES : return None if ( topNode . hasChildNodes ( ) ) : # childList = topNode._get_childNodes() # childCount = childList._get_length() childList = topNode . childNodes childCount = len ( childList ) for i in range ( childCount ) : childNode = childList . item ( i ) if ( childNode . nodeType != Node . ELEMENT_NODE ) : continue if childNode . localName != UcsLoginXml . UCS : continue lName = None lUsername = None lPassword = None lNoSsl = False lPort = None if childNode . hasAttribute ( UcsLoginXml . NAME ) : lName = childNode . getAttribute ( UcsLoginXml . NAME ) if childNode . hasAttribute ( UcsLoginXml . USER_NAME ) : lUsername = childNode . getAttribute ( UcsLoginXml . USER_NAME ) if childNode . hasAttribute ( UcsLoginXml . PASSWORD ) : # lPassword = p3_decrypt(childNode.getAttribute(UcsLoginXml.PASSWORD), key) lPassword = UcsUtils . DecryptPassword ( childNode . getAttribute ( UcsLoginXml . PASSWORD ) , key ) if childNode . hasAttribute ( UcsLoginXml . NO_SSL ) : lNoSsl = childNode . getAttribute ( UcsLoginXml . NO_SSL ) if childNode . hasAttribute ( UcsLoginXml . PORT ) : lPort = childNode . getAttribute ( UcsLoginXml . PORT ) # Process Login if ( ( lName is None ) or ( lUsername == None ) or ( lPassword == None ) ) : # WriteUcsWarning("[Warning] Insufficient information for login ...") continue try : handle = UcsHandle ( ) handle . Login ( name = lName , username = lUsername , password = lPassword , noSsl = lNoSsl , port = lPort ) except Exception , err : # TODO: Add Warning/Error messages in Logger. WriteUcsWarning ( "[Connection Error<%s>] %s" % ( lName , str ( err ) ) )
This operation will do a login to each UCS which is present in credential file .
724
16
3,470
def Uri ( self ) : return ( "%s://%s%s" % ( ( "https" , "http" ) [ self . _noSsl == True ] , self . _name , ( ":" + str ( self . _port ) , "" ) [ ( ( ( self . _noSsl == False ) and ( self . _port == 80 ) ) or ( ( self . _noSsl == True ) and ( self . _port == 443 ) ) ) ] ) )
Constructs the connection URI from name noSsl and port instance variables .
106
15
3,471
def UndoTransaction ( self ) : from Ucs import ConfigMap self . _transactionInProgress = False self . _configMap = ConfigMap ( )
Cancels any running transaction .
33
7
3,472
def CompleteTransaction ( self , dumpXml = None ) : from Ucs import ConfigMap , Pair from UcsBase import ManagedObject , WriteUcsWarning , WriteObject , UcsException self . _transactionInProgress = False ccm = self . ConfigConfMos ( self . _configMap , YesOrNo . FALSE , dumpXml ) self . _configMap = ConfigMap ( ) if ccm . errorCode == 0 : moList = [ ] for child in ccm . OutConfigs . GetChild ( ) : if ( isinstance ( child , Pair ) == True ) : for mo in child . GetChild ( ) : moList . append ( mo ) elif ( isinstance ( child , ManagedObject ) == True ) : moList . append ( child ) # WriteObject(moList) return moList else : # raise Exception('[Error]: CompleteTransaction [Code]:' + ccm.errorCode + ' [Description]:' + ccm.errorDescr) raise UcsException ( ccm . errorCode , ccm . errorDescr )
Completes a transaction .
230
6
3,473
def XmlRawQuery ( self , xml , dumpXml = None ) : if ( dumpXml == None ) : dumpXml = self . _dumpXml uri = self . Uri ( ) + '/nuova' if ( dumpXml in _AffirmativeList ) : print '%s ====> %s' % ( self . _ucs , xml ) # req = urllib2.Request(url=uri,data=xml) # f = urllib2.urlopen(req) w = xml . dom . minidom . Document ( ) if ( self . _noSsl ) : req = urllib2 . Request ( url = uri , data = w . toxml ( ) ) opener = urllib2 . build_opener ( SmartRedirectHandler ( ) ) f = opener . open ( req ) # print "##", f , "##" if type ( f ) is list : if ( len ( f ) == 2 and f [ 0 ] == 302 ) : # self._noSsl = False # uri = self.Uri() + '/nuova' uri = f [ 1 ] req = urllib2 . Request ( url = uri , data = w . toxml ( ) ) f = urllib2 . urlopen ( req ) # print "status code is:",f[0] # print "location is:", f[1] else : req = urllib2 . Request ( url = uri , data = w . toxml ( ) ) f = urllib2 . urlopen ( req ) rsp = f . read ( ) if ( dumpXml in _AffirmativeList ) : print '%s <==== %s' % ( self . _ucs , rsp ) return rsp
Accepts xmlQuery String and returns xml response String . No object manipulation is done in this method .
388
20
3,474
def Logout ( self , dumpXml = None ) : from UcsBase import UcsException if ( self . _cookie == None ) : return True if self . _refreshTimer : self . _refreshTimer . cancel ( ) response = self . AaaLogout ( dumpXml ) self . _cookie = None self . _lastUpdateTime = str ( time . asctime ( ) ) self . _domains = None self . _priv = None self . _sessionId = None self . _version = None if self . _ucs in defaultUcs : del defaultUcs [ self . _ucs ] if ( response . errorCode != 0 ) : raise UcsException ( response . errorCode , response . errorDescr ) # raise Exception('[Error]: Logout [Code]:' + response.errorCode + '[Description]:' + response.errorDescr) return True
Logout method disconnects from UCS .
193
8
3,475
def _Start_refresh_timer ( self ) : if self . _refreshPeriod > 60 : interval = self . _refreshPeriod - 60 else : interval = 60 self . _refreshTimer = Timer ( self . _refreshPeriod , self . Refresh ) # TODO:handle exit and logout active connections. revert from daemon then self . _refreshTimer . setDaemon ( True ) self . _refreshTimer . start ( )
Internal method to support auto - refresh functionality .
100
9
3,476
def _start_enqueue_thread ( self ) : self . _enqueueThreadSignal . acquire ( ) self . _enqueueThread = Thread ( target = self . _enqueue_function ) self . _enqueueThread . daemon = True self . _enqueueThread . start ( ) self . _enqueueThreadSignal . wait ( ) self . _enqueueThreadSignal . release ( )
Internal method to start the enqueue thread which adds the events in an internal queue .
87
17
3,477
def _add_watch_block ( self , params , filterCb , capacity = 500 , cb = None ) : if ( self . _wbslock == None ) : self . _wbslock = Lock ( ) self . _wbslock . acquire ( ) wb = WatchBlock ( params , filterCb , capacity , cb ) # Add a List of Watchers if ( ( wb != None ) and ( wb . cb == None ) ) : wb . cb = wb . _dequeue_default_cb self . _wbs . append ( wb ) self . _wbslock . release ( ) if self . _cookie == None : return None if wb != None and len ( self . _wbs ) == 1 and wb . params [ "pollSec" ] == None : self . _start_enqueue_thread ( ) if self . _enqueueThread == None : return wb self . _enqueueThreadSignal . acquire ( ) self . _enqueueThreadSignal . notify ( ) # Notify self . _enqueueThreadSignal . release ( ) # Release the Lock return wb
Internal method to add a watch block for starting event monitoring .
249
12
3,478
def _remove_watch_block ( self , wb ) : if ( self . _wbslock == None ) : self . _wbslock = Lock ( ) self . _wbslock . acquire ( ) self . _wbs . remove ( wb ) if len ( self . _wbs ) == 0 : self . _stop_enqueue_thread ( ) self . _stop_dequeue_thread ( ) self . _wbslock . release ( )
Internal method to remove a watch block for stopping event monitoring .
101
12
3,479
def RemoveEventHandler ( self , wb ) : from UcsBase import WriteUcsWarning if wb in self . _wbs : self . _remove_watch_block ( wb ) else : WriteUcsWarning ( "Event handler not found" )
Removes an event handler .
56
6
3,480
def _start_dequeue_thread ( self ) : self . _dequeueThread = Thread ( target = self . _dequeue_function ) self . _dequeueThread . daemon = True self . _dequeueThread . start ( )
Internal method to start dequeue thread .
51
8
3,481
def StartGuiSession ( self ) : from UcsBase import WriteUcsWarning , UcsUtils , UcsValidationException import urllib , tempfile , fileinput , os , subprocess , platform osSupport = [ "Windows" , "Linux" , "Microsoft" ] if platform . system ( ) not in osSupport : raise UcsValidationException ( "Currently works with Windows OS and Ubuntu" ) # raise Exception("Currently works with Windows OS and Ubuntu") try : javawsPath = UcsUtils . GetJavaInstallationPath ( ) # print r"%s" %(javawsPath) if javawsPath != None : url = "%s/ucsm/ucsm.jnlp" % ( self . Uri ( ) ) source = urllib . urlopen ( url ) . read ( ) jnlpdir = tempfile . gettempdir ( ) jnlpfile = os . path . join ( jnlpdir , "temp.jnlp" ) if os . path . exists ( jnlpfile ) : os . remove ( jnlpfile ) jnlpFH = open ( jnlpfile , "w+" ) jnlpFH . write ( source ) jnlpFH . close ( ) for line in fileinput . input ( jnlpfile , inplace = 1 ) : if re . search ( r'^\s*</resources>\s*$' , line ) : print '\t<property name="log.show.encrypted" value="true"/>' print line , subprocess . call ( [ javawsPath , jnlpfile ] ) if os . path . exists ( jnlpfile ) : os . remove ( jnlpfile ) else : return None except Exception , err : fileinput . close ( ) if os . path . exists ( jnlpfile ) : os . remove ( jnlpfile ) raise
Launches the UCSM GUI via specific UCS handle .
414
11
3,482
def ImportUcsBackup ( self , path = None , merge = False , dumpXml = False ) : from UcsBase import WriteUcsWarning , UcsUtils , ManagedObject , WriteObject , UcsUtils , UcsException , UcsValidationException from Ucs import ConfigConfig from Mos import MgmtImporter from datetime import datetime if ( self . _transactionInProgress ) : raise UcsValidationException ( "UCS transaction in progress. Cannot execute ImportUcsBackup. Complete or Undo UCS transaction." ) # raise Exception("UCS transaction in progress. Cannot execute ImportUcsBackup. Complete or Undo UCS transaction.") if not path : raise UcsValidationException ( "path parameter is not provided." ) # raise Exception("Please provide path") if not os . path . exists ( path ) : raise UcsValidationException ( "Backup File not found <%s>" % ( path ) ) # raise Exception("Backup File not found <%s>" %(path)) dn = None filePath = path localFile = os . path . basename ( filePath ) topSystem = ManagedObject ( NamingId . TOP_SYSTEM ) mgmtImporter = ManagedObject ( NamingId . MGMT_IMPORTER ) mgmtImporter . Hostname = os . environ [ 'COMPUTERNAME' ] . lower ( ) + datetime . now ( ) . strftime ( '%Y%m%d%H%M' ) dn = UcsUtils . MakeDn ( [ topSystem . MakeRn ( ) , mgmtImporter . MakeRn ( ) ] ) mgmtImporter . Dn = dn mgmtImporter . Status = Status . CREATED mgmtImporter . RemoteFile = filePath mgmtImporter . Proto = MgmtImporter . CONST_PROTO_HTTP mgmtImporter . AdminState = MgmtImporter . CONST_ADMIN_STATE_ENABLED if merge : mgmtImporter . Action = MgmtImporter . CONST_ACTION_MERGE else : mgmtImporter . Action = MgmtImporter . CONST_ACTION_REPLACE inConfig = ConfigConfig ( ) inConfig . AddChild ( mgmtImporter ) uri = "%s/operations/file-%s/importconfig.txt" % ( self . Uri ( ) , localFile ) if sys . version_info < ( 2 , 6 ) : uploadFileHandle = open ( filePath , 'rb' ) stream = uploadFileHandle . read ( ) else : progress = Progress ( ) stream = file_with_callback ( filePath , 'rb' , progress . update , filePath ) request = urllib2 . Request ( uri ) request . add_header ( 'Cookie' , 'ucsm-cookie=%s' % ( self . _cookie ) ) request . add_data ( stream ) response = urllib2 . urlopen ( request ) . read ( ) if not response : raise UcsValidationException ( "Unable to upload properly." ) # WriteUcsWarning("Unable to upload properly.") ccm = self . ConfigConfMo ( dn = dn , inConfig = inConfig , inHierarchical = YesOrNo . FALSE , dumpXml = dumpXml ) if ( ccm . errorCode != 0 ) : raise UcsException ( ccm . errorCode , ccm . errorDescr ) # raise Exception('[Error]: BackupUcs [Code]:' + ccm.errorCode + ' [Description]:' + ccm.errorDescr) return ccm . OutConfig . GetChild ( )
Imports backUp . This operation will upload the UCSM backup taken earlier via GUI or BackupUcs operation for all configuration system configuration and logical configuration files . User can perform an import while the system is up and running . - path specifies path of the backup file . - merge specifies whether to merge the backup configuration with the existing UCSM configuration .
806
70
3,483
def SendUcsFirmware ( self , path = None , dumpXml = False ) : from UcsBase import WriteUcsWarning , UcsUtils , ManagedObject , WriteObject , UcsUtils , UcsValidationException , UcsException from Ucs import ConfigConfig from Mos import FirmwareDownloader if ( self . _transactionInProgress ) : raise UcsValidationException ( "UCS transaction in progress. Cannot execute SendUcsFirmware. Complete or Undo UCS transaction." ) # raise Exception("UCS transaction in progress. Cannot execute SendUcsFirmware. Complete or Undo UCS transaction.") if not path : raise UcsValidationException ( "path parameter is not provided." ) # raise Exception("Please provide path") if not os . path . exists ( path ) : raise UcsValidationException ( "Image not found <%s>" % ( path ) ) # raise Exception("Image not found <%s>" %(path)) dn = None filePath = path localFile = os . path . basename ( filePath ) # Exit if image already exist on UCSM topSystem = ManagedObject ( NamingId . TOP_SYSTEM ) firmwareCatalogue = ManagedObject ( NamingId . FIRMWARE_CATALOGUE ) firmwareDistributable = ManagedObject ( NamingId . FIRMWARE_DISTRIBUTABLE ) firmwareDistributable . Name = localFile dn = UcsUtils . MakeDn ( [ topSystem . MakeRn ( ) , firmwareCatalogue . MakeRn ( ) , firmwareDistributable . MakeRn ( ) ] ) crDn = self . ConfigResolveDn ( dn , inHierarchical = YesOrNo . FALSE , dumpXml = dumpXml ) if ( crDn . OutConfig . GetChildCount ( ) > 0 ) : raise UcsValidationException ( "Image file <%s> already exist on FI." % ( filePath ) ) # raise Exception("Image file <%s> already exist on FI." %(filePath)) # Create object of type <firmwareDownloader> firmwareDownloader = ManagedObject ( NamingId . FIRMWARE_DOWNLOADER ) firmwareDownloader . FileName = localFile dn = UcsUtils . MakeDn ( [ topSystem . MakeRn ( ) , firmwareCatalogue . MakeRn ( ) , firmwareDownloader . MakeRn ( ) ] ) firmwareDownloader . Dn = dn firmwareDownloader . Status = Status . CREATED firmwareDownloader . FileName = localFile firmwareDownloader . Server = FirmwareDownloader . CONST_PROTOCOL_LOCAL firmwareDownloader . Protocol = FirmwareDownloader . CONST_PROTOCOL_LOCAL inConfig = ConfigConfig ( ) inConfig . AddChild ( firmwareDownloader ) uri = "%s/operations/file-%s/image.txt" % ( self . Uri ( ) , localFile ) progress = Progress ( ) stream = file_with_callback ( filePath , 'rb' , progress . update , filePath ) request = urllib2 . Request ( uri ) request . add_header ( 'Cookie' , 'ucsm-cookie=%s' % ( self . _cookie ) ) request . add_data ( stream ) response = urllib2 . urlopen ( request ) . read ( ) if not response : raise UcsValidationException ( "Unable to upload properly." ) # WriteUcsWarning("Unable to upload properly.") ccm = self . ConfigConfMo ( dn = dn , inConfig = inConfig , inHierarchical = YesOrNo . FALSE , dumpXml = dumpXml ) if ( ccm . errorCode != 0 ) : raise UcsException ( ccm . errorCode , ccm . errorDescr ) return ccm . OutConfig . GetChild ( )
Uploads a specific CCO Image on UCS . - path specifies the path of the image to be uploaded .
856
22
3,484
def GetUcsChild ( self , inMo = None , inDn = None , classId = None , inHierarchical = False , dumpXml = None ) : from UcsBase import UcsValidationException , UcsException , UcsUtils if not inDn and not inMo : raise UcsValidationException ( '[Error]: get_ucs_child: Provide in_mo or in_dn.' ) if inMo : parentDn = inMo . getattr ( "Dn" ) elif inDn : parentDn = inDn crc = self . ConfigResolveChildren ( classId , parentDn , None , inHierarchical , dumpXml ) if crc . errorCode == 0 : moList = UcsUtils . extractMolistFromMethodResponse ( crc , inHierarchical ) return moList else : raise UcsException ( crc . errorCode , crc . error_descr )
Gets Child Managed Object from UCS .
214
9
3,485
def code ( sentence , pad = ' ' , format = 'army' ) : try : return ALPHABET [ 'code' ] [ format ] ( sentence , pad or CODE_PADDING [ format ] ) except KeyError : raise TypeError ( 'Unsupported code alphabet "%s"' % ( format , ) )
Transform a sentence using the code spelling alphabet multiple international code alphabets are supported .
69
18
3,486
def nato ( sentence , pad = ' ' , format = 'telephony' ) : try : return '' + ALPHABET [ 'nato' ] [ format ] ( sentence , pad ) except KeyError : raise TypeError ( 'Unsupported NATO alphabet "%s"' % ( format , ) )
Transform a sentence using the NATO spelling alphabet .
64
9
3,487
def clause ( self , * args , * * kwargs ) : if args and isinstance ( args [ 0 ] , Clause ) : clause = args [ 0 ] else : clause = Clause ( * args , * * kwargs ) if not clause . fields : clause . fields = self . all_fields if ( clause . wildcard & Query . WILDCARD_LEADING ) and ( clause . term [ 0 ] != Query . WILDCARD ) : clause . term = Query . WILDCARD + clause . term if ( clause . wildcard & Query . WILDCARD_TRAILING ) and ( clause . term [ - 1 ] != Query . WILDCARD ) : clause . term = clause . term + Query . WILDCARD self . clauses . append ( clause ) return self
Adds a lunr . Clause to this query .
175
10
3,488
def term ( self , term , * * kwargs ) : if isinstance ( term , ( list , tuple ) ) : for t in term : self . term ( t , * * kwargs ) else : self . clause ( str ( term ) , * * kwargs ) return self
Adds a term to the current query creating a Clause and adds it to the list of clauses making up this Query .
63
23
3,489
def is_negated ( self ) : return all ( clause . presence == QueryPresence . PROHIBITED for clause in self . clauses )
A negated query is one in which every clause has a presence of prohibited . These queries require some special processing to return the expected results .
31
28
3,490
def send_templated_email ( recipients , template_path , context = None , from_email = settings . DEFAULT_FROM_EMAIL , fail_silently = False , extra_headers = None ) : recipient_pks = [ r . pk for r in recipients if isinstance ( r , get_user_model ( ) ) ] recipient_emails = [ e for e in recipients if not isinstance ( e , get_user_model ( ) ) ] send = _send_task . delay if use_celery else _send msg = send ( recipient_pks , recipient_emails , template_path , context , from_email , fail_silently , extra_headers = extra_headers ) return msg
recipients can be either a list of emails or a list of users if it is users the system will change to the language that the user has set as theyr mother toungue
159
38
3,491
def remove_duplicates ( seq ) : last_boundary = False for char in seq : if char == '\x00' : if not last_boundary : last_boundary = True yield char else : last_boundary = False yield char
Removes duplicate boundary token characters from the given character iterable .
55
13
3,492
def pretty_print_str ( self ) : retval = '' # dfs todo = [ self . root ] while todo : current = todo . pop ( ) for char in reversed ( sorted ( current . keys ( ) ) ) : todo . append ( current [ char ] ) indent = ' ' * ( current . depth * 2 ) retval += indent + current . __unicode__ ( ) + '\n' return retval . rstrip ( '\n' )
Create a string to pretty - print this trie to standard output .
104
14
3,493
def _reset_suffix_links ( self ) : self . _suffix_links_set = False for current , _parent in self . dfs ( ) : current . suffix = None current . dict_suffix = None current . longest_prefix = None
Reset all suffix links in all nodes in this trie .
56
13
3,494
def _set_suffix_links ( self ) : self . _suffix_links_set = True for current , parent in self . bfs ( ) : # skip the root node if parent is None : continue current . longest_prefix = parent . longest_prefix if parent . has_value : current . longest_prefix = parent # the root doesn't get a suffix link # also, skip previously set suffix links if current . has_suffix : continue # current is not the root and has no suffix # set current's suffix to parent's suffix suffix = parent while True : if not suffix . has_suffix : current . suffix = self . root break else : suffix = suffix . suffix if current . uplink in suffix : current . suffix = suffix [ current . uplink ] break # now find the dict_suffix value suffix = current . suffix while not suffix . has_value and suffix . has_suffix : suffix = suffix . suffix if suffix . has_value : current . dict_suffix = suffix
Sets all suffix links in all nodes in this trie .
215
13
3,495
def greedy_replace ( self , seq ) : if not self . _suffix_links_set : self . _set_suffix_links ( ) # start at the root current = self . root buffered = '' outstr = '' for char in seq : while char not in current : if current . has_dict_suffix : current = current . dict_suffix outstr += buffered [ : - current . depth ] outstr += current . value buffered = '' current = self . root break elif current . has_suffix : current = current . suffix if current . depth : outstr += buffered [ : - current . depth ] buffered = buffered [ - current . depth : ] else : outstr += buffered buffered = '' break else : current = self . root outstr += buffered buffered = '' break if char in current : buffered += char current = current [ char ] if current . has_value : outstr += buffered [ : - current . depth ] outstr += current . value buffered = '' current = self . root else : assert current is self . root outstr += buffered + char buffered = '' if current . has_dict_suffix : current = current . dict_suffix outstr += buffered [ : - current . depth ] outstr += current . value else : outstr += buffered return outstr
Greedily matches strings in seq and replaces them with their node values .
294
15
3,496
def _write_mo ( mo ) : # from UcsBase import UcsUtils classNotFound = False if ( UcsUtils . FindClassIdInMoMetaIgnoreCase ( mo . classId ) == None ) : classNotFound = True tabsize = 8 outstr = "\n" if classNotFound : outstr += "Managed Object\t\t\t:\t" + str ( UcsUtils . WordU ( mo . classId ) ) + "\n" else : outstr += "Managed Object\t\t\t:\t" + str ( mo . propMoMeta . name ) + "\n" outstr += "-" * len ( "Managed Object" ) + "\n" if ( not classNotFound ) : for prop in UcsUtils . GetUcsPropertyMetaAttributeList ( mo . propMoMeta . name ) : propMeta = UcsUtils . GetUcsPropertyMeta ( mo . propMoMeta . name , prop ) if ( propMeta . access == UcsPropertyMeta . Internal ) : continue val = mo . getattr ( prop ) # if val != None and val != "": outstr += str ( prop ) . ljust ( tabsize * 4 ) + ':' + str ( val ) + "\n" else : for prop in mo . __dict__ : if ( prop in [ 'classId' , 'XtraProperty' , 'handle' , 'propMoMeta' , 'dirtyMask' , 'child' ] ) : continue val = mo . __dict__ [ prop ] outstr += str ( UcsUtils . WordU ( prop ) ) . ljust ( tabsize * 4 ) + ':' + str ( val ) + "\n" if mo . __dict__ . has_key ( 'XtraProperty' ) : for xtraProp in mo . __dict__ [ 'XtraProperty' ] : outstr += '[X]' + str ( UcsUtils . WordU ( xtraProp ) ) . ljust ( tabsize * 4 ) + ':' + str ( mo . __dict__ [ 'XtraProperty' ] [ xtraProp ] ) + "\n" outstr += str ( "Ucs" ) . ljust ( tabsize * 4 ) + ':' + str ( mo . handle . _ucs ) + "\n" outstr += "\n" return outstr
Method to return string representation of a managed object .
518
10
3,497
def WriteObject ( moList ) : from Ucs import Dn from UcsHandle import UcsMoDiff tabsize = 8 if ( isinstance ( moList , _GenericMO ) == True ) : print str ( moList ) elif ( isinstance ( moList , ExternalMethod ) == True ) : if ( hasattr ( moList , "OutConfigs" ) == True ) : for child in moList . OutConfigs . GetChild ( ) : if ( isinstance ( child , ManagedObject ) == True ) : WriteObject ( child ) elif ( isinstance ( moList , ManagedObject ) == True ) : print str ( _write_mo ( moList ) ) elif ( ( isinstance ( moList , list ) == True ) and ( len ( moList ) > 0 ) ) : if ( isinstance ( moList [ 0 ] , UcsMoDiff ) ) : print "Dn" . ljust ( tabsize * 10 ) , "InputObject" . ljust ( tabsize * 4 ) , "SideIndicator" . ljust ( tabsize * 3 ) , "DiffProperty" print "--" . ljust ( tabsize * 10 ) , "-----------" . ljust ( tabsize * 4 ) , "-------------" . ljust ( tabsize * 3 ) , "------------" for mo in moList : if ( isinstance ( mo , ManagedObject ) == True ) : print str ( _write_mo ( mo ) ) elif ( isinstance ( mo , Dn ) == True ) : print mo . getattr ( "value" ) elif ( isinstance ( mo , UcsMoDiff ) == True ) : WriteMoDiff ( mo )
Writes the managed object on the terminal in form of key value pairs .
363
15
3,498
def childWriteXml ( self , w , option ) : ch = [ ] for c in self . child : ch . append ( c . WriteXml ( w , option ) ) return ch
Method writes the xml representation for the object .
41
9
3,499
def setattr ( self , key , value ) : if ( UcsUtils . FindClassIdInMoMetaIgnoreCase ( self . classId ) != None ) : if ( key in _ManagedObjectMeta [ self . classId ] ) : propMeta = UcsUtils . GetUcsPropertyMeta ( self . classId , key ) if ( propMeta . ValidatePropertyValue ( value ) == False ) : # print "Validation Failure" return False if ( propMeta . mask != None ) : self . dirtyMask |= propMeta . mask self . __dict__ [ key ] = value else : self . __dict__ [ 'XtraProperty' ] [ key ] = value else : """ no such property """ self . __dict__ [ 'XtraProperty' ] [ UcsUtils . WordU ( key ) ] = value
This method sets attribute of a Managed Object .
182
10