Skip to content

Commit 4ff216b

Browse files
committed
Updated README, added deprecation warning
1 parent 4a1c779 commit 4ff216b

File tree

5 files changed

+105
-109
lines changed

5 files changed

+105
-109
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# CUSTOM ===========================================================
22

3+
# script for upload to PIP
34
register.sh
5+
# tmp file for testing some snippets of code
6+
test.py
47

58

69

@@ -70,4 +73,4 @@ Desktop.ini
7073
$RECYCLE.BIN/
7174

7275
# Editor
73-
*.sublime-*
76+
*.sublime-*

README.rst

Lines changed: 56 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,24 @@
22
pycaching - Geocaching for Python
33
=================================
44

5-
A Python 3 interface for working with Geocaching.com website.
6-
75
--------
86
Features
97
--------
108

119
- **login** to Geocaching.com
1210
- **search** caches
13-
11+
1412
- normal search (unlimited number of caches from any point)
1513
- quick search (all caches inside some area)
16-
17-
- **load cache** and its details by WP or URL
14+
15+
- **load cache** and its details
1816

1917
- normal loading (loads all details)
2018
- quick loading (loads just basic info very quickly)
2119
- lazy loading (create cache object and load info on demand)
2220
- load logbook for given cache
2321

2422
- **post log** to cache logbook
25-
2623
- **load trackable** details by tracking code
2724
- **geocode** given location
2825

@@ -42,13 +39,15 @@ Dev version - manually from GIT:
4239
.. code:: bash
4340
4441
git clone https://github.com/tomasbedrich/pycaching.git
42+
pip install ./pycaching
4543
46-
Requirements
47-
~~~~~~~~~~~~
44+
Pycaching has following requirements:
45+
46+
.. code::
4847
49-
- Python >= 3.4
50-
- MechanicalSoup >= 0.3.0
51-
- geopy >= 1.0.0
48+
Python>=3.4
49+
MechanicalSoup>=0.3.0
50+
geopy>=1.0.0
5251
5352
5453
-------------
@@ -63,143 +62,100 @@ Login
6362
import pycaching
6463
geocaching = pycaching.login("user", "pass")
6564
66-
The above is just shortcut for:
67-
68-
.. code:: python
69-
70-
from pycaching import Geocaching
71-
geocaching = Geocaching()
72-
geocaching.login("user", "pass")
73-
7465
Load a cache details
7566
~~~~~~~~~~~~~~~~~~~~
7667

7768
.. code:: python
7869
79-
import pycaching
70+
cache = geocaching.get_cache("GC1PAR2")
71+
print(cache.name) # cache.load() is automatically called
72+
print(cache.location) # stored in cache, printed immediately
8073
81-
geocaching = pycaching.login("user", "pass")
82-
cache = geocaching.load_cache("GC1PAR2")
83-
print(cache.name)
74+
This uses lazy loading, so the ``Cache`` object is created immediately and the
75+
page is loaded when needed (accessing the name).
8476

85-
Using lazy loading:
77+
You can use different method of loading cache details. It will be much faster,
78+
but it will load less details:
8679

8780
.. code:: python
8881
89-
from pycaching import Geocaching, Cache
82+
cache = geocaching.get_cache("GC1PAR2")
83+
cache.load_quick() # takes a small while
84+
print(cache.name) # stored in cache, printed immediately
85+
print(cache.location) # NOT stored in cache, will trigger full loading
9086
91-
geocaching = Geocaching()
92-
geocaching.login("user", "pass")
93-
cache = Cache("GC1PAR2", geocaching)
94-
print(cache.name)
87+
You can also load a logbook for cache:
9588

96-
The difference is, that ``Cache`` object is created immediately and the
97-
page is loaded when needed (accessing the name).
89+
.. code:: python
9890
99-
Post a new log to a cache
100-
~~~~~~~~~~~~~~~~~~~~~~~~~
91+
for log in cache.load_logbook(limit=200):
92+
print(log.visited, log.type, log.author, log.text)
10193
102-
:: code:: python
94+
Or its trackables:
10395

