Skip to content

Commit 3855f25

Browse files
authored
Merge pull request #173 from stevenhua0320/deprecate-parser
chore: deprecate parseLines method in all parsers
2 parents fe2ac51 + 0f4ddda commit 3855f25

File tree

11 files changed

+292
-5
lines changed

11 files changed

+292
-5
lines changed

news/deprecate-parser-1.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
**Added:**
2+
3+
* Added ``parse_lines`` method in ``p_auto.py``
4+
* Added ``parse_lines`` method in ``p_cif.py``
5+
* Added ``parse_lines`` method in ``p_discus.py``
6+
* Added ``parse_lines`` method in ``p_pdb.py``
7+
* Added ``parse_lines`` method in ``p_pdffit.py``
8+
* Added ``parse_lines`` method in ``p_rawxyz.py``
9+
* Added ``parse_lines`` method in ``p_xcfg.py``
10+
* Added ``parse_lines`` method in ``p_xyz.py``
11+
* Added ``parse_lines`` method in ``structureparser.py``
12+
* Added ``_suppress_cif_parser_output`` method in ``p_cif.py``
13+
14+
**Changed:**
15+
16+
* <news item>
17+
18+
**Deprecated:**
19+
20+
* Deprecated ``parseLines`` method in ``p_auto.py`` for removal in version 4.0.0
21+
* Deprecated ``parseLines`` method in ``p_cif.py`` for removal in version 4.0.0
22+
* Deprecated ``parseLines`` method in ``p_discus.py`` for removal in version 4.0.0
23+
* Deprecated ``parseLines`` method in ``p_pdb.py`` for removal in version 4.0.0
24+
* Deprecated ``parseLines`` method in ``p_pdffit.py`` for removal in version 4.0.0
25+
* Deprecated ``parseLines`` method in ``p_rawxyz.py`` for removal in version 4.0.0
26+
* Deprecated ``parseLines`` method in ``p_xcfg.py`` for removal in version 4.0.0
27+
* Deprecated ``parseLines`` method in ``p_xyz.py`` for removal in version 4.0.0
28+
* Deprecated ``parseLines`` method in ``structureparser.py`` for removal in version 4.0.0
29+
30+
**Removed:**
31+
32+
* <news item>
33+
34+
**Fixed:**
35+
36+
* <news item>
37+
38+
**Security:**
39+
40+
* <news item>

src/diffpy/structure/parsers/p_auto.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,20 @@
1818
"""
1919

2020
import os
21+
from typing import Any
2122

2223
from diffpy.structure.parsers import StructureParser, parser_index
2324
from diffpy.structure.structureerrors import StructureFormatError
25+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
26+
27+
base = "diffpy.structure.P_auto"
28+
removal_version = "4.0.0"
29+
parseLines_deprecation_msg = build_deprecation_message(
30+
base,
31+
"parseLines",
32+
"parse_lines",
33+
removal_version,
34+
)
2435

2536

2637
class P_auto(StructureParser):
@@ -76,7 +87,16 @@ def _get_ordered_formats(self):
7687
ofmts.insert(0, fmt)
7788
return ofmts
7889

90+
@deprecated(parseLines_deprecation_msg)
7991
def parseLines(self, lines):
92+
"""This function has been deprecated and will be removed in
93+
version 4.0.0.
94+
95+
Please use diffpy.structure.P_auto.parse_lines instead.
96+
"""
97+
return self.parse_lines(lines)
98+
99+
def parse_lines(self, lines):
80100
"""Detect format and create `Structure` instance from a list of
81101
lines.
82102
@@ -96,7 +116,7 @@ def parseLines(self, lines):
96116
------
97117
StructureFormatError
98118
"""
99-
return self._wrap_parse_method("parseLines", lines)
119+
return self._wrap_parse_method("parse_lines", lines)
100120

101121
def parse(self, s):
102122
"""Detect format and create `Structure` instance from a string.
@@ -145,7 +165,7 @@ def parseFile(self, filename):
145165
self.filename = filename
146166
return self._wrap_parse_method("parseFile", filename)
147167

148-
def _wrap_parse_method(self, method, *args, **kwargs):
168+
def _wrap_parse_method(self, method: object, *args: object, **kwargs: object) -> Any:
149169
"""A helper evaluator method that try the specified parse method
150170
with each registered structure parser and return the first
151171
successful result.

src/diffpy/structure/parsers/p_cif.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,21 @@
3737
from diffpy.structure import Atom, Lattice, Structure
3838
from diffpy.structure.parsers import StructureParser
3939
from diffpy.structure.structureerrors import StructureFormatError
40+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
4041

4142
# ----------------------------------------------------------------------------
4243

4344

45+
base = "diffpy.structure.P_cif"
46+
removal_version = "4.0.0"
47+
parseLines_deprecation_msg = build_deprecation_message(
48+
base,
49+
"parseLines",
50+
"parse_lines",
51+
removal_version,
52+
)
53+
54+
4455
class P_cif(StructureParser):
4556
"""Simple parser for CIF structure format.
4657
@@ -330,7 +341,17 @@ def parse(self, s):
330341
rv = self._parseCifDataSource(fp)
331342
return rv
332343

