Skip to content

Commit

Permalink
Expose and improve the docstrings of export_od and import_id (#473)
Browse files Browse the repository at this point in the history
Resolves #472

* Expose and improve the docstrings of export_od and import_id
* export_od(od, None, doctype) does not return a string; it writes to stdout
* Clarify 'doc_type' details
* Add exception info
  • Loading branch information
erlend-aasland authored Jul 2, 2024
1 parent d141e13 commit 028a57f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
39 changes: 26 additions & 13 deletions canopen/objectdictionary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,27 @@
logger = logging.getLogger(__name__)


def export_od(od, dest: Union[str, TextIO, None] = None, doc_type: Optional[str] = None):
""" Export :class: ObjectDictionary to a file.
def export_od(
od: ObjectDictionary,
dest: Union[str, TextIO, None] = None,
doc_type: Optional[str] = None
) -> None:
"""Export an object dictionary.
:param od:
:class: ObjectDictionary object to be exported
The object dictionary to be exported.
:param dest:
export destination. filename, or file-like object or None.
if None, the document is returned as string
:param doc_type: type of document to export.
If a filename is given for dest, this default to the file extension.
Otherwise, this defaults to "eds"
:rtype: str or None
The export destination as a filename, a file-like object, or ``None``.
If ``None``, the document is written to :data:`sys.stdout`.
:param doc_type:
The type of document to export.
If *dest* is a file-like object or ``None``,
*doc_type* must be explicitly provided.
If *dest* is a filename and its extension is ``.eds`` or ``.dcf``,
*doc_type* defaults to that extension (the preceeding dot excluded);
else, it defaults to ``eds``.
:raises ValueError:
When exporting to an unknown format.
"""

opened_here = False
Expand Down Expand Up @@ -68,10 +77,14 @@ def import_od(
"""Parse an EDS, DCF, or EPF file.
:param source:
Path to object dictionary file or a file like object or an EPF XML tree.
:return:
An Object Dictionary instance.
The path to object dictionary file, a file like object, or an EPF XML tree.
:param node_id:
For EDS and DCF files, the node ID to use.
For other formats, this parameter is ignored.
:raises ObjectDictionaryError:
For object dictionary errors and inconsistencies.
:raises ValueError:
When passed a file of an unknown format.
"""
if source is None:
return ObjectDictionary()
Expand Down
4 changes: 4 additions & 0 deletions doc/od.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ You can access the objects using either index/subindex or names::
API
---

.. autofunction:: canopen.export_od

.. autofunction:: canopen.import_od

.. autoclass:: canopen.ObjectDictionary
:members:

Expand Down

0 comments on commit 028a57f

Please sign in to comment.