Comments (2)
Hi,
except:
print("Your JSON is broken, reloading to spit the error out\n\n")
data = json.load(filehandle)
quit()
This is strange, the Exception is caught and then the script tries to parse the file again?
Second by just using quit()
it is not possible to check if the script ran successfully or not.
sys.exit
with the message from the exception could replace the whole block.
except json.JSONDecodeError as e:
sys.exit(f"Your JSON is broken\n {e}")
Thirdly why was tempfile.NamedTemporaryFile
not used?
I'm asking, because json.dumps
could be replaced with json.dump
and all the file handling/writing could be removed.
See below
#!/usr/bin/env python3
import sys
import json
import tempfile
import shutil
filename = sys.argv[1]
with open(filename, 'r') as filehandle:
try:
json_data = json.load(filehandle)
except json.JSONDecodeError as e:
sys.exit(f"Your JSON is broken\n {e}")
tmp_path = None
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as tmp_file:
json.dump(json_data, tmp_file.file, indent=2)
tmp_path = tmp_file.name
shutil.move(tmp_path, filename)
There is also the option of using jq . --indent 2 "$file" > "$tmp"; mv "$tmp" "$file"
from gsd-tools.
I moved this to the gsd-tools repo
json.tool 3.9 allows you to specify whitespace, and since @raphaelahrens pasted that jq command from let's just suggest that. Humans should not be touching this data (I understand they have to at the moment because we need better tools), I don't want to encourage bad behavior
from gsd-tools.
Related Issues (20)
- Reach out to Ruby Advisory Database community about participation with GSD
- API Endpoint to request/reserve a GSD ID HOT 1
- Update gsd-web to use jsonschema validator HOT 2
- Update gsd-schema with kurtseifried/gsd-schema changes
- Link in https://github.com/cloudsecurityalliance/gsd-tools/blob/main/securitylist/README.md goes to 404 HOT 1
- schema.gsd.id Cloudflare Worker for schema $refs HOT 7
- Fix GSD Bot creating multiple affected packages instead of using multiple ranges
- Update GSD Bot to use INTRODUCED/FIX for reference types HOT 2
- Add tooltips for data.gsd.id add reference type
- Add text box for commit message when editing on data.gsd.id
- Update GSD Web to update modified at when changing a GSD
- Update Edit Button to support all OSV fields HOT 3
- GSD data normalization and format cross-compatibility/conversion
- Vulnerability Data Source Landscape HOT 1
- Update data.gsd.id show page to include all OSV values
- Create an OpenAPI v3.1 Spec for GSD API
- Create Python Interface for GSD HOT 1
- @dawiddczarnecki - counting CVE HOT 1
- Update NVD automation to use the V2 api HOT 1
- Where do kernel vulnerabilities come from ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gsd-tools.