stock-analysis-plugin 0.1.13 → 0.1.14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stock-analysis-plugin",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "Pi Agent extension for stock analysis, screening, and strategy backtesting across A/HK/US markets",
5
5
  "type": "module",
6
6
  "main": "./pi/index.ts",
package/tools/_subproc.py CHANGED
@@ -14,20 +14,33 @@ import sys
14
14
  TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
15
15
 
16
16
 
17
- def run_tool(script: str, args: list, timeout: int = 30):
17
+ def find_python() -> str:
18
+ """Prefer the repo .venv interpreter; fall back to the current one.
19
+
20
+ Windows venvs ship .venv/Scripts/python.exe (there is no python3);
21
+ POSIX venvs ship .venv/bin/python3 — same rule as hermes' _find_python.
22
+ """
23
+ parts = ("Scripts", "python.exe") if sys.platform == "win32" else ("bin", "python3")
24
+ venv = os.path.join(os.path.dirname(TOOLS_DIR), ".venv", *parts)
25
+ return venv if os.path.exists(venv) else sys.executable
26
+
27
+
28
+ def run_tool(script: str, args: list, timeout: int = 30, parse_json: bool = True):
18
29
  """Run tools/<script> with an argv list (never a shell). Returns parsed JSON
19
- stdout, or None on any failure (non-zero exit, empty/invalid output, timeout) —
20
- callers pick their own sentinel with `or {}` / `or []`."""
30
+ stdout (raw stripped stdout when parse_json=False), or None on any failure
31
+ (non-zero exit, empty/invalid output, timeout) — callers pick their own
32
+ sentinel with `or {}` / `or []`. Interpreter: find_python() (venv-first,
33
+ so child processes get the .venv deps)."""
21
34
  try:
22
35
  r = subprocess.run(
23
- [sys.executable, os.path.join(TOOLS_DIR, script), *args],
36
+ [find_python(), os.path.join(TOOLS_DIR, script), *args],
24
37
  capture_output=True,
25
38
  text=True,
26
39
  encoding="utf-8",
27
40
  timeout=timeout,
28
41
  )
29
42
  if r.returncode == 0 and r.stdout.strip():
30
- return json.loads(r.stdout)
43
+ return json.loads(r.stdout) if parse_json else r.stdout.strip()
31
44
  except Exception:
32
45
  pass
33
46
  return None
@@ -6,33 +6,14 @@ Does NOT store history, does NOT push, does NOT schedule. Host agent decides whe
6
6
 
7
7
  import argparse
8
8
  import json
9
- import subprocess
10
9
  import sys
11
10
  from datetime import datetime, timezone
12
11
  from pathlib import Path
13
12
 
14
13
  sys.path.insert(0, str(Path(__file__).resolve().parent))
14
+ from _subproc import run_tool as _run_json # noqa: E402
15
15
  from stock_data import detect_market # noqa: E402
16
16
 
17
- TOOLS_DIR = Path(__file__).resolve().parent
18
-
19
-
20
- def _find_python() -> str:
21
- venv = TOOLS_DIR.parent / ".venv" / "bin" / "python3"
22
- return str(venv) if venv.exists() else sys.executable
23
-
24
-
25
- def _run_json(script: str, args: list[str], timeout: int = 30):
26
- cmd = [_find_python(), str(TOOLS_DIR / script)] + args
27
- try:
28
- r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", timeout=timeout)
29
- if r.returncode == 0 and r.stdout.strip():
30
- return json.loads(r.stdout)
31
- except Exception:
32
- pass
33
- return None
34
-
35
-
36
17
  RULE_TYPES = {
37
18
  "price_below",
38
19
  "price_above",
package/tools/gather.py CHANGED
@@ -3,33 +3,19 @@
3
3
 
4
4
  import argparse
5
5
  import json
6
- import subprocess
7
6
  import sys
8
7
  from concurrent.futures import ThreadPoolExecutor, as_completed
9
8
  from pathlib import Path
10
9
 
11
- TOOLS_DIR = Path(__file__).resolve().parent
10
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
11
+ from _subproc import run_tool # noqa: E402
12
12
 
13
13
 
14
- def _find_python() -> str:
15
- venv = TOOLS_DIR.parent / ".venv" / "bin" / "python3"
16
- if venv.exists():
17
- return str(venv)
18
- return sys.executable
19
-
20
-
21
- def _run(script: str, args: list[str], timeout: int = 60) -> str | None:
22
- python = _find_python()
23
- cmd = [python, str(TOOLS_DIR / script)] + args
24
- try:
25
- r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", timeout=timeout)
26
- if r.returncode == 0 and r.stdout.strip():
27
- return r.stdout.strip()
28
- except subprocess.TimeoutExpired:
29
- pass
30
- except Exception:
31
- pass
32
- return None
14
+ # gather fans out the heavier CLIs, so the default timeout stays 60s; JSON parsing
15
+ # is _parse_json's job, so raw stdout is wanted here. A def wrapper (not
16
+ # functools.partial) keeps timeout passable both positionally and by keyword.
17
+ def _run(script, args, timeout=60):
18
+ return run_tool(script, args, timeout=timeout, parse_json=False)
33
19
 
34
20
 
35
21
  def _parse_json(raw: str | None):
@@ -6,32 +6,14 @@ Not a portfolio system. Stateless: user passes cost/quantity/stops each call.
6
6
 
7
7
  import argparse
8
8
  import json
9
- import subprocess
10
9
  import sys
11
10
  from datetime import datetime, timezone
12
11
  from pathlib import Path
13
12
 
14
13
  sys.path.insert(0, str(Path(__file__).resolve().parent))
14
+ from _subproc import run_tool as _run_json # noqa: E402
15
15
  from stock_data import detect_market # noqa: E402
16
16
 
17
- TOOLS_DIR = Path(__file__).resolve().parent
18
-
19
-
20
- def _find_python() -> str:
21
- venv = TOOLS_DIR.parent / ".venv" / "bin" / "python3"
22
- return str(venv) if venv.exists() else sys.executable
23
-
24
-
25
- def _run_json(script: str, args: list[str], timeout: int = 30):
26
- cmd = [_find_python(), str(TOOLS_DIR / script)] + args
27
- try:
28
- r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", timeout=timeout)
29
- if r.returncode == 0 and r.stdout.strip():
30
- return json.loads(r.stdout)
31
- except Exception:
32
- pass
33
- return None
34
-
35
17
 
36
18
  def _risk_level(distance_to_stop_pct: float, pnl_pct: float, trend: str) -> str:
37
19
  # ponytail: simple 3-bucket heuristic, upgrade to volatility-adjusted if needed
@@ -7,34 +7,16 @@ so the host agent can decide what to do next (write daily briefing, dig deeper,
7
7
 
8
8
  import argparse
9
9
  import json
10
- import subprocess
11
10
  import sys
12
11
  from concurrent.futures import ThreadPoolExecutor, as_completed
13
12
  from datetime import datetime, timezone
14
13
  from pathlib import Path
15
14
 
16
15
  sys.path.insert(0, str(Path(__file__).resolve().parent))
16
+ from _subproc import run_tool as _run_json # noqa: E402
17
17
  from stock_data import detect_market # noqa: E402
18
18
  from watchlist import analyze_watchlist # noqa: E402
19
19
 
20
- TOOLS_DIR = Path(__file__).resolve().parent
21
-
22
-
23
- def _find_python() -> str:
24
- venv = TOOLS_DIR.parent / ".venv" / "bin" / "python3"
25
- return str(venv) if venv.exists() else sys.executable
26
-
27
-
28
- def _run_json(script: str, args: list[str], timeout: int = 30):
29
- cmd = [_find_python(), str(TOOLS_DIR / script)] + args
30
- try:
31
- r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", timeout=timeout)
32
- if r.returncode == 0 and r.stdout.strip():
33
- return json.loads(r.stdout)
34
- except Exception:
35
- pass
36
- return None
37
-
38
20
 
39
21
  def _trend_label(overall: str) -> str:
40
22
  return {