Skip to content

Commit db8ba7d

Browse files
committed
Refactor: config.py, defs.py, dirs.py
1 parent 0e139b0 commit db8ba7d

8 files changed

+58
-64
lines changed

argos/info.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,19 @@
4545
KEY_VERSION = '_version'
4646

4747
# TODO: move to utils locations
48-
def program_directory():
48+
def program_directory() -> str:
4949
""" Returns the program directory where this program is installed
5050
"""
5151
return os.path.abspath(os.path.dirname(__file__))
5252

5353

54-
def resource_directory():
54+
def resource_directory() -> str:
5555
""" Returns directory with resources (images, style sheets, etc)
5656
"""
5757
return os.path.join(program_directory(), 'img/')
5858

5959

60-
61-
def icons_directory():
60+
def icons_directory() -> str:
6261
""" Returns the icons directory
6362
"""
6463
return os.path.join(program_directory(), 'img/snipicons')

argos/utils/config.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
""" Various function related to config files.
44
"""
55
import logging
6-
from typing import Dict, Any
6+
from typing import Any, Dict, List
77

88
logger = logging.getLogger(__name__)
99

1010
ConfigDict = Dict[str, Any]
1111

12-
def __findConfigParameter(cfg, parts, orgPath):
12+
def _findConfigParameter(cfg: ConfigDict, parts: List[str], orgPath: str) -> Any:
1313
""" Recursively finds a parameter in a (config) dict.
1414
"""
1515
if len(parts) == 0:
@@ -18,7 +18,7 @@ def __findConfigParameter(cfg, parts, orgPath):
1818
head, tail = parts[0], parts[1:]
1919

2020
if head == "": # A leading slash or double orgPath
21-
return __findConfigParameter(cfg, tail, orgPath)
21+
return _findConfigParameter(cfg, tail, orgPath)
2222

2323
if head not in cfg:
2424
msg = "Path {!r} not found in dict. {!r} not in: {}".format(orgPath, head, list(cfg.keys()))
@@ -27,32 +27,36 @@ def __findConfigParameter(cfg, parts, orgPath):
2727
if len(tail) == 0:
2828
return cfg[head]
2929
else:
30-
return __findConfigParameter(cfg[head], tail, orgPath)
30+
return _findConfigParameter(cfg[head], tail, orgPath)
3131

3232

33-
def findConfigParameter(cfg, path):
33+
def findConfigParameter(cfg: ConfigDict, path: str) -> Any:
3434
""" Recursively finds a parameter in a config dict.
3535
36-
Raises KeyError if the path cannot be found.
36+
Raises:
37+
KeyError: if the path cannot be found.
3738
38-
:param cfg: config dictionary. Can be a recursive dict (a dict of dicts, etc)
39-
:param path: Slash separated parameter path. E.g.: '/dict1/dict2/parameter'
39+
Args:
40+
cfg: config dictionary. Can be a recursive dict (a dict of dicts, etc.)
41+
path: Slash-separated parameter path. E.g.: '/dict1/dict2/parameter'
4042
"""
4143
if not path:
4244
# An empty path is most likely a programming error
4345
raise KeyError("Empty path given to findConfigParameter: {}".format(path))
4446
else:
4547
parts = path.split('/')
46-
return __findConfigParameter(cfg, parts, path)
48+
return _findConfigParameter(cfg, parts, path)
4749

4850

49-
def getConfigParameter(cfg, path, alt=None):
51+
def getConfigParameter(cfg: ConfigDict, path: str, alt: Any = None) -> Any:
5052
""" Finds the findConfigParameter. Returns alternative value if not found.
5153
5254
Will still log a warning if the parameter is not found.
5355
54-
:param cfg: config dictionary. Can be a recursive dict (a dict of dicts, etc)
55-
:param path: Slash separated parameter path. E.g.: '/dict1/dict2/parameter'
56+
Args:
57+
cfg: config dictionary. Can be a recursive dict (a dict of dicts, etc.)
58+
path: Slash separated parameter path. E.g.: '/dict1/dict2/parameter'
59+
alt: Alternative value returned when the value is not found in the dictionary.
5660
"""
5761
try:
5862
return findConfigParameter(cfg, path)
@@ -61,13 +65,13 @@ def getConfigParameter(cfg, path, alt=None):
6165
return alt
6266

