2023-02-14 15:03:56 -08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
#
|
|
|
|
|
# The OpenSearch Contributors require contributions made to
|
|
|
|
|
# this file be licensed under the Apache-2.0 license or a
|
|
|
|
|
# compatible open source license.
|
|
|
|
|
#
|
|
|
|
|
# Modifications Copyright OpenSearch Contributors. See
|
|
|
|
|
# GitHub history for details.
|
|
|
|
|
#
|
|
|
|
|
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
|
|
|
# license agreements. See the NOTICE file distributed with
|
|
|
|
|
# this work for additional information regarding copyright
|
|
|
|
|
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
|
|
|
# the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing,
|
|
|
|
|
# software distributed under the License is distributed on an
|
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
# KIND, either express or implied. See the License for the
|
|
|
|
|
# specific language governing permissions and limitations
|
|
|
|
|
# under the License.
|
|
|
|
|
|
|
|
|
|
import operator
|
2023-11-06 13:08:19 -05:00
|
|
|
from typing import Any
|
2023-02-14 15:03:56 -08:00
|
|
|
|
|
|
|
|
from .utils import AttrDict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Range(AttrDict):
|
|
|
|
|
OPS = {
|
|
|
|
|
"lt": operator.lt,
|
|
|
|
|
"lte": operator.le,
|
|
|
|
|
"gt": operator.gt,
|
|
|
|
|
"gte": operator.ge,
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
2023-02-14 15:03:56 -08:00
|
|
|
if args and (len(args) > 1 or kwargs or not isinstance(args[0], dict)):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Range accepts a single dictionary or a set of keyword arguments."
|
|
|
|
|
)
|
|
|
|
|
data = args[0] if args else kwargs
|
|
|
|
|
|
|
|
|
|
for k in data:
|
|
|
|
|
if k not in self.OPS:
|
2024-07-20 23:19:20 +03:00
|
|
|
raise ValueError(f"Range received an unknown operator {k!r}")
|
2023-02-14 15:03:56 -08:00
|
|
|
|
|
|
|
|
if "gt" in data and "gte" in data:
|
|
|
|
|
raise ValueError("You cannot specify both gt and gte for Range.")
|
|
|
|
|
|
|
|
|
|
if "lt" in data and "lte" in data:
|
|
|
|
|
raise ValueError("You cannot specify both lt and lte for Range.")
|
|
|
|
|
|
2024-07-20 19:30:37 +03:00
|
|
|
super().__init__(args[0] if args else kwargs)
|
2023-02-14 15:03:56 -08:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def __repr__(self) -> str:
|
2024-07-20 19:30:37 +03:00
|
|
|
return "Range(%s)" % ", ".join("%s=%r" % op for op in self._d_.items())
|
2023-02-14 15:03:56 -08:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def __contains__(self, item: Any) -> bool:
|
2024-07-20 19:30:37 +03:00
|
|
|
if isinstance(item, str):
|
|
|
|
|
return super().__contains__(item)
|
2023-02-14 15:03:56 -08:00
|
|
|
|
|
|
|
|
for op in self.OPS:
|
|
|
|
|
if op in self._d_ and not self.OPS[op](item, self._d_[op]):
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
@property
|
2023-11-06 13:08:19 -05:00
|
|
|
def upper(self) -> Any:
|
2023-02-14 15:03:56 -08:00
|
|
|
if "lt" in self._d_:
|
|
|
|
|
return self._d_["lt"], False
|
|
|
|
|
if "lte" in self._d_:
|
|
|
|
|
return self._d_["lte"], True
|
|
|
|
|
return None, False
|
|
|
|
|
|
|
|
|
|
@property
|
2023-11-06 13:08:19 -05:00
|
|
|
def lower(self) -> Any:
|
2023-02-14 15:03:56 -08:00
|
|
|
if "gt" in self._d_:
|
|
|
|
|
return self._d_["gt"], False
|
|
|
|
|
if "gte" in self._d_:
|
|
|
|
|
return self._d_["gte"], True
|
|
|
|
|
return None, False
|
2023-11-06 13:08:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["Range"]
|