asp-format 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ __pycache__/
2
+ *.pyc
3
+ *.swp
4
+ .coverage
5
+
6
+ # build outputs (hatchling)
7
+ dist/
8
+ build/
9
+ *.egg-info/
@@ -0,0 +1,116 @@
1
+ # AGENTS.md
2
+
3
+ Context file for an AI agent (e.g. pi) continuing work in this repository.
4
+ This file holds the **durable, static** facts about the project/
5
+ The **moving** state — what is in progress, what is uncommitted, what to do next — lives in
6
+ `HANDOFF.md`, which is rewritten after each task.
7
+
8
+ ## What this project is
9
+
10
+ `asp-format` — a formatter for Answer Set Programming (ASP) programs. It
11
+ parses ASP (input like the input to clingo) with `clingo.ast`, walks the AST
12
+ with a `clingo.ast.Transformer` subclass, and re-renders it in a clean,
13
+ deterministic style.
14
+
15
+ ## Layout
16
+
17
+ - `asp_format/format.py` — the **single** module. Everything lives here:
18
+ - `asp_format/__pycache__/` is build output — ignored, not source.
19
+ - No separate `tests/` directory; no packaging/`setup.py`. It is a small
20
+ single-file project.
21
+
22
+ ## Dependencies / environment
23
+
24
+ - Python 3.14, `clingo` (imported as `clingo.ast`), and the `selftest`
25
+ framework (installed in the `prorail` env on this machine).
26
+ - `textwrap` (standard library, imported at the top of `format.py`) is used for
27
+ both body/argument indentation and comment wrapping.
28
+ - `black` is the project code formatter — keep it installed and available.
29
+ - `coverage` (`coverage run` / `coverage report`) is used to check branch
30
+ coverage of the embedded suite.
31
+ - selftest (Python, PyPi) is an in-source testing library.
32
+
33
+ ## Testing
34
+
35
+ Tests are **embedded** in `asp_format/format.py` at the bottom using the
36
+ `selftest` framework, **not** a separate test file or framework:
37
+
38
+ ```python
39
+ import selftest
40
+ test = selftest.get_tester(__name__)
41
+
42
+ @test
43
+ def some_test():
44
+ test.eq(expected, actual)
45
+ ```
46
+
47
+ Run them with:
48
+
49
+ ```sh
50
+ selftest <file or module>
51
+ ```
52
+
53
+ `main()` only formats the files passed on the command line, but importing the
54
+ module (or running it) executes the registered tests. **The tester stops at the
55
+ first failing test**, so when adding/adjusting expected-output strings, fix and
56
+ re-run iteratively until the full list of `UNIT:` lines prints with no
57
+ AssertionError.
58
+
59
+ Note: the selftest output (the `UNIT:` lines) is written to **stderr**, so
60
+ `2>/dev/null` hides it. To count tests use
61
+ `python3 -m asp_format.format 2>&1 | grep -c '^UNIT:'` and to see tracebacks do
62
+ not redirect stderr away.
63
+
64
+ Two tests carry the full golden output of the reference program
65
+ (`probeer_reference` and `cli_formats_a_file`); their `expected` strings must be
66
+ kept in sync with each other and with the formatter code. `format_text` returns
67
+ **no trailing newline**, so golden literals must not add one.
68
+
69
+
70
+ ## Formatting contract (high level)
71
+
72
+ Lines must never exceed 80 characters, some exceptions like very long names of path.
73
+ Code wraps on separators like :- : ; , . ( ) etc.
74
+ Wrapped code indents with 4 spaces relative to the parent level.
75
+ Some things like expression and intervals are mostly better on one line.
76
+ When in doubt make it look like the rest. Still in doubt, look what Python black does.
77
+
78
+ Comment spacing: a comment (or a whole run of consecutive comments) opens with
79
+ one blank line *before* it, unless the block immediately before is itself a
80
+ comment (so a series stays glued together and the blank lives before the first
81
+ line). Nothing is left blank *after* a comment — whatever follows, including
82
+ other comments, glues immediately. `#program` directives still open their
83
+ section with two blank lines; multi-line code blocks take one blank, single-line
84
+ blocks glue with a single newline. A body literal that itself wraps over several
85
+ lines counts as a multi-line block, so it opens one blank line before the
86
+ *following* body literal — just as a split rule opens a blank before the next
87
+ rule; a literal that fits on its own line still glues with a comma.
88
+
89
+ A comment that sits on a body literal's source line is a *body* comment: it
90
+ stays inside the rule, rendered just above the literal it annotates at the body
91
+ indent, rather than being hoisted above the rule head. It is matched to the
92
+ first body literal starting on or after the comment's source line (or the last
93
+ body literal that it trails), and its wrapping width is the body's indent
94
+ column, not the page margin. A body comment opens one blank line above itself
95
+ unless the line just written is itself a comment, so a run of consecutive body
96
+ comments glues together while a lone body comment brackets the literal it
97
+ leads; a comment that leads the very first body literal glues directly below
98
+ the rule's `head :-` (no blank).
99
+
100
+ Comment wrapping keeps the source's framing. A comment that already fits on
101
+ every line is left untouched. A comment that overflows is reflowed on
102
+ whitespace but keeps its *kind*: a `%*` … `*%` *block* comment stays one block
103
+ whose continuation lines are indented and closed with `*%`; a `%` *line*
104
+ comment is spread over consecutive `%`-prefixed lines. The formatter never turns
105
+ a line comment into a block comment the source did not already contain (that is
106
+ what the `a_long_line_comment_spreads_over_lines` and
107
+ `long_block_comment_is_rewrapped` tests guard).
108
+
109
+
110
+ ## Working in this repo (stable conventions)
111
+
112
+ 1. **Always keep a local git repository** and commit meaningful changes. Use a
113
+ clear, descriptive commit message. **One logical change per commit.**
114
+ 2. `.gitignore` keeps out Python bytecode, editor swap files and the coverage
115
+ database: `__pycache__/`, `*.pyc`, `*.swp`, `.coverage`. (The tracked
116
+ `.coverage`/`.coverage.*` in the tree are scratch and ignored, not source.)
Binary file
@@ -0,0 +1,59 @@
1
+ # HANDOFF — installable package, API, fail-safe CLI
2
+
3
+ ## In flight
4
+ All six commits are in. The project ships as an installable Python package with a
5
+ public API and an `asp-format` command, both fail safe.
6
+ Commits newest first: `1b9d08e` ship the package, `40a3385` teach the entry point
7
+ to parse `--in-place`, `fb716d0` expose the public API, `ac5ff7a` add the
8
+ fail-safe in-place editor, `bfd4e3d` lock in the no-selftest import, and
9
+ `8b5e183` gate the embedded self tests.
10
+
11
+ ## Goals — all done
12
+ - A clear API: from asp_format import format_text, format_file, format_files,
13
+ parse, render, ast_skeleton, main, Formatter.
14
+ - A published `asp-format` command via `[project.scripts]` in `pyproject.toml`.
15
+ - Both paths are fail safe: a failed parse never touches the source file.
16
+ - Modern packaging: hatchling build backend, a PEP 621 `pyproject.toml`, an
17
+ optional `selftest` extra, and `clingo` plus `black` as runtime deps.
18
+ - The embedded self tests stay import-time tests of the selftest philosophy, but
19
+ the `selftest` import is optional: a plain install keeps the formatter usable
20
+ when the harness is absent.
21
+
22
+ ## What changed
23
+ - `asp_format/__init__.py` re-exports the public helpers, so consumers import
24
+ the helper names from the package instead of from `asp_format.format`.
25
+ - `format_file(path, *, in_place=False)` plus a private `_atomic_write` write
26
+ through a sibling temp file and an atomic `os.replace`, so a parse failure
27
+ leaves the original on disk.
28
+ - `main(argv=None)` is now an argparse entry point with a `-i` / `--in-place`
29
+ switch that routes to `format_file(in_place=True)`.
30
+ - The tests gate on `ASP_FORMAT_RUN_TESTS` (off means 0 / false / no / off), and
31
+ the `selftest` import is guarded so the package imports without it.
32
+
33
+ ## Where it stands
34
+ - The embedded suite has 106 tests, 0 AssertionErrors, and clean black output.
35
+ - `python -m build .` produces a wheel and an sdist; a no-deps pip install lands
36
+ a working `bin/asp-format` that formats to stdout and in place.
37
+ - With `sys.modules["selftest"] = None`, importing `asp_format` still works.
38
+
39
+ ## Commands / environment notes
40
+ ```sh
41
+ # the Python is the prorail env, which has clingo, black, selftest, coverage
42
+ PYTHON=/Users/erikgroeneveld/dev/prorail/.env/bin/python3
43
+ $PYTHON -m asp_format.format 2>&1 | grep -c '^UNIT:'
44
+ $PYTHON -m black --check asp_format/format.py asp_format/__init__.py
45
+ $PYTHON -m build .
46
+ $PYTHON -m pip install --no-deps dist/asp_format-0.1.0-py3-none-any.whl --target /tmp/aspcheck
47
+ ```
48
+
49
+ ## Next steps
50
+ 1. Wire a CI job that runs `python -m build .` and the embedded suite.
51
+ 2. Decide whether `selftest` stays a declared optional extra.
52
+ 3. The long body-comment width undercount in `_render_body` is still open.
53
+
54
+ ## Deliberately left as is
55
+ - The selftest philosophy is preserved: tests run on import unless
56
+ `ASP_FORMAT_RUN_TESTS` is off.
57
+ - Under `python -m asp_format.format` the suite runs twice, so the
58
+ `UNIT:` count doubles; a plain import runs it once.
59
+ - `visit_BodyAggregate` remains unimplemented.
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,52 @@
1
+ Metadata-Version: 2.5
2
+ Name: asp-format
3
+ Version: 0.1.0
4
+ Summary: A formatter for Answer Set Programming (ASP) programs
5
+ Project-URL: Homepage, https://codeberg.org/erikgroeneveld/asp-format
6
+ Author: Erik Groeneveld
7
+ License-Expression: LGPL-3.0-or-later
8
+ Keywords: answer set programming,asp,clingo,formatter,lint
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Topic :: Text Processing :: General
15
+ Requires-Python: >=3.14
16
+ Requires-Dist: black
17
+ Requires-Dist: clingo
18
+ Provides-Extra: selftest
19
+ Requires-Dist: selftest; extra == 'selftest'
20
+ Description-Content-Type: text/markdown
21
+
22
+ # About who wrote this.
23
+
24
+ There is this new rule on codeberg:
25
+
26
+ > You must not share projects that mostly consist of code written by "generative AI"-tools (including services such as *Claude*, *OpenAI Codex*). Such projects having an unclear copyright status (see requirements § 2 (1) 1 and § 2 (1) 3) and furthermore have little safeguards to ensure that they do not include harmful code (c.f. § 2 (1) 5).
27
+
28
+ So I must clarify what I have done here.
29
+
30
+ This code has been set up by myself: I created the parser and the ASP AST visitor that formats the AST into neatly formatted code.
31
+
32
+ After that I decided to see how if a local LLM on my laptop could bring this idea further. I fact, it is some sort of punishment, because the indentation of Python code requires exact counting, but the formatter I want also must indent properly, requireing exact indenting too.
33
+
34
+ LLM's can (still) not count, and the quantized 4- or 8-bit versions that run on laptops are even less capable of it. From its point of view there is indenting and counting on two levels: one direct, in Python, and one indirect, in the ASP code. Indeed, it can spent 12 hours struggling with indentations and never get it right. So I do it.
35
+
36
+ I reviewed all the code and steered when necessary, not by pointing out what I want to see changed, but by asking questions. Asking questions opens up an LLM's learned knowledge, while pointing out changes makes it placating.
37
+
38
+ I'll continue to push the limits of what it can do within my reins. I review everything and edit it when I think I can do better or faster.
39
+
40
+ ## Copyright
41
+
42
+ It is a valid point in the Codeberg terms: much code generated by LLMs could very well be a derived work, and subject to copyright of the code learned during training. That could **prohibit** publication, or it could **require** publication in the case of GPL-licences.
43
+
44
+ The code here is created by myself and then extended by an LMM. Both the idea and the architecture are mine. Also, there is no other ASP formatter anywhere on the internet; this implementation is mine and it is unique. It is free for anyone to use.
45
+
46
+ ## License
47
+
48
+ This project is licensed under the **GNU Lesser General Public License, version 3
49
+ or any later version (LGPL-3.0-or-later)**. The full text is in
50
+ [LICENSES/LGPL-3.0.txt](LICENSES/LGPL-3.0.txt); each source file carries the
51
+ matching SPDX header (`SPDX-License-Identifier: LGPL-3.0-or-later`).
52
+
@@ -0,0 +1,31 @@
1
+ # About who wrote this.
2
+
3
+ There is this new rule on codeberg:
4
+
5
+ > You must not share projects that mostly consist of code written by "generative AI"-tools (including services such as *Claude*, *OpenAI Codex*). Such projects having an unclear copyright status (see requirements § 2 (1) 1 and § 2 (1) 3) and furthermore have little safeguards to ensure that they do not include harmful code (c.f. § 2 (1) 5).
6
+
7
+ So I must clarify what I have done here.
8
+
9
+ This code has been set up by myself: I created the parser and the ASP AST visitor that formats the AST into neatly formatted code.
10
+
11
+ After that I decided to see how if a local LLM on my laptop could bring this idea further. I fact, it is some sort of punishment, because the indentation of Python code requires exact counting, but the formatter I want also must indent properly, requireing exact indenting too.
12
+
13
+ LLM's can (still) not count, and the quantized 4- or 8-bit versions that run on laptops are even less capable of it. From its point of view there is indenting and counting on two levels: one direct, in Python, and one indirect, in the ASP code. Indeed, it can spent 12 hours struggling with indentations and never get it right. So I do it.
14
+
15
+ I reviewed all the code and steered when necessary, not by pointing out what I want to see changed, but by asking questions. Asking questions opens up an LLM's learned knowledge, while pointing out changes makes it placating.
16
+
17
+ I'll continue to push the limits of what it can do within my reins. I review everything and edit it when I think I can do better or faster.
18
+
19
+ ## Copyright
20
+
21
+ It is a valid point in the Codeberg terms: much code generated by LLMs could very well be a derived work, and subject to copyright of the code learned during training. That could **prohibit** publication, or it could **require** publication in the case of GPL-licences.
22
+
23
+ The code here is created by myself and then extended by an LMM. Both the idea and the architecture are mine. Also, there is no other ASP formatter anywhere on the internet; this implementation is mine and it is unique. It is free for anyone to use.
24
+
25
+ ## License
26
+
27
+ This project is licensed under the **GNU Lesser General Public License, version 3
28
+ or any later version (LGPL-3.0-or-later)**. The full text is in
29
+ [LICENSES/LGPL-3.0.txt](LICENSES/LGPL-3.0.txt); each source file carries the
30
+ matching SPDX header (`SPDX-License-Identifier: LGPL-3.0-or-later`).
31
+
@@ -0,0 +1,3 @@
1
+ # IDEA's FOR FORMATTING ASP
2
+
3
+ - [ ] MUST: Python calls must not be ripped off their @. Now it turns @format(...) into format(...).
@@ -0,0 +1,37 @@
1
+ # SPDX-FileCopyrightText: 2026 Erik Groeneveld
2
+ # SPDX-License-Identifier: LGPL-3.0-or-later
3
+ #
4
+ # "asp-format": a formatter for Answer Set Programming (ASP) programs,
5
+ # packaged for install and import.
6
+ #
7
+ # Licensed under the GNU Lesser General Public License, version 3 or
8
+ # any later version. See LICENSES/LGPL-3.0.txt for the full license.
9
+
10
+ # A thin re-export layer: consumers get the formatter behind
11
+ # `asp_format.format_text` and friends instead of reaching into the one
12
+ # module that carries the embedded self-tests.
13
+ from asp_format.format import (
14
+ Formatter,
15
+ ast_skeleton,
16
+ format_file,
17
+ format_files,
18
+ format_text,
19
+ main,
20
+ parse,
21
+ protect_includes,
22
+ render,
23
+ )
24
+
25
+ __all__ = [
26
+ "Formatter",
27
+ "ast_skeleton",
28
+ "format_file",
29
+ "format_files",
30
+ "format_text",
31
+ "main",
32
+ "parse",
33
+ "protect_includes",
34
+ "render",
35
+ ]
36
+
37
+ __version__ = "0.1.0"