104-
from pycaching import Geocaching, Cache, Log, enums
105-
from datetime import date
96+
.. code:: python
10697
107-
geocaching = Geocaching()
108-
geocaching.login("user", "pass")
109-
cache = Cache("GC1PAR2", geocaching)
98+
for trackable in cache.load_trackables(limit=5):
99+
print(trackable.name)
110100
111-
log = Log()
112-
log.text = "Found cache in the rain. Nice Place, TFTC!"
113-
log.type = enums.LogType.found_it
114-
log.visited = date.today
101+
Post a log to cache
102+
~~~~~~~~~~~~~~~~~~~
115103

116-
cache.post_log(log)
104+
.. code:: python
117105
118-
Find all traditional caches around
119-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106+
geocaching.post_log("GC1PAR2", "Found cache in the rain. Nice Place, TFTC!")
120107
121-
Notice the ``limit`` in search function. It is because ``search()``
122-
returns a generator object, which would fetch the caches forever in case
123-
of simple loop.
108+
It is also possible to call post_log on ``Cache`` object, but you would have
109+
to create ``Log`` object manually and pass it to this method.
110+
111+
Search for all traditional caches around
112+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124113

125114
.. code:: python
126115
127-
from pycaching import Geocaching, Point, Type
116+
from pycaching import Point
117+
from pycaching.cache import Type
128118
129119
point = Point(56.25263, 15.26738)
130-
geocaching = Geocaching()
131-
geocaching.login("user", "pass")
132120
133121
for cache in geocaching.search(point, limit=50):
134122
if cache.type == Type.traditional:
135123
print(cache.name)
136124
137-
Find all caches on some adress
138-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125+
Notice the ``limit`` in search function. It is because ``search()``
126+
returns a generator object, which would fetch the caches forever in case
127+
of simple loop.
139128

140-
.. code:: python
129+
Geocode adress and search around
130+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141131

142-
import pycaching
132+
.. code:: python
143133
144-
geocaching = pycaching.login("user", "pass")
145-
point = geocaching.geocode("10900 Euclid Ave in Cleveland")
134+
point = geocaching.geocode("Prague")
146135
147136
for cache in geocaching.search(point, limit=10):
148137
print(cache.name)
149138
150-
Find approximate location of caches in area
151-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139+
Find caches with their approximate locations in some area
140+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152141

153142
.. code:: python
154143
155-
from pycaching import Geocaching, Point, Rectangle
144+
from pycaching import Point, Rectangle
156145
157-
geocaching = pycaching.Geocaching()
158-
geocaching.login("user", "pass")
159146
rect = Rectangle(Point(60.15, 24.95), Point(60.17, 25.00))
160147
161148
for cache in geocaching.search_quick(rect, strict=True):
162149
print(cache.name, cache.location.precision)
163150
164151
165-
Load trackable details
166-
~~~~~~~~~~~~~~~~~~~~~~
167-
168-
.. code:: python
169-
170-
import pycaching
171-
geocaching = pycaching.login("user", "pass")
172-
trackable = geocaching.load_trackable("TB3ZGT2")
173-
print(trackable.name, trackable.goal, trackable.description, trackable.location)
174-
175-
176-
Find all nearby caches with trackables in them
177-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178-
179-
.. code:: python
180-
181-
from pycaching import Geocaching, Point
182-
183-
point = Point(56.25263, 15.26738)
184-
geocaching = Geocaching()
185-
geocaching.login("user", "pass")
186-
187-
for cache in geocaching.search(point, limit=50):
188-
if len(cache.trackables) > 0:
189-
print(cache.name)
190-
191-
192-
Load logbook for a cache
152+
Load a trackable details
193153
~~~~~~~~~~~~~~~~~~~~~~~~
194154

195155
.. code:: python
196156
197-
import pycaching
198-
199-
geocaching = pycaching.login("user", "pass")
200-
cache = geocaching.load_cache("GC1PAR2")
201-
for log in cache.load_logbook(limit=200):
202-
print(log.visited, log.type, log.author, log.text)
157+
trackable = geocaching.get_trackable("TB3ZGT2")
158+
print(trackable.name, trackable.goal, trackable.description, trackable.location)
203159
204160
205161
--------
@@ -229,10 +185,12 @@ Although the new version was massively rewritten, I'd like to thank to their aut
229185
Author
230186
~~~~~~
231187

232-
| Tomas Bedrich
188+
| Tomáš Bedřich
233189
| `tbedrich.cz <http://tbedrich.cz>`__
234190
235191
192+
Thanks to `all contributors <https://github.com/tomasbedrich/pycaching/graphs/contributors>`__!
193+
236194
------------------------------------------------------------------------------------
237195

