Skip to content

Commit 383ec81

Browse files
committed
Handle deprecation/removal of pkg_resources.
Add `resource_string.py` that provides a compatibility layer between versions of Python that do and don't have the `pkg_resources` library, removed in version 3.12.
1 parent 1e8fc77 commit 383ec81

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

popper/gen2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
import clingo
77
import clingo.script
8-
import pkg_resources
98
from clingo import Function, Number, Tuple_
109

1110
from .util import Constraint, Literal, Settings
1211
from . abs_generate import Generator as AbstractGenerator
12+
from .resources import resource_string
1313

1414

1515
def arg_to_symbol(arg):
@@ -47,7 +47,7 @@ def __init__(self, settings: Settings, bkcons=[]):
4747
self.pruned_sizes = set()
4848

4949
encoding = []
50-
alan = pkg_resources.resource_string(__name__, "lp/alan.pl").decode()
50+
alan = resource_string(__name__, "lp/alan.pl").decode()
5151
encoding.append(alan)
5252

5353
with open(settings.bias_file) as f:

popper/gen3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import operator
88
import numbers
99
import clingo.script
10-
import pkg_resources
1110
from collections import defaultdict
1211
from . util import rule_is_recursive, Constraint, Literal
1312
from . abs_generate import Generator as AbstractGenerator
13+
from .resources import resource_string
1414

1515
clingo.script.enable_python()
1616
from clingo import Function, Number, Tuple_
@@ -56,7 +56,7 @@ def __init__(self, settings, bkcons=[]):
5656
self.new_ground_cons = set()
5757

5858
encoding = []
59-
alan = pkg_resources.resource_string(__name__, "lp/alan-old.pl").decode()
59+
alan = resource_string(__name__, "lp/alan-old.pl").decode()
6060
encoding.append(alan)
6161

6262
with open(settings.bias_file) as f:
@@ -717,4 +717,4 @@ def remap_variables(rule):
717717
new_atom = Literal(atom.predicate, tuple(new_args))
718718
new_body.add(new_atom)
719719

720-
return head, frozenset(new_body)
720+
return head, frozenset(new_body)

popper/generate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import clingo
1111
import clingo.script
12-
import pkg_resources
1312

1413
from .util import rule_is_recursive, Constraint, Literal, remap_variables
1514

@@ -19,6 +18,7 @@
1918
import dataclasses
2019
from . abs_generate import Generator as AbstractGenerator
2120
from . abs_generate import Rule, RuleBase
21+
from .resources import resource_string
2222

2323
@dataclasses.dataclass(frozen=True)
2424
class Var:
@@ -109,7 +109,7 @@ def __init__(self, settings, bkcons=[]):
109109
self.new_ground_cons = set()
110110

111111
encoding = []
112-
alan = pkg_resources.resource_string(__name__, "lp/alan-old.pl").decode()
112+
alan = resource_string(__name__, "lp/alan-old.pl").decode()
113113
# alan = pkg_resources.resource_string(__name__, "lp/alan.pl").decode()
114114
encoding.append(alan)
115115

@@ -1204,4 +1204,4 @@ def gteq(a, b):
12041204
return Literal('>=', (a,b))
12051205

12061206
def body_size_literal(clause_var, body_size):
1207-
return Literal('body_size', (clause_var, body_size))
1207+
return Literal('body_size', (clause_var, body_size))

popper/resources.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Dict, Any
2+
3+
4+
try:
5+
from pkg_resources import resource_string, resource_filename
6+
def close_resource_file(ref: Any):
7+
pass
8+
except:
9+
import importlib.resources
10+
from contextlib import ExitStack
11+
from importlib.resources.abc import Traversable
12+
def resource_string(package: str, path: str) -> bytes:
13+
ref = importlib.resources.files(package).joinpath(path)
14+
with ref.open('rb') as fp:
15+
my_bytes = fp.read()
16+
return my_bytes
17+
managers: Dict[Traversable, ExitStack] = {}
18+
def resource_filename(pkg: str, path: str) -> Traversable:
19+
manager = ExitStack()
20+
ref = manager.enter_context(importlib.resources.files(pkg) / path)
21+
managers[ref] = manager
22+
return ref
23+
def close_resource_file(ref: Traversable):
24+
if ref in managers:
25+
managers[ref].close()
26+
del managers[ref]

popper/tester.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from itertools import product
66
from typing import Tuple
77

8-
import pkg_resources
98
from bitarray import bitarray, frozenbitarray
109
from bitarray.util import ones
1110
from janus_swi import consult, query_once
1211

1312
from .util import Literal, calc_prog_size, calc_rule_size, format_rule, order_prog, prog_hash, prog_is_recursive
13+
from .resources import resource_string, resource_filename, close_resource_file
1414

1515

1616
def format_literal_janus(literal):
@@ -27,7 +27,7 @@ def __init__(self, settings):
2727

2828
bk_pl_path = self.settings.bk_file
2929
exs_pl_path = self.settings.ex_file
30-
test_pl_path = pkg_resources.resource_filename(__name__, "lp/test.pl")
30+
test_pl_path = resource_filename(__name__, "lp/test.pl")
3131

3232
if not settings.pi_enabled:
3333
consult('prog', f':- dynamic {settings.head_literal.predicate}/{len(settings.head_literal.arguments)}.')
@@ -36,6 +36,7 @@ def __init__(self, settings):
3636
if os.name == 'nt': # if on Windows, SWI requires escaped directory separators
3737
x = x.replace('\\', '\\\\')
3838
consult(x)
39+
close_resource_file(x)
3940

4041
query_once('load_examples')
4142

@@ -521,4 +522,4 @@ def deduce_neg_example_recalls(settings, atoms):
521522
return all_recalls
522523

523524
def generate_binary_strings(bit_count):
524-
return list(product((0,1), repeat=bit_count))[1:-1]
525+
return list(product((0,1), repeat=bit_count))[1:-1]

0 commit comments

Comments
 (0)