~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/scheduler/filters/extra_specs_ops.py

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman, Adam Gandelman, Chuck Short
  • Date: 2012-08-27 15:37:18 UTC
  • mfrom: (1.1.60)
  • Revision ID: package-import@ubuntu.com-20120827153718-lj8er44eqqz1gsrj
Tags: 2012.2~rc1~20120827.15815-0ubuntu1
[ Adam Gandelman ]
* New upstream release.

[ Chuck Short ]
* debian/patches/0001-Update-tools-hacking-for-pep8-1.2-and-
  beyond.patch: Dropped we dont run pep8 tests anymore.
* debian/control: Drop pep8 build depends
* debian/*.upstart.in: Make sure we transition correctly from runlevel
  1 to 2. (LP: #820694)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2011 OpenStack, LLC.
 
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.
 
15
 
 
16
import operator
 
17
 
 
18
# 1. The following operations are supported:
 
19
#   =, s==, s!=, s>=, s>, s<=, s<, <in>, <or>, ==, !=, >=, <=
 
20
# 2. Note that <or> is handled in a different way below.
 
21
# 3. If the first word in the extra_specs is not one of the operators,
 
22
#   it is ignored.
 
23
_op_methods = {'=': lambda x, y: float(x) >= float(y),
 
24
               '<in>': lambda x, y: y in x,
 
25
               '==': lambda x, y: float(x) == float(y),
 
26
               '!=': lambda x, y: float(x) != float(y),
 
27
               '>=': lambda x, y: float(x) >= float(y),
 
28
               '<=': lambda x, y: float(x) <= float(y),
 
29
               's==': operator.eq,
 
30
               's!=': operator.ne,
 
31
               's<': operator.lt,
 
32
               's<=': operator.le,
 
33
               's>': operator.gt,
 
34
               's>=': operator.ge}
 
35
 
 
36
 
 
37
def match(value, req):
 
38
    words = req.split()
 
39
 
 
40
    op = method = None
 
41
    if words:
 
42
        op = words.pop(0)
 
43
        method = _op_methods.get(op)
 
44
 
 
45
    if op != '<or>' and not method:
 
46
        return value == req
 
47
 
 
48
    if value is None:
 
49
        return False
 
50
 
 
51
    if op == '<or>':  # Ex: <or> v1 <or> v2 <or> v3
 
52
        while True:
 
53
            if words.pop(0) == value:
 
54
                return True
 
55
            if not words:
 
56
                break
 
57
            op = words.pop(0)  # remove a keyword <or>
 
58
            if not words:
 
59
                break
 
60
        return False
 
61
 
 
62
    if words and method(value, words[0]):
 
63
        return True
 
64
 
 
65
    return False