|
###############################################################################
|
|
#
|
|
# Copyright 2018 Stichting Nederlandse Wetenschappelijk Onderzoek Instituten
|
|
# ASTRON Netherlands Institute for Radio Astronomy
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
###############################################################################
|
|
|
import os
|
import os
|
import inspect
|
import inspect
|
|
|
|
|
def listify(obj):
|
def listify(obj):
|
"""
|
"""
|
Can be used to force method input to a list.
|
Can be used to force method input to a list.
|
"""
|
"""
|
if isinstance(obj, list):
|
if isinstance(obj, list):
|
return obj
|
return obj
|
else:
|
else:
|
return [obj]
|
return [obj]
|
|
|
# try:
|
# try:
|
# return list(obj)
|
# return list(obj)
|
# except TypeError:
|
# except TypeError:
|
# return [obj]
|
# return [obj]
|
|
|
|
|
def unlistify(obj):
|
def unlistify(obj):
|
"""
|
"""
|
Converts 1-element list to x.
|
Converts 1-element list to x.
|
"""
|
"""
|
# The isinstance() built-in function is recommended over the type()
|
# The isinstance() built-in function is recommended over the type()
|
# built-in function for testing the type of an object
|
# built-in function for testing the type of an object
|
if isinstance(obj, list):
|
if isinstance(obj, list):
|
if len(obj) == 1:
|
if len(obj) == 1:
|
return obj[0]
|
return obj[0]
|
return obj
|
return obj
|
|
|
|
|
def remove_from_list_string(list_str, item_str, sep=' '):
|
def remove_from_list_string(list_str, item_str, sep=' '):
|
"""Treat the string list_str as a list of items that are separated by sep and then
|
"""Treat the string list_str as a list of items that are separated by sep and then
|
remove the specified item_str string from the list and return the list as a
|
remove the specified item_str string from the list and return the list as a
|
string of items separated by sep. Also remove any duplicate items.
|
string of items separated by sep. Also remove any duplicate items.
|
"""
|
"""
|
_list_str = list_str.split(sep)
|
_list_str = list_str.split(sep)
|
_list_str = unique(_list_str)
|
_list_str = unique(_list_str)
|
_list_str.remove(item_str)
|
_list_str.remove(item_str)
|
return sep.join(_list_str)
|
return sep.join(_list_str)
|
|
|
|
|
def unique(in_list):
|
def unique(in_list):
|
"""
|
"""
|
Extract unique list elements (without changing the order like set() does)
|
Extract unique list elements (without changing the order like set() does)
|
"""
|
"""
|
result = []
|
result = []
|
for item in in_list:
|
for item in in_list:
|
if item in result:
|
if item in result:
|
continue
|
continue
|
result.append(item)
|
result.append(item)
|
return result
|
return result
|
|
|
|
|
def method_name(caller_depth=0):
|
def method_name(caller_depth=0):
|
"""
|
"""
|
Returns the name of the caller method.
|
Returns the name of the caller method.
|
"""
|
"""
|
# Note: inspect.stack()[0][3] would return the name of this method.
|
# Note: inspect.stack()[0][3] would return the name of this method.
|
return inspect.stack()[caller_depth+1][3]
|
return inspect.stack()[caller_depth+1][3]
|
|
|
|
|
def mkdir(path):
|
def mkdir(path):
|
"""Recursively create leave directory and intermediate directories if they do not already exist."""
|
"""Recursively create leave directory and intermediate directories if they do not already exist."""
|
expand_path = os.path.expandvars(path) # support using environment variables in the file path
|
expand_path = os.path.expandvars(path) # support using environment variables in the file path
|
expand_path = os.path.expanduser(expand_path) # support using ~ in the file path
|
expand_path = os.path.expanduser(expand_path) # support using ~ in the file path
|
if not os.path.exists(expand_path):
|
if not os.path.exists(expand_path):
|
os.makedirs(expand_path)
|
os.makedirs(expand_path)
|
|
|
|
|
def expand_file_path_name(fpn, dir_path=''):
|
def expand_file_path_name(fpn, dir_path=''):
|
""" Expand environment variables in fpn to get file_path_name.
|
""" Expand environment variables in fpn to get file_path_name.
|
- if it is an absolute path return file_path_name else
|
- if it is an absolute path return file_path_name else
|
- if it still has a local file path prepend dir_path to the file_path_name and return dir_path + file_path_name.
|
- if it still has a local file path prepend dir_path to the file_path_name and return dir_path + file_path_name.
|
"""
|
"""
|
file_path_name = os.path.expandvars(fpn) # support using environment variables in the file path
|
file_path_name = os.path.expandvars(fpn) # support using environment variables in the file path
|
file_path_name = os.path.expanduser(file_path_name) # support using ~ in the file path
|
file_path_name = os.path.expanduser(file_path_name) # support using ~ in the file path
|
if os.path.isabs(file_path_name):
|
if os.path.isabs(file_path_name):
|
return file_path_name # use absolute path to file
|
return file_path_name # use absolute path to file
|
|
|
# derive path to file from the directory path and a directory path to the file
|
# derive path to file from the directory path and a directory path to the file
|
return os.path.join(os.path.expandvars(dir_path), file_path_name)
|
return os.path.join(os.path.expandvars(dir_path), file_path_name)
|
|
|