File: utils.py

package info (click to toggle)
python-pyepics 3.5.9%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,308 kB
  • sloc: python: 10,303; makefile: 109; javascript: 104; sh: 54
file content (99 lines) | stat: -rw-r--r-- 2,176 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
String and data utils
"""
import sys
import os
import platform

try:
    from charset_normalizer import from_bytes
except ImportError:
    from_bytes = None

IOENCODING =  os.environ.get('PYEPICS_ENCODING',
              os.environ.get('PYTHONIOENCODING',
                             'utf-8'))

def str2bytes(st1):
    'string to byte conversion'
    if isinstance(st1, bytes):
        return st1
    return bytes(st1, IOENCODING)

def bytes2str(st1):
    'byte to string conversion'
    if isinstance(st1, str):
        return st1
    elif isinstance(st1, bytes):
        try:
            return str(st1, IOENCODING)
        except UnicodeDecodeError:
            if from_bytes is None:
                raise
            return str(from_bytes(st1).best())
    else:
        return str(st1)

def strjoin(sep, seq):
    "join string sequence with a separator"
    if isinstance(sep, bytes):
        sep = bytes2str(sep)
    if len(seq) == 0:
        seq = ''
    elif isinstance(seq[0], bytes):
        tmp =[]
        for i in seq:
            if i == b'\x00':
                break
            tmp.append(bytes2str(i))
        seq = tmp
    return sep.join(seq)


def clib_search_path(lib):
    '''Assemble path to c library.

    Parameters
    ----------
    lib : str
        Either 'ca' or 'Com'.

    Returns
    --------
    str : string

    Examples
    --------
    >>> clib_search_path('ca')
    'linux64/libca.so'

    '''

    # determine which libca / libCom dll is appropriate
    try:
        nbits = platform.architecture()[0]
        mach = platform.machine()
    except:
        nbits = '32bit'
        mach = 'x86_64'

    nbits = nbits.replace('bit', '')
    if mach.startswith('arm'):
        nbits = 'arm'

    libfmt = 'lib%s.so'
    if os.name == 'nt':
        libsrc = 'win'
        libfmt = '%s.dll'
    elif sys.platform == 'darwin':
        libsrc = 'darwin'
        if platform.machine() == 'arm64':
            libsrc, nbits = 'darwinarm', '64'
        libfmt = 'lib%s.dylib'
    elif sys.platform.startswith('linux'):
        libsrc = 'linux'

    else:
        return None

    return os.path.join("%s%s" % (libsrc, nbits), libfmt % lib)