Skip to content

Commit 4968c25

Browse files
committed
Move dzn parser into dzn extra
1 parent d521c89 commit 4968c25

File tree

8 files changed

+39
-17
lines changed

8 files changed

+39
-17
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Added
1616

1717
Changed
1818
^^^^^^^
19+
- The DZN parser functionality has been moved into the ``dzn`` extra. If your
20+
application requires parsed ``dzn`` information, then you have to ensure your
21+
MiniZinc Python is installed with this extra enabled:
22+
``pip install minizinc[dzn]``.
1923
- ``Solver`` has been turned into a ``dataclass`` and has been updated with all
2024
attributes used in the compiler.
2125

docs/getting_started.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ installing MiniZinc Python is using the following command:
3333
On machines that have both Python 2 and Python 3 installed you might have to
3434
use ``pip3`` instead of ``pip``
3535

36+
.. note::
37+
38+
If you require the parsed information of ``.dzn`` files within your python
39+
environment, then you have to install the ``dzn`` extra with the MiniZinc
40+
package: ``pip install minizinc[dzn]``
41+
3642
A basic example
3743
---------------
3844

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
],
3939
install_requires=[
4040
"dataclasses>=0.6.0; python_version < '3.7'",
41-
"lark-parser>=0.7.5",
42-
"pygments>=2.5",
4341
],
42+
extras_require={
43+
"dzn": ["lark-parser>=0.7.5"],
44+
},
4445
entry_points='''
4546
[pygments.lexers]
4647
minizinclexer = minizinc.pygments:MiniZincLexer

src/minizinc/CLI/instance.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
from typing import Any, Dict, Iterator, List, Optional, Type, cast
2020

2121
import minizinc
22-
from minizinc.dzn import UnknownExpression
2322
from minizinc.error import parse_error
2423
from minizinc.instance import Instance
2524
from minizinc.json import MZNJSONEncoder
26-
from minizinc.model import Method, Model, ParPath
25+
from minizinc.model import Method, Model, ParPath, UnknownExpression
2726
from minizinc.result import Result, Status, parse_solution, set_stat
2827
from minizinc.solver import Solver
2928

src/minizinc/dzn.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from lark import Lark, Transformer
99

10+
from minizinc.model import UnknownExpression
11+
1012
dzn_grammar = r"""
1113
items: [item (";" item)* ";"?]
1214
item: ident "=" value | ident "=" unknown
@@ -39,10 +41,6 @@
3941
"""
4042

4143

42-
class UnknownExpression(str):
43-
pass
44-
45-
4644
def arg1_construct(cls):
4745
return lambda self, s: cls(s[0])
4846

src/minizinc/model.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
from pathlib import Path
1010
from typing import Any, Dict, List, Optional, Type, Union
1111

12-
from lark.exceptions import LarkError
13-
14-
from .dzn import parse_dzn
15-
1612
ParPath = Union[Path, str]
1713

1814

@@ -34,7 +30,7 @@ def from_string(cls, s: str):
3430
"""Get Method represented by the string s.
3531
3632
Args:
37-
s:
33+
s: String expected to contain either "sat", "min", or "max".
3834
3935
Returns:
4036
Method: Method represented by s
@@ -51,6 +47,10 @@ def from_string(cls, s: str):
5147
)
5248

5349

50+
class UnknownExpression(str):
51+
pass
52+
53+
5454
class Model:
5555
"""The representation of a MiniZinc model in Python
5656
@@ -150,7 +150,8 @@ def add_file(self, file: ParPath, parse_data: bool = True) -> None:
150150
Args:
151151
file (Union[Path, str]): Path to the file to be added
152152
parse_data (bool): Signal if the data should be parsed for usage
153-
within Python.
153+
within Python. This option is ignored if the extra `dzn` is
154+
not enabled.
154155
Raises:
155156
MiniZincError: when an error occurs during the parsing or
156157
type checking of the model object.
@@ -166,8 +167,12 @@ def add_file(self, file: ParPath, parse_data: bool = True) -> None:
166167
data = json.load(file.open())
167168
for k, v in data.items():
168169
self.__setitem__(k, v)
169-
elif file.suffix == ".dzn":
170+
elif file.suffix == ".dzn" and parse_data:
170171
try:
172+
from lark.exceptions import LarkError
173+
174+
from .dzn import parse_dzn
175+
171176
data = parse_dzn(file)
172177
for k, v in data.items():
173178
self.__setitem__(k, v)
@@ -176,9 +181,12 @@ def add_file(self, file: ParPath, parse_data: bool = True) -> None:
176181
f"Could not parse {file}. Parameters included within this file are "
177182
f"not available in Python"
178183
)
184+
except ImportError:
185+
pass
186+
finally:
179187
with self._lock:
180188
self._includes.append(file)
181-
elif file.suffix not in [".mzn", ".mzc"]:
189+
elif file.suffix not in [".dzn", ".mzn", ".mzc"]:
182190
raise NameError("Unknown file suffix %s", file.suffix)
183191
else:
184192
with self._lock:

tests/test_dzn.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5-
from minizinc.dzn import UnknownExpression, parse_dzn
5+
import pytest
6+
7+
from minizinc.model import UnknownExpression
8+
9+
lark = pytest.importorskip("lark")
10+
from minizinc.dzn import parse_dzn # noqa: E402
611

712

813
def test_dzn_empty():

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ envlist =
1010
[testenv]
1111
deps = pytest
1212
commands = pytest {posargs}
13+
extras = dzn
1314

1415
[testenv:check]
1516
deps =

0 commit comments

Comments
 (0)