238196
|Build Status| |Coverage Status| |PyPI monthly downloads|

pycaching/cache.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,13 @@ def load_quick(self):
428428
429429
Loads just basic cache details, but very quickly."""
430430

431-
res = self.geocaching._request("http://tiles01.geocaching.com/map.details",
432-
params={"i": self.wp}, expect="json")
431+
res = self.geocaching._request("http://tiles01.geocaching.com/map.details", params={
432+
"i": self.wp
433+
}, expect="json")
433434

434435
if res["status"] == "failed" or len(res["data"]) != 1:
435-
error_msg = res[
436-
"msg"] if "msg" in res else "Unknown error (probably not existing cache)"
437-
raise errors.LoadError("Waypoint '{}' cannot be loaded: {}".format(self.wp, error_msg))
436+
msg = res["msg"] if "msg" in res else "Unknown error (probably not existing cache)"
437+
raise errors.LoadError("Waypoint '{}' cannot be loaded: {}".format(self.wp, msg))
438438

439439
data = res["data"][0]
440440

@@ -508,6 +508,9 @@ def load_trackables(self, limit=float("inf")):
508508
self.trackables = []
509509

510510
url = self.trackable_page_url # will trigger lazy_loading if needed
511+
if not url:
512+
# no link to all trackables = no trackables in cache
513+
raise StopIteration()
511514
res = self.geocaching._request(url)
512515

513516
trackable_table = res.find_all("table")[1]

pycaching/geocaching.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pycaching.geo import Point
1212
from pycaching.trackable import Trackable
1313
from pycaching.errors import Error, NotLoggedInException, LoginFailedException
14-
from pycaching.util import parse_date
14+
from pycaching.util import parse_date, deprecated
1515

1616

1717
class Geocaching(object):
@@ -210,20 +210,30 @@ def search_quick(self, area, *, strict=False, zoom=None):
210210
# can yield more caches (which are not exactly in desired area)
211211
yield cache
212212

213-
# add some shortcuts ensure backwards compatibility
213+
# add some shortcuts ------------------------------------------------------
214214

215215
def geocode(self, location):
216216
return Point.from_location(self, location)
217217

218-
def load_cache(self, wp):
218+
def get_cache(self, wp):
219219
"""Return a cache by its WP."""
220220
return Cache(self, wp)
221221

222-
def load_trackable(self, tid):
222+
def get_trackable(self, tid):
223223
"""Return a cache by its TID."""
224224
return Trackable(self, tid)
225225

226226
def post_log(self, wp, text, type=LogType.found_it, date=datetime.date.today()):
227227
"""Post log for cache."""
228228
l = Log(type=type, text=text, visited=date)
229-
self.load_cache(wp).post_log(l)
229+
self.get_cache(wp).post_log(l)
230+
231+
# ensure backwards compatibility -----------------------------------------
232+
233+
@deprecated
234+
def load_cache(self, wp):
235+
return self.get_cache(wp)
236+
237+
@deprecated
238+
def load_trackable(self, tid):
239+
return self.get_trackable(tid)

pycaching/util.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import logging
44
import re
5+
import warnings
6+
import inspect
7+
import functools
58
from datetime import datetime
69
from pycaching import errors
710

@@ -29,6 +32,25 @@ def wrapper(*args, **kwargs):
2932
return wrapper
3033

3134

35+
# copied from:
36+
# https://wiki.python.org/moin/PythonDecoratorLibrary#Generating_Deprecation_Warnings
37+
def deprecated(func):
38+
"""This is a decorator which can be used to mark functions
39+
as deprecated. It will result in a warning being emitted
40+
when the function is used."""
41+
42+
@functools.wraps(func)
43+
def new_func(*args, **kwargs):
44+
warnings.warn_explicit(
45+
"Call to deprecated function {}.".format(func.__name__),
46+
category=FutureWarning,
47+
filename=inspect.getfile(func),
48+
lineno=inspect.getsourcelines(func)[1] + 1
49+
)
50+
return func(*args, **kwargs)
51+
return new_func
52+
53+
3254
def rot13(text):
3355
"""Returns a text encoded by rot13 cipher."""
3456
return str.translate(text, _rot13codeTable)

0 commit comments

Comments
 (0)