mirror of
https://github.com/platomav/BIOSUtilities.git
synced 2025-05-13 06:34:42 -04:00
Fix handling of quote-encased user input paths
This commit is contained in:
parent
982e3f3fc9
commit
9b29c37c65
2 changed files with 21 additions and 8 deletions
|
@ -11,7 +11,7 @@ import sys
|
||||||
import shutil
|
import shutil
|
||||||
from pathlib import Path, PurePath
|
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
|
# Fix illegal/reserved Windows characters
|
||||||
def safe_name(in_name):
|
def safe_name(in_name):
|
||||||
|
@ -95,6 +95,15 @@ def get_path_files(in_path):
|
||||||
|
|
||||||
return path_files
|
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
|
# Get absolute file path of argparse object
|
||||||
def get_argparse_path(argparse_path):
|
def get_argparse_path(argparse_path):
|
||||||
if not 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])
|
output_path = argparse_args.output_dir or argparse_args.input_dir or path_parent(input_files[0])
|
||||||
else:
|
else:
|
||||||
# Script w/o parameters
|
# 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_path_full = get_argparse_path(input_path_user) if input_path_user else ''
|
||||||
input_files = get_path_files(input_path_full)
|
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)
|
output_path_final = get_argparse_path(output_path)
|
||||||
|
|
||||||
|
|
|
@ -10,13 +10,13 @@ def padder(padd_count, tab=False):
|
||||||
return ('\t' if tab else ' ') * padd_count
|
return ('\t' if tab else ' ') * padd_count
|
||||||
|
|
||||||
# Get String from given input object
|
# Get String from given input object
|
||||||
def to_string(input_object, sep_char=''):
|
def to_string(in_object, sep_char=''):
|
||||||
if type(input_object).__name__ in ('list','tuple'):
|
if type(in_object).__name__ in ('list','tuple'):
|
||||||
output_string = sep_char.join(map(str, input_object))
|
out_string = sep_char.join(map(str, in_object))
|
||||||
else:
|
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
|
# Get Bytes from given buffer or file path
|
||||||
def file_to_bytes(in_object):
|
def file_to_bytes(in_object):
|
||||||
|
@ -27,3 +27,7 @@ def file_to_bytes(in_object):
|
||||||
object_bytes = object_data.read()
|
object_bytes = object_data.read()
|
||||||
|
|
||||||
return object_bytes
|
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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue