From 9b29c37c65e987872aaed527510725598e111b57 Mon Sep 17 00:00:00 2001 From: platomav Date: Sun, 1 May 2022 01:33:43 +0300 Subject: [PATCH] Fix handling of quote-encased user input paths --- common/path_ops.py | 15 ++++++++++++--- common/text_ops.py | 14 +++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/common/path_ops.py b/common/path_ops.py index d08cfb1..06b97af 100644 --- a/common/path_ops.py +++ b/common/path_ops.py @@ -11,7 +11,7 @@ import sys import shutil from pathlib import Path, PurePath -from common.text_ops import to_string +from common.text_ops import is_encased, to_string # Fix illegal/reserved Windows characters def safe_name(in_name): @@ -95,6 +95,15 @@ def get_path_files(in_path): return path_files +# Get path without leading/trailing quotes +def get_dequoted_path(in_path): + out_path = to_string(in_path).strip() + + if len(out_path) >= 2 and is_encased(out_path, ("'",'"')): + out_path = out_path[1:-1] + + return out_path + # Get absolute file path of argparse object def get_argparse_path(argparse_path): if not argparse_path: @@ -127,11 +136,11 @@ def process_input_files(argparse_args, sys_argv=None): output_path = argparse_args.output_dir or argparse_args.input_dir or path_parent(input_files[0]) else: # Script w/o parameters - input_path_user = input('\nEnter input directory path: ') + input_path_user = get_dequoted_path(input('\nEnter input directory path: ')) input_path_full = get_argparse_path(input_path_user) if input_path_user else '' input_files = get_path_files(input_path_full) - output_path = input('\nEnter output directory path: ') + output_path = get_dequoted_path(input('\nEnter output directory path: ')) output_path_final = get_argparse_path(output_path) diff --git a/common/text_ops.py b/common/text_ops.py index 8384ee8..f007051 100644 --- a/common/text_ops.py +++ b/common/text_ops.py @@ -10,13 +10,13 @@ def padder(padd_count, tab=False): return ('\t' if tab else ' ') * padd_count # Get String from given input object -def to_string(input_object, sep_char=''): - if type(input_object).__name__ in ('list','tuple'): - output_string = sep_char.join(map(str, input_object)) +def to_string(in_object, sep_char=''): + if type(in_object).__name__ in ('list','tuple'): + out_string = sep_char.join(map(str, in_object)) else: - output_string = str(input_object) + out_string = str(in_object) - return output_string + return out_string # Get Bytes from given buffer or file path def file_to_bytes(in_object): @@ -27,3 +27,7 @@ def file_to_bytes(in_object): object_bytes = object_data.read() return object_bytes + +# Check if string starts and ends with given character(s) +def is_encased(in_string, chars): + return in_string.startswith(chars) and in_string.endswith(chars)