6367

68+
def deleteParameter(cfg: ConfigDict, parentPath: str, parName: str) -> None:
69+
""" Deletes a parameter from the config dictionary.
6470
65-
def deleteParameter(cfg, parentPath, parName):
66-
""" Deletes a parameter from the config dict
67-
68-
:param cfg: config dictionary
69-
:param parentPath: the path of the parent dict that contains the paremeter
70-
:param parName: name of the element that will be removed
71+
Args:
72+
cfg: config dictionary
73+
parentPath: the path of the parent dict that contains the paremeter
74+
parName: name of the element that will be removed
7175
"""
7276
logger.debug("Deleting {!r} from parent dict: {!r}".format(parName, parentPath))
7377
try:

argos/utils/defs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# along with Argos. If not, see <http://www.gnu.org/licenses/>.
1717

1818

19-
""" Various definitions, errors and constants that can be used throughout the program
19+
""" Various definitions, errors and constants that can be used throughout the program.
2020
2121
"""
2222
import sys
@@ -39,6 +39,6 @@
3939

4040

4141
class InvalidInputError(Exception):
42-
""" Exception raised when the input is invalid after editing
42+
""" Exception raised when the input is invalid after editing.z
4343
"""
4444
pass

argos/utils/dirs.py

+22-36
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
r""" Default config and log directories under the different platforms.
1919
20-
Config files are stored in a subdirectory of the genericConfigLocation. Log files are stored
21-
in a sub directory of the genericLocalDataLocation. These are:
20+
Config files are stored in a subdirectory of the baseConfigLocation. Log files are stored
21+
in a subdirectory of the baseLocalDataLocation. These are: ::
2222
2323
Windows:
24-
baseConfigLocation -> C:\Users\<user>\AppData\Local\
25-
baseLocalDataLocation -> C:\Users\<user>\AppData\Local\
24+
baseConfigLocation -> C:\Users\<user>\AppData\Local
25+
baseLocalDataLocation -> C:\Users\<user>\AppData\Local
2626
2727
OS-X:
2828
baseConfigLocation -> ~/Library/Preferences
@@ -33,7 +33,6 @@
3333
baseLocalDataLocation -> ~/.local/share
3434
3535
See http://doc.qt.io/qt-5/qsettings.html#platform-specific-notes.
36-
3736
"""
3837

3938
import logging
@@ -45,10 +44,10 @@
4544
logger = logging.getLogger(__name__)
4645

4746

48-
def normRealPath(path):
47+
def normRealPath(path: str) -> str:
4948
""" Returns the normalized real path.
5049
51-
If the path is empty or None it is returned as-is. This is to prevent expanding to the
50+
If the path is empty or None, it is returned as-is. This is to prevent expanding to the
5251
current directory in case of undefined paths.
5352
"""
5453
if path:
@@ -57,18 +56,22 @@ def normRealPath(path):
5756
return path
5857

5958

60-
def ensureDirectoryExists(dirName):
59+
def ensureDirectoryExists(dirName: str) -> None:
6160
""" Creates a directory if it doesn't yet exist.
6261
"""
6362
if not os.path.exists(dirName):
6463
logger.info("Creating directory: {}".format(normRealPath(dirName)))
6564
os.makedirs(dirName)
6665

6766

68-
def ensureFileExists(pathName):
69-
""" Creates an empty file file if it doesn't yet exist. Also creates necessary directory path.
67+
def ensureFileExists(pathName: str) -> str:
68+
""" Creates an empty file if it doesn't yet exist. Also creates necessary directory path.
69+
70+
Args:
71+
pathName: file path.
7072
71-
:returns: the normRealPath of the path name.
73+
Returns:
74+
The normRealPath of the path name.
7275
"""
7376
pathName = normRealPath(pathName)
7477
dirName, fileName = os.path.split(pathName)
@@ -84,23 +87,20 @@ def ensureFileExists(pathName):
8487
return pathName
8588

