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
e41e36b1
Commit
e41e36b1
authored
8 years ago
by
James Graham
Browse files
Options
Downloads
Patches
Plain Diff
Add proper error message if a config file contains no sections
parent
577dfc6b
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
-4
17 additions, 4 deletions
pycgtool/parsers/cfg.py
test/test_parsers_cfg.py
+5
-1
5 additions, 1 deletion
test/test_parsers_cfg.py
with
22 additions
and
5 deletions
pycgtool/parsers/cfg.py
+
17
−
4
View file @
e41e36b1
...
...
@@ -8,12 +8,22 @@ import os
import
collections
class
DuplicateSectionError
(
Exception
):
class
DuplicateSectionError
(
KeyError
):
"""
Exception used to indicate that a section has appeared twice in a file.
"""
def
__repr__
(
self
):
return
"
Section {0} appears twice in file {1}.
"
.
format
(
*
self
.
args
)
def
__init__
(
self
,
section
,
filename
):
msg
=
"
Section
'
{0}
'
appears twice in file
'
{1}
'
.
"
.
format
(
section
,
filename
)
super
(
DuplicateSectionError
,
self
).
__init__
(
msg
)
class
NoSectionError
(
KeyError
):
"""
Exception used to indicate that a file contains no sections.
"""
def
__init__
(
self
,
filename
):
msg
=
"
File
'
{0}
'
contains no
'
[]
'
section headers.
"
.
format
(
filename
)
super
(
NoSectionError
,
self
).
__init__
(
msg
)
class
CFG
(
collections
.
OrderedDict
):
...
...
@@ -55,7 +65,10 @@ class CFG(collections.OrderedDict):
continue
toks
=
tuple
(
line
.
split
())
try
:
self
[
curr_section
].
append
(
toks
)
except
KeyError
as
e
:
raise
NoSectionError
(
self
.
filename
)
from
e
def
__enter__
(
self
):
return
self
...
...
This diff is collapsed.
Click to expand it.
test/test_parsers_cfg.py
+
5
−
1
View file @
e41e36b1
import
unittest
from
pycgtool.parsers.cfg
import
DuplicateSectionError
from
pycgtool.parsers.cfg
import
DuplicateSectionError
,
NoSectionError
from
pycgtool.parsers
import
CFG
...
...
@@ -38,6 +38,10 @@ class TestParsersCFG(unittest.TestCase):
self
.
assertTrue
(
"
DOPC
"
in
cfg
)
self
.
assertTrue
(
"
GLY
"
in
cfg
)
def
test_error_no_sections
(
self
):
with
self
.
assertRaises
(
NoSectionError
):
CFG
(
"
test/data/nosections.cfg
"
)
if
__name__
==
'
__main__
'
:
unittest
.
main
()
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