Debian Codegraph @

lib/ansible/plugins/test/uri.py

raw
1 # (c) Ansible Project
2
3 from __future__ import annotations
4
5 from urllib.parse import urlparse
6
7
8 def is_uri(value, schemes=None):
9 """ Will verify that the string passed is a valid 'URI', if given a list of valid schemes it will match those """
10 try:
11 x = urlparse(value)
12 isit = all([x.scheme is not None, x.path is not None, not schemes or x.scheme in schemes])
13 except Exception as e:
14 isit = False
15 return isit
16
17
18 def is_url(value, schemes=None):
19 """ Will verify that the string passed is a valid 'URL' """
20
21 isit = is_uri(value, schemes)
22 if isit:
23 x = urlparse(value)
24 isit = bool(x.netloc or x.scheme == 'file')
25 return isit
26
27
28 def is_urn(value):
29 return is_uri(value, ['urn'])
30
31
32 class TestModule(object):
33 """ Ansible URI jinja2 test """
34
35 def tests(self):
36 return {
37 # file testing
38 'uri': is_uri,
39 'url': is_url,
40 'urn': is_urn,
41 }