Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
PCigales authored Mar 5, 2023
1 parent 1bc0439 commit 123f6c9
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions GPXTweaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ def __new__(cls, url, method=None, headers=None, data=None, timeout=30, max_leng
if method is None:
method = 'GET' if data is None else 'POST'
redir = 0
retry = False
retried = False
exceeded = [False]
try:
url_p = urllib.parse.urlsplit(url, allow_fragments=False)
Expand Down Expand Up @@ -1139,14 +1139,15 @@ def __new__(cls, url, method=None, headers=None, data=None, timeout=30, max_leng
else:
pconnection[0].sendall(msg.encode('iso-8859-1') + (data or b''))
except:
if retry:
if retried:
raise
retry = True
retried = True
try:
pconnection[0].close()
except:
pass
pconnection[0] = None
pconnection[2].pop()
continue
while code == '100':
resp = HTTPMessage(pconnection[0], body=(method.upper() != 'HEAD'), decode=None, timeout=timeout, max_length=max_length, max_hlength=max_hlength, decompress=decompress, exceeded=exceeded)
Expand All @@ -1156,16 +1157,17 @@ def __new__(cls, url, method=None, headers=None, data=None, timeout=30, max_leng
if redir > 5:
raise
if code is None:
if retry or exceeded == [True]:
if retried or exceeded == [True]:
raise
retry = True
retried = True
try:
pconnection[0].close()
except:
pass
pconnection[0] = None
pconnection[2].pop()
continue
retry = False
retried = False
if basic_auth is not None and resp.header('Set-Cookie') is not None:
cook.update(resp.cookies(cls._netloc_split(url_p.netloc)[0], url_p.path.split('#', 1)[0]))
if code == '401':
Expand Down Expand Up @@ -1508,12 +1510,13 @@ def _retrieveitem():
self.log(2, 'cancel', row, col)
return
if (a := self.GAvailable):
tgen = self.Generators[(i := a.pop())]
tgen = self.Generators[a.pop()]
gen = tgen[1]
else:
self.GCondition.wait()
tgen[0] = False
try:
if (tile := tgen[1](None, None, row, col)['tile']) is None:
if (tile := gen(None, None, row, col)['tile']) is None:
self.log(1, 'error', row, col)
else:
self.log(2, 'load', row, col)
Expand All @@ -1523,10 +1526,13 @@ def _retrieveitem():
finally:
with self.GCondition:
if tgen[0]:
tgen[1](close_connection=True)
gen(close_connection=True)
else:
tgen[0] = True
a.append(i)
if gen.pconnection[0] is None:
a.insert(0, gen.ind)
else:
a.append(gen.ind)
self.GCondition.notify()
ptile[0] = tile
e.set()
Expand Down Expand Up @@ -1593,10 +1599,10 @@ def Configure(self, rid, tile_generator_builder):
self.Seq += 1
pconnections = [[None] for i in range(self.Threads)]
with self.GCondition:
for ind, g in enumerate(self.Generators):
for g in self.Generators:
try:
if (self.Id or (None, None))[0] == rid[0] and g[0]:
pconnections[ind] = g[1](close_connection=None)
pconnections[g[1].ind] = g[1].pconnection
elif g[0]:
g[1](close_connection=True)
else:
Expand Down Expand Up @@ -2479,7 +2485,7 @@ def TileGenerator(self, infos_base, matrix, local_pattern=None, local_expiration
if not infos_set:
if only_local:
return None
if not self.GetTileInfos(infos_completed, matrix, None, None, key, referer, user_agent, basic_auth, pconnections[0]):
if not self.GetTileInfos(infos_completed, matrix, None, None, key, referer, user_agent, basic_auth, next((pconnection for pconnection in pconnections if pconnection[0] is not None), pconnections[0])):
return None
if local_store:
if not self.SaveTile(local_pattern, infos_completed):
Expand Down Expand Up @@ -2537,10 +2543,13 @@ def gen_tiles():
return gen_tiles()
except:
return None
if number == 1:
return retrieve_tiles
else:
return [partial(retrieve_tiles, ind=i) for i in range(number)]
def bind_retrieve_tiles(ind):
def bound_retrieve_tiles(a=None, b=None, c=None, d=None, just_box=False, close_connection=False):
return retrieve_tiles(a, b, c, d, just_box, close_connection, ind)
bound_retrieve_tiles.ind = ind
bound_retrieve_tiles.pconnection = pconnections[ind]
return bound_retrieve_tiles
return bind_retrieve_tiles(0) if number == 1 else [bind_retrieve_tiles(i) for i in range(number)]

def RetrieveTiles(self, infos, matrix, minlat, maxlat, minlon, maxlon, local_pattern=None, local_expiration=None, local_store=False, memory_store=None, key=None, referer=None, user_agent='GPXTweaker', basic_auth=None, only_local=False, threads=10):
if not local_store and memory_store is None:
Expand Down

0 comments on commit 123f6c9

Please sign in to comment.