344+
@deprecated(parseLines_deprecation_msg)
333345
def parseLines(self, lines):
346+
"""This function has been deprecated and will be removed in
347+
version 4.0.0.
348+
349+
Please use diffpy.structure.P_cif.parse_lines instead.
350+
"""
351+
return self.parse_lines(lines)
352+
353+
@deprecated(parseLines_deprecation_msg)
354+
def parse_lines(self, lines):
334355
"""Parse list of lines in CIF format.
335356
336357
Parameters
@@ -400,7 +421,7 @@ def _parseCifDataSource(self, datasource):
400421

401422
self.stru = None
402423
try:
403-
with _suppressCifParserOutput():
424+
with _suppress_cif_parser_output():
404425
# Use `grammar` option to digest values with curly-brackets.
405426
# Ref: https://bitbucket.org/jamesrhester/pycifrw/issues/19
406427
self.ciffile = CifFile(datasource, grammar="auto")
@@ -871,7 +892,7 @@ def getParser(eps=None):
871892

872893

873894
@contextmanager
874-
def _suppressCifParserOutput():
895+
def _suppress_cif_parser_output():
875896
"""Context manager which suppresses diagnostic messages from CIF
876897
parser."""
877898
from CifFile import yapps3_compiled_rt

src/diffpy/structure/parsers/p_discus.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,81 @@ def parseLines(self, lines):
134134
raise e.with_traceback(exc_traceback)
135135
return self.stru
136136

137+
def parse_lines(self, lines):
138+
"""Parse list of lines in DISCUS format.
139+
140+
Parameters
141+
----------
142+
lines : list of str
143+
List of lines from the input file.
144+
145+
Returns
146+
-------
147+
PDFFitStructure
148+
Parsed `PDFFitStructure` instance.
149+
150+
Raises
151+
------
152+
StructureFormatError
153+
If the file is not in DISCUS format.
154+
"""
155+
self.lines = lines
156+
ilines = self._linesIterator()
157+
self.stru = PDFFitStructure()
158+
record_parsers = {
159+
"cell": self._parse_cell,
160+
"format": self._parse_format,
161+
"generator": self._parse_not_implemented,
162+
"molecule": self._parse_not_implemented,
163+
"ncell": self._parse_ncell,
164+
"spcgr": self._parse_spcgr,
165+
"symmetry": self._parse_not_implemented,
166+
"title": self._parse_title,
167+
"shape": self._parse_shape,
168+
}
169+
try:
170+
# parse header
171+
for self.line in ilines:
172+
words = self.line.split()
173+
if not words or words[0][0] == "#":
174+
continue
175+
if words[0] == "atoms":
176+
break
177+
rp = record_parsers.get(words[0], self._parse_unknown_record)
178+
rp(words)
179+
# check if cell has been defined
180+
if not self.cell_read:
181+
emsg = "%d: unit cell not defined" % self.nl
182+
raise StructureFormatError(emsg)
183+
# parse atoms
184+
for self.line in ilines:
185+
words = self.line.replace(",", " ").split()
186+
if not words or words[0][0] == "#":
187+
continue
188+
self._parse_atom(words)
189+
# self consistency check
190+
exp_natoms = reduce(lambda x, y: x * y, self.stru.pdffit["ncell"])
191+
# only check if ncell record exists
192+
if self.ncell_read and exp_natoms != len(self.stru):
193+
emsg = "Expected %d atoms, read %d." % (
194+
exp_natoms,
195+
len(self.stru),
196+
)
197+
raise StructureFormatError(emsg)
198+
# take care of superlattice
199+
if self.stru.pdffit["ncell"][:3] != [1, 1, 1]:
200+
latpars = list(self.stru.lattice.abcABG())
201+
superlatpars = [latpars[i] * self.stru.pdffit["ncell"][i] for i in range(3)] + latpars[3:]
202+
superlattice = Lattice(*superlatpars)
203+
self.stru.place_in_lattice(superlattice)
204+
self.stru.pdffit["ncell"] = [1, 1, 1, exp_natoms]
205+
except (ValueError, IndexError):
206+
exc_type, exc_value, exc_traceback = sys.exc_info()
207+
emsg = "%d: file is not in DISCUS format" % self.nl
208+
e = StructureFormatError(emsg)
209+
raise e.with_traceback(exc_traceback)
210+
return self.stru
211+
137212
def toLines(self, stru):
138213
"""Convert `Structure` stru to a list of lines in DISCUS format.
139214

