Replace json parsing

This commit is contained in:
Daniel Lindmark 2024-05-20 18:58:56 -05:00
parent 103c09d976
commit 5bfabc60a3

View File

@ -5,6 +5,7 @@ import argparse
import sys
import os
import json
from enum import Enum
print("Starting Test")
@ -51,25 +52,31 @@ print("AllSpice Version: " + allspice.get_version())
# Test private API call
print("API-Token belongs to user: " + allspice.get_user().username)
file_name = "py-allspice-documentation.md"
file_name = "py-allspice-documentation.json"
def get_attributes(obj):
return [attr for attr in dir(obj) if not attr.startswith('_')]
return [attr for attr in dir(obj) if not attr.startswith('_') and not callable(getattr(obj, attr))]
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Enum):
return obj.name # Convert Enum to its name
if isinstance(obj, type) and issubclass(obj, Enum):
return list(obj) # Convert EnumMeta to a list of its members
return super().default(obj)
# Write string to file
def write_to_file(file_name, string):
with open(file_name, 'a') as f:
with open(file_name, 'w') as f: # Use 'w' mode to overwrite the file
f.write(string + os.linesep)
f.close()
# Get the repository object
repository = Repository.request(allspice, "AllSpice-Demos", "Actions-demo")
attributes = get_attributes(repository)
# repository_dict = {attribute: getattr(repository, attribute) for attribute in attributes}
repository_dict = {attribute: getattr(repository, attribute) for attribute in attributes}
# Convert the repository dictionary to a JSON string
repository_json = json.dumps(attributes, indent=4)
# Convert the repository dictionary to a JSON string using the custom encoder
repository_json = json.dumps(repository_dict, indent=4, cls=CustomEncoder)
# Write the JSON string to a file
write_to_file(file_name, repository_json)