mirror of
https://github.com/AllSpiceIO/carbon-emission-calculator.git
synced 2025-03-29 17:06:32 +00:00
Baseline commit of feature complete add-on with CSV data source
This commit is contained in:
parent
f87a190723
commit
b012f58595
10
.github/dependabot.yml
vendored
Normal file
10
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: pip
|
||||
directory: /
|
||||
schedule:
|
||||
interval: monthly
|
||||
- package-ecosystem: github-actions
|
||||
directory: /
|
||||
schedule:
|
||||
interval: monthly
|
31
.github/workflows/check-formatting-and-lint.yml
vendored
Normal file
31
.github/workflows/check-formatting-and-lint.yml
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
name: Lint and test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: ["**"]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-22.04
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: ["3.10", "3.11", "3.12"]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
pip install -r requirements-test.txt
|
||||
- name: Check formatting
|
||||
run: ruff format --diff .
|
||||
- name: Lint with ruff
|
||||
run: ruff check --target-version=py310 .
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Git Ignore File
|
||||
|
||||
**.DS_Store
|
8
Dockerfile
Normal file
8
Dockerfile
Normal file
@ -0,0 +1,8 @@
|
||||
FROM python:3.12-bookworm
|
||||
|
||||
COPY entrypoint.py /entrypoint.py
|
||||
COPY requirements.txt /requirements.txt
|
||||
|
||||
RUN pip install -r /requirements.txt
|
||||
|
||||
ENTRYPOINT [ "/entrypoint.py" ]
|
19
README.md
Normal file
19
README.md
Normal file
@ -0,0 +1,19 @@
|
||||
# Carbon Emissions Calculator for PCBA
|
||||
An actions repository for demonstrating the calculation of the sum carbon emission for a PCBA given an input BOM and a data source with component emissions data
|
||||
|
||||
## Usage
|
||||
|
||||
Add the following step to your actions:
|
||||
|
||||
```yaml
|
||||
- name: Generate carbon emissions report for a PCBA given its BOM
|
||||
uses: https://hub.allspice.io/Actions/carbon-emission-calculator@v1
|
||||
with:
|
||||
bom_file: bom.csv
|
||||
```
|
||||
|
||||
## Input BOM
|
||||
|
||||
The input BOM to this Action is assumed to be generated from the py-allspice BOM generation utility. The column names referenced and used in this Action script assume the naming convention as populated by the py-allspice BOM generation function. The user is to adjust the expected column positions and naming conventions when using their own BOM file input.
|
||||
|
||||
A typical workflow is to use the [BOM generation Actions add-on](https://hub.allspice.io/Actions/generate-bom) to generate the BOM first, and use the generated BOM as an input to this Action.
|
14
action.yml
Normal file
14
action.yml
Normal file
@ -0,0 +1,14 @@
|
||||
name: "Calculate the carbon sum emission figure for the components in a PCBA given a data source with emissions data"
|
||||
description: >
|
||||
Calculate the carbon sum emission figure for the components in a PCBA given a data source with emissions data
|
||||
inputs:
|
||||
bom_file:
|
||||
description: "Path to the BOM CSV file"
|
||||
required: true
|
||||
runs:
|
||||
using: "docker"
|
||||
image: "Dockerfile"
|
||||
args:
|
||||
- ${{ inputs.bom_file }}
|
||||
env:
|
||||
ALLSPICE_AUTH_TOKEN: ${{ github.token }}
|
77
entrypoint.py
Executable file
77
entrypoint.py
Executable file
@ -0,0 +1,77 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
from argparse import ArgumentParser
|
||||
import requests
|
||||
import csv
|
||||
|
||||
ALLSPICE_DEMO_CARBON_EMISSION_DATA_URL = "https://hub.allspice.io/AllSpice-Demos/Demo-Data-Source/raw/branch/main/Carbon-Emissions-Figures-Archimajor/archimajor-carbon-emissions-figures.csv"
|
||||
|
||||
|
||||
################################################################################
|
||||
def get_carbon_emission_data_dict_from_source(url):
|
||||
# Post the request and get the response
|
||||
response = requests.get(url)
|
||||
# Get the text representation of the CSV data
|
||||
data_text = response.text
|
||||
# Ingest emissions data CSV into a dictionary
|
||||
emission_data = {}
|
||||
data_reader = csv.reader(data_text.splitlines(), delimiter=",", quotechar='"')
|
||||
for row in data_reader:
|
||||
emission_data[str(row[0])] = row[1]
|
||||
# Return the data
|
||||
return emission_data
|
||||
|
||||
|
||||
################################################################################
|
||||
def query_demo_carbon_emission_data_for_mfr_part_number(data, part_number):
|
||||
# Look up part number in dictionary, return emission figure if exists
|
||||
try:
|
||||
return data[part_number]
|
||||
# Return 0 as a default if part doesn't exist in data source
|
||||
except KeyError:
|
||||
return 0.0
|
||||
|
||||
|
||||
################################################################################
|
||||
if __name__ == "__main__":
|
||||
# Initialize argument parser
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument("bom_file", help="Path to the BOM file")
|
||||
args = parser.parse_args()
|
||||
|
||||
# Read the BOM file into list
|
||||
with open(args.bom_file, newline="") as bomfile:
|
||||
# Comma delimited file with " as quote character to be included
|
||||
bomreader = csv.reader(bomfile, delimiter=",", quotechar='"')
|
||||
# Save as a list
|
||||
bom_line_items = list(bomreader)
|
||||
# Skip the header
|
||||
del bom_line_items[0]
|
||||
|
||||
# Initialize list of BOM item emissions data
|
||||
bom_items_emissions_data = []
|
||||
|
||||
print("- Fetching carbon emissions data from demo data source")
|
||||
print("")
|
||||
emission_data = get_carbon_emission_data_dict_from_source(
|
||||
ALLSPICE_DEMO_CARBON_EMISSION_DATA_URL
|
||||
)
|
||||
|
||||
# Fetch emissions figures for all parts in the BOM
|
||||
for line_item in bom_line_items:
|
||||
print("- Fetching info for " + line_item[0] + "... ", end="")
|
||||
# Search for emission figure for a part in demo data source
|
||||
emission_figure = query_demo_carbon_emission_data_for_mfr_part_number(
|
||||
emission_data, line_item[0]
|
||||
)
|
||||
# Add the obtained figure to the list of BOM items part data
|
||||
bom_items_emissions_data.append((line_item[0], float(emission_figure)))
|
||||
# Print the obtained figure
|
||||
print(str(emission_figure) + "\n", end="", flush=True)
|
||||
|
||||
# Report the sum of all component emissions
|
||||
print("")
|
||||
total_emission_for_pcba_BOM = 0.0
|
||||
for bom_item in bom_items_emissions_data:
|
||||
total_emission_for_pcba_BOM += bom_item[1]
|
||||
print("Total emissions from BOM parts: " + str(total_emission_for_pcba_BOM))
|
2
pyproject.toml
Normal file
2
pyproject.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[tool.ruff]
|
||||
exclude = []
|
1
requirements-test.txt
Normal file
1
requirements-test.txt
Normal file
@ -0,0 +1 @@
|
||||
ruff==0.4.7
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
requests==2.32.3
|
Loading…
Reference in New Issue
Block a user