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

Subversion Repositories radiohdl

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 danv
 
2
import os
3
import inspect
4
 
5
 
6
def listify(obj):
7
    """
8
    Can be used to force method input to a list.
9
    """
10
    if isinstance(obj, list):
11
        return obj
12
    else:
13
        return [obj]
14
 
15
    # try:
16
    #     return list(obj)
17
    # except TypeError:
18
    #     return [obj]
19
 
20
 
21
def unlistify(obj):
22
    """
23
    Converts 1-element list to x.
24
    """
25
    # The isinstance() built-in function is recommended over the type()
26
    # built-in function for testing the type of an object
27
    if isinstance(obj, list):
28
        if len(obj) == 1:
29
            return obj[0]
30
    return obj
31
 
32
 
33
def remove_from_list_string(list_str, item_str, sep=' '):
34
    """Treat the string list_str as a list of items that are separated by sep and then
35
       remove the specified item_str string from the list and return the list as a
36
       string of items separated by sep. Also remove any duplicate items.
37
    """
38
    _list_str = list_str.split(sep)
39
    _list_str = unique(_list_str)
40
    _list_str.remove(item_str)
41
    return sep.join(_list_str)
42
 
43
 
44
def unique(in_list):
45
    """
46
    Extract unique list elements (without changing the order like set() does)
47
    """
48
    result = []
49
    for item in in_list:
50
        if item in result:
51
            continue
52
        result.append(item)
53
    return result
54
 
55
 
56
def method_name(caller_depth=0):
57
    """
58
    Returns the name of the caller method.
59
    """
60
    # Note: inspect.stack()[0][3] would return the name of this method.
61
    return inspect.stack()[caller_depth+1][3]
62
 
63
 
64
def mkdir(path):
65
    """Recursively create leave directory and intermediate directories if they do not already exist."""
66
    expand_path = os.path.expandvars(path)        # support using environment variables in the file path
67
    expand_path = os.path.expanduser(expand_path)  # support using ~ in the file path
68
    if not os.path.exists(expand_path):
69
        os.makedirs(expand_path)
70
 
71
 
72
def expand_file_path_name(fpn, dir_path=''):
73
    """ Expand environment variables in fpn to get file_path_name.
74
    - if it is an absolute path return file_path_name else
75
    - if it still has a local file path prepend dir_path to the file_path_name and return dir_path + file_path_name.
76
    """
77
    file_path_name = os.path.expandvars(fpn)           # support using environment variables in the file path
78
    file_path_name = os.path.expanduser(file_path_name)  # support using ~ in the file path
79
    if os.path.isabs(file_path_name):
80
        return file_path_name                          # use absolute path to file
81
 
82
    # derive path to file from the directory path and a directory path to the file
83
    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.