Skip to content

Commit 2a2e0f7

Browse files
authored
Merge pull request #26 from realshouzy/refactor-args-handling-funcs
Refactor ``args_handling`` functions
2 parents bed5dbb + 4a14cae commit 2a2e0f7

File tree

7 files changed

+336
-270
lines changed

7 files changed

+336
-270
lines changed

auto_file_sorter/args_handling.py

Lines changed: 21 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"handle_locations_args",
99
]
1010

11-
import json
1211
import logging
1312
import os.path
1413
import platform
@@ -20,14 +19,18 @@
2019

2120
from watchdog.observers import Observer
2221

23-
from auto_file_sorter.configs_handling import read_from_configs, write_to_configs
24-
from auto_file_sorter.constants import (
25-
CONFIG_LOG_LEVEL,
26-
EXIT_FAILURE,
27-
EXIT_SUCCESS,
28-
FILE_EXTENSION_PATTERN,
22+
from auto_file_sorter.configs_handling import (
23+
add_new_config_to_configs,
24+
get_extension_paths_from_configs,
25+
get_selected_configs,
26+
load_json_file_into_configs,
27+
read_from_configs,
28+
remove_configs,
29+
write_to_configs,
2930
)
31+
from auto_file_sorter.constants import CONFIG_LOG_LEVEL, EXIT_FAILURE, EXIT_SUCCESS
3032
from auto_file_sorter.event_handling import OnModifiedEventHandler
33+
from auto_file_sorter.utils import resolved_path_from_str
3134

3235
if TYPE_CHECKING:
3336
import argparse
@@ -109,146 +112,24 @@ def _add_to_startup(
109112
)
110113

111114

112-
def resolved_path_from_str(path_as_str: str) -> Path:
113-
"""Return the absolute path given a string of a path."""
114-
return Path(path_as_str.strip()).resolve()
115-
116-
117115
def handle_write_args(args: argparse.Namespace) -> int:
118116
"""Handle the ``write`` subcommand."""
117+
exit_code: int = EXIT_SUCCESS
118+
119119
args_handling_logger.debug("Reading from configs")
120120
configs: dict[str, str] = read_from_configs(configs=args.configs_location)
121121

122122
if args.new_config is not None:
123-
args_handling_logger.debug("args.new_config=%s", repr(args.new_config))
124-
125-
new_extension, new_path = (
126-
args.new_config[0].lower().replace(" ", ""),
127-
args.new_config[1].strip(),
128-
)
129-
args_handling_logger.debug(
130-
"Normalized '%s' to '%s'",
131-
args.new_config[0],
132-
new_extension,
133-
)
134-
args_handling_logger.debug(
135-
"Normalized '%s' to '%s'",
136-
args.new_config[1],
137-
new_path,
138-
)
139-
140-
if not new_extension or not new_path:
141-
args_handling_logger.critical(
142-
"Either an empty extension '%s' or an empty path '%s' was specified to add, "
143-
"which is invalid",
144-
new_extension,
145-
new_path,
146-
)
147-
return EXIT_FAILURE
148-
149-
if FILE_EXTENSION_PATTERN.fullmatch(new_extension) is None:
150-
args_handling_logger.critical(
151-
"Given extension '%s' is invalid",
152-
new_extension,
153-
)
154-
return EXIT_FAILURE
155-
156-
args_handling_logger.debug("Got '%s': '%s'", new_extension, new_path)
157-
158-
configs[new_extension] = new_path
159-
args_handling_logger.log(
160-
CONFIG_LOG_LEVEL,
161-
"Updated '%s': '%s' from '%s'",
162-
new_extension,
163-
new_path,
164-
args.configs_location,
165-
)
123+
exit_code |= add_new_config_to_configs(args.new_config, configs)
166124

167125
if args.json_file is not None:
168-
args_handling_logger.debug(
169-
"args.json_file='%s'",
170-
repr(args.json_file),
171-
)
172-
173-
if args.json_file.suffix.lower() != ".json":
174-
args_handling_logger.critical(
175-
"Configs can only be read from json files",
176-
)
177-
return EXIT_FAILURE
178-
179-
args_handling_logger.debug("Reading from '%s'", args.json_file)
180-
try:
181-
new_configs_from_json: dict[str, str] = json.loads(
182-
args.json_file.read_text(encoding="utf-8"),
183-
)
184-
except FileNotFoundError:
185-
args_handling_logger.critical(
186-
"Unable to find '%s'",
187-
args.json_file,
188-
)
189-
return EXIT_FAILURE
190-
except PermissionError:
191-
args_handling_logger.critical(
192-
"Permission denied to open and read from '%s'",
193-
args.json_file,
194-
)
195-
return EXIT_FAILURE
196-
except OSError:
197-
args_handling_logger.critical(
198-
"Operating system-related error occurred while opening and reading from '%s'",
199-
args.json_file,
200-
)
201-
return EXIT_FAILURE
202-
except json.JSONDecodeError:
203-
args_handling_logger.critical(
204-
"Given JSON file is not correctly formatted: '%s'",
205-
args.json_file,
206-
)
207-
return EXIT_FAILURE
208-
args_handling_logger.debug("Read from '%s'", args.json_file)
209-
210-
configs.update(new_configs_from_json)
211-
args_handling_logger.log(
212-
CONFIG_LOG_LEVEL,
213-
"Loaded '%s' into configs",
214-
args.json_file,
215-
)
126+
exit_code |= load_json_file_into_configs(args.json_file, configs)
216127

217128
if args.configs_to_be_removed is not None:
218-
args_handling_logger.debug(
219-
"args.configs_to_be_removed=%s",
220-
repr(args.configs_to_be_removed),
221-
)
129+
exit_code |= remove_configs(args.configs_to_be_removed, configs)
222130

223-
for config in args.configs_to_be_removed:
224-
extension: str = config.replace(" ", "").lower()
225-
args_handling_logger.debug("Stripped '%s' to '%s'", config, extension)
226-
227-
if FILE_EXTENSION_PATTERN.fullmatch(extension) is None:
228-
args_handling_logger.warning(
229-
"Skipping invalid extension: '%s'",
230-
extension,
231-
)
232-
continue
233-
234-
args_handling_logger.debug("Normalized '%s' to '%s'", config, extension)
235-
236-
args_handling_logger.debug("Removing '%s'", extension)
237-
try:
238-
del configs[extension]
239-
except KeyError:
240-
args_handling_logger.warning(
241-
"Ignoring '%s', because it is not in the configs",
242-
extension,
243-
)
244-
continue
245-
args_handling_logger.log(
246-
CONFIG_LOG_LEVEL,
247-
"Removed '%s'",
248-
extension,
249-
)
250131
write_to_configs(configs, configs=args.configs_location)
251-
return EXIT_SUCCESS
132+
return exit_code
252133

253134

254135
def handle_read_args(args: argparse.Namespace) -> int:
@@ -263,43 +144,10 @@ def handle_read_args(args: argparse.Namespace) -> int:
263144
"Getting selected configs and storing them in dict",
264145
)
265146

