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