src/diffpy/structure/parsers/p_pdb.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
from diffpy.structure import Structure
3030
from diffpy.structure.parsers import StructureParser
3131
from diffpy.structure.structureerrors import StructureFormatError
32+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
33+
34+
base = "diffpy.structure.P_pdb"
35+
removal_version = "4.0.0"
36+
parseLines_deprecation_msg = build_deprecation_message(
37+
base,
38+
"parseLines",
39+
"parse_lines",
40+
removal_version,
41+
)
3242

3343

3444
class P_pdb(StructureParser):
@@ -111,7 +121,16 @@ def __init__(self):
111121
self.format = "pdb"
112122
return
113123

124+
@deprecated(parseLines_deprecation_msg)
114125
def parseLines(self, lines):
126+
"""This function has been deprecated and will be removed in
127+
version 4.0.0.
128+
129+
Please use diffpy.structure.P_pdb.parse_lines instead.
130+
"""
131+
return self.parse_lines(lines)
132+
133+
def parse_lines(self, lines):
115134
"""Parse list of lines in PDB format.
116135
117136
Parameters

src/diffpy/structure/parsers/p_pdffit.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
from diffpy.structure import Lattice, PDFFitStructure
2323
from diffpy.structure.parsers import StructureParser
2424
from diffpy.structure.structureerrors import StructureFormatError
25+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
26+
27+
base = "diffpy.structure.P_pdffit"
28+
removal_version = "4.0.0"
29+
parseLines_deprecation_msg = build_deprecation_message(
30+
base,
31+
"parseLines",
32+
"parse_lines",
33+
removal_version,
34+
)
2535

2636

2737
class P_pdffit(StructureParser):
@@ -44,7 +54,16 @@ def __init__(self):
4454
self.stru = None
4555
return
4656

57+
@deprecated(parseLines_deprecation_msg)
4758
def parseLines(self, lines):
59+
"""This function has been deprecated and will be removed in
60+
version 4.0.0.
61+
62+
Please use diffpy.structure.P_pdffit.parse_lines instead.
63+
"""
64+
return self.parse_lines(lines)
65+
66+
def parse_lines(self, lines):
4867
"""Parse list of lines in PDFfit format.
4968
5069
Parameters

src/diffpy/structure/parsers/p_rawxyz.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
from diffpy.structure.parsers import StructureParser
2525
from diffpy.structure.structureerrors import StructureFormatError
2626
from diffpy.structure.utils import isfloat
27+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
28+
29+
base = "diffpy.structure.P_rawxyz"
30+
removal_version = "4.0.0"
31+
parseLines_deprecation_msg = build_deprecation_message(
32+
base,
33+
"parseLines",
34+
"parse_lines",
35+
removal_version,
36+
)
2737

2838

2939
class P_rawxyz(StructureParser):
@@ -40,7 +50,16 @@ def __init__(self):
4050
self.format = "rawxyz"
4151
return
4252

53+
@deprecated(parseLines_deprecation_msg)
4354
def parseLines(self, lines):
55+
"""This function has been deprecated and will be removed in
56+
version 4.0.0.
57+
58+
Please use diffpy.structure.P_rawxyz.parse_lines instead.
59+
"""
60+
return self.parse_lines(lines)
61+
62+
def parse_lines(self, lines):
4463
"""Parse list of lines in RAWXYZ format.
4564
4665
Parameters

src/diffpy/structure/parsers/p_xcfg.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from diffpy.structure.parsers import StructureParser
3030
from diffpy.structure.structureerrors import StructureFormatError
3131
from diffpy.structure.utils import isfloat
32+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
3233

3334
# Constants ------------------------------------------------------------------
3435

@@ -151,6 +152,15 @@
151152

152153
# ----------------------------------------------------------------------------
153154

155+
base = "diffpy.structure.P_xcfg"
156+
removal_version = "4.0.0"
157+
parseLines_deprecation_msg = build_deprecation_message(
158+
base,
159+
"parseLines",
160+
"parse_lines",
161+
removal_version,
162+
)
163+
154164

155165
class P_xcfg(StructureParser):
156166
"""Parser for AtomEye extended CFG format.
@@ -171,7 +181,16 @@ def __init__(self):
171181
self.format = "xcfg"
172182
return
173183

184+
@deprecated(parseLines_deprecation_msg)
174185
def parseLines(self, lines):
186+
"""This function has been deprecated and will be removed in
187+
version 4.0.0.
188+
189+
Please use diffpy.structure.P_xcfg.parse_lines instead.
190+
"""
191+
return self.parse_lines(lines)
192+
193+
def parse_lines(self, lines):
175194
"""Parse list of lines in XCFG format.
176195
177196
Parameters

0 commit comments

Comments
 (0)