8689

87-
def homeDirectory():
90+
def homeDirectory() -> str:
8891
""" Returns the user's home directory.
8992
90-
https://stackoverflow.com/a/4028943/625350
93+
See: https://stackoverflow.com/a/4028943/625350
9194
"""
9295
return os.path.expanduser("~")
9396

9497

95-
96-
97-
9898
################
9999
# Config files #
100100
################
101101

102102

103-
def baseConfigLocation():
103+
def baseConfigLocation() -> str:
104104
r""" Gets the base configuration directory (for all applications of the user).
105105
106106
See the module doc string at the top for details.
@@ -121,39 +121,25 @@ def baseConfigLocation():
121121
return normRealPath(configDir)
122122

123123

124-
def argosConfigDirectory():
124+
def argosConfigDirectory() -> str:
125125
r""" Gets the Argos configuration directory.
126126
127127
The config directory is platform dependent. (See the module doc string at the top).
128128
"""
129129
return os.path.join(baseConfigLocation(), ORGANIZATION_NAME, SCRIPT_NAME)
130130

131-
#
132-
# def appConfigFileName(configFile):
133-
# """ Returns default config file if configFile is emtpy string or None
134-
# """
135-
# if configFile:
136-
# # Config file specified on the command line
137-
# return configFile
138-
# else:
139-
# # Use the default config file name
140-
# configDir = argosConfigDirectory()
141-
# configFile = normRealPath(os.path.join(configDir, 'config.yaml'))
142-
# return configFile
143-
144-
145131

146132
#############
147133
# Log files #
148134
#############
149135

150136

151-
def baseLocalDataLocation():
137+
def baseLocalDataLocation() -> str:
152138
r""" Gets the base configuration directory (for all applications of the user).
153139
154140
The config directory is platform dependent (see the Qt documentation for baseDataLocation).
155141
On Windows this will be something like:
156-
C:\Users\<user>\AppData\Local\
142+
``C:\Users\<user>\AppData\Local\``
157143
158144
See the module doc string at the top for details.
159145
"""
@@ -173,7 +159,7 @@ def baseLocalDataLocation():
173159
return normRealPath(cfgDir)
174160

175161

176-
def argosLocalDataDirectory():
162+
def argosLocalDataDirectory() -> str:
177163
r""" Returns a directory where Argos can store files locally (not roaming).
178164
179165
This directory is platform dependent. (See the module doc string at the top).
@@ -182,7 +168,7 @@ def argosLocalDataDirectory():
182168

183169

184170

185-
def argosLogDirectory():
171+
def argosLogDirectory() -> str:
186172
r""" Returns the directory where Argos can store its log files.
187173
188174
This is the 'logs' subdirectory of the argosLocalDataDirectory()

development/environment.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ dependencies:
2222

2323
# The following dependencies are for development only and therefore have no version number
2424
- mypy
25+
- pylint
2526
- sphinx
2627
- sphinx_rtd_theme

development/environment_min.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ dependencies:
2525

2626
# The following dependencies are for development only and therefore have no version number
2727
- mypy
28+
- pylint
2829
- sphinx
2930
- sphinx_rtd_theme

development/to_be_checked.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ repo/rtiplugins/pillowio.py
8888
repo/rtiplugins/scipyio.py
8989
repo/testdata.py
9090
utils/__init__.py
91-
utils/cls.py
92-
utils/config.py
93-
utils/defs.py
9491
utils/dirs.py
9592
utils/logs.py
9693
utils/masks.py
@@ -108,4 +105,9 @@ widgets/testwalkdialog.py
108105

109106

110107
# Done
108+
utils/cls.py
109+
utils/config.py
110+
utils/defs.py
111+
112+
111113

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ disable = [
369369
"too-few-public-methods",
370370
"consider-iterating-dictionary",
371371
"trailing-newlines",
372-
372+
"unnecessary-pass",
373+
"consider-using-in",
373374

374375
]
375376

0 commit comments

Comments
 (0)