本文整理汇总了Python中pynipap.Prefix.search方法的典型用法代码示例。如果您正苦于以下问题:Python Prefix.search方法的具体用法?Python Prefix.search怎么用?Python Prefix.search使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pynipap.Prefix
的用法示例。
在下文中一共展示了Prefix.search方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search_prefix
# 需要导入模块: from pynipap import Prefix [as 别名]
# 或者: from pynipap.Prefix import search [as 别名]
def search_prefix(self):
""" Search prefixes. Does not yet incorporate all the functions of the
search_prefix API function due to difficulties with transferring
a complete 'dict-to-sql' encoded data structure.
Instead, a list of prefix attributes can be given which will be
matched with the 'equals' operator if notheing else is specified. If
multiple attributes are given, they will be combined with the 'and'
operator. Currently, it is not possible to specify different
operators for different attributes.
"""
# extract operator
if 'operator' in request.json:
operator = request.json['operator']
else:
operator = 'equals'
# fetch attributes from request.json
attr = XhrController.extract_prefix_attr(request.json)
# build query dict
n = 0
q = {}
for key, val in attr.items():
if n == 0:
q = {
'operator': operator,
'val1': key,
'val2': val
}
else:
q = {
'operator': 'and',
'val1': {
'operator': operator,
'val1': key,
'val2': val
},
'val2': q
}
n += 1
# extract search options
search_opts = {}
if 'children_depth' in request.json:
search_opts['children_depth'] = request.json['children_depth']
if 'parents_depth' in request.json:
search_opts['parents_depth'] = request.json['parents_depth']
if 'include_neighbors' in request.json:
search_opts['include_neighbors'] = request.json['include_neighbors']
if 'max_result' in request.json:
search_opts['max_result'] = request.json['max_result']
if 'offset' in request.json:
search_opts['offset'] = request.json['offset']
try:
result = Prefix.search(q, search_opts)
except NipapError, e:
return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
示例2: view_prefix
# 需要导入模块: from pynipap import Prefix [as 别名]
# 或者: from pynipap.Prefix import search [as 别名]
def view_prefix(arg, opts):
""" View a single prefix.
"""
s = get_schema()
res = Prefix.search(s, { 'operator': 'equals', 'val1': 'prefix', 'val2': arg }, {})
if len(res['result']) == 0:
print "Address %s not found." % arg
return
p = res['result'][0]
print "-- Address "
print " %-15s : %s" % ("Prefix", p.prefix)
print " %-15s : %s" % ("Display prefix", p.display_prefix)
print " %-15s : %s" % ("Type", p.type)
print " %-15s : IPv%s" % ("Family", p.family)
print " %-15s : %s" % ("Description", p.description)
print " %-15s : %s" % ("Node", p.node)
print " %-15s : %s" % ("Order", p.order_id)
print " %-15s : %s" % ("VRF", p.vrf)
print " %-15s : %s" % ("Alarm priority", p.alarm_priority)
print " %-15s : %s" % ("Monitor", p.monitor)
print "-- Comment"
print p.comment
示例3: replace
# 需要导入模块: from pynipap import Prefix [as 别名]
# 或者: from pynipap.Prefix import search [as 别名]
def replace(pattern, replacement):
# Fetch prefixes matching the string to replace
print "Fetching prefixes from NIPAP... ",
sys.stdout.flush()
n = 1
prefix_list = []
t0 = time.time()
query = {
'operator': 'or',
'val1': {
'operator': 'regex_match',
'val1': 'description',
'val2': pattern
},
'val2': {
'operator': 'regex_match',
'val1': 'node',
'val2': pattern
}
}
full_result = Prefix.search(query, { 'parents_depth': -1, 'max_result': BATCH_SIZE })
prefix_result = full_result['result']
prefix_list += prefix_result
print len(prefix_list),
sys.stdout.flush()
while len(prefix_result) == 100:
full_result = Prefix.smart_search(pattern, { 'parents_depth': -1, 'max_result': BATCH_SIZE, 'offset': n * BATCH_SIZE })
prefix_result = full_result['result']
prefix_list += prefix_result
print len(prefix_list),
sys.stdout.flush()
n += 1
t1 = time.time()
print " done in %.1f seconds" % (t1 - t0)
# Display list
print_pattern = "%-2s%-14s%-2s%-30s%-20s%s"
print "\n\nPrefixes to change:"
print print_pattern % ("", "VRF", "", "Prefix", "Node", "Description")
i_match = 0
for i, prefix in enumerate(prefix_list):
if prefix.match:
print COLOR_RESET,
print " -- %d --" % i
color = COLOR_RED
else:
color = COLOR_RESET
print (color + print_pattern) % (
"-" if prefix.match else "",
prefix.vrf.rt,
prefix.type[0].upper(),
((" " * prefix.indent) + prefix.display_prefix)[:min([ len(prefix.display_prefix) + 2*prefix.indent, 30 ])],
(prefix.node or '')[:min([ len(prefix.node or ''), 20 ])],
(prefix.description or '')[:min([ len(prefix.description or ''), 900 ])]
)
if prefix.match:
new_prefix_node = re.sub(pattern, replacement, (prefix.node or ''), flags=re.IGNORECASE)
new_prefix_desc = re.sub(pattern, replacement, (prefix.description or ''), flags=re.IGNORECASE)
print (COLOR_GREEN + print_pattern) % (
"+",
prefix.vrf.rt,
prefix.type[0].upper(),
(" " * prefix.indent + prefix.display_prefix)[:min([ len(prefix.display_prefix) + 2*prefix.indent, 30 ])],
new_prefix_node[:min([ len(new_prefix_node), 20 ])],
new_prefix_desc[:min([ len(new_prefix_desc), 90 ])]
)
# reset colors
print COLOR_RESET,
# Perform action?
print "Select replacements to perform"
print "Enter comma-separated selection (eg. 5,7,10) or \"all\" for all prefixes."
print "Prefix list with ! to invert selection (eg !5,7,10 to perform operation on all except the entered prefixes)"
inp = raw_input("Selection: ").strip()
if len(inp) == 0:
print "Empty selection, quitting."
sys.exit(0)
invert = False
if inp[0] == "!":
inp = inp[1:]
invert = True
rename_all = False
if inp == 'all':
rename_all = True
selection = []
else:
selection = inp.split(",")
try:
selection = map(lambda x: int(x.strip()), selection)
except ValueError as e:
print >> sys.stderr, "Could not parse selection: %s" % str(e)
sys.exit(1)
#.........这里部分代码省略.........
示例4: test_search_prefix
# 需要导入模块: from pynipap import Prefix [as 别名]
# 或者: from pynipap.Prefix import search [as 别名]
def test_search_prefix(self):
""" We should be able to execute search_prefix as read-only user
"""
p = Prefix.search({ 'val1': 'id',
'operator': 'equals',
'val2': 0 })