Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
pycgtool
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
James Graham
pycgtool
Commits
55d5faac
Verified
Commit
55d5faac
authored
4 years ago
by
James Graham
Browse files
Options
Downloads
Patches
Plain Diff
style: resolve mypy complaints in parsers
parent
1086fe1d
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
pycgtool/parsers/cfg.py
+17
-24
17 additions, 24 deletions
pycgtool/parsers/cfg.py
pycgtool/parsers/itp.py
+10
-9
10 additions, 9 deletions
pycgtool/parsers/itp.py
with
27 additions
and
33 deletions
pycgtool/parsers/cfg.py
+
17
−
24
View file @
55d5faac
...
...
@@ -8,6 +8,8 @@ import contextlib
import
pathlib
import
typing
PathLike
=
typing
.
Union
[
str
,
pathlib
.
Path
]
class
DuplicateSectionError
(
KeyError
):
"""
Exception used to indicate that a section has appeared twice in a file.
"""
...
...
@@ -28,53 +30,44 @@ class CFG(collections.OrderedDict, contextlib.AbstractContextManager):
Contains a dictionary of Sections.
"""
def
__init__
(
self
,
filename
:
typing
.
Optional
[
typing
.
Union
[
str
,
pathlib
.
Path
]]
=
None
):
"""
Parse a config file and extract Sections.
:param filename: Path of file to read
"""
def
__init__
(
self
,
filepath
:
typing
.
Optional
[
PathLike
]
=
None
):
"""
Parse a config file and extract Sections.
"""
super
().
__init__
()
self
.
file
name
=
None
self
.
file
path
=
None
if
file
name
is
not
None
:
self
.
file
name
=
pathlib
.
Path
(
file
name
)
self
.
_read_file
()
if
file
path
is
not
None
:
self
.
file
path
=
pathlib
.
Path
(
file
path
)
self
.
_read_file
(
self
.
filepath
)
def
_read_line
(
self
,
line
:
str
)
->
typing
.
Optional
[
str
]
:
def
_read_line
(
self
,
line
:
str
,
filepath
:
pathlib
.
Path
)
->
str
:
# Strip comments
line
=
line
.
split
(
'
;
'
)[
0
].
strip
()
if
not
line
:
return
None
# Handle include directive
if
line
.
startswith
(
'
#include
'
):
include_file
=
line
.
split
()[
1
].
strip
(
'"'
)
other
=
type
(
self
)(
self
.
filename
.
parent
.
joinpath
(
include_file
))
other
=
type
(
self
)(
filepath
.
parent
.
joinpath
(
include_file
))
self
.
update
(
other
)
return
No
ne
return
''
# Handle include then treat as empty li
ne
return
line
def
_read_file
(
self
)
->
None
:
with
open
(
self
.
filename
)
as
cfg_file
:
def
_read_file
(
self
,
filepath
:
pathlib
.
Path
)
->
None
:
with
open
(
filepath
)
as
cfg_file
:
curr_section
=
None
for
line
in
cfg_file
:
line
=
self
.
_read_line
(
line
)
line
=
self
.
_read_line
(
line
,
filepath
)
if
line
is
No
ne
:
if
not
li
ne
:
continue
if
line
.
startswith
(
"
[
"
):
curr_section
=
line
.
strip
(
"
[ ]
"
)
if
curr_section
in
self
:
raise
DuplicateSectionError
(
curr_section
,
self
.
filename
)
raise
DuplicateSectionError
(
curr_section
,
filepath
)
self
[
curr_section
]
=
[]
...
...
@@ -86,7 +79,7 @@ class CFG(collections.OrderedDict, contextlib.AbstractContextManager):
self
[
curr_section
].
append
(
toks
)
except
KeyError
as
exc
:
raise
NoSectionError
(
self
.
filename
)
from
exc
raise
NoSectionError
(
filepath
)
from
exc
def
__exit__
(
self
,
exc_type
,
exc_value
,
traceback
):
pass
This diff is collapsed.
Click to expand it.
pycgtool/parsers/itp.py
+
10
−
9
View file @
55d5faac
...
...
@@ -2,6 +2,7 @@
import
collections
import
logging
import
pathlib
from
.cfg
import
CFG
,
NoSectionError
,
DuplicateSectionError
...
...
@@ -9,21 +10,21 @@ logger = logging.getLogger(__name__)
class
ITP
(
CFG
):
"""
Class representing an .itp file
"""
Class representing an .itp file
.
Contains a dictionary for every molecule definition
Contains a dictionary
of sections
for every molecule definition
.
"""
def
_read_file
(
self
)
->
None
:
def
_read_file
(
self
,
filepath
:
pathlib
.
Path
)
->
None
:
mol_sections
=
[
"
atoms
"
,
"
bonds
"
,
"
angles
"
,
"
dihedrals
"
]
with
open
(
self
.
filename
)
as
itp_file
:
with
open
(
filepath
)
as
itp_file
:
curr_section
=
None
curr_mol
=
None
for
line
in
itp_file
:
line
=
self
.
_read_line
(
line
)
line
=
self
.
_read_line
(
line
,
filepath
)
if
line
is
No
ne
:
if
not
li
ne
:
continue
if
line
.
startswith
(
"
[
"
):
...
...
@@ -42,7 +43,7 @@ class ITP(CFG):
curr_mol
=
toks
[
0
]
if
curr_mol
in
self
:
raise
DuplicateSectionError
(
curr_mol
,
self
.
filename
)
raise
DuplicateSectionError
(
curr_mol
,
filepath
)
self
[
curr_mol
]
=
collections
.
OrderedDict
()
...
...
@@ -50,8 +51,8 @@ class ITP(CFG):
self
[
curr_mol
][
curr_section
].
append
(
toks
)
elif
curr_section
is
None
:
raise
NoSectionError
(
self
.
filename
)
raise
NoSectionError
(
filepath
)
else
:
logger
.
info
(
"
File
'
%s
'
contains unexpected section
'
%s
'"
,
self
.
filename
,
curr_section
)
filepath
,
curr_section
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment