Skip to content
Open
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
35 changes: 33 additions & 2 deletions src/orgparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@
"""
# [[[end]]]

from __future__ import annotations

from collections.abc import Iterable
from itertools import chain
from pathlib import Path
from typing import Optional, TextIO, Union

from .node import OrgEnv, OrgNode, parse_lines # todo basenode??
from .node import OrgBaseNode, OrgEnv, OrgNode, parse_lines # todo basenode??

__all__ = ["load", "loadi", "loads"]
__all__ = ["dump", "dumps", "load", "loadi", "loads"]


def load(path: Union[str, Path, TextIO], env: Optional[OrgEnv] = None) -> OrgNode:
Expand Down Expand Up @@ -145,6 +148,34 @@ def load(path: Union[str, Path, TextIO], env: Optional[OrgEnv] = None) -> OrgNod
return loadi(all_lines, filename=filename, env=env)


def dumps(nodes: OrgBaseNode | Iterable[OrgBaseNode]) -> str:
"""
Dump org-mode nodes to a string.

:arg nodes: Org root node or iterable of nodes to serialize.
:rtype: str
"""
if isinstance(nodes, OrgBaseNode):
if nodes.is_root():
lines = chain.from_iterable(node._render_lines() for node in nodes.env.nodes)
return "\n".join(lines)
return str(nodes)
return "\n".join(str(node) for node in nodes)


def dump(nodes: OrgBaseNode | Iterable[OrgBaseNode], path: Union[str, Path]) -> None:
"""
Dump org-mode nodes to a file.

:type path: str or Path
:arg path: Path to write org-mode contents.
"""
content = dumps(nodes)
if isinstance(path, str):
path = Path(path)
path.write_text(content, encoding="utf8")


def loads(string: str, filename: str = '<string>', env: Optional[OrgEnv] = None) -> OrgNode:
"""
Load org-mode document from a string.
Expand Down
22 changes: 17 additions & 5 deletions src/orgparse/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,26 +692,34 @@ class OrgDateRepeatedTask(OrgDate):

_active_default = False

def __init__(self, start, before: str, after: str, active=None) -> None:
def __init__(self, start, before: str, after: str, active=None, *, comment: str | None = None) -> None:
super().__init__(start, active=active)
self._before = before
self._after = after
self._comment = comment

def __repr__(self) -> str:
args: list = [self._date_to_tuple(self.start), self.before, self.after]
args: list[str] = [
repr(self._date_to_tuple(self.start)),
repr(self.before),
repr(self.after),
]
if self._active is not self._active_default:
args.append(self._active)
return '{}({})'.format(self.__class__.__name__, ', '.join(map(repr, args)))
args.append(repr(self._active))
if self._comment is not None:
args.append(f"comment={self._comment!r}")
return "{}({})".format(self.__class__.__name__, ", ".join(args))

def __hash__(self) -> int:
return hash((self._before, self._after))
return hash((self._before, self._after, self._comment))

def __eq__(self, other) -> bool:
return (
super().__eq__(other)
and isinstance(other, self.__class__)
and self._before == other._before
and self._after == other._after
and self._comment == other._comment
)

@property
Expand All @@ -737,3 +745,7 @@ def after(self) -> str:

"""
return self._after

@property
def comment(self) -> str | None:
return self._comment
Loading