Skip to content

Commit

Permalink
Merge remote-tracking branch 'LordFlashmeow/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
leonhard-s committed May 11, 2020
2 parents f428fac + 0c7f8ef commit f16efbf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
15 changes: 15 additions & 0 deletions auraxium/census.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ def to_url(self) -> str:
return self.field + modifier + _value_to_str(self.value)


def generate_term(field, value: str):
MODIFIER_LIST: List[str] = ['', '!', '<', '[', '>', ']', '^', '*']

modifier = 0

for (modifier_index, operator) in enumerate(MODIFIER_LIST[1:], start=1): # Skip the first item, since we don't care
# if the modifier is '='
if operator in value:
modifier = SearchModifier(modifier_index)
value = value.replace(operator, '') # remove the operator from the term

break

return Term(field, value, modifier)

def retrieve(url: str, convert: bool) -> Dict[str, Any]:
"""Retrieve the server's response for a given URL."""
logger.debug('Performing request: %s', url)
Expand Down
27 changes: 21 additions & 6 deletions auraxium/join.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Tuple, Union
from .census import List, Term

from .census import List, Term, SearchModifier, generate_term
from .log import logger
from .type import CensusValue

Expand Down Expand Up @@ -27,8 +28,22 @@ def __init__(self, collection: str, inject_at: str = '',
self.show = [] if show is None else show
self.hide = [] if hide is None else hide
# Additional kwargs are passed on to the `add_term` method
self._terms: List[Term] = []
_ = [Term(k.replace('__', '.'), kwargs[k]) for k in kwargs]
self.terms: List[Term] = []

for field, value in kwargs.items():
self.terms.append(generate_term(field.replace('__', '.'), value))

def add_term(self, field: str, value: CensusValue,
modifier: SearchModifier = SearchModifier.EQUAL_TO) -> 'Join':
"""Add a search term to this join.
Any results returned by a join must meet every term defined
for it.
"""

new_term = Term(field, value, modifier)
self.terms.append(new_term)
return self

def set_hide(self, *args: Union[str, List[str]]) -> 'Join':
"""Hide the given field names from the response."""
Expand Down Expand Up @@ -59,7 +74,7 @@ def set_show(self, *args: Union[str, List[str]]) -> 'Join':

def terms(self, *args: Term) -> 'Join':
"""Apply the given list of terms to the join."""
self._terms = list(args)
self.terms = list(args)
return self

def process_join(self) -> str:
Expand Down Expand Up @@ -88,8 +103,8 @@ def process_join(self) -> str:
elif self.hide:
string += '^hide:' + "'".join(self.hide)
# Terms
if self._terms:
string += '^terms:' + '\''.join([t.to_url() for t in self._terms])
if self.terms:
string += '^terms:' + '\''.join([t.to_url() for t in self.terms])
# Process inner joins
if self._inner_joins:
string += '('
Expand Down
7 changes: 4 additions & 3 deletions auraxium/query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any, Dict, List, Optional, Tuple
from .census import retrieve, SearchModifier, Term
from .census import retrieve, SearchModifier, Term, generate_term
from .constants import CENSUS_ENDPOINT
from .join import Join
from .type import CensusValue
Expand Down Expand Up @@ -44,9 +44,10 @@ def __init__(self, collection: str = '', namespace: str = '',
self.start = start
self.sort_by: List[str] = []
self.timing = timing
# Additional kwargs are passed on to the `add_term` method
# Additional kwargs are passed on to the `generate_term` method
self.terms: List[Term] = []
_ = [self.add_term(k.replace('__', '.'), kwargs[k]) for k in kwargs]
for field, value in kwargs.items():
self.terms.append(generate_term(field.replace('__', '.'), value))

def add_term(self, field: str, value: CensusValue,
modifier: SearchModifier = SearchModifier.EQUAL_TO) -> 'Query':
Expand Down

0 comments on commit f16efbf

Please sign in to comment.