citeget 0.2.22__tar.gz → 0.2.23__tar.gz

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.
Files changed (70) hide show
  1. {citeget-0.2.22 → citeget-0.2.23}/PKG-INFO +1 -1
  2. {citeget-0.2.22 → citeget-0.2.23}/citeget/netguard.py +54 -13
  3. {citeget-0.2.22 → citeget-0.2.23}/pyproject.toml +1 -1
  4. citeget-0.2.23/tests/test_browser_session.py +116 -0
  5. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/acquire-references/SKILL.md +0 -0
  6. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/add-references/SKILL.md +0 -0
  7. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/add-references/add-references.skill +0 -0
  8. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/add-references/references/TEMPLATE.md +0 -0
  9. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/add-references/references/ai-ml.md +0 -0
  10. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/add-references/references/math-combinatorics.md +0 -0
  11. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/add-references/references/parameters.md +0 -0
  12. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/add-references/references/social-sciences.md +0 -0
  13. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/add-references/references/tech-blog.md +0 -0
  14. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/check-submission-fit/SKILL.md +0 -0
  15. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/fetch-resources/SKILL.md +0 -0
  16. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/format-for-journal/SKILL.md +0 -0
  17. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/get-books/SKILL.md +0 -0
  18. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/prepare-submission/SKILL.md +0 -0
  19. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/research-topic/SKILL.md +0 -0
  20. {citeget-0.2.22 → citeget-0.2.23}/.claude/skills/review-article/SKILL.md +0 -0
  21. {citeget-0.2.22 → citeget-0.2.23}/.editorconfig +0 -0
  22. {citeget-0.2.22 → citeget-0.2.23}/.gitattributes +0 -0
  23. {citeget-0.2.22 → citeget-0.2.23}/.github/workflows/ci.yml +0 -0
  24. {citeget-0.2.22 → citeget-0.2.23}/.gitignore +0 -0
  25. {citeget-0.2.22 → citeget-0.2.23}/CHANGELOG.md +0 -0
  26. {citeget-0.2.22 → citeget-0.2.23}/CLAUDE.md +0 -0
  27. {citeget-0.2.22 → citeget-0.2.23}/LICENSE +0 -0
  28. {citeget-0.2.22 → citeget-0.2.23}/README.md +0 -0
  29. {citeget-0.2.22 → citeget-0.2.23}/citeget/__init__.py +0 -0
  30. {citeget-0.2.22 → citeget-0.2.23}/citeget/__main__.py +0 -0
  31. {citeget-0.2.22 → citeget-0.2.23}/citeget/acquire_references.py +0 -0
  32. {citeget-0.2.22 → citeget-0.2.23}/citeget/article_pub/__init__.py +0 -0
  33. {citeget-0.2.22 → citeget-0.2.23}/citeget/article_pub/data/journal_profiles.json +0 -0
  34. {citeget-0.2.22 → citeget-0.2.23}/citeget/article_pub/scripts/check_article.py +0 -0
  35. {citeget-0.2.22 → citeget-0.2.23}/citeget/article_pub/scripts/extract_references.py +0 -0
  36. {citeget-0.2.22 → citeget-0.2.23}/citeget/article_pub/scripts/word_count.py +0 -0
  37. {citeget-0.2.22 → citeget-0.2.23}/citeget/cli.py +0 -0
  38. {citeget-0.2.22 → citeget-0.2.23}/citeget/content.py +0 -0
  39. {citeget-0.2.22 → citeget-0.2.23}/citeget/core.py +0 -0
  40. {citeget-0.2.22 → citeget-0.2.23}/citeget/data/libgen_vg_report.md +0 -0
  41. {citeget-0.2.22 → citeget-0.2.23}/citeget/extract.py +0 -0
  42. {citeget-0.2.22 → citeget-0.2.23}/citeget/fetch.py +0 -0
  43. {citeget-0.2.22 → citeget-0.2.23}/citeget/joblimit.py +0 -0
  44. {citeget-0.2.22 → citeget-0.2.23}/citeget/names.py +0 -0
  45. {citeget-0.2.22 → citeget-0.2.23}/citeget/rank.py +0 -0
  46. {citeget-0.2.22 → citeget-0.2.23}/citeget/resolve.py +0 -0
  47. {citeget-0.2.22 → citeget-0.2.23}/citeget/validate.py +0 -0
  48. {citeget-0.2.22 → citeget-0.2.23}/cookies.txt +0 -0
  49. {citeget-0.2.22 → citeget-0.2.23}/tests/__init__.py +0 -0
  50. {citeget-0.2.22 → citeget-0.2.23}/tests/cli_goldens/citeget.json +0 -0
  51. {citeget-0.2.22 → citeget-0.2.23}/tests/cli_goldens/citeget_module.json +0 -0
  52. {citeget-0.2.22 → citeget-0.2.23}/tests/conftest.py +0 -0
  53. {citeget-0.2.22 → citeget-0.2.23}/tests/test_browserguard.py +0 -0
  54. {citeget-0.2.22 → citeget-0.2.23}/tests/test_cli_surface.py +0 -0
  55. {citeget-0.2.22 → citeget-0.2.23}/tests/test_core_mirrors.py +0 -0
  56. {citeget-0.2.22 → citeget-0.2.23}/tests/test_download_paths.py +0 -0
  57. {citeget-0.2.22 → citeget-0.2.23}/tests/test_extract.py +0 -0
  58. {citeget-0.2.22 → citeget-0.2.23}/tests/test_fetch_query_collision.py +0 -0
  59. {citeget-0.2.22 → citeget-0.2.23}/tests/test_fetch_retry_titles.py +0 -0
  60. {citeget-0.2.22 → citeget-0.2.23}/tests/test_finalize.py +0 -0
  61. {citeget-0.2.22 → citeget-0.2.23}/tests/test_html_to_markdown.py +0 -0
  62. {citeget-0.2.22 → citeget-0.2.23}/tests/test_job_budget.py +0 -0
  63. {citeget-0.2.22 → citeget-0.2.23}/tests/test_joblimit.py +0 -0
  64. {citeget-0.2.22 → citeget-0.2.23}/tests/test_main_content.py +0 -0
  65. {citeget-0.2.22 → citeget-0.2.23}/tests/test_names.py +0 -0
  66. {citeget-0.2.22 → citeget-0.2.23}/tests/test_netguard.py +0 -0
  67. {citeget-0.2.22 → citeget-0.2.23}/tests/test_rank.py +0 -0
  68. {citeget-0.2.22 → citeget-0.2.23}/tests/test_resolve.py +0 -0
  69. {citeget-0.2.22 → citeget-0.2.23}/tests/test_search_retries.py +0 -0
  70. {citeget-0.2.22 → citeget-0.2.23}/tests/test_validate.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: citeget
3
- Version: 0.2.22
3
+ Version: 0.2.23
4
4
  Summary: Find, acquire, and manage academic references — for AI agents and humans.
5
5
  Project-URL: Homepage, https://github.com/thorwhalen/citeget
6
6
  Project-URL: Repository, https://github.com/thorwhalen/citeget
@@ -594,9 +594,21 @@ def _deadline(policy: NetPolicy) -> Optional[float]:
594
594
  return time.monotonic() + seconds if seconds else None
595
595
 
596
596
 
597
- def request(method: str, url: str, *, policy: Optional[NetPolicy] = None, **kwargs):
597
+ def request(
598
+ method: str,
599
+ url: str,
600
+ *,
601
+ policy: Optional[NetPolicy] = None,
602
+ session=None,
603
+ **kwargs,
604
+ ):
598
605
  """``requests.request`` with the destination guard. Same arguments and return.
599
606
 
607
+ ``session`` is a :func:`guarded_session` (under the same *policy*) to reuse, so
608
+ successive requests share connections; the caller closes it. The session's cookie
609
+ jar is emptied before and after each call: a redirect chain still carries cookies
610
+ from hop to hop, and nothing is kept between calls. Not for concurrent use.
611
+
600
612
  The body is read before returning (or, with ``stream=True``, as the caller
601
613
  iterates), up to ``policy.max_response_bytes``, and the whole exchange,
602
614
  redirects included, must finish within ``policy.max_response_seconds`` of
@@ -615,14 +627,24 @@ def request(method: str, url: str, *, policy: Optional[NetPolicy] = None, **kwar
615
627
  stream = kwargs.pop("stream", False)
616
628
  kwargs.setdefault("timeout", DEFAULT_TIMEOUT)
617
629
  deadline = _deadline(policy)
618
- with guarded_session(policy=policy) as session:
619
- session._citeget_deadline = deadline
620
- resp = session.request(method, url, stream=True, **kwargs)
621
- # stream=True: the caller reads, through a capped iterator, at constant
622
- # memory. Otherwise the body is read here, capped, like requests does.
623
- if stream:
624
- return _guard_response(resp, policy, deadline=deadline)
625
- return _read_capped(resp, policy, deadline=deadline)
630
+ if session is None:
631
+ with guarded_session(policy=policy) as own:
632
+ return _send(own, method, url, policy, deadline, stream, kwargs)
633
+ session.cookies.clear()
634
+ try:
635
+ return _send(session, method, url, policy, deadline, stream, kwargs)
636
+ finally:
637
+ session.cookies.clear()
638
+
639
+
640
+ def _send(session, method, url, policy, deadline, stream, kwargs):
641
+ session._citeget_deadline = deadline
642
+ resp = session.request(method, url, stream=True, **kwargs)
643
+ # stream=True: the caller reads, through a capped iterator, at constant
644
+ # memory. Otherwise the body is read here, capped, like requests does.
645
+ if stream:
646
+ return _guard_response(resp, policy, deadline=deadline)
647
+ return _read_capped(resp, policy, deadline=deadline)
626
648
 
627
649
 
628
650
  def get(url: str, *, policy: Optional[NetPolicy] = None, **kwargs):
@@ -898,7 +920,12 @@ def browser_route_handler(
898
920
  :class:`BrowserDownloads`); a new one if not given. Exposed on the
899
921
  handler as ``handler.downloads``.
900
922
  fetch: The fetcher, ``(method, url, **requests_kwargs) -> Response``;
901
- defaults to :func:`request` with *policy*. A seam for tests.
923
+ defaults to :func:`request` with *policy* over one guarded session
924
+ for the whole handler, so a page's subresources reuse connections
925
+ (the browser owns cookies; the session keeps none between requests).
926
+ A seam for tests. Playwright calls the handler from one thread.
927
+
928
+ The handler has ``.downloads`` and ``.close()``, which closes the session.
902
929
  """
