| 273 | |
| 274 | |
| 275 | class SpecPredicate(Predicate): |
| 276 | def __init__(self, db, op=None, spec=None, description=None): |
| 277 | self.db = db |
| 278 | self.op = op |
| 279 | self.spec = spec |
| 280 | self.description = description |
| 281 | |
| 282 | _ops = { |
| 283 | "<": operator.lt, |
| 284 | ">": operator.gt, |
| 285 | "==": operator.eq, |
| 286 | "!=": operator.ne, |
| 287 | "<=": operator.le, |
| 288 | ">=": operator.ge, |
| 289 | "in": operator.contains, |
| 290 | "between": lambda val, pair: val >= pair[0] and val <= pair[1], |
| 291 | } |
| 292 | |
| 293 | def __call__(self, config): |
| 294 | if config is None: |
| 295 | return False |
| 296 | |
| 297 | engine = config.db |
| 298 | |
| 299 | if "+" in self.db: |
| 300 | dialect, driver = self.db.split("+") |
| 301 | else: |
| 302 | dialect, driver = self.db, None |
| 303 | |
| 304 | if dialect and engine.name != dialect: |
| 305 | return False |
| 306 | if driver is not None and engine.driver != driver: |
| 307 | return False |
| 308 | |
| 309 | if self.op is not None: |
| 310 | assert driver is None, "DBAPI version specs not supported yet" |
| 311 | |
| 312 | version = _server_version(engine) |
| 313 | oper = ( |
| 314 | hasattr(self.op, "__call__") and self.op or self._ops[self.op] |
| 315 | ) |
| 316 | return oper(version, self.spec) |
| 317 | else: |
| 318 | return True |
| 319 | |
| 320 | def _as_string(self, config, negate=False): |
| 321 | if self.description is not None: |
| 322 | return self._format_description(config) |
| 323 | elif self.op is None: |
| 324 | if negate: |
| 325 | return "not %s" % self.db |
| 326 | else: |
| 327 | return "%s" % self.db |
| 328 | else: |
| 329 | if negate: |
| 330 | return "not %s %s %s" % (self.db, self.op, self.spec) |
| 331 | else: |
| 332 | return "%s %s %s" % (self.db, self.op, self.spec) |
no outgoing calls
no test coverage detected