|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | + |
| 14 | +import functools |
| 15 | +from .. import This, DatumInContext, JSONPath |
| 16 | + |
| 17 | + |
| 18 | +class SortedThis(This): |
| 19 | + """The JSONPath referring to the sorted version of the current object. |
| 20 | +
|
| 21 | + Concrete syntax is '`sorted`' or [\\field,/field]. |
| 22 | + """ |
| 23 | + def __init__(self, expressions=None): |
| 24 | + self.expressions = expressions |
| 25 | + |
| 26 | + def _compare(self, left, right): |
| 27 | + left = DatumInContext.wrap(left) |
| 28 | + right = DatumInContext.wrap(right) |
| 29 | + |
| 30 | + for expr in self.expressions: |
| 31 | + field, reverse = expr |
| 32 | + l_datum = field.find(left) |
| 33 | + r_datum = field.find(right) |
| 34 | + if (not l_datum or not r_datum or |
| 35 | + len(l_datum) > 1 or len(r_datum) > 1 or |
| 36 | + l_datum[0].value == r_datum[0].value): |
| 37 | + # NOTE(sileht): should we do something if the expression |
| 38 | + # match multiple fields, for now ignore them |
| 39 | + continue |
| 40 | + elif l_datum[0].value < r_datum[0].value: |
| 41 | + return 1 if reverse else -1 |
| 42 | + else: |
| 43 | + return -1 if reverse else 1 |
| 44 | + return 0 |
| 45 | + |
| 46 | + def find(self, datum): |
| 47 | + """Return sorted value of This if list or dict.""" |
| 48 | + if isinstance(datum.value, dict) and self.expressions: |
| 49 | + return datum |
| 50 | + |
| 51 | + if isinstance(datum.value, dict) or isinstance(datum.value, list): |
| 52 | + key = (functools.cmp_to_key(self._compare) |
| 53 | + if self.expressions else None) |
| 54 | + return [DatumInContext.wrap( |
| 55 | + [value for value in sorted(datum.value, key=key)])] |
| 56 | + return datum |
| 57 | + |
| 58 | + def __eq__(self, other): |
| 59 | + return isinstance(other, Len) |
| 60 | + |
| 61 | + def __repr__(self): |
| 62 | + return '%s(%r)' % (self.__class__.__name__, self.expressions) |
| 63 | + |
| 64 | + def __str__(self): |
| 65 | + return '[?%s]' % self.expressions |
| 66 | + |
| 67 | + |
| 68 | +class Len(JSONPath): |
| 69 | + """The JSONPath referring to the len of the current object. |
| 70 | +
|
| 71 | + Concrete syntax is '`len`'. |
| 72 | + """ |
| 73 | + |
| 74 | + def find(self, datum): |
| 75 | + datum = DatumInContext.wrap(datum) |
| 76 | + try: |
| 77 | + value = len(datum.value) |
| 78 | + except TypeError: |
| 79 | + return [] |
| 80 | + else: |
| 81 | + return [DatumInContext(value, |
| 82 | + context=None, |
| 83 | + path=Len())] |
| 84 | + |
| 85 | + def __eq__(self, other): |
| 86 | + return isinstance(other, Len) |
| 87 | + |
| 88 | + def __str__(self): |
| 89 | + return '`len`' |
| 90 | + |
| 91 | + def __repr__(self): |
| 92 | + return 'Len()' |
0 commit comments