Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cache.status property #193

Merged
merged 19 commits into from
Aug 28, 2022
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions pycaching/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pycaching.log import Log
from pycaching.log import Type as LogType
from pycaching.trackable import Trackable
from pycaching.util import lazy_loaded, parse_date, rot13
from pycaching.util import deprecated, lazy_loaded, parse_date, rot13

# prefix _type() function to avoid collisions with cache type
_type = type
Expand Down Expand Up @@ -145,7 +145,6 @@ def _from_print_page(cls, geocaching, guid, soup):
content.find(class_="HalfRight AlignRight").p.text.strip().partition(":")[2].strip()
)
cache_info["location"] = Point.from_string(content.find(class_="LatLong").text.strip())
cache_info["state"] = None # not on the page
attributes = [
img["src"].split("/")[-1].partition(".")[0].rpartition("-")
for img in content.find(class_="sortables").find_all("img")
Expand All @@ -171,6 +170,7 @@ def _from_api_record(cls, geocaching, record):
name=record["name"],
type=Type.from_number(record["geocacheType"]),
state=Status(record["cacheStatus"]) == Status.enabled,
status=Status(record["cacheStatus"]),
found=record["userFound"],
size=Size.from_number(record["containerType"]),
difficulty=record["difficulty"],
Expand Down Expand Up @@ -214,6 +214,7 @@ def __init__(self, geocaching, wp, **kwargs):
"location",
"original_location",
"state",
"status",
"found",
"size",
"difficulty",
Expand Down Expand Up @@ -405,18 +406,31 @@ def type(self, type):

@property
@lazy_loaded
@deprecated
def state(self):
"""The cache status.

:code:`True` if cache is enabled, :code:`False` if cache is disabled.
:code:`True` if cache is enabled, otherwise :code:`False`.

:type: :class:`bool`
"""
return self._state
return self._status == Status.enabled

@state.setter
def state(self, state):
self._state = bool(state)
@property
@lazy_loaded
def status(self):
"""The cache status (Enabled, Disabled, Archived, Unpublished, Locked).

:type: :class:`.cache.Status`
"""
return self._status

@status.setter
def status(self, status):
if isinstance(status, Status):
self._status = status
else:
raise errors.ValueError("Passed object is not Status instance.")

@property
@lazy_loaded
Expand Down Expand Up @@ -780,7 +794,17 @@ def load(self):

self.location = Point.from_string(root.find(id="uxLatLon").text)

self.state = root.find("ul", "OldWarning") is None
# Cache status
BelKed marked this conversation as resolved.
Show resolved Hide resolved
if root.find(id="ctl00_ContentBody_disabledMessage"):
self.status = Status.disabled
elif root.find(id="ctl00_ContentBody_archivedMessage"):
self.status = Status.archived
elif root.find(id="unpublishedMessage") or root.find(id="unpublishedReviewerNoteMessage"):
self.status = Status.unpublished
elif root.find(id="ctl00_ContentBody_lockedMessage"):
self.status = Status.locked
else:
self.status = Status.enabled

log_image = root.find(id="ctl00_ContentBody_GeoNav_logTypeImage")
if log_image:
Expand Down Expand Up @@ -839,6 +863,7 @@ def load_quick(self):
Use information from geocaching map tooltips. Therefore loading is very quick, but
the only loaded properties are: `name`, `type`, `state`, `size`, `difficulty`, `terrain`,
`hidden`, `author`, `favorites` and `pm_only`.
It also loads `status`, however locked caches are considered archived.
BelKed marked this conversation as resolved.
Show resolved Hide resolved

:raise .LoadError: If cache loading fails (probably because of not existing cache).
"""
Expand All @@ -853,7 +878,12 @@ def load_quick(self):
# prettify data
self.name = data["name"]
self.type = Type.from_string(data["type"]["text"])
self.state = data["available"]

if data["available"]:
self.status = Status.enabled
else:
self.status = Status.archived if data["archived"] else Status.disabled
BelKed marked this conversation as resolved.
Show resolved Hide resolved

self.size = Size.from_string(data["container"]["text"])
self.difficulty = data["difficulty"]["text"]
self.terrain = data["terrain"]["text"]
Expand Down Expand Up @@ -1412,3 +1442,4 @@ class Status(enum.IntEnum):
disabled = 1
archived = 2
unpublished = 3
locked = 4