def __call__(self, data: str):
     try:
         url = URL(data)
     except ValueError:
         raise ValidationError(_("URL cannot be parsed"),
                               code="parse_error")
     if url.has_query_param('db'):
         if not url.query_param('db').isdigit():
             raise ValidationError(_("Invalid port specified"),
                                   code="invalid_port")
     if url.scheme() == "unix":
         if url.host():
             raise ValidationError(
                 _("Hostname not supported for unix domain sockets"),
                 code="unix_domain_socket_hostname")
         if url.port():
             raise ValidationError(
                 _("Port not supported for unix domain sockets"),
                 code="unix_domain_socket_port")
         if not url.path():
             raise ValidationError(
                 _("No path specified for unix domain socket"),
                 code="unix_domain_socket_path")
     if url.scheme() in ("redis", "redis+tls"):
         if not url.host():
             raise ValidationError(_("No host specified"),
                                   code="host_missing")
Example #2
0
u = URL('postgres://*****:*****@localhost:1234/test?ssl=true')
print(u.scheme())
print(u.host())
print(u.domain())
print(u.username())
print(u.password())
print(u.netloc())
print(u.port())
print(u.path())
print(u.query())
print(u.path_segments())
print(u.query_param('ssl'))
print(u.query_param('ssl', as_list=True))
print(u.query_params())
print(u.has_query_param('ssl'))
print(u.subdomains())

u = URL.from_string('https://github.com/minwook-shin')
print(u.path_segment(0))

new_url = u.add_path_segment('minwook-shin.github.com')
print(new_url.as_string())

from purl import expand
print(expand(u"{/path*}", {'path': ['sub', 'index']}))

from purl import Template
template = Template("http://example.com{/path*}")
url = template.expand({'path': ['sub', 'index']})
print(url.as_string())