Skip to main content

Compact DST for editing blocks of plain text

Project description

Project generated with PyScaffold

texted

Compact domain specific language (DSL) for editing blocks of plain text

texted is a line-oriented DSL that focus on simple text editing operations, operating on independent chunks of text (for example commenting/uncommenting lines).

It is not suitable for complex changes. If you need those, please consider using a library that is syntax-aware, like configupdater for .ini/.cfg files, tomlkit for .toml and libCST or refactor for Python files.

Installation

You can install texted with the help of pip:

$ pip install texted

After doing that you will be able to use texted in your Python scripts.

Quickstart

Using texted involves the following workflow:

  1. Select the relevant lines of a given text.

  2. Perform an edition operation over the selection.

This is workflow is shown in the example below:

>>> from texted import edit, find, until, contains, startswith, blank, remove_prefix
>>> example = """\
... # [testenv:typecheck]
... # deps = mypy
... # commands = python -m mypy {posargs:src}
... [testenv:docs]
... deps = sphinx
... changedir = docs
... commands = python -m sphinx -W --keep-going . {toxinidir}/build/html
... """
>>> new_text = edit(
...    example,
...    find(contains("[testenv:typecheck]")) >> until(startswith("[testenv") | blank),
...    remove_prefix("# "),
... )
>>> print(new_text)
[testenv:typecheck]
deps = mypy
commands = python -m mypy {posargs:src}
[testenv:docs]
deps = sphinx
changedir = docs
commands = python -m sphinx -W --keep-going . {toxinidir}/build/html

One of the most basic kinds of select operations can be created with find. This operation will select the first line that matches a criteria. For example:

find(lambda line: "[testenv:typecheck]" in line)

… will select the first line of a text that contains the "[testenv:typecheck]" string. We can then extend (>>) this selection for all the contiguous lines that are not empty with:

find(lambda line: "[testenv:typecheck]" in line) >> whilist(bool)
# => bool is handy! bool("") == False, bool("not empty") == True

After you have selected the block of lines, you can apply a edit operation. For example:

add_prefix("# ")  # => equivalent to: replace(lambda line: "# " + line)

… will add the prefix "# " to all the non empty lines in the selection.

Note that all these functions are lazy and don’t do anything until they are called with edit.

Note

This project has been set up using PyScaffold 4.4. For details and usage information on PyScaffold see https://pyscaffold.org/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

texted-0b0.tar.gz (32.7 kB view details)

Uploaded Source

Built Distribution

texted-0b0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file texted-0b0.tar.gz.

File metadata

  • Download URL: texted-0b0.tar.gz
  • Upload date:
  • Size: 32.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for texted-0b0.tar.gz
Algorithm Hash digest
SHA256 5977d1000fd2ace050b6c257f114f7451a3877a524ba87a78368cf9c13bc4bc5
MD5 7c50e4dc2c062686515b4f606356d8de
BLAKE2b-256 dfa20daf973dd2a7fbdbcce147bc618a38ca7c3b8732a1c1bbd68c3b556582de

See more details on using hashes here.

File details

Details for the file texted-0b0-py3-none-any.whl.

File metadata

  • Download URL: texted-0b0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for texted-0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 4745753283aa04e98b2045995fc844ad3ea1f9e51012f1d44fc7152d6d0778aa
MD5 78d42350434cbcbb55152f70bb2780e5
BLAKE2b-256 4b06a3ece8e515b067aa1733a517a1d7ba7ceda1a81218d4e4562d85b7dd85ad

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page