Question: How to create an OrgDoc from within python #5

Open
opened 2024-08-02 11:57:01 +00:00 by lyz · 2 comments
Collaborator

I'm trying to build some OrgDoc data for some tests from within python:

def create_headline(
    tags: Optional[List[str]] = None,
    state: str = "INACTIVE",
    scheduled: Optional[datetime] = None,
    deadline: Optional[datetime] = None,
    children: Optional[List[Headline]] = None,
) -> Headline:
    """Create a Headline instance with specified attributes."""

    if scheduled:
        scheduled_org_time = OrgTime(Timestamp(datetime_=scheduled))
    else:
        scheduled_org_time = None
    if deadline:
        deadline_org_time = OrgTime(Timestamp(datetime_=deadline))
    else:
        deadline_org_time = None
    return Headline(
        tags=tags if tags is not None else [],
        state=state,
        children=children if children is not None else [],
        start_line=1,
        depth=1,
        orig=None,
        properties=[],
        keywords=[],
        priority_start=None,
        priority=None,
        title_start=None,
        title="",
        tags_start=None,
        contents=[],
        structural=[],
        delimiters=[],
        list_items=[],
        table_rows=[],
        parent=None,
        is_todo=False,
        is_done=False,
        spacing=" ",
        scheduled=scheduled_org_time,
        deadline=deadline_org_time,
    )


def create_org_doc(headlines: List[Headline]) -> OrgDoc:
    """Create an OrgDoc instance with specified headlines."""
    return OrgDoc(
        headlines=headlines,
        keywords=[],
        contents=[],
        list_items=[],
        structural=[],
        properties=[],
        environment={"org-todo-keywords": "TODO READY INACTIVE | DONE"},
    )

But it looks that the headlines argument is not a List[Headline] but a Dict.

How would you create an OrgDoc from within python with some headlines?

I'm trying to build some OrgDoc data for some tests from within python: ```python def create_headline( tags: Optional[List[str]] = None, state: str = "INACTIVE", scheduled: Optional[datetime] = None, deadline: Optional[datetime] = None, children: Optional[List[Headline]] = None, ) -> Headline: """Create a Headline instance with specified attributes.""" if scheduled: scheduled_org_time = OrgTime(Timestamp(datetime_=scheduled)) else: scheduled_org_time = None if deadline: deadline_org_time = OrgTime(Timestamp(datetime_=deadline)) else: deadline_org_time = None return Headline( tags=tags if tags is not None else [], state=state, children=children if children is not None else [], start_line=1, depth=1, orig=None, properties=[], keywords=[], priority_start=None, priority=None, title_start=None, title="", tags_start=None, contents=[], structural=[], delimiters=[], list_items=[], table_rows=[], parent=None, is_todo=False, is_done=False, spacing=" ", scheduled=scheduled_org_time, deadline=deadline_org_time, ) def create_org_doc(headlines: List[Headline]) -> OrgDoc: """Create an OrgDoc instance with specified headlines.""" return OrgDoc( headlines=headlines, keywords=[], contents=[], list_items=[], structural=[], properties=[], environment={"org-todo-keywords": "TODO READY INACTIVE | DONE"}, ) ``` But it looks that the `headlines` argument is not a `List[Headline]` but a `Dict`. How would you create an OrgDoc from within python with some headlines?
Author
Collaborator

I guess that the easiest solution is to do:

orig = '''* NEW_TODO_STATE First entry 

* NEW_DONE_STATE Second entry''' 
doc = loads(orig, environment={ 
  'org-todo-keywords': "NEW_TODO_STATE | NEW_DONE_STATE" 
}) 
I guess that the easiest solution is to do: ```python orig = '''* NEW_TODO_STATE First entry * NEW_DONE_STATE Second entry''' doc = loads(orig, environment={ 'org-todo-keywords': "NEW_TODO_STATE | NEW_DONE_STATE" }) ```
Owner

Yeah, if you don't need very strong typing (like for tests) doing a loads is ok.

What do you intend to do? Create a blank OrgDoc that you will later fill?

Yeah, if you don't need very strong typing (like for tests) doing a `loads` is ok. What do you intend to do? Create a blank OrgDoc that you will later fill?
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: kenkeiras/org-rw#5
No description provided.