Skip to content

Instantly share code, notes, and snippets.

@mattdodge
Last active May 8, 2018 19:36
Show Gist options
  • Save mattdodge/5d447ebf67797302e0b9cb2f5bfce7b3 to your computer and use it in GitHub Desktop.
Save mattdodge/5d447ebf67797302e0b9cb2f5bfce7b3 to your computer and use it in GitHub Desktop.
Add IDs to nio blocks and services - migrate from nio 2.x to 3.0
# A script to add IDs to block and service resources.
# Useful for migrating a project from nio 2.x to 3.0
#
# Usage:
# Run this script from inside the etc/blocks or etc/services folders of your
# project
#
# Example:
# cd etc/blocks
# python add_ids.py
#
# Any blocks/services that don't have and ID will get an ID that is the old
# name of the block/service
from uuid import uuid4
import os
import json
for filename in os.listdir('.'):
if not filename.endswith(".cfg"):
continue
print("Checking file {}".format(filename))
try:
with open(filename) as fd:
file_contents = json.load(fd)
except Exception:
print("ERROR : Couldn't parse file {}".format(filename))
if file_contents.get("id"):
print("File has an ID already, skipping")
continue
file_contents["id"] = file_contents.get("name", str(uuid4()))
with open(filename, 'w') as fd:
json.dump(file_contents, fd, indent=4, separators=(',', ': '),
default=str, sort_keys=True)
print("File updated")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment