projectnetzero 0.1.0__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.
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: projectnetzero
3
+ Version: 0.1.0
4
+ Summary: Optimize your Python code for lower CO2 emissions
5
+ Requires-Python: >=3.10
6
+ Requires-Dist: click>=8.0
7
+ Requires-Dist: httpx>=0.27
@@ -0,0 +1,19 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68.0", "setuptools-scm"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "projectnetzero"
7
+ version = "0.1.0"
8
+ description = "Optimize your Python code for lower CO2 emissions"
9
+ requires-python = ">=3.10"
10
+ dependencies = [
11
+ "click>=8.0",
12
+ "httpx>=0.27",
13
+ ]
14
+
15
+ [project.scripts]
16
+ projectnetzero = "projectnetzero.cli:main"
17
+
18
+ [tool.setuptools.packages.find]
19
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: ecofy
3
+ Version: 0.1.0
4
+ Summary: Optimize your Python code for lower CO2 emissions
5
+ Requires-Python: >=3.10
6
+ Requires-Dist: click>=8.0
7
+ Requires-Dist: httpx>=0.27
@@ -0,0 +1,15 @@
1
+ pyproject.toml
2
+ src/ecofy/__init__.py
3
+ src/ecofy/api.py
4
+ src/ecofy/cli.py
5
+ src/ecofy/runner.py
6
+ src/ecofy.egg-info/PKG-INFO
7
+ src/ecofy.egg-info/SOURCES.txt
8
+ src/ecofy.egg-info/dependency_links.txt
9
+ src/ecofy.egg-info/entry_points.txt
10
+ src/ecofy.egg-info/requires.txt
11
+ src/ecofy.egg-info/top_level.txt
12
+ src/ecofy/__pycache__/__init__.cpython-314.pyc
13
+ src/ecofy/__pycache__/api.cpython-314.pyc
14
+ src/ecofy/__pycache__/cli.cpython-314.pyc
15
+ src/ecofy/__pycache__/runner.cpython-314.pyc
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ ecofy = ecofy.cli:main
@@ -0,0 +1,2 @@
1
+ click>=8.0
2
+ httpx>=0.27
@@ -0,0 +1,3 @@
1
+ """Project Net Zero - Optimize your Python code for lower CO2 emissions."""
2
+
3
+ __version__ = "0.1.0"
@@ -0,0 +1,41 @@
1
+ """Client for the Project Net Zero optimization backend API."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import httpx
6
+
7
+ DEFAULT_BASE_URL = "http://localhost:8000"
8
+
9
+
10
+ class ProjectNetZeroAPIError(Exception):
11
+ """Raised when the optimization API returns an error."""
12
+
13
+
14
+ def optimize(source_code: str, *, base_url: str = DEFAULT_BASE_URL) -> str:
15
+ """Send source code to the backend and return the optimized version.
16
+
17
+ Args:
18
+ source_code: The Python source code to optimize.
19
+ base_url: Base URL of the optimization API.
20
+
21
+ Returns:
22
+ The optimized Python source code.
23
+
24
+ Raises:
25
+ ProjectNetZeroAPIError: If the API returns a non-success status.
26
+ """
27
+ url = f"{base_url.rstrip('/')}/optimize"
28
+
29
+ response = httpx.post(
30
+ url,
31
+ json={"source_code": source_code},
32
+ timeout=120,
33
+ )
34
+
35
+ if response.status_code != 200:
36
+ raise ProjectNetZeroAPIError(
37
+ f"API returned status {response.status_code}: {response.text}"
38
+ )
39
+
40
+ data = response.json()
41
+ return data["optimized_code"]
@@ -0,0 +1,50 @@
1
+ """Project Net Zero CLI - optimize and run Python code with lower CO2 emissions."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ import click
9
+
10
+ from projectnetzero import __version__
11
+ from projectnetzero.api import DEFAULT_BASE_URL, ProjectNetZeroAPIError, optimize
12
+ from projectnetzero.runner import run_optimized
13
+
14
+
15
+ @click.group()
16
+ @click.version_option(version=__version__, prog_name="projectnetzero")
17
+ def main() -> None:
18
+ """Project Net Zero - Optimize your Python code for lower CO2 emissions."""
19
+
20
+
21
+ @main.command()
22
+ @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path))
23
+ @click.option(
24
+ "--api-url",
25
+ default=DEFAULT_BASE_URL,
26
+ envvar="PROJECTNETZERO_API_URL",
27
+ help="Base URL of the optimization API.",
28
+ show_default=True,
29
+ )
30
+ def run(file: Path, api_url: str) -> None:
31
+ """Optimize a Python file and run the optimized version.
32
+
33
+ The original FILE is never modified.
34
+ """
35
+ source_code = file.read_text(encoding="utf-8")
36
+
37
+ click.echo(f"Sending {file} to optimization API at {api_url} ...")
38
+
39
+ try:
40
+ optimized_code = optimize(source_code, base_url=api_url)
41
+ except ProjectNetZeroAPIError as exc:
42
+ click.echo(f"Error: {exc}", err=True)
43
+ sys.exit(1)
44
+ except Exception as exc:
45
+ click.echo(f"Could not reach API: {exc}", err=True)
46
+ sys.exit(1)
47
+
48
+ click.echo("Running optimized version ...\n")
49
+ exit_code = run_optimized(optimized_code, file)
50
+ sys.exit(exit_code)
@@ -0,0 +1,41 @@
1
+ """Run optimized code in a temporary file without modifying the original."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import subprocess
6
+ import sys
7
+ import tempfile
8
+ from pathlib import Path
9
+
10
+
11
+ def run_optimized(optimized_code: str, original_path: Path) -> int:
12
+ """Write optimized code to a temp file and execute it.
13
+
14
+ The original file is never modified. The temp file is placed in the same
15
+ directory so that relative imports and file paths still work.
16
+
17
+ Args:
18
+ optimized_code: The optimized Python source code.
19
+ original_path: Path to the original file (used to resolve the working directory).
20
+
21
+ Returns:
22
+ The exit code of the executed process.
23
+ """
24
+ work_dir = original_path.resolve().parent
25
+
26
+ with tempfile.NamedTemporaryFile(
27
+ mode="w",
28
+ suffix=".py",
29
+ dir=work_dir,
30
+ prefix=".projectnetzero_",
31
+ delete=True,
32
+ ) as tmp:
33
+ tmp.write(optimized_code)
34
+ tmp.flush()
35
+
36
+ result = subprocess.run(
37
+ [sys.executable, tmp.name],
38
+ cwd=work_dir,
39
+ )
40
+
41
+ return result.returncode
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: projectnetzero
3
+ Version: 0.1.0
4
+ Summary: Optimize your Python code for lower CO2 emissions
5
+ Requires-Python: >=3.10
6
+ Requires-Dist: click>=8.0
7
+ Requires-Dist: httpx>=0.27
@@ -0,0 +1,17 @@
1
+ pyproject.toml
2
+ src/ecofy.egg-info/PKG-INFO
3
+ src/ecofy.egg-info/SOURCES.txt
4
+ src/ecofy.egg-info/dependency_links.txt
5
+ src/ecofy.egg-info/entry_points.txt
6
+ src/ecofy.egg-info/requires.txt
7
+ src/ecofy.egg-info/top_level.txt
8
+ src/projectnetzero/__init__.py
9
+ src/projectnetzero/api.py
10
+ src/projectnetzero/cli.py
11
+ src/projectnetzero/runner.py
12
+ src/projectnetzero.egg-info/PKG-INFO
13
+ src/projectnetzero.egg-info/SOURCES.txt
14
+ src/projectnetzero.egg-info/dependency_links.txt
15
+ src/projectnetzero.egg-info/entry_points.txt
16
+ src/projectnetzero.egg-info/requires.txt
17
+ src/projectnetzero.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ projectnetzero = projectnetzero.cli:main
@@ -0,0 +1,2 @@
1
+ click>=8.0
2
+ httpx>=0.27
@@ -0,0 +1 @@
1
+ projectnetzero