Skip to content

Commit

Permalink
Add script to communicate with networking hardware database
Browse files Browse the repository at this point in the history
Closes #46
  • Loading branch information
julianstirling committed Sep 27, 2024
1 parent 70db70c commit 6c1ec68
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ _gb_temp_/
build/
_cache_/
OrchestratorConfigOptions.json
networking_hardware.csv

.vscode/*
.venv/
Expand Down
4 changes: 2 additions & 2 deletions nimble_build_system/utils/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
The utilities sub-module of the nimble build system contains scripts that are needed for project specific tasks such as updating config files

## devices_json_updater.py
## nimble_devices_updater.py

This converts the CSV that is downloaded from NocoDB into something that can be used by the orchestration/generation scripts. The scripts use the `devices.json` file that is in the root of this repository. This should eventually be replaced by a direct connection via the NocoDB API. This utility requires that it be passed the path to the CSV file to convert.

**Usage:**

```bash
./devices_json_updater.py /full/path/to/NocoDB/CSV/file
./nimble_devices_updater.py
```

The `devices.json` file it creates is committed to the repository.
Expand Down
65 changes: 0 additions & 65 deletions nimble_build_system/utils/devices_json_updater.py

This file was deleted.

104 changes: 104 additions & 0 deletions nimble_build_system/utils/nimble_devices_updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env python

"""
This module is used to update the devices.json file from a CSV downloaded from nocodb.
"""

import sys
import csv
import json
import argparse

import requests

BASE_URL = "https://nocodb.wakoma.net/api/v1/db/public"
VIEW_URL = "/shared-view/a25a734f-a0d9-4e7f-976f-40723b9f22b6/rows/export/csv"


def parse_args():
"""
Prints a usage message for this utility.
"""
msg = ("This utility exists to update the devices.json file in the "
"root of the repository. The orchestration and generation scripts "
"rely on this file to generate the models and assemblies. "
"The json file is created from a CSV file that can be pulled from "
"our database. Alternatively a local file can be used.")
parser = argparse.ArgumentParser(description=msg)
parser.add_argument(
"--url",
metavar="https://...",
help="Override the default API url for collecting the data"
)
parser.add_argument(
"--local",
metavar="filename.csv",
help="Use local csv"
)
return parser.parse_args()


def get_remote_csv(url):
"""
Get the CSV from the database API, and save locally for processing. If no url
is input then a default is used.
Return the name of the written CSV file.
"""
if url is None:
url = BASE_URL+VIEW_URL
ret = requests.get(url, timeout=10)
if ret.status_code != 200:
raise RuntimeError("Failed to get csv data from database")
csv_name = "networking_hardware.csv"
with open(csv_name, 'wb') as file_obj:
file_obj.write(ret.content)
return csv_name


def main():
"""
Main script to turn a CSV file into a JSON file. It does not do pretty formatting,
it is a JSON file with no newlines.
"""

args = parse_args()

if args.local:
if args.url:
raise RuntimeError("Cannot set an API path and a local file. "
"Pick one or the other.")
csv_path = args.local
else:
csv_path = get_remote_csv(args.url)

# Keeps track of the device entries
devices = []

with open(csv_path, newline='', encoding="utf-8") as csvfile:
# Parse the entire file into rows
devices_csv = csv.reader(csvfile, delimiter=',')

headers = next(devices_csv)

# Process the rows into objects with key/value pairs
for row in devices_csv:
# The current object being assembled

# Generate a unique ID for each device
device_id = row[1].replace(" ", "_")
cur_obj = {"ID": device_id}

# Match each row with its header
for i, entry in enumerate(row):
cur_obj[headers[i]] = entry

# Save the current device
devices.append(cur_obj)

# Write the JSON data to file
with open('devices.json', 'w', encoding="utf-8") as f:
json.dump(devices, f)

if __name__ == "__main__":
main()
11 changes: 8 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
'cadscript>=0.5.2',
'exsource-tools',
'cq-cli @ git+https://github.com/CadQuery/cq-cli.git',
'gitbuilding==0.15.0a2',
'gitbuilding==0.15.0a4',
'cq-annotate @ git+https://github.com/jmwright/cq-annotate.git',
'cq_warehouse @ git+https://github.com/gumyr/cq_warehouse.git',
'cadorchestrator @ git+https://gitlab.com/gitbuilding/cadorchestrator.git'
'cadorchestrator'
],
extras_require={
'dev': [
Expand All @@ -27,5 +27,10 @@
'pytest'
]
},
entry_points={'console_scripts': ['gen_nimble_conf_options = nimble_build_system.utils.gen_nimble_conf_options:main']}
entry_points={
'console_scripts': [
'gen_nimble_conf_options = nimble_build_system.utils.gen_nimble_conf_options:main',
'nimble_devices_updater = nimble_build_system.utils.nimble_devices_updater:main'
]
}
)

0 comments on commit 6c1ec68

Please sign in to comment.