#NEWLINE# def parse_search_value( search_value ) : #NEWLINE# #INDENT# 'The <search-value> can be of the form::\n\n        d<device_id>r<region>z<zone>-<ip>:<port>R<r_ip>:<r_port>/\n         <device_name>_<meta>\n\n    Where <r_ip> and <r_port> are replication ip and port.\n\n    Any part is optional, but you must include at least one part.\n\n    Examples::\n\n        d74              Matches the device id 74\n        r4               Matches devices in region 4\n        z1               Matches devices in zone 1\n        z1-1.2.3.4       Matches devices in zone 1 with the ip 1.2.3.4\n        1.2.3.4          Matches devices in any zone with the ip 1.2.3.4\n        z1:5678          Matches devices in zone 1 using port 5678\n        :5678            Matches devices that use port 5678\n        R5.6.7.8         Matches devices that use replication ip 5.6.7.8\n        R:5678           Matches devices that use replication port 5678\n        1.2.3.4R5.6.7.8  Matches devices that use ip 1.2.3.4 and replication ip\n                         5.6.7.8\n        /sdb1            Matches devices with the device name sdb1\n        _shiny           Matches devices with shiny in the meta data\n        _"snet: 5.6.7.8" Matches devices with snet: 5.6.7.8 in the meta data\n        [::1]            Matches devices in any zone with the ip ::1\n        z1-[::1]:5678    Matches devices in zone 1 with ip ::1 and port 5678\n\n    Most specific example::\n\n        d74r4z1-1.2.3.4:5678/sdb1_"snet: 5.6.7.8"\n\n    Nerd explanation:\n\n        All items require their single character prefix except the ip, in which\n        case the - is optional unless the device id or zone is also included.\n    ' #NEWLINE# orig_search_value = search_value #NEWLINE# match = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if search_value . startswith ( 'd' ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and search_value [ i ] . isdigit ( ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# match [ 'id' ] = int ( search_value [ 1 : i ] ) #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# if search_value . startswith ( 'r' ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and search_value [ i ] . isdigit ( ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# match [ 'region' ] = int ( search_value [ 1 : i ] ) #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# if search_value . startswith ( 'z' ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and search_value [ i ] . isdigit ( ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# match [ 'zone' ] = int ( search_value [ 1 : i ] ) #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# if search_value . startswith ( '-' ) : #NEWLINE# #INDENT# search_value = search_value [ 1 : ] #NEWLINE# #UNINDENT# if ( search_value and search_value [ 0 ] . isdigit ( ) ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and ( search_value [ i ] in '0123456789.' ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# match [ 'ip' ] = search_value [ : i ] #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( search_value and search_value . startswith ( '[' ) ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and ( search_value [ i ] != ']' ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# i += 1 #NEWLINE# match [ 'ip' ] = search_value [ : i ] . lstrip ( '[' ) . rstrip ( ']' ) #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# #UNINDENT# if ( 'ip' in match ) : #NEWLINE# #INDENT# match [ 'ip' ] = validate_and_normalize_ip ( match [ 'ip' ] ) #NEWLINE# #UNINDENT# if search_value . startswith ( ':' ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and search_value [ i ] . isdigit ( ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# match [ 'port' ] = int ( search_value [ 1 : i ] ) #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# if search_value . startswith ( 'R' ) : #NEWLINE# #INDENT# search_value = search_value [ 1 : ] #NEWLINE# if ( search_value and search_value [ 0 ] . isdigit ( ) ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and ( search_value [ i ] in '0123456789.' ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# match [ 'replication_ip' ] = search_value [ : i ] #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( search_value and search_value . startswith ( '[' ) ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and ( search_value [ i ] != ']' ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# i += 1 #NEWLINE# match [ 'replication_ip' ] = search_value [ : i ] . lstrip ( '[' ) . rstrip ( ']' ) #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# #UNINDENT# if ( 'replication_ip' in match ) : #NEWLINE# #INDENT# match [ 'replication_ip' ] = validate_and_normalize_ip ( search_value [ 'replication_ip' ] ) #NEWLINE# #UNINDENT# if search_value . startswith ( ':' ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and search_value [ i ] . isdigit ( ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# match [ 'replication_port' ] = int ( search_value [ 1 : i ] ) #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# #UNINDENT# if search_value . startswith ( '/' ) : #NEWLINE# #INDENT# i = 1 #NEWLINE# while ( ( i < len ( search_value ) ) and ( search_value [ i ] != '_' ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# match [ 'device' ] = search_value [ 1 : i ] #NEWLINE# search_value = search_value [ i : ] #NEWLINE# #UNINDENT# if search_value . startswith ( '_' ) : #NEWLINE# #INDENT# match [ 'meta' ] = search_value [ 1 : ] #NEWLINE# search_value = '' #NEWLINE# #UNINDENT# if search_value : #NEWLINE# #INDENT# raise ValueError ( ( 'Invalid <search-value>: %s' % repr ( orig_search_value ) ) ) #NEWLINE# #UNINDENT# return match