OpenCores
URL https://opencores.org/ocsvn/radiohdl/radiohdl/trunk

Subversion Repositories radiohdl

[/] [radiohdl/] [trunk/] [core/] [common_radiohdl.py] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 danv
###############################################################################
2
#
3
# Copyright 2018 Stichting Nederlandse Wetenschappelijk Onderzoek Instituten
4
# ASTRON Netherlands Institute for Radio Astronomy
5
# 
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
9
# 
10
#    http://www.apache.org/licenses/LICENSE-2.0
11
# 
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License. 
17
#
18
###############################################################################
19 4 danv
 
20
import os
21
import inspect
22
 
23
 
24
def listify(obj):
25
    """
26
    Can be used to force method input to a list.
27
    """
28
    if isinstance(obj, list):
29
        return obj
30
    else:
31
        return [obj]
32
 
33
    # try:
34
    #     return list(obj)
35
    # except TypeError:
36
    #     return [obj]
37
 
38
 
39
def unlistify(obj):
40
    """
41
    Converts 1-element list to x.
42
    """
43
    # The isinstance() built-in function is recommended over the type()
44
    # built-in function for testing the type of an object
45
    if isinstance(obj, list):
46
        if len(obj) == 1:
47
            return obj[0]
48
    return obj
49
 
50
 
51
def remove_from_list_string(list_str, item_str, sep=' '):
52
    """Treat the string list_str as a list of items that are separated by sep and then
53
       remove the specified item_str string from the list and return the list as a
54
       string of items separated by sep. Also remove any duplicate items.
55
    """
56
    _list_str = list_str.split(sep)
57
    _list_str = unique(_list_str)
58
    _list_str.remove(item_str)
59
    return sep.join(_list_str)
60
 
61
 
62
def unique(in_list):
63
    """
64
    Extract unique list elements (without changing the order like set() does)
65
    """
66
    result = []
67
    for item in in_list:
68
        if item in result:
69
            continue
70
        result.append(item)
71
    return result
72
 
73
 
74
def method_name(caller_depth=0):
75
    """
76
    Returns the name of the caller method.
77
    """
78
    # Note: inspect.stack()[0][3] would return the name of this method.
79
    return inspect.stack()[caller_depth+1][3]
80
 
81
 
82
def mkdir(path):
83
    """Recursively create leave directory and intermediate directories if they do not already exist."""
84
    expand_path = os.path.expandvars(path)        # support using environment variables in the file path
85
    expand_path = os.path.expanduser(expand_path)  # support using ~ in the file path
86
    if not os.path.exists(expand_path):
87
        os.makedirs(expand_path)
88
 
89
 
90
def expand_file_path_name(fpn, dir_path=''):
91
    """ Expand environment variables in fpn to get file_path_name.
92
    - if it is an absolute path return file_path_name else
93
    - if it still has a local file path prepend dir_path to the file_path_name and return dir_path + file_path_name.
94
    """
95
    file_path_name = os.path.expandvars(fpn)           # support using environment variables in the file path
96
    file_path_name = os.path.expanduser(file_path_name)  # support using ~ in the file path
97
    if os.path.isabs(file_path_name):
98
        return file_path_name                          # use absolute path to file
99
 
100
    # derive path to file from the directory path and a directory path to the file
101
    return os.path.join(os.path.expandvars(dir_path), file_path_name)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.