Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wowtor committed Dec 4, 2024
2 parents 7391c83 + ccf06fd commit 21fcf8f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 46 deletions.
58 changes: 34 additions & 24 deletions telcell/cell_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Radio(Enum):
NR = "NR"


CELL_IDENTITY_PATTERN = re.compile(r"""
CELL_IDENTITY_PATTERN = re.compile(
r"""
^
((?P<radio>[a-zA-Z]+)/)?
(?P<mcc>[0-9]+|\?)
Expand All @@ -20,7 +21,9 @@ class Radio(Enum):
-(?P<ci>[0-9]+|\?)|(?P<eci>[0-9]+|\?)
))?
$
""", re.VERBOSE)
""",
re.VERBOSE,
)


class CellIdentity:
Expand All @@ -33,14 +36,15 @@ class CellIdentity:
"""

@staticmethod
def create(*,
radio: Optional[Union[Radio, str]] = None,
mcc: Optional[int] = None,
mnc: Optional[int] = None,
lac: Optional[int] = None,
ci: Optional[int] = None,
eci: Optional[int] = None,
) -> "CellIdentity":
def create(
*,
radio: Optional[Union[Radio, str]] = None,
mcc: Optional[int] = None,
mnc: Optional[int] = None,
lac: Optional[int] = None,
ci: Optional[int] = None,
eci: Optional[int] = None,
) -> "CellIdentity":

if isinstance(radio, Radio):
radio = radio.value
Expand All @@ -59,10 +63,14 @@ def create(*,
raise ValueError(f"unsupported radio technology: {radio}")
elif eci is not None: # we have: `eci`; missing: `radio`
if ci is not None or lac is not None:
raise ValueError("either `ci` or `eci` should be provided, but not both")
raise ValueError(
"either `lac`/`ci` or `eci` should be provided, but not both"
)
# guess LTE/NR
return EutranCellGlobalIdentity(mcc, mnc, eci)
elif ci is not None or lac is not None: # we have: `ci` or `lac`; missing: `radio`, `eci`
elif (
ci is not None or lac is not None
): # we have: `ci` or `lac`; missing: `radio`, `eci`
# guess GSM/UMTS
return CellGlobalIdentity(mcc, mnc, lac, ci)
else: # missing: `radio`, `eci`, `ci`, `lac`
Expand Down Expand Up @@ -97,7 +105,9 @@ def convert_value(key: str, value: str) -> Any:
else:
return int(value)

groups = {key: convert_value(key, value) for key, value in m.groupdict().items()}
groups = {
key: convert_value(key, value) for key, value in m.groupdict().items()
}
return CellIdentity.create(**groups)

def __init__(self, mcc: Optional[int], mnc: Optional[int]):
Expand Down Expand Up @@ -145,10 +155,10 @@ def is_valid(self) -> bool:

def __eq__(self, other):
return (
isinstance(other, CellIdentity)
and self.radio == other.radio
and self.mcc == other.mcc
and self.mnc == other.mnc
isinstance(other, CellIdentity)
and self.radio == other.radio
and self.mcc == other.mcc
and self.mnc == other.mnc
)

def __repr__(self) -> str:
Expand Down Expand Up @@ -207,10 +217,10 @@ def is_valid(self) -> bool:

def __eq__(self, other):
return (
isinstance(other, CellGlobalIdentity)
and super().__eq__(other)
and self.lac == other.lac
and self.ci == other.ci
isinstance(other, CellGlobalIdentity)
and super().__eq__(other)
and self.lac == other.lac
and self.ci == other.ci
)


Expand Down Expand Up @@ -288,9 +298,9 @@ def is_valid(self) -> bool:

def __eq__(self, other):
return (
isinstance(other, EutranCellGlobalIdentity)
and super().__eq__(other)
and self.eci == other.eci
isinstance(other, EutranCellGlobalIdentity)
and super().__eq__(other)
and self.eci == other.eci
)


Expand Down
42 changes: 20 additions & 22 deletions telcell/celldb/pgdatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _build_antenna(row: Tuple) -> Properties:


def _build_cell_identity_query(ci):
if ci is not None and not isinstance(ci, CellIdentity):
if not isinstance(ci, CellIdentity):
raise ValueError(f"ci expected to be CellIdentity; found: {type(ci)}")

qwhere = []
Expand Down Expand Up @@ -146,12 +146,14 @@ def search(
qwhere = list(self._qwhere)
qargs = list(self._qargs)

if coords is not None and distance_limit_m is None and count_limit is None:
raise ValueError(f"coords argument requires either distance_limit or count_limit")
if coords and distance_limit_m is None and count_limit is None:
raise ValueError(
"coords argument requires either distance_limit or count_limit"
)

if distance_limit_m is not None:
if coords is None:
raise ValueError(f"distance_limit argument requires coords")
raise ValueError("distance_limit argument requires coords")

x, y = point_to_rd(coords)
qwhere.append(
Expand All @@ -162,32 +164,28 @@ def search(
f"NOT ST_DWithin(rd, 'SRID=4326;POINT({x} {y})', {distance_lower_limit_m})"
)

if date is not None:
if date:
qwhere.append("(date_start is NULL OR %s >= date_start)")
qwhere.append("(date_end is NULL OR %s < date_end)")
qargs.extend([date, date])

if ci is not None:
if ci:
add_qwhere, add_qargs = _build_cell_identity_query(ci)
qwhere.append(add_qwhere)
qargs.extend(add_qargs)

assert radio is None, "radio argument makes no sense in combination with ci"
assert mcc is None, "mcc argument makes no sense in combination with ci"
assert mnc is None, "mnc argument makes no sense in combination with ci"
else:
if radio is not None:
if isinstance(radio, str):
radio = [radio]
qwhere.append(f"({' OR '.join(['radio = %s'])})")
qargs.extend(radio)

if mcc is not None:
qwhere.append(f"mcc = %s")
qargs.append(mcc)
if mnc is not None:
qwhere.append(f"mnc = %s")
qargs.append(mnc)
if radio is not None:
if isinstance(radio, str):
radio = [radio]
qwhere.append(f"({' OR '.join(['radio = %s'])})")
qargs.extend(radio)

if mcc is not None:
qwhere.append("mcc = %s")
qargs.append(mcc)
if mnc is not None:
qwhere.append("mnc = %s")
qargs.append(mnc)

if exclude is not None:
if isinstance(exclude, CellIdentity):
Expand Down

0 comments on commit 21fcf8f

Please sign in to comment.