2015-11-26 12:15:02 +00:00
|
|
|
import os
|
2017-05-12 22:47:28 +00:00
|
|
|
import shutil, errno
|
2019-06-28 15:10:08 +00:00
|
|
|
import json
|
2016-02-26 18:50:12 +00:00
|
|
|
|
2015-11-26 12:15:02 +00:00
|
|
|
def absolute_path_file(path_file):
|
|
|
|
return os.path.abspath(path_file)
|
|
|
|
|
2016-02-26 18:50:12 +00:00
|
|
|
|
2015-11-26 12:15:02 +00:00
|
|
|
def extract_path_from_file(file):
|
|
|
|
path_file = os.path.abspath(os.path.dirname(file))
|
|
|
|
path, file = os.path.split(path_file)
|
|
|
|
return path
|
|
|
|
|
2016-02-26 18:50:12 +00:00
|
|
|
|
2020-10-27 21:10:10 +00:00
|
|
|
def join_paths(*args):
|
|
|
|
return os.path.join(*args)
|
2015-11-26 12:15:02 +00:00
|
|
|
|
2016-02-26 18:50:12 +00:00
|
|
|
|
2015-11-26 12:15:02 +00:00
|
|
|
def file_exists(path_file):
|
2016-02-26 18:50:12 +00:00
|
|
|
return os.path.isfile(path_file)
|
|
|
|
|
2015-12-02 11:16:30 +00:00
|
|
|
|
|
|
|
def dir_exists(dirname):
|
2016-02-26 18:50:12 +00:00
|
|
|
return os.path.isdir(dirname)
|
2017-05-12 22:47:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def copy(src, dst):
|
2018-06-27 18:32:49 +00:00
|
|
|
try:
|
2017-05-12 22:47:28 +00:00
|
|
|
shutil.copytree(src, dst)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == errno.ENOTDIR:
|
|
|
|
shutil.copy(src, dst)
|
|
|
|
else: raise
|
2018-01-26 19:38:26 +00:00
|
|
|
|
2018-06-27 18:32:49 +00:00
|
|
|
def rename_file(src, dst):
|
|
|
|
try:
|
|
|
|
os.rename(src, dst)
|
2018-06-30 23:36:53 +00:00
|
|
|
return True
|
2018-06-27 18:32:49 +00:00
|
|
|
except OSError as e:
|
2018-06-30 23:36:53 +00:00
|
|
|
if e.errno == errno.ENOENT:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2018-01-26 19:38:26 +00:00
|
|
|
|
|
|
|
# find a file in the root directory
|
|
|
|
def find(filename, folder):
|
|
|
|
for root, dirs, files in os.walk(folder):
|
|
|
|
return '/'.join((root, filename)) if filename in files else None
|
2019-04-30 20:09:03 +00:00
|
|
|
|
|
|
|
|
2019-11-04 21:29:43 +00:00
|
|
|
def related_file_path(input_file_path, prefix="", postfix="", replace_base=None):
|
2019-04-30 20:09:03 +00:00
|
|
|
"""
|
|
|
|
For example: related_file_path("/path/to/file.ext", "a.", ".b")
|
|
|
|
--> "/path/to/a.file.b.ext"
|
|
|
|
"""
|
|
|
|
path, filename = os.path.split(input_file_path)
|
|
|
|
|
|
|
|
# path = path/to
|
|
|
|
# filename = file.ext
|
|
|
|
|
|
|
|
basename, ext = os.path.splitext(filename)
|
|
|
|
# basename = file
|
|
|
|
# ext = .ext
|
|
|
|
|
2019-11-04 21:29:43 +00:00
|
|
|
if replace_base is not None:
|
|
|
|
basename = replace_base
|
|
|
|
|
2019-06-28 15:10:08 +00:00
|
|
|
return os.path.join(path, "{}{}{}{}".format(prefix, basename, postfix, ext))
|
|
|
|
|
|
|
|
def path_or_json_string_to_dict(string):
|
|
|
|
if string == "":
|
|
|
|
return {}
|
|
|
|
|
|
|
|
if string.startswith("[") or string.startswith("{"):
|
|
|
|
try:
|
|
|
|
return json.loads(string)
|
|
|
|
except:
|
|
|
|
raise ValueError("{0} is not a valid JSON string.".format(string))
|
|
|
|
elif file_exists(string):
|
|
|
|
try:
|
|
|
|
with open(string, 'r') as f:
|
|
|
|
return json.loads(f.read())
|
|
|
|
except:
|
|
|
|
raise ValueError("{0} is not a valid JSON file.".format(string))
|
|
|
|
else:
|
|
|
|
raise ValueError("{0} is not a valid JSON file or string.".format(string))
|