Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/psycopg2/_ipaddress.py: 22%
27 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""Implementation of the ipaddres-based network types adaptation
2"""
4# psycopg/_ipaddress.py - Ipaddres-based network types adaptation
5#
6# Copyright (C) 2016-2019 Daniele Varrazzo <daniele.varrazzo@gmail.com>
7# Copyright (C) 2020-2021 The Psycopg Team
8#
9# psycopg2 is free software: you can redistribute it and/or modify it
10# under the terms of the GNU Lesser General Public License as published
11# by the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# In addition, as a special exception, the copyright holders give
15# permission to link this program with the OpenSSL library (or with
16# modified versions of OpenSSL that use the same license as OpenSSL),
17# and distribute linked combinations including the two.
18#
19# You must obey the GNU Lesser General Public License in all respects for
20# all of the code used other than OpenSSL.
21#
22# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
23# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
25# License for more details.
27from psycopg2.extensions import (
28 new_type, new_array_type, register_type, register_adapter, QuotedString)
30# The module is imported on register_ipaddress
31ipaddress = None
33# The typecasters are created only once
34_casters = None
37def register_ipaddress(conn_or_curs=None):
38 """
39 Register conversion support between `ipaddress` objects and `network types`__.
41 :param conn_or_curs: the scope where to register the type casters.
42 If `!None` register them globally.
44 After the function is called, PostgreSQL :sql:`inet` values will be
45 converted into `~ipaddress.IPv4Interface` or `~ipaddress.IPv6Interface`
46 objects, :sql:`cidr` values into into `~ipaddress.IPv4Network` or
47 `~ipaddress.IPv6Network`.
49 .. __: https://www.postgresql.org/docs/current/static/datatype-net-types.html
50 """
51 global ipaddress
52 import ipaddress
54 global _casters
55 if _casters is None:
56 _casters = _make_casters()
58 for c in _casters:
59 register_type(c, conn_or_curs)
61 for t in [ipaddress.IPv4Interface, ipaddress.IPv6Interface,
62 ipaddress.IPv4Network, ipaddress.IPv6Network]:
63 register_adapter(t, adapt_ipaddress)
66def _make_casters():
67 inet = new_type((869,), 'INET', cast_interface)
68 ainet = new_array_type((1041,), 'INET[]', inet)
70 cidr = new_type((650,), 'CIDR', cast_network)
71 acidr = new_array_type((651,), 'CIDR[]', cidr)
73 return [inet, ainet, cidr, acidr]
76def cast_interface(s, cur=None):
77 if s is None:
78 return None
79 # Py2 version force the use of unicode. meh.
80 return ipaddress.ip_interface(str(s))
83def cast_network(s, cur=None):
84 if s is None:
85 return None
86 return ipaddress.ip_network(str(s))
89def adapt_ipaddress(obj):
90 return QuotedString(str(obj))