903
930
  import logging
904
931
 
@@ -906,9 +933,15 @@ def browser_route_handler(
906
933
  # Resolved now, not per route: Playwright dispatches the handler from its
907
934
  # own loop, which need not see this context's response_ceiling.
908
935
  policy = capped(policy or default_policy())
909
- fetch = fetch or (
910
- lambda method, url, **kw: request(method, url, policy=policy, **kw)
911
- )
936
+ session = None # made on the first request, closed by handler.close()
937
+
938
+ def shared_fetch(method, url, **kw):
939
+ nonlocal session
940
+ if session is None:
941
+ session = guarded_session(policy=policy)
942
+ return request(method, url, policy=policy, session=session, **kw)
943
+
944
+ fetch = fetch or shared_fetch
912
945
  downloads = downloads if downloads is not None else BrowserDownloads()
913
946
 
914
947
  def handler(route):
@@ -961,7 +994,14 @@ def browser_route_handler(
961
994
  status=resp.status_code, headers=fulfill_headers, body=body
962
995
  )
963
996
 
997
+ def close():
998
+ nonlocal session
999
+ if session is not None:
1000
+ session.close()
1001
+ session = None
1002
+
964
1003
  handler.downloads = downloads
1004
+ handler.close = close
965
1005
  return handler
966
1006
 
967
1007
 
@@ -1003,6 +1043,7 @@ def guard_browser_context(
1003
1043
  stash = _CONTEXT_DOWNLOADS.pop(id(context), None)
1004
1044
  if stash is not None:
1005
1045
  stash.discard_all()
1046
+ handler.close()
1006
1047
 
1007
1048
  context.route("**/*", handler)
1008
1049
  if hasattr(context, "route_web_socket"): # Playwright >= 1.48
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
6
6
 
7
7
  [project]
8
8
  name = "citeget"
9
- version = "0.2.22"
9
+ version = "0.2.23"
10
10
  description = "Find, acquire, and manage academic references — for AI agents and humans."
11
11
  readme = "README.md"
12
12
  requires-python = ">=3.10"
@@ -0,0 +1,116 @@
1
+ """The browser route handler reuses one guarded session (citeget#23): connections are
2
+ shared across a page's requests, cookies carry across a redirect chain and are kept nowhere."""
3
+
4
+ import http.server
5
+ import threading
6
+
7
+ import pytest
8
+
9
+ from citeget import netguard
10
+
11
+ LOCAL = netguard.NetPolicy(allow_private=True)
12
+
13
+
14
+ class _Handler(http.server.BaseHTTPRequestHandler):
15
+ protocol_version = "HTTP/1.1" # keep-alive
16
+
17
+ def log_message(self, *a):
18
+ pass
19
+
20
+ def do_GET(self):
21
+ server = self.server
22
+ server.seen_cookies.append(self.headers.get("Cookie"))
23
+ if self.path == "/start":
24
+ self.send_response(302)
25
+ self.send_header("Location", "/next")
26
+ self.send_header("Set-Cookie", "hop=1")
27
+ self.send_header("Content-Length", "0")
28
+ self.end_headers()
29
+ return
30
+ body = b"ok"
31
+ self.send_response(200)
32
+ self.send_header("Content-Type", "text/plain")
33
+ self.send_header("Content-Length", str(len(body)))
34
+ self.end_headers()
35
+ self.wfile.write(body)
36
+
37
+
38
+ class _Server(http.server.ThreadingHTTPServer):
39
+ daemon_threads = True
40
+
41
+ def __init__(self):
42
+ super().__init__(("127.0.0.1", 0), _Handler)
43
+ self.connections = 0
44
+ self.seen_cookies = []
45
+
46
+ def get_request(self):
47
+ self.connections += 1
48
+ return super().get_request()
49
+
50
+
51
+ @pytest.fixture
52
+ def server():
53
+ srv = _Server()
54
+ threading.Thread(target=srv.serve_forever, daemon=True).start()
55
+ yield srv
56
+ srv.shutdown()
57
+ srv.server_close()
58
+
59
+
60
+ class _Req:
61
+ resource_type = "script"
62
+ method = "GET"
63
+ post_data_buffer = None
64
+
65
+ def __init__(self, url):
66
+ self.url = url
67
+
68
+ def all_headers(self):
69
+ return {"user-agent": "t"}
70
+
71
+
72
+ class _Route:
73
+ def __init__(self, url):
74
+ self.request = _Req(url)
75
+ self.outcome = None
76
+
77
+ def abort(self, reason="failed"):
78
+ self.outcome = ("abort", reason)
79
+
80
+ def fulfill(self, *, status, headers, body):
81
+ self.outcome = ("fulfill", status, body)
82
+
83
+
84
+ def test_a_pages_requests_share_one_connection(server):
85
+ handler = netguard.browser_route_handler(policy=LOCAL)
86
+ base = f"http://127.0.0.1:{server.server_port}"
87
+ for i in range(6):
88
+ route = _Route(f"{base}/a{i}")
89
+ handler(route)
90
+ assert route.outcome == ("fulfill", 200, b"ok")
91
+ handler.close()
92
+ assert server.connections == 1
93
+
94
+
95
+ def test_a_redirect_chain_carries_its_cookie_but_nothing_is_kept_between_requests(server):
96
+ handler = netguard.browser_route_handler(policy=LOCAL)
97
+ base = f"http://127.0.0.1:{server.server_port}"
98
+ handler(_Route(f"{base}/start"))
99
+ handler(_Route(f"{base}/later"))
100
+ handler.close()
101
+ # /start (none), /next (hop=1 carried across the chain), /later (nothing stored)
102
+ assert server.seen_cookies == [None, "hop=1", None]
103
+
104
+
105
+ def test_closing_the_context_closes_the_session(server):
106
+ class Context:
107
+ def route(self, pattern, handler):
108
+ self.handler = handler
109
+
110
+ def on(self, event, callback):
111
+ self.on_close = callback
112
+
113
+ context = netguard.guard_browser_context(Context(), policy=LOCAL)
114
+ context.handler(_Route(f"http://127.0.0.1:{server.server_port}/x"))
115
+ context.on_close()
116
+ assert server.connections == 1
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes