Skip to content

Allow setting created_by #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions jsondoc/bin/convert_jsondoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from jsondoc.convert.html import html_to_jsondoc
from jsondoc.convert.markdown import jsondoc_to_markdown
from jsondoc.serialize import jsondoc_dump_json, load_jsondoc
from jsondoc.utils import set_created_by

ALLOWED_FORMATS = [
"jsondoc",
Expand Down Expand Up @@ -67,6 +68,7 @@ def convert_to_jsondoc(
source_format: str | None = None,
target_format: str | None = None,
force_page: bool = False,
created_by: str | None = None,
):
"""
Convert to and from JSON-DOC format.
Expand Down Expand Up @@ -147,6 +149,8 @@ def convert_to_jsondoc(
raise e

jsondoc = html_to_jsondoc(html_content, force_page=force_page)
if created_by is not None:
set_created_by(jsondoc, created_by)

# Serialize the jsondoc
serialized_jsondoc = jsondoc_dump_json(jsondoc, indent=indent)
Expand Down Expand Up @@ -198,6 +202,12 @@ def main():
help="Force the creation of a page even if the input doesn't "
"contain a top-level HTML structure",
)
parser.add_argument(
"--created-by",
type=str,
help="An identifier for the entity that created the JSON-DOC file",
default=None,
)
args = parser.parse_args()

try:
Expand All @@ -208,6 +218,7 @@ def main():
source_format=args.source_format,
target_format=args.target_format,
force_page=args.force_page,
created_by=args.created_by,
)
except (ValueError, RuntimeError) as e:
print(e)
Expand Down
55 changes: 55 additions & 0 deletions jsondoc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,58 @@ def set_dict_recursive(d: dict | list, key: str, value: str):
for item in d:
if isinstance(item, dict):
set_dict_recursive(item, key, value)


def set_field_recursive(obj: any, field_name: str, value: any) -> None:
"""
Recursively sets all fields with name 'field_name' to 'value' in the given object.
Works with dictionaries, lists, Pydantic models, and other objects with attributes.

Args:
obj: The object to traverse (dict, list, Pydantic model, or other object)
field_name: The name of the field to set
value: The value to set the field to
"""
from pydantic import BaseModel

# Handle dictionary
if isinstance(obj, dict):
for k, v in list(obj.items()):
if k == field_name:
obj[k] = value
else:
set_field_recursive(v, field_name, value)

# Handle list
elif isinstance(obj, list):
for item in obj:
set_field_recursive(item, field_name, value)

# Handle Pydantic models
elif isinstance(obj, BaseModel):
# Get the model fields as a dict and process them
data = obj.model_dump()
for k, v in data.items():
if k == field_name:
setattr(obj, k, value)
else:
model_value = getattr(obj, k)
set_field_recursive(model_value, field_name, value)

# # Handle other objects with attributes (non-primitive types)
# elif hasattr(obj, "__dict__") and not isinstance(
# obj, (str, int, float, bool, type(None))
# ):
# for k, v in list(obj.__dict__.items()):
# if k == field_name:
# setattr(obj, k, value)
# else:
# set_field_recursive(v, field_name, value)


def set_created_by(obj: any, created_by: str) -> None:
"""
Recursively sets the 'created_by' field to the given value in the given object.
"""
assert isinstance(created_by, str)
set_field_recursive(obj, "created_by", created_by)
Loading