266-
selected_configs: dict[str, Path] = {}
267-
268-
for config in args.get_configs:
269-
extension: str = config.replace(" ", "").lower()
270-
271-
if FILE_EXTENSION_PATTERN.fullmatch(extension) is None:
272-
args_handling_logger.warning(
273-
"Ignoring invalid extension '%s'",
274-
extension,
275-
)
276-
continue
277-
278-
if extension not in configs:
279-
args_handling_logger.warning(
280-
"Ignoring '%s', because it is not in the configs",
281-
extension,
282-
)
283-
continue
284-
285-
try:
286-
selected_configs[extension] = resolved_path_from_str(configs[extension])
287-
except KeyError: # pragma: no cover
288-
args_handling_logger.warning(
289-
"Unable to get the respetive path from '%s' "
290-
"of one of the given extensions '%s'",
291-
args.configs_location,
292-
extension,
293-
)
294-
continue
295-
296-
if not selected_configs:
297-
args_handling_logger.critical("No valid extensions selected")
298-
args_handling_logger.debug(
299-
"repr(selected_configs)=%s",
300-
repr(selected_configs),
301-
)
302-
return EXIT_FAILURE
147+
selected_configs: dict[str, Path] = get_selected_configs(
148+
args.get_configs,
149+
configs,
150+
)
303151

304152
args_handling_logger.debug("Printing from %s", selected_configs)
305153
for extension, path in selected_configs.items():
@@ -318,19 +166,6 @@ def handle_read_args(args: argparse.Namespace) -> int:
318166
return EXIT_SUCCESS
319167

320168

321-
def _get_extension_paths_from_configs(
322-
configs: dict[str, str],
323-
) -> dict[str, Path]:
324-
"""Get the extension and their respective path from the configs."""
325-
args_handling_logger.debug("Resolving extension paths from %s", configs)
326-
extension_paths: dict[str, Path] = {
327-
extension: resolved_path_from_str(path_as_str)
328-
for extension, path_as_str in configs.items()
329-
}
330-
args_handling_logger.info("Got extension paths")
331-
return extension_paths
332-
333-
334169
def _create_observers(
335170
tracked_paths: list[Path],
336171
extension_paths: dict[str, Path],
@@ -409,7 +244,7 @@ def handle_track_args(args: argparse.Namespace) -> int:
409244
)
410245
return EXIT_FAILURE
411246

412-
extension_paths: dict[str, Path] = _get_extension_paths_from_configs(configs)
247+
extension_paths: dict[str, Path] = get_extension_paths_from_configs(configs)
413248

414249
observers: list[BaseObserver] = _create_observers(
415250
args.tracked_paths,

0 commit comments

Comments
 (0)