Skip to content

Commit 9afd34b

Browse files
committed
3rd stage of refactoring
1 parent 6ab110f commit 9afd34b

File tree

16 files changed

+660
-940
lines changed

16 files changed

+660
-940
lines changed

pycaching/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/usr/bin/env python3
22

3-
from pycaching.area import Rectangle # NOQA
43
from pycaching.cache import Cache # NOQA
54
from pycaching.geocaching import Geocaching # NOQA
65
from pycaching.log import Log # NOQA
7-
from pycaching.point import Point # NOQA
86
from pycaching.trackable import Trackable # NOQA
7+
from pycaching.geo import Point, Rectangle # NOQA
98

109

1110
def login(username, password):

pycaching/area.py

Lines changed: 0 additions & 68 deletions
This file was deleted.

pycaching/cache.py

Lines changed: 108 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import logging
44
import datetime
55
import re
6+
import enum
67
from pycaching import errors
7-
from pycaching.point import Point
8-
from pycaching.enums import Type, Size
8+
from pycaching.geo import Point
99
from pycaching.trackable import Trackable
1010
from pycaching.log import Log
1111
from pycaching.util import parse_date, rot13, lazy_loaded
@@ -87,72 +87,33 @@ class Cache(object):
8787
"wirelessbeacon": "Wireless Beacon"
8888
}
8989

90-
def __init__(self, geocaching, wp, *, name=None, type=None, location=None, state=None,
91-
found=None, size=None, difficulty=None, terrain=None, author=None, hidden=None,
92-
attributes=None, summary=None, description=None, hint=None, favorites=None,
93-
pm_only=None, url=None, trackable_page_url=None, logbook_token=None,
94-
log_page_url=None):
90+
def __init__(self, geocaching, wp, **kwargs):
91+
9592
self.geocaching = geocaching
96-
# properties
9793
if wp is not None:
9894
self.wp = wp
99-
if name is not None:
100-
self.name = name
101-
if type is not None:
102-
self.type = type
103-
if location is not None:
104-
self.location = location
105-
if state is not None:
106-
self.state = state
107-
if found is not None:
108-
self.found = found
109-
if size is not None:
110-
self.size = size
111-
if difficulty is not None:
112-
self.difficulty = difficulty
113-
if terrain is not None:
114-
self.terrain = terrain
115-
if author is not None:
116-
self.author = author
117-
if hidden is not None:
118-
self.hidden = hidden
119-
if attributes is not None:
120-
self.attributes = attributes
121-
if summary is not None:
122-
self.summary = summary
123-
if description is not None:
124-
self.description = description
125-
if hint is not None:
126-
self.hint = hint
127-
if favorites is not None:
128-
self.favorites = favorites
129-
if pm_only is not None:
130-
self.pm_only = pm_only
131-
if url is not None:
132-
self.url = url
133-
# related
134-
self.logbook = []
135-
self.trackables = []
136-
if logbook_token is not None:
137-
self.logbook_token = logbook_token
138-
if trackable_page_url is not None:
139-
self.trackable_page_url = trackable_page_url
140-
if log_page_url is not None:
141-
self.log_page_url = log_page_url
95+
96+
known_kwargs = {"name", "type", "location", "state", "found", "size", "difficulty", "terrain",
97+
"author", "hidden", "attributes", "summary", "description", "hint", "favorites",
98+
"pm_only", "url", "trackable_page_url", "logbook_token", "log_page_url"}
99+
100+
for name in known_kwargs:
101+
if name in kwargs:
102+
setattr(self, name, kwargs[name])
142103

143104
def __str__(self):
144105
return self.wp
145106

146107
def __eq__(self, other):
147-
return self.wp == other.wp
108+
return self.geocaching == other.geocaching and self.wp == other.wp
148109

149110
@classmethod
150111
def from_trackable(cls, trackable):
151112
return cls(trackable.geocaching, None, url=trackable.location_url)
152113

153114
@classmethod
154-
def from_block(cls, geocaching, block):
155-
c = cls(geocaching, block.cache_wp, name=block.cache_name)
115+
def from_block(cls, block):
116+
c = cls(block.tile.geocaching, block.cache_wp, name=block.cache_name)
156117
c.location = Point.from_block(block)
157118
return c
158119

