xref: /openbmc/pyphosphor/obmc/utils/misc.py (revision 8ffe1e44)
1*8ffe1e44SBrad Bishop# Contributors Listed Below - COPYRIGHT 2016
2*8ffe1e44SBrad Bishop# [+] International Business Machines Corp.
3*8ffe1e44SBrad Bishop#
4*8ffe1e44SBrad Bishop#
5*8ffe1e44SBrad Bishop# Licensed under the Apache License, Version 2.0 (the "License");
6*8ffe1e44SBrad Bishop# you may not use this file except in compliance with the License.
7*8ffe1e44SBrad Bishop# You may obtain a copy of the License at
8*8ffe1e44SBrad Bishop#
9*8ffe1e44SBrad Bishop#     http://www.apache.org/licenses/LICENSE-2.0
10*8ffe1e44SBrad Bishop#
11*8ffe1e44SBrad Bishop# Unless required by applicable law or agreed to in writing, software
12*8ffe1e44SBrad Bishop# distributed under the License is distributed on an "AS IS" BASIS,
13*8ffe1e44SBrad Bishop# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14*8ffe1e44SBrad Bishop# implied. See the License for the specific language governing
15*8ffe1e44SBrad Bishop# permissions and limitations under the License.
16*8ffe1e44SBrad Bishop
17*8ffe1e44SBrad Bishop
18*8ffe1e44SBrad Bishopclass Path:
19*8ffe1e44SBrad Bishop    def __init__(self, path):
20*8ffe1e44SBrad Bishop        self.parts = filter(bool, path.split('/'))
21*8ffe1e44SBrad Bishop
22*8ffe1e44SBrad Bishop    def rel(self, first=None, last=None):
23*8ffe1e44SBrad Bishop        # relative
24*8ffe1e44SBrad Bishop        return self.get('', first, last)
25*8ffe1e44SBrad Bishop
26*8ffe1e44SBrad Bishop    def fq(self, first=None, last=None):
27*8ffe1e44SBrad Bishop        # fully qualified
28*8ffe1e44SBrad Bishop        return self.get('/', first, last)
29*8ffe1e44SBrad Bishop
30*8ffe1e44SBrad Bishop    def depth(self):
31*8ffe1e44SBrad Bishop        return len(self.parts)
32*8ffe1e44SBrad Bishop
33*8ffe1e44SBrad Bishop    def get(self, prefix='/', first=None, last=None):
34*8ffe1e44SBrad Bishop        if not first:
35*8ffe1e44SBrad Bishop            first = 0
36*8ffe1e44SBrad Bishop        if not last:
37*8ffe1e44SBrad Bishop            last = self.depth()
38*8ffe1e44SBrad Bishop        return prefix + '/'.join(self.parts[first:last])
39*8ffe1e44SBrad Bishop
40*8ffe1e44SBrad Bishop
41*8ffe1e44SBrad Bishopdef org_dot_openbmc_match(name):
42*8ffe1e44SBrad Bishop    return 'org.openbmc' in name
43*8ffe1e44SBrad Bishop
44*8ffe1e44SBrad Bishop
45*8ffe1e44SBrad Bishopclass ListMatch(object):
46*8ffe1e44SBrad Bishop    def __init__(self, l):
47*8ffe1e44SBrad Bishop        self.l = l
48*8ffe1e44SBrad Bishop
49*8ffe1e44SBrad Bishop    def __call__(self, match):
50*8ffe1e44SBrad Bishop        return match in self.l
51*8ffe1e44SBrad Bishop
52*8ffe1e44SBrad Bishop
53*8ffe1e44SBrad Bishopdef find_case_insensitive(value, lst):
54*8ffe1e44SBrad Bishop    return next((x for x in lst if x.lower() == value.lower()), None)
55*8ffe1e44SBrad Bishop
56*8ffe1e44SBrad Bishop
57*8ffe1e44SBrad Bishopdef makelist(data):
58*8ffe1e44SBrad Bishop    if isinstance(data, list):
59*8ffe1e44SBrad Bishop            return data
60*8ffe1e44SBrad Bishop    elif data:
61*8ffe1e44SBrad Bishop            return [data]
62*8ffe1e44SBrad Bishop    else:
63*8ffe1e44SBrad Bishop            return []
64