Skip to content

Commit 5583f16

Browse files
committed
Use the files API instead of path from importlib.resources
Signed-off-by: Ivan Kanakarakis <ivan.kanak@gmail.com>
1 parent 8e44c77 commit 5583f16

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ install_requires =
5353
pytz
5454
requests >= 1.0.0
5555
six
56-
importlib_resources;python_version<'3.7'
56+
importlib_resources;python_version<'3.9'
5757
xmlschema >= 1.2.1
5858

5959

src/saml2/sigver.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
from subprocess import Popen
1717
from subprocess import PIPE
1818

19+
# importlib.resources was introduced in python 3.7
20+
# files API from importlib.resources introduced in python 3.9
21+
if sys.version_info[:2] >= (3, 9):
22+
from importlib.resources import files as _resource_files
23+
else:
24+
from importlib_resources import files as _resource_files
25+
1926
from OpenSSL import crypto
2027

2128
import pytz
@@ -56,11 +63,6 @@
5663
from saml2.xml.schema import node_to_schema
5764
from saml2.xml.schema import XMLSchemaError
5865

59-
# importlib.resources was introduced in python 3.7
60-
if sys.version_info[:2] >= (3, 7):
61-
from importlib.resources import path as _resource_path
62-
else:
63-
from importlib_resources import path as _resource_path
6466

6567
logger = logging.getLogger(__name__)
6668

@@ -1306,8 +1308,8 @@ def __init__(
13061308
self.only_use_keys_in_metadata = only_use_keys_in_metadata
13071309

13081310
if not template:
1309-
with _resource_path(_data_template, "template_enc.xml") as fp:
1310-
self.template = str(fp)
1311+
fp = str(_resource_files(_data_template).joinpath("template_enc.xml"))
1312+
self.template = str(fp)
13111313
else:
13121314
self.template = template
13131315

src/saml2/xml/schema/__init__.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import sys
22

3+
# importlib.resources was introduced in python 3.7
4+
# files API from importlib.resources introduced in python 3.9
5+
if sys.version_info[:2] >= (3, 9):
6+
from importlib.resources import files as _resource_files
7+
else:
8+
from importlib_resources import files as _resource_files
9+
310
from xmlschema import XMLSchema as _XMLSchema
411
from xmlschema.exceptions import XMLSchemaException as XMLSchemaError
512

613
import saml2.data.schemas as _data_schemas
714

8-
# importlib.resources was introduced in python 3.7
9-
if sys.version_info[:2] >= (3, 7):
10-
from importlib.resources import path as _resource_path
11-
else:
12-
from importlib_resources import path as _resource_path
1315

1416
def _create_xml_schema_validator(source, **kwargs):
1517
kwargs = {
@@ -23,37 +25,34 @@ def _create_xml_schema_validator(source, **kwargs):
2325
return _XMLSchema(source, **kwargs)
2426

2527

26-
with _resource_path(_data_schemas, "xml.xsd") as fp:
27-
_path_schema_xml = str(fp)
28-
with _resource_path(_data_schemas, "envelope.xsd") as fp:
29-
_path_schema_envelope = str(fp)
30-
with _resource_path(_data_schemas, "xenc-schema.xsd") as fp:
31-
_path_schema_xenc = str(fp)
32-
with _resource_path(_data_schemas, "xmldsig-core-schema.xsd") as fp:
33-
_path_schema_xmldsig_core = str(fp)
34-
with _resource_path(_data_schemas, "saml-schema-assertion-2.0.xsd") as fp:
35-
_path_schema_saml_assertion = str(fp)
36-
with _resource_path(_data_schemas, "saml-schema-metadata-2.0.xsd") as fp:
37-
_path_schema_saml_metadata = str(fp)
38-
with _resource_path(_data_schemas, "saml-schema-protocol-2.0.xsd") as fp:
39-
_path_schema_saml_protocol = str(fp)
28+
_schema_resources = _resource_files(_data_schemas)
29+
_path_schema_xml = str(_schema_resources.joinpath("xml.xsd"))
30+
_path_schema_envelope = str(_schema_resources.joinpath("envelope.xsd"))
31+
_path_schema_xenc = str(_schema_resources.joinpath("xenc-schema.xsd"))
32+
_path_schema_xmldsig_core = str(_schema_resources.joinpath("xmldsig-core-schema.xsd"))
33+
_path_schema_saml_assertion = str(
34+
_schema_resources.joinpath("saml-schema-assertion-2.0.xsd")
35+
)
36+
_path_schema_saml_metadata = str(
37+
_schema_resources.joinpath("saml-schema-metadata-2.0.xsd")
38+
)
39+
_path_schema_saml_protocol = str(
40+
_schema_resources.joinpath("saml-schema-protocol-2.0.xsd")
41+
)
4042

4143
_locations = {
4244
"http://www.w3.org/XML/1998/namespace": _path_schema_xml,
4345
"http://schemas.xmlsoap.org/soap/envelope/": _path_schema_envelope,
4446
"http://www.w3.org/2001/04/xmlenc#": _path_schema_xenc,
4547
"http://www.w3.org/2000/09/xmldsig#": _path_schema_xmldsig_core,
4648
"urn:oasis:names:tc:SAML:2.0:assertion": _path_schema_saml_assertion,
49+
"urn:oasis:names:tc:SAML:2.0:metadata": _path_schema_saml_metadata,
4750
"urn:oasis:names:tc:SAML:2.0:protocol": _path_schema_saml_protocol,
4851
}
4952

50-
with _resource_path(_data_schemas, "saml-schema-assertion-2.0.xsd") as fp:
51-
schema_saml_assertion = _create_xml_schema_validator(str(fp))
52-
with _resource_path(_data_schemas, "saml-schema-metadata-2.0.xsd") as fp:
53-
schema_saml_metadata = _create_xml_schema_validator(str(fp))
54-
with _resource_path(_data_schemas, "saml-schema-protocol-2.0.xsd") as fp:
55-
schema_saml_protocol = _create_xml_schema_validator(str(fp))
56-
53+
schema_saml_assertion = _create_xml_schema_validator(_path_schema_saml_assertion)
54+
schema_saml_metadata = _create_xml_schema_validator(_path_schema_saml_metadata)
55+
schema_saml_protocol = _create_xml_schema_validator(_path_schema_saml_protocol)
5756

5857
node_to_schema = {
5958
# AssertionType

0 commit comments

Comments
 (0)