Skip to content

Commit 24a2c75

Browse files
authored
Allow setting created_by (#7)
1 parent 68117b2 commit 24a2c75

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

jsondoc/bin/convert_jsondoc.py

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from jsondoc.convert.html import html_to_jsondoc
77
from jsondoc.convert.markdown import jsondoc_to_markdown
88
from jsondoc.serialize import jsondoc_dump_json, load_jsondoc
9+
from jsondoc.utils import set_created_by
910

1011
ALLOWED_FORMATS = [
1112
"jsondoc",
@@ -67,6 +68,7 @@ def convert_to_jsondoc(
6768
source_format: str | None = None,
6869
target_format: str | None = None,
6970
force_page: bool = False,
71+
created_by: str | None = None,
7072
):
7173
"""
7274
Convert to and from JSON-DOC format.
@@ -147,6 +149,8 @@ def convert_to_jsondoc(
147149
raise e
148150

149151
jsondoc = html_to_jsondoc(html_content, force_page=force_page)
152+
if created_by is not None:
153+
set_created_by(jsondoc, created_by)
150154

151155
# Serialize the jsondoc
152156
serialized_jsondoc = jsondoc_dump_json(jsondoc, indent=indent)
@@ -198,6 +202,12 @@ def main():
198202
help="Force the creation of a page even if the input doesn't "
199203
"contain a top-level HTML structure",
200204
)
205+
parser.add_argument(
206+
"--created-by",
207+
type=str,
208+
help="An identifier for the entity that created the JSON-DOC file",
209+
default=None,
210+
)
201211
args = parser.parse_args()
202212

203213
try:
@@ -208,6 +218,7 @@ def main():
208218
source_format=args.source_format,
209219
target_format=args.target_format,
210220
force_page=args.force_page,
221+
created_by=args.created_by,
211222
)
212223
except (ValueError, RuntimeError) as e:
213224
print(e)

jsondoc/utils.py

+55
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,58 @@ def set_dict_recursive(d: dict | list, key: str, value: str):
139139
for item in d:
140140
if isinstance(item, dict):
141141
set_dict_recursive(item, key, value)
142+
143+
144+
def set_field_recursive(obj: any, field_name: str, value: any) -> None:
145+
"""
146+
Recursively sets all fields with name 'field_name' to 'value' in the given object.
147+
Works with dictionaries, lists, Pydantic models, and other objects with attributes.
148+
149+
Args:
150+
obj: The object to traverse (dict, list, Pydantic model, or other object)
151+
field_name: The name of the field to set
152+
value: The value to set the field to
153+
"""
154+
from pydantic import BaseModel
155+
156+
# Handle dictionary
157+
if isinstance(obj, dict):
158+
for k, v in list(obj.items()):
159+
if k == field_name:
160+
obj[k] = value
161+
else:
162+
set_field_recursive(v, field_name, value)
163+
164+
# Handle list
165+
elif isinstance(obj, list):
166+
for item in obj:
167+
set_field_recursive(item, field_name, value)
168+
169+
# Handle Pydantic models
170+
elif isinstance(obj, BaseModel):
171+
# Get the model fields as a dict and process them
172+
data = obj.model_dump()
173+
for k, v in data.items():
174+
if k == field_name:
175+
setattr(obj, k, value)
176+
else:
177+
model_value = getattr(obj, k)
178+
set_field_recursive(model_value, field_name, value)
179+
180+
# # Handle other objects with attributes (non-primitive types)
181+
# elif hasattr(obj, "__dict__") and not isinstance(
182+
# obj, (str, int, float, bool, type(None))
183+
# ):
184+
# for k, v in list(obj.__dict__.items()):
185+
# if k == field_name:
186+
# setattr(obj, k, value)
187+
# else:
188+
# set_field_recursive(v, field_name, value)
189+
190+
191+
def set_created_by(obj: any, created_by: str) -> None:
192+
"""
193+
Recursively sets the 'created_by' field to the given value in the given object.
194+
"""
195+
assert isinstance(created_by, str)
196+
set_field_recursive(obj, "created_by", created_by)

0 commit comments

Comments
 (0)