careful 0.3.1__py3-none-any.whl → 0.3.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
careful/httpx/__init__.py CHANGED
@@ -1,4 +1,6 @@
1
1
  import os
2
+ import logging
3
+ import pathlib
2
4
  from urllib.parse import urlparse
3
5
  from .retries import make_retry_client, retry_default_rule
4
6
  from .throttle import make_throttled_client
@@ -21,6 +23,8 @@ from .dev_cache import (
21
23
  from ._types import ResponsePredicate, CacheKeyfunc
22
24
  from httpx import Client
23
25
 
26
+ log = logging.getLogger("careful")
27
+
24
28
 
25
29
  def make_careful_client(
26
30
  *,
@@ -120,12 +124,25 @@ def _cache_env(var_name: str, default: CacheStorage | None) -> CacheStorage | No
120
124
  if not cache_str:
121
125
  return default
122
126
  parsed = urlparse(cache_str)
127
+ # urlparse always starts with a / (var needs :/// to skip netloc -> into path)
128
+ true_path = parsed.path[1:] if parsed.path.startswith("/") else parsed.path
129
+ # if it starts with a //// then it is an absolute path
130
+ if true_path.startswith("/"):
131
+ path = pathlib.Path(true_path)
132
+ else:
133
+ path = pathlib.Path.cwd() / true_path
123
134
  if parsed.scheme == "memory":
135
+ log.info("cache from env %s => MemoryCache", var_name)
124
136
  return MemoryCache()
125
137
  elif parsed.scheme == "file":
126
- return FileCache(parsed.path)
138
+ log.info("cache from env %s => FileCache(%s)", var_name, path)
139
+ return FileCache(path)
127
140
  elif parsed.scheme == "sqlite":
128
- return SqliteCache(parsed.path)
141
+ log.info("cache from env %s => SqliteCache(%s)", var_name, path)
142
+ return SqliteCache(path)
143
+ else:
144
+ log.warning("invalid cache %s=%s", var_name, cache_str)
145
+ return default
129
146
 
130
147
 
131
148
  def make_careful_client_from_env(
@@ -1,5 +1,6 @@
1
1
  import types
2
2
  import functools
3
+ import pathlib
3
4
  import logging
4
5
  import re
5
6
  import os
@@ -238,6 +239,7 @@ class FileCache(CacheStorage):
238
239
  content=resp_content,
239
240
  default_encoding=resp_headers.pop("encoding"),
240
241
  headers=resp_headers,
242
+ request=Request("GET", key), # not perfect, but it'll do
241
243
  )
242
244
  return resp
243
245
  except IOError:
@@ -254,6 +256,8 @@ class FileCache(CacheStorage):
254
256
  encoding_str = "encoding: {0}\n".format(response.encoding)
255
257
  f.write(encoding_str.encode("utf8"))
256
258
  for h, v in response.headers.items():
259
+ if h.lower() in ("content-encoding", "content-length"):
260
+ continue
257
261
  # header: value\n
258
262
  f.write(h.encode("utf8"))
259
263
  f.write(b": ")
@@ -286,12 +290,17 @@ class SqliteCache(CacheStorage):
286
290
 
287
291
  _columns = ["key", "status", "modified", "encoding", "data", "headers"]
288
292
 
289
- def __init__(self, cache_path: str, check_last_modified: bool = False):
293
+ def __init__(
294
+ self, cache_path: str | pathlib.Path, check_last_modified: bool = False
295
+ ):
290
296
  self.cache_path = cache_path
291
297
  self.check_last_modified = check_last_modified
292
- self._conn = sqlite3.connect(cache_path)
298
+ self._conn = sqlite3.connect(str(cache_path))
293
299
  self._conn.text_factory = str
294
300
  self._build_table()
301
+ # self._conn.execute("PRAGMA journal_mode=WAL;")
302
+ # self._conn.execute("PRAGMA synchronous=1;")
303
+ # self._conn.isolation_level = None
295
304
 
296
305
  def _build_table(self) -> None:
297
306
  """Create table for storing request information and response."""
@@ -304,6 +313,8 @@ class SqliteCache(CacheStorage):
304
313
  def set(self, key: str, response: Response) -> None:
305
314
  """Set cache entry for key with contents of response."""
306
315
  mod = response.headers.pop("last-modified", None)
316
+ response.headers.pop("content-encoding", None)
317
+ response.headers.pop("content-length", None)
307
318
  status = int(response.status_code)
308
319
  rec = (
309
320
  key,
@@ -340,6 +351,7 @@ class SqliteCache(CacheStorage):
340
351
  content=rec["data"],
341
352
  default_encoding=rec["encoding"],
342
353
  headers=json.loads(rec["headers"]),
354
+ request=Request("GET", key), # not perfect, but it'll do
343
355
  )
344
356
  return resp
345
357
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: careful
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: a small library for writing resilient, well-behaved HTTP code
5
5
  Project-URL: Repository, https://codeberg.org/jpt/careful
6
6
  Author-email: jpt <dev@jpt.sh>
@@ -1,12 +1,12 @@
1
1
  careful/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- careful/httpx/__init__.py,sha256=sIKMgGf-Ea5VEsEFqxXFbAiaF1tYOYm9CIOvSCPooqw,6674
2
+ careful/httpx/__init__.py,sha256=Ul_K5XSMMW8yo3M-lq8nDbvtDEQS0N-vmurIGnzE8dY,7381
3
3
  careful/httpx/_types.py,sha256=jefYDxSbLRUatU8QKeyxStc9UC3AJwAba2SfhNkM0RY,151
4
- careful/httpx/dev_cache.py,sha256=sfHX2TkKZQRTrOXSGSMgIWE0HbX3Fvuz88o8_jK9P8g,11316
4
+ careful/httpx/dev_cache.py,sha256=cc4_rLKFc6Xggpx5MKc8DBurn5KXTDLHO19U_SipXiY,11873
5
5
  careful/httpx/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  careful/httpx/retries.py,sha256=3kjuHKYnK1N4Rtum5gUyY_XO4o4cL4jc59d17Y6UwrI,2949
7
7
  careful/httpx/robots.py,sha256=jfqQdplTap_RCENu6MHEIabFVznFLruMvSIaG_u0v_8,2168
8
8
  careful/httpx/throttle.py,sha256=b1fbmUskcm343D1bbPbY-ITLdL1zVm1dXtjt9LT1bEA,1412
9
- careful-0.3.1.dist-info/METADATA,sha256=fkI2V9YX5JKhPTYZga5q_3cyfatB4B_PMdTci3sRMmc,2692
10
- careful-0.3.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
11
- careful-0.3.1.dist-info/licenses/LICENSE,sha256=oHe4LmCuo6CZne42DRXfiR3uqqIfGsk4dAvDKucAi4M,1315
12
- careful-0.3.1.dist-info/RECORD,,
9
+ careful-0.3.2.dist-info/METADATA,sha256=fN6sNY5n4PFna9Eq9uTfxDbvzCpct-qXcLOkzrdItGE,2692
10
+ careful-0.3.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
11
+ careful-0.3.2.dist-info/licenses/LICENSE,sha256=oHe4LmCuo6CZne42DRXfiR3uqqIfGsk4dAvDKucAi4M,1315
12
+ careful-0.3.2.dist-info/RECORD,,