Skip to content

Commit

Permalink
[doc] Document public API
Browse files Browse the repository at this point in the history
  • Loading branch information
tueda committed May 28, 2017
1 parent 6ee2219 commit 065b712
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 32 deletions.
20 changes: 20 additions & 0 deletions doc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = python -msphinx
SPHINXPROJ = python-form
SOURCEDIR = .
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
183 changes: 183 additions & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# -*- coding: utf-8 -*-
#
# pyton-form documentation build configuration file, created by
# sphinx-quickstart on Sat May 20 19:27:52 2017.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

"""Sphinx configuration."""

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('..'))

# Import the package metadata from setup.py.
from setup import _metadata # noqa


# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.viewcode',
]

# Add any paths that contain templates here, relative to this directory.
# templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = _metadata['name']
copyright = u'2015-2017, ' + _metadata['author']
author = _metadata['author']

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = _metadata['version']
# The full version, including alpha/beta/rc tags.
release = _metadata['version']

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = 'en'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ['build', 'Thumbs.db', '.DS_Store']

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False


# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'classic'

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
# html_static_path = ['_static']


# -- Options for HTMLHelp output ------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'python-formdoc'


# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'python-form.tex', u'python-form Documentation',
author, 'manual'),
]


# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'pyton-form', u'python-form Documentation',
[author], 1)
]


# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'python-form', u'python-form Documentation',
author, 'python-form', ' A Python package for communicating with FORM.',
'Miscellaneous'),
]


# -- Options for Epub output ----------------------------------------------

# Bibliographic Dublin Core info.
epub_title = project
epub_author = author
epub_publisher = author
epub_copyright = copyright

# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#
# epub_identifier = ''

# A unique identification for the text.
#
# epub_uid = ''

# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']
11 changes: 11 additions & 0 deletions doc/form.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
form module
===========

.. automodule:: form
:members:
:undoc-members:
:show-inheritance:

.. autoclass:: form.FormLink
:members:
:undoc-members:
16 changes: 16 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
python-form
===========

.. toctree::
:maxdepth: 4
:caption: Contents:

modules


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
7 changes: 7 additions & 0 deletions doc/modules.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
API Documentation
=================

.. toctree::
:maxdepth: 4

form
15 changes: 8 additions & 7 deletions form/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""A Python package for communicating with FORM.
"""Provide routines for communicating with FORM.
Example
-------
>>> import form
>>> with form.open() as f:
... f.write('''
... AutoDeclare Vector p;
Expand All @@ -21,9 +20,11 @@
def open(args=None, keep_log=False):
"""Open a connection to FORM and return a link object.
Open a connection to a new FORM process and return a link object.
The opened connection should be closed by `close()` method of the returned
object. This can be guaranteed by the "with" statement:
Open a connection to a new FORM process and return a
:class:`link object <form.FormLink>`.
The opened connection should be closed by
:meth:`close() <form.FormLink.close>` of the returned object, which is
automatically done by use of the "with" statement:
>>> import form
>>> with form.open() as formlink:
Expand All @@ -39,8 +40,8 @@ def open(args=None, keep_log=False):
is >= 2, it specifies the maximum number of lines for the scrollback.
The default value is False.
Caveats
-------
Note
----
In the current implementation, keep_log=True may cause a dead lock when the
listing of the input is enabled and very long input is sent to FORM.
"""
Expand Down
37 changes: 22 additions & 15 deletions form/formlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class FormLink(object):
"""An object representing a connection to FORM."""
"""Connection to a FORM process."""

# The input file for FORM.
_INIT_FRM = get_data_path('form', 'init.frm')
Expand All @@ -25,7 +25,7 @@ class FormLink(object):
_PROMPT = '\n__READY__\n'

def __init__(self, args=None, keep_log=False):
"""Initialize a connection to a FORM process."""
"""Open a connection to a FORM process."""
self._closed = True
self._head = None
self._log = None
Expand Down Expand Up @@ -62,14 +62,17 @@ def open(self, args=None, keep_log=False):
"""Open a connection to FORM.
Open a connection to a new FORM process. The opened connection should
be closed by `close()`. Since `open()` is called from the initializer
(or `form.open()`), this can be guaranteed by use of the "with"
statement:
be closed by :meth:`close`, which can be guaranteed by use of the
"with" statement:
>>> import form
>>> with form.open() as formlink:
... pass # use formlink ...
If this method is called for a link object that has an established
connection to a FORM process, then the existing connection will be
closed and a new connection will be created.
The optional argument `args` is for the FORM command, a string or
a sequence of strings. For example '/path/to/form' or ['tform', '-w4'].
By default, the value of the environment variable `$FORM` is used if
Expand All @@ -80,8 +83,8 @@ def open(self, args=None, keep_log=False):
If the value is >= 2, it specifies the maximum number of lines for
the scrollback. The default value is False.
Caveats
-------
Note
----
In the current implementation, keep_log=True may cause a dead lock when
the listing of the input is enabled and very long input is sent to
FORM.
Expand Down Expand Up @@ -201,8 +204,10 @@ def open(self, args=None, keep_log=False):
def close(self):
"""Close the connection to FORM.
Close the connection to the FORM process established by `open()`.
The user should call this method after use of each FormLink object.
Close the connection to the FORM process established by :meth:`open`.
Do nothing if the connection is already closed. The user should call
this method after use of a link object, which is usually guaranteed by
use of the "with" statement.
"""
self._close()

Expand Down Expand Up @@ -284,8 +289,8 @@ def write(self, script):
"""Send a script to FORM.
Write the given script to the communication channel to FORM. It could
be buffered and so FORM may not execute the sent script until `flush()`
or `read()` is called.
be buffered and so FORM may not execute the sent script until
:meth:`flush` or :meth:`read` is called.
"""
if self._closed:
raise IOError('tried to write to closed connection')
Expand All @@ -297,14 +302,16 @@ def write(self, script):
def flush(self):
"""Flush the channel to FORM.
Flush the communication channel to FORM.
Flush the communication channel to FORM. Because :meth:`write` is
buffered and :meth:`read` is a blocking operation, this method is used
for asynchronous execution of FORM scripts.
"""
if self._closed:
raise IOError('tried to flush closed connection')
self._parentout.flush()

def read(self, *names):
"""Read results from FORM.
r"""Read results from FORM.
Wait for a response of FORM to obtain the results specified by
the given names and return a corresponding string or (nested) list of
Expand All @@ -317,15 +324,15 @@ def read(self, *names):
"F" expression F
"$x" $-variable $x
"$x[]" factorized $-variable $x
"`A'" preprocessor variable A
"\`A'" preprocessor variable A
========== =============================
Note that the communication for the reading is performed within the
preprocessor of FORM (i.e., at compile-time), so one may need to write
".sort" to get the correct result.
If non-string objects are passed, they are considered as sequences, and
the return value becomes the list corresponding to the arguments. If
the return value becomes a list corresponding to the arguments. If
a sequence is passed as the argument to this method, it is guaranteed
that the return value is always a list:
Expand Down
Loading

0 comments on commit 065b712

Please sign in to comment.