jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/961275 )
Change subject: Validate file path input for category_graph.py. ......................................................................
Validate file path input for category_graph.py.
Validate file path input for category_graph.py in order to handle nonexistent paths or undesired overwrites BEFORE the script runs. On nonexistent path, path will be created. If file exists, user will be given y/n prompt to override with "n" exiting bot and any other key overwriting. Switched from input() to pywikibot.input_choice().
Update logic for file path creation.
Because the path specified by -to is not an actual file in that it has no file extension, special logic is required in order to create the directory appropriately. If the path does not contain '/' it is assumed to be at the top level, and no directory creation is necessary. Otherwise, the directory is created if it does not exist.
Change to non-reserved word for input result.
Change from "prompt" to "choice" for prompt variable name, as "prompt" is a part of the standard lib. Minor syntax changes to follow guidelines.
Change to double quotes to single quotes.`
Replace reserved word 'input'. Use pathlib to extract folder and file.
The reserved word 'input' was still being used. This has been replaced by the word 'choice'. Pathlib is now being used to extract the folder and file in order to gracefully handle filepaths containing '/' or ''.
Revise exception handling. Use path.mkdir instead of os.mkdir.
Bug: T346417 Change-Id: I637bc0ef0bc99d5a59095890cc6b5be99bfd34fb --- M scripts/category_graph.py M AUTHORS.rst 2 files changed, 61 insertions(+), 0 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/AUTHORS.rst b/AUTHORS.rst index 636aa5a..1fad649 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -91,6 +91,7 @@
Ebrahim Byagowi Egon + Enag2000 Eranroz Erwin Evrifaessa diff --git a/scripts/category_graph.py b/scripts/category_graph.py index baf552c..c50a1dc 100755 --- a/scripts/category_graph.py +++ b/scripts/category_graph.py @@ -44,7 +44,10 @@ from __future__ import annotations
import argparse +import glob +import sys from collections import defaultdict +from pathlib import Path
import pywikibot from pywikibot import config @@ -205,6 +208,23 @@ local_args = pywikibot.handle_args() args, rest = ap.parse_known_args()
+ file_path = args.to + # If file exists, ask user if ok to overwrite. Otherwise, make + # the file, including directories unless it is top level. + if glob.glob(file_path + '.*'): + choice = pywikibot.input_yn(f'Files exist for {file_path}. Overwrite?', + 'n', automatic_quit=False) + if not choice: + pywikibot.info('Exiting...') + sys.exit(1) + else: + try: + dir_path = Path(file_path) + dir_path.parent.mkdir(parents=True, exist_ok=True) + # Except ValueError in the event that the directory is top level + # and does not contain any slashes. + except FileNotFoundError: + pass if not suggest_help(missing_action='from' not in args): import pydot bot = CategoryGraphBot(ap, args)
pywikibot-commits@lists.wikimedia.org