Fix attribute parsing

This commit is contained in:
Daniel Lindmark 2024-05-21 16:52:34 -05:00
parent 7e33222546
commit 5b06ec276f

View File

@ -52,7 +52,7 @@ 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.json"
file_name = "repo_attributes.json"
def get_attributes(obj):
return [attr for attr in dir(obj) if not attr.startswith('_') and not callable(getattr(obj, attr))]
@ -61,8 +61,8 @@ 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
if hasattr(obj, '__dict__'):
return {key: value for key, value in obj.__dict__.items() if not key.startswith('_')}
return super().default(obj)
def write_to_file(file_name, string):
@ -70,15 +70,21 @@ def write_to_file(file_name, string):
f.write(string + os.linesep)
# Get the repository object
repository = Repository.request(allspice, "AllSpice-Demos", "Actions-demo")
repo = Repository.request(allspice, "AllSpice-Demos", "Actions-demo")
attributes = get_attributes(repository)
repository_dict = {attribute: getattr(repository, attribute) for attribute in attributes}
# Get all attributes and their values ∑ßß
attributes = get_attributes(repo)
repo_dict = {}
# Convert the repository dictionary to a JSON string using the custom encoder
repository_json = json.dumps(repository_dict, indent=4, cls=CustomEncoder)
for attribute in attributes:
value = getattr(repo, attribute)
if isinstance(value, (str, int, float, bool, type(None))):
repo_dict[attribute] = value
else:
repo_dict[attribute] = str(value)
# Convert the dictionary to a JSON string using the custom encoder
repo_json = json.dumps(repo_dict, indent=4, cls=CustomEncoder)
# Write the JSON string to a file
write_to_file(file_name, repository_json)
print("End-of-file")
write_to_file(file_name, repo_json)