Skip to content

Commit

Permalink
Merge pull request #9 from chrisburr/moderisations
Browse files Browse the repository at this point in the history
Fix setup.cfg and apply suggestions from ruff
  • Loading branch information
chrisburr authored Feb 20, 2023
2 parents 0d86881 + 264403f commit 5e6a0ba
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool:pytest]
addopts = "--cov=diraccfg --cov-report=term-missing -rx -v --color=yes --tb=long"
addopts=--cov=diraccfg --cov-report=term-missing -rx -v --color=yes --tb=long

[pep8]
# Pep8 codes:
Expand Down
41 changes: 17 additions & 24 deletions src/diraccfg/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
import threading


# Avoid using six as this file should be usable without any dependencies
try:
string_types = (basestring,)
except NameError:
string_types = (str,)


def S_ERROR(messageString=""):
return {"OK": False, "Message": str(messageString)}

Expand All @@ -26,7 +19,7 @@ def S_OK(value=""):
class ListDummy:
def fromChar(self, inputString, sepChar=","):
if not (
isinstance(inputString, string_types) and isinstance(sepChar, string_types) and sepChar
isinstance(inputString, str) and isinstance(sepChar, str) and sepChar
): # to prevent getting an empty String as argument
return None

Expand Down Expand Up @@ -110,7 +103,7 @@ def createNewSection(self, sectionName, comment="", contents=False):
if not recDict:
return S_ERROR(f"Parent section does not exist {sectionName}")
parentSection = recDict["value"]
if isinstance(parentSection, string_types):
if isinstance(parentSection, str):
raise KeyError(f"Entry {recDict['key']} doesn't seem to be a section")
return parentSection.createNewSection(recDict["levelsBelow"], comment, contents)
self.__addEntry(sectionName, comment)
Expand Down Expand Up @@ -155,7 +148,7 @@ def setOption(self, optionName, value, comment=""):
if not recDict:
return S_ERROR(f"Parent section does not exist {optionName}")
parentSection = recDict["value"]
if isinstance(parentSection, string_types):
if isinstance(parentSection, str):
raise KeyError(f"Entry {recDict['key']} doesn't seem to be a section")
return parentSection.setOption(recDict["levelsBelow"], value, comment)
self.__addEntry(optionName, comment)
Expand Down Expand Up @@ -269,9 +262,9 @@ def listOptions(self, ordered=True):
:return: List with the option names
"""
if ordered:
return [sKey for sKey in self.__orderedList if isinstance(self.__dataDict[sKey], string_types)]
return [sKey for sKey in self.__orderedList if isinstance(self.__dataDict[sKey], str)]
else:
return [sKey for sKey in self.__dataDict.keys() if isinstance(self.__dataDict[sKey], string_types)]
return [sKey for sKey in self.__dataDict.keys() if isinstance(self.__dataDict[sKey], str)]

@gCFGSynchro
def listSections(self, ordered=True):
Expand All @@ -283,9 +276,9 @@ def listSections(self, ordered=True):
:return: List with the subsection names
"""
if ordered:
return [sKey for sKey in self.__orderedList if not isinstance(self.__dataDict[sKey], string_types)]
return [sKey for sKey in self.__orderedList if not isinstance(self.__dataDict[sKey], str)]
else:
return [sKey for sKey in self.__dataDict.keys() if not isinstance(self.__dataDict[sKey], string_types)]
return [sKey for sKey in self.__dataDict.keys() if not isinstance(self.__dataDict[sKey], str)]

@gCFGSynchro
def isSection(self, key):
Expand All @@ -301,11 +294,11 @@ def isSection(self, key):
if not keyDict:
return False
section = keyDict["value"]
if isinstance(section, string_types):
if isinstance(section, str):
return False
secKey = keyDict["levelsBelow"]
return section.isSection(secKey)
return key in self.__dataDict and not isinstance(self.__dataDict[key], string_types)
return key in self.__dataDict and not isinstance(self.__dataDict[key], str)

@gCFGSynchro
def isOption(self, key):
Expand All @@ -321,11 +314,11 @@ def isOption(self, key):
if not keyDict:
return False
section = keyDict["value"]
if isinstance(section, string_types):
if isinstance(section, str):
return False
secKey = keyDict["levelsBelow"]
return section.isOption(secKey)
return key in self.__dataDict and isinstance(self.__dataDict[key], string_types)
return key in self.__dataDict and isinstance(self.__dataDict[key], str)

def listAll(self):
"""
Expand Down Expand Up @@ -411,7 +404,7 @@ def getOption(self, opName, defaultValue=None):
return defaultValue
dataD = dataV

if not isinstance(dataV, string_types):
if not isinstance(dataV, str):
optionValue = defaultValue
else:
optionValue = dataV
Expand Down Expand Up @@ -477,7 +470,7 @@ def getAsDict(self, path=""):
if not reqDict:
return resVal
keyCfg = reqDict["value"]
if isinstance(keyCfg, string_types):
if isinstance(keyCfg, str):
return resVal
return keyCfg.getAsDict()
for op in self.listOptions():
Expand Down Expand Up @@ -600,7 +593,7 @@ def __contains__(self, key):
"""
Check if a key is defined
"""
if not isinstance(key, string_types) or not key:
if not isinstance(key, str) or not key:
return False
return bool(self.getRecursive(key))

Expand Down Expand Up @@ -650,8 +643,8 @@ def getComment(self, entryName):
"""
try:
return self.__commentDict[entryName]
except BaseException:
raise ValueError(f"{entryName} does not have any comment defined")
except KeyError:
raise ValueError(f"{entryName} does not have any comment defined") from None

@gCFGSynchro
def setComment(self, entryName, comment):
Expand Down Expand Up @@ -937,7 +930,7 @@ def loadFromBuffer(self, data):
except IndexError:
raise ValueError(
"The cfg file seems to close more sections than it opens (i.e. to many '}' vs '{'"
)
) from None
elif line[index] == "=":
lFields = line.split("=")
currentLevel.setOption(lFields[0].strip(), "=".join(lFields[1:]).strip(), currentComment)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ def test_load():

def test_comment():
c = CFG().loadFromFile(EXAMPLE_CFG_FILE)
c.getComment("Releases").strip() == "Here is where the releases go:"
assert c.getComment("Releases").strip() == "Here is where the releases go:"


def test_sanity():
with pytest.raises(ValueError) as excinfo:
rels = CFG().loadFromFile(BROKEN_OPEN_CFG_FILE)
CFG().loadFromFile(BROKEN_OPEN_CFG_FILE)
assert "close more section" in str(excinfo)

with pytest.raises(ValueError) as excinfo:
rels = CFG().loadFromFile(BROKEN_CLOSE_CFG_FILE)
CFG().loadFromFile(BROKEN_CLOSE_CFG_FILE)
assert "open more section" in str(excinfo)


Expand Down

0 comments on commit 5e6a0ba

Please sign in to comment.