| 1 |
# Copyright 2009 Shikhar Bhushan |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
'Boilerplate ugliness' |
| 16 |
|
| 17 |
from ncclient.xml_ import * |
| 18 |
from ncclient.operations.errors import OperationError, MissingCapabilityError |
| 19 |
try: |
| 20 |
from urlparse import urlparse |
| 21 |
except: |
| 22 |
from urllib.parse import urlparse |
| 23 |
|
| 24 |
def one_of(*args): |
| 25 |
"Verifies that only one of the arguments is not None" |
| 26 |
for i, arg in enumerate(args): |
| 27 |
if arg is not None: |
| 28 |
for argh in args[i+1:]: |
| 29 |
if argh is not None: |
| 30 |
raise OperationError("Too many parameters") |
| 31 |
else: |
| 32 |
return |
| 33 |
raise OperationError("Insufficient parameters") |
| 34 |
|
| 35 |
def datastore_or_url(wha, loc, capcheck=None): |
| 36 |
node = new_ele(wha) |
| 37 |
if "://" in loc: # e.g. http://, file://, ftp:// |
| 38 |
if capcheck is not None: |
| 39 |
capcheck(":url") # url schema check at some point! |
| 40 |
sub_ele(node, "url").text = loc |
| 41 |
else: |
| 42 |
#if loc == 'candidate': |
| 43 |
# capcheck(':candidate') |
| 44 |
#elif loc == 'startup': |
| 45 |
# capcheck(':startup') |
| 46 |
#elif loc == 'running' and wha == 'target': |
| 47 |
# capcheck(':writable-running') |
| 48 |
sub_ele(node, loc) |
| 49 |
return node |
| 50 |
|
| 51 |
def build_filter(spec, capcheck=None): |
| 52 |
type = None |
| 53 |
if isinstance(spec, tuple): |
| 54 |
type, criteria = spec |
| 55 |
if type == "xpath": |
| 56 |
if isinstance(criteria, tuple): |
| 57 |
ns, select = criteria |
| 58 |
rep = new_ele_nsmap("filter", ns, type=type) |
| 59 |
rep.attrib["select"] = select |
| 60 |
else: |
| 61 |
rep = new_ele("filter", type=type) |
| 62 |
rep.attrib["select"]=criteria |
| 63 |
elif type == "subtree": |
| 64 |
rep = new_ele("filter", type=type) |
| 65 |
rep.append(to_ele(criteria)) |
| 66 |
else: |
| 67 |
raise OperationError("Invalid filter type") |
| 68 |
elif isinstance(spec, list): |
| 69 |
rep = new_ele("filter", type="subtree") |
| 70 |
for cri in spec: |
| 71 |
rep.append(to_ele(cri)) |
| 72 |
else: |
| 73 |
|
| 74 |
rep = validated_element(spec, ("filter", qualify("filter"), |
| 75 |
qualify("filter", ns=NETCONF_NOTIFICATION_NS))) |
| 76 |
# results in XMLError: line 105 ncclient/xml_.py - commented by earies - 5/10/13 |
| 77 |
#rep = validated_element(spec, ("filter", qualify("filter")), |
| 78 |
# attrs=("type",)) |
| 79 |
# TODO set type var here, check if select attr present in case of xpath.. |
| 80 |
if type == "xpath" and capcheck is not None: |
| 81 |
capcheck(":xpath") |
| 82 |
return rep |
| 83 |
|
| 84 |
def validate_args(arg_name, value, args_list): |
| 85 |
# this is a common method, which used to check whether a value is in args_list |
| 86 |
if value not in args_list: |
| 87 |
raise OperationError('Invalid value "%s" in "%s" element' % (value, arg_name)) |
| 88 |
return True |
| 89 |
|
| 90 |
def url_validator(url): |
| 91 |
try: |
| 92 |
result = urlparse(url) |
| 93 |
return all([result.scheme, result.netloc]) |
| 94 |
except: |
| 95 |
return False |