@@ -355,10 +316,6 @@ def pm_only(self):
355316
def pm_only(self, pm_only):
356317
self._pm_only = bool(pm_only)
357318

358-
def inside_area(self, area):
359-
"""Calculate if geocache is inside given area"""
360-
return area.inside_area(self.location)
361-
362319
@property
363320
@lazy_loaded
364321
def logbook_token(self):
@@ -600,3 +557,96 @@ def post_log(self, l):
600557
post["ctl00$ContentBody$LogBookPanel1$uxLogInfo"] = l.text
601558

602559
self.geocaching._request(self.log_page_url, method="POST", data=post)
560+
561+
562+
class Type(enum.Enum):
563+
564+
# value is cache image filename (http://www.geocaching.com/images/WptTypes/[VALUE].gif)
565+
traditional = "2"
566+
multicache = "3"
567+
mystery = unknown = "8"
568+
letterbox = "5"
569+
event = "6"
570+
mega_event = "mega"
571+
giga_event = "giga"
572+
earthcache = "137"
573+
cito = cache_in_trash_out_event = "13"
574+
webcam = "11"
575+
virtual = "4"
576+
wherigo = "1858"
577+
lost_and_found_event = "10Years_32"
578+
project_ape = "ape_32"
579+
groundspeak_hq = "HQ_32"
580+
gps_adventures_exhibit = "1304"
581+
groundspeak_block_party = "4738"
582+
locationless = reverse = "12"
583+
584+
@classmethod
585+
def from_filename(cls, filename):
586+
"""Returns cache type from its image filename"""
587+
588+
if filename == "earthcache":
589+
filename = "137" # fuck Groundspeak, they use 2 exactly same icons with 2 different names
590+
591+
return cls(filename)
592+
593+
@classmethod
594+
def from_string(cls, name):
595+
"""Returns cache type from its human readable name"""
596+
597+
name = name.replace(" Geocache", "") # with space!
598+
name = name.replace(" Cache", "") # with space!
599+
name = name.lower().strip()
600+
601+
name_mapping = {
602+
"traditional": cls.traditional,
603+
"multi-cache": cls.multicache,
604+
"mystery": cls.mystery,
605+
"unknown": cls.unknown,
606+
"letterbox hybrid": cls.letterbox,
607+
"event": cls.event,
608+
"mega-event": cls.mega_event,
609+
"giga-event": cls.giga_event,
610+
"earthcache": cls.earthcache,
611+
"cito": cls.cito,
612+
"cache in trash out event": cls.cache_in_trash_out_event,
613+
"webcam": cls.webcam,
614+
"virtual": cls.virtual,
615+
"wherigo": cls.wherigo,
616+
"lost and found event": cls.lost_and_found_event,
617+
"project ape": cls.project_ape,
618+
"groundspeak hq": cls.groundspeak_hq,
619+
"gps adventures exhibit": cls.gps_adventures_exhibit,
620+
"groundspeak block party": cls.groundspeak_block_party,
621+
"locationless (reverse)": cls.locationless,
622+
}
623+
624+
try:
625+
return name_mapping[name]
626+
except KeyError as e:
627+
raise errors.ValueError("Unknown cache type '{}'.".format(name)) from e
628+
629+
630+
class Size(enum.Enum):
631+
micro = "micro"
632+
small = "small"
633+
regular = "regular"
634+
large = "large"
635+
not_chosen = "not chosen"
636+
virtual = "virtual"
637+
other = "other"
638+
639+
@classmethod
640+
def from_filename(cls, filename):
641+
"""Returns cache size from its image filename"""
642+
return cls[filename]
643+
644+
@classmethod
645+
def from_string(cls, name):
646+
"""Returns cache size from its human readable name"""
647+
name = name.strip().lower()
648+
649+
try:
650+
return cls(name)
651+
except ValueError as e:
652+
raise errors.ValueError("Unknown cache type '{}'.".format(name)) from e

0 commit comments

Comments
 (0)