Coverage for manila/scheduler/filters/extra_specs_ops.py: 91%
34 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright (c) 2011 OpenStack Foundation.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16import operator
18from oslo_utils import strutils
20# 1. The following operations are supported:
21# =, s==, s!=, s>=, s>, s<=, s<, <in>, <is>, <or>, ==, !=, >=, <=
22# 2. Note that <or> is handled in a different way below.
23# 3. If the first word in the extra_specs is not one of the operators,
24# it is ignored.
25_op_methods = {'=': lambda x, y: float(x) >= float(y),
26 '<in>': lambda x, y: y in x,
27 '<is>': lambda x, y: (strutils.bool_from_string(x) is
28 strutils.bool_from_string(y)),
29 '==': lambda x, y: float(x) == float(y),
30 '!=': lambda x, y: float(x) != float(y),
31 '>=': lambda x, y: float(x) >= float(y),
32 '<=': lambda x, y: float(x) <= float(y),
33 's==': operator.eq,
34 's!=': operator.ne,
35 's<': operator.lt,
36 's<=': operator.le,
37 's>': operator.gt,
38 's>=': operator.ge}
41def match(value, req):
42 # Make case-insensitive
43 if (isinstance(value, str)):
44 value = value.lower()
45 req = req.lower()
46 words = req.split()
48 op = method = None
49 if words: 49 ↛ 53line 49 didn't jump to line 53 because the condition on line 49 was always true
50 op = words.pop(0)
51 method = _op_methods.get(op)
53 if op != '<or>' and not method:
54 if type(value) is bool:
55 return value == strutils.bool_from_string(
56 req, strict=False, default=req)
57 else:
58 return value == req
60 if value is None: 60 ↛ 61line 60 didn't jump to line 61 because the condition on line 60 was never true
61 return False
63 if op == '<or>': # Ex: <or> v1 <or> v2 <or> v3
64 while True:
65 if words.pop(0) == value:
66 return True
67 if not words:
68 break
69 op = words.pop(0) # remove a keyword <or>
70 if not words:
71 break
72 return False
74 try:
75 if words and method(value, words[0]):
76 return True
77 except ValueError:
78 pass
80 return False