def to_latin1(s):
- """ take given unicode value and convert it to a latin1-encoded string """
+ """Take given unicode str and convert it to a latin1-encoded `bytes`."""
return s.encode("latin-1")
def from_latin1(s):
- """ take given latin1-encoded string value and convert it to unicode """
+ """Take given latin1-encoded `bytes` value and convert it to `str`."""
return s.decode("latin-1")
#
def renumber_vars(root, parent=None, toplevel=False):
"""
- renumber_vars -- Number cnfvars linearly. If *parent* is specified,
- numbering will start at this offset. Also, the VAR *root* will be assigned
- this number as a parent lineno unless *toplevel* is set (the root var in
- a CNF tree obviously can’t have a parent).
+ Number cnfvars linearly.
+
+ If *parent* is specified, numbering will start at this offset. Also, the
+ VAR *root* will be assigned this number as a parent lineno unless
+ *toplevel* is set (the root var in a CNF tree obviously can’t have a
+ parent).
The *toplevel* parameter is useful when renumbering an existing variable
starting at a given offset without at the same time having that offset
def print_cnf_raw(root, out=None):
+ """`if root is not None: out.write(root)`."""
if root is not None:
out.write(root)
def write_cnf_raw(*argv, **kw_argv):
+ """Alias for :py:func:`print_cnf_raw`."""
print_cnf_raw(*argv, **kw_argv)
def dump_cnf_bytes (root, renumber=False):
"""
- dump_cnf_bytes -- Serialize CNF var structure, returning the result as a
- byte sequence.
+ Serialize CNF var structure, returning the result as a byte sequence.
"""
cnf = cnf_root(root)
out = io.BytesIO()
def dump_cnf_string(root, renumber=False):
"""
- dump_cnf_string -- Serialize CNF var structure, returning the result as a
- string.
+ Serialize CNF var structure, returning a latin1-encode byte string.
+
+ .. todo::this is identical to :py:func:`dump_cnf_bytes`!
"""
cnf = cnf_root(root)
out = io.BytesIO()
out.close()
return res
+
def print_cnf(root, out=None, renumber=False):
+ """
+ Print given CNF_VAR structure to stdout (or other file-like object).
+
+ Note that per default the config is printed to sys.stdout using the shell's
+ preferred encoding. If the shell cannot handle unicode this might raise
+ `UnicodeError`.
+
+ All params forwarded to :py:func:`output_cnf`. See args there.
+ """
if root is not None:
output_cnf(root, out or sys.stdout, renumber=renumber)
def write_cnf(*argv, **kw_argv):
+ """Alias for :py:func:`print_cnf`."""
print_cnf(*argv, **kw_argv)
def output_json(root, out, renumber=False):
+ """
+ Dump CNF_VAR structure to file-like object in json format.
+
+ :param root: CNF_VAR structure
+ :type root: dict or list or anything that :py:func:`cnf_root` accepts
+ :param out: file-like object, used as argument to :py:func:`json.dumps` so
+ probably has to accept `str` (as opposed to `bytes`).
+ :param bool renumber: whether to renumber variables before dupming.
+ """
# Sanity check incompatible with Autotest. Who needs a seatbelt anyways?
# if not isinstance(out, file):
#raise TypeError("%s (%s) is not a stream." % (out, type(out)))
data = json.dumps(root)
out.write(data)
out.write("\n")
+ # TODO: else raise value error?
def dump_json_string(root, renumber=False):
"""
- dump_json_string -- Serialize CNF var structure as JSON, returning the
- result as a string.
+ Serialize CNF var structure as JSON, returning the result as a string.
"""
out = io.StringIO()
output_json(root, out, renumber=renumber)
def print_cnf_json(root, out=None, renumber=False):
+ """
+ Print CNF_VAR structure in json format to stdout.
+
+ Calls :py:func:`output_json` with `sys.stdout` if `out` is not given or
+ `None`.
+ """
if root is not None:
output_json(root, out or sys.stdout, renumber=renumber)
def write_cnf_json(*argv, **kw_argv):
+ """Alias for :py:func:`print_cnf_json`."""
print_cnf_json(*argv, **kw_argv)
#