docstring-to-text 0.0.2__tar.gz → 1.0.1__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.
@@ -33,22 +33,32 @@ on:
33
33
  paths:
34
34
  - "src/docstring_to_text/__package_meta.py"
35
35
 
36
+ # ====== CONCURRENCY ======
37
+
38
+ # Prevent multiple simultaneous builds for the same version:
39
+ concurrency:
40
+ group: pypi-publish
41
+ # To group by version:
42
+ #group: pypi-publish-${{ github.ref }}-${{ hashFiles('src/docstring_to_text/__package_meta.py') }}
43
+ cancel-in-progress: false # the second pending run is put into a queue, the third one is cancelled (GitHub limitation)
44
+
36
45
  # ======= ENV VARS =======
37
46
 
38
47
  env:
39
48
  # The name on PyPI:
40
49
  PACKAGE_NAME: 'docstring-to-text'
50
+ VERSION_FILE: 'src/docstring_to_text/__package_meta.py'
41
51
  VERSION_MODULE: 'src.docstring_to_text.__package_meta'
42
52
  VERSION_VARIABLE: 'VERSION'
43
53
 
44
-
45
54
  # ========= JOBS =========
46
55
 
47
56
  jobs:
48
57
 
49
58
  detect-version:
50
59
  # https://emojidb.org/query-emojis
51
- name: Parse version 🔢 + add Tag 🏷️
60
+ name: Parse version 🔢
61
+ # Just to be nice - let's check the GitHub user and prevent unwanted auto-runs on forks made solely for a PR:
52
62
  if: ${{ github.repository_owner == 'Lex-DRL' }}
53
63
  runs-on: ubuntu-latest
54
64
  permissions:
@@ -64,15 +74,53 @@ jobs:
64
74
  with:
65
75
  fetch-depth: 0 # Fetch full history to avoid issues with tags and branches
66
76
 
67
- - name: Extract version from package meta 📝
77
+ - name: Set up Python
78
+ uses: actions/setup-python@v5
79
+ with:
80
+ python-version: "3.x"
81
+
82
+ - name: Extract version 🔢 from package meta 📝
68
83
  id: get_version_string
69
84
  run: |
70
- # VERSION=$(grep -oP 'VERSION\s*=\s*["'\'']\K[^"'\'']+' src/docstring_to_text/__package_meta.py)
71
- VERSION=$(python -c "from ${{ env.VERSION_MODULE }} import ${{ env.VERSION_VARIABLE }}; print(${{ env.VERSION_VARIABLE }})")
85
+ # Try doing it with Python first:
86
+ VERSION=$(
87
+ python -c \
88
+ "from ${{ env.VERSION_MODULE }} import ${{ env.VERSION_VARIABLE }}; print(${{ env.VERSION_VARIABLE }})" \
89
+ 2>/dev/null \
90
+ | head -1
91
+ )
92
+ if [ $? -ne 0 ] || [ -z "$VERSION" ]; then # previous command had non-zero exit status or version is empty
93
+ echo "Failed to retrieve version with python. Attempting to parse it directly from file, with regexp..."
94
+ VERSION=$(
95
+ grep -oP "^\s*${{ env.VERSION_VARIABLE }}\s*=\s*['\"]\K[^'\"]+" "${{ env.VERSION_FILE }}" \
96
+ 2>/dev/null \
97
+ | head -1
98
+ )
99
+ fi
100
+ if [ $? -ne 0 ] || [ -z "$VERSION" ]; then
101
+ echo "Error: Failed to retrieve version." >&2
102
+ exit 1
103
+ fi
72
104
  echo "Version string: $VERSION"
73
105
  echo "version=${VERSION}" >> $GITHUB_OUTPUT
74
106
 
75
- - name: Parse Version 🧱
107
+ # TODO: get rid of the following bash insanity and just parse it with a special python script / shared module.
108
+ # Writing the output would use:
109
+ # with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
110
+ # print(f"{key}={value}", file=fh)
111
+ # - name: Parse Version with Python
112
+ # id: parse-with-python
113
+ # run: >-
114
+ # python parse_version.py
115
+ # "${{ steps.get_version_string.outputs.version }}"
116
+ # # Alternatively:
117
+ # #run: |
118
+ # # python -c "
119
+ # # # The entire python script inline
120
+ # # version_str = '${{ steps.get_version_string.outputs.version }}'.lstrip('v')
121
+ # # "
122
+
123
+ - name: Parse Version into parts 🧱
76
124
  id: parse
77
125
  # The main magic happens in regexps...
78
126
  # - `grep -qP`: verify that the tag matches the given pattern
@@ -121,29 +169,16 @@ jobs:
121
169
  echo "version_num=$VERSION_NUMBER" >> "$GITHUB_OUTPUT"
122
170
  echo "suffix=$SUFFIX" >> "$GITHUB_OUTPUT"
123
171
 
124
- - name: Create tag 🏷️ from version
125
- uses: actions/github-script@v7
126
- with:
127
- script: |
128
- github.rest.git.createRef({
129
- owner: context.repo.owner,
130
- repo: context.repo.repo,
131
- ref: `refs/tags/${process.env.TAG}`,
132
- sha: context.sha
133
- })
134
- env:
135
- TAG: ${{ steps.parse.outputs.tag_name }}
136
-
137
172
  # --------------------------------------------------------
138
173
 
139
174
  build:
140
175
  name: Build distribution 📦
141
176
  needs: [detect-version] # No need to even try, if we failed tag parsing
142
- # Just to be nice - let's check the GitHub user and prevent unwanted auto-runs on forks made solely for a PR.
143
177
  if: >-
144
178
  github.repository_owner == 'Lex-DRL'
145
179
  # Second condition: only run on tag pushes...
146
- # It was in the template from official tutorial, but it got redundant, since the only trigger is `on.push.tags`
180
+ # It was in the template from official tutorial, but it got redundant with our deep version parsing and verification
181
+ # Kept here just as a condition example:
147
182
  # && startsWith(github.ref, 'refs/tags/v')
148
183
  runs-on: ubuntu-latest
149
184
 
@@ -165,6 +200,8 @@ jobs:
165
200
  build
166
201
  --user
167
202
 
203
+ # TODO: run tests before the build
204
+
168
205
  - name: Build a binary wheel and a source tarball
169
206
  run: python3 -m build
170
207
 
@@ -233,30 +270,43 @@ jobs:
233
270
  # --------------------------------------------------------
234
271
 
235
272
  github_release:
236
- name: Create GitHub Release 🔄
237
- needs: [build, detect-version]
238
- runs-on: ubuntu-latest
239
- permissions:
240
- contents: write
241
- steps:
242
- - name: Checkout Code
243
- uses: actions/checkout@v4
244
- with:
245
- fetch-depth: 0 # Fetch full history to avoid issues with tags and branches
246
-
247
- - name: Download artifacts
248
- uses: actions/download-artifact@v4
249
- with:
250
- name: python-package-distributions
251
- path: dist/
252
-
253
- - name: Create GitHub Release
254
- id: create_release
255
- env:
256
- GH_TOKEN: ${{ github.token }}
257
- run: >-
258
- gh release create
259
- ${{ needs.detect-version.outputs.tag_name }}
260
- dist/*
261
- --title ${{ needs.detect-version.outputs.tag_name }}
262
- --generate-notes
273
+ name: Create GitHub Tag 🏷️ and Release 🔄
274
+ needs: [build, detect-version]
275
+ runs-on: ubuntu-latest
276
+ permissions:
277
+ contents: write
278
+ steps:
279
+ - name: Checkout Code
280
+ uses: actions/checkout@v4
281
+ with:
282
+ fetch-depth: 0 # Fetch full history to avoid issues with tags and branches
283
+
284
+ - name: Download artifacts
285
+ uses: actions/download-artifact@v4
286
+ with:
287
+ name: python-package-distributions
288
+ path: dist/
289
+
290
+ - name: Create tag 🏷️ from version
291
+ uses: actions/github-script@v7
292
+ with:
293
+ script: |
294
+ github.rest.git.createRef({
295
+ owner: context.repo.owner,
296
+ repo: context.repo.repo,
297
+ ref: `refs/tags/${process.env.TAG}`,
298
+ sha: context.sha
299
+ })
300
+ env:
301
+ TAG: ${{ needs.detect-version.outputs.tag_name }}
302
+
303
+ - name: Create GitHub Release 🔄
304
+ id: create_release
305
+ env:
306
+ GH_TOKEN: ${{ github.token }}
307
+ run: >-
308
+ gh release create
309
+ ${{ needs.detect-version.outputs.tag_name }}
310
+ dist/*
311
+ --title ${{ needs.detect-version.outputs.tag_name }}
312
+ --generate-notes
@@ -0,0 +1,74 @@
1
+ Metadata-Version: 2.4
2
+ Name: docstring-to-text
3
+ Version: 1.0.1
4
+ Summary: A simple pip package converting docstrings into clean text (proper paragraphs and indents)
5
+ Project-URL: Source Code, https://github.com/Lex-DRL/Py-docstring-to-text
6
+ Project-URL: Issues, https://github.com/Lex-DRL/Py-docstring-to-text/issues
7
+ Author: Lex Darlog (Lex-DRL)
8
+ License-Expression: MPL-2.0
9
+ License-File: LICENSE.md
10
+ Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Requires-Python: >=3.7
14
+ Description-Content-Type: text/markdown
15
+
16
+ # docstring-to-text
17
+
18
+ A simple pip package converting docstrings into clean text (proper paragraphs and indents).
19
+
20
+ For example, here's a class docstring:
21
+ ```python
22
+ class MyClass:
23
+ """
24
+ Here's a class.
25
+
26
+
27
+ It has sphinx-like paragraphs, which can
28
+ span multiple lines. Any modern IDE would
29
+ display them as a single line, that wraps
30
+ the given width.
31
+
32
+ You can't just remove all the new lines
33
+ in the entire string, because you want
34
+ to preserve paragraphs themselves.
35
+
36
+ Also, when it comes to lists:
37
+ - You probably want to separate items
38
+ with new lines.
39
+ - However, you don't want to preserve
40
+ lines inside each item.
41
+
42
+ And...
43
+ * ... you might need various bullet
44
+ characters.
45
+ • Including unicode ones.
46
+
47
+ And don't forget that the list still needs
48
+ to be separated from the following text.
49
+ """
50
+ ...
51
+ ```
52
+
53
+ With this package, you could do:
54
+ ```python
55
+ from docstring_to_text import *
56
+
57
+ clean_text = format_docstring(cleandoc(MyClass.__doc__))
58
+ clean_text = format_object_docstring(MyClass)
59
+ ```
60
+
61
+ Then, the resulting string would be:
62
+ ```text
63
+ Here's a class.
64
+
65
+ It has sphinx-like paragraphs, which can span multiple lines. Any modern IDE would display them as a single line, that wraps the given width.
66
+ You can't just remove all the new lines in the entire string, because you want to preserve paragraphs themselves.
67
+ Also, when it comes to lists:
68
+ - You probably want to separate items with new lines.
69
+ - However, you don't want to preserve lines inside each item.
70
+ And...
71
+ * ... you might need various bullet characters.
72
+ • Including unicode ones.
73
+ And don't forget that the list still needs to be separated from the following text.
74
+ ```
@@ -0,0 +1,59 @@
1
+ # docstring-to-text
2
+
3
+ A simple pip package converting docstrings into clean text (proper paragraphs and indents).
4
+
5
+ For example, here's a class docstring:
6
+ ```python
7
+ class MyClass:
8
+ """
9
+ Here's a class.
10
+
11
+
12
+ It has sphinx-like paragraphs, which can
13
+ span multiple lines. Any modern IDE would
14
+ display them as a single line, that wraps
15
+ the given width.
16
+
17
+ You can't just remove all the new lines
18
+ in the entire string, because you want
19
+ to preserve paragraphs themselves.
20
+
21
+ Also, when it comes to lists:
22
+ - You probably want to separate items
23
+ with new lines.
24
+ - However, you don't want to preserve
25
+ lines inside each item.
26
+
27
+ And...
28
+ * ... you might need various bullet
29
+ characters.
30
+ • Including unicode ones.
31
+
32
+ And don't forget that the list still needs
33
+ to be separated from the following text.
34
+ """
35
+ ...
36
+ ```
37
+
38
+ With this package, you could do:
39
+ ```python
40
+ from docstring_to_text import *
41
+
42
+ clean_text = format_docstring(cleandoc(MyClass.__doc__))
43
+ clean_text = format_object_docstring(MyClass)
44
+ ```
45
+
46
+ Then, the resulting string would be:
47
+ ```text
48
+ Here's a class.
49
+
50
+ It has sphinx-like paragraphs, which can span multiple lines. Any modern IDE would display them as a single line, that wraps the given width.
51
+ You can't just remove all the new lines in the entire string, because you want to preserve paragraphs themselves.
52
+ Also, when it comes to lists:
53
+ - You probably want to separate items with new lines.
54
+ - However, you don't want to preserve lines inside each item.
55
+ And...
56
+ * ... you might need various bullet characters.
57
+ • Including unicode ones.
58
+ And don't forget that the list still needs to be separated from the following text.
59
+ ```
@@ -0,0 +1,167 @@
1
+ # encoding: utf-8
2
+ """
3
+ A simple pip package converting docstrings into clean text (proper paragraphs and indents).
4
+ """
5
+
6
+ import typing as _t
7
+
8
+ from inspect import cleandoc, getdoc
9
+ import re as _re
10
+
11
+ from .__package_meta import VERSION
12
+ from .__package_meta import VERSION as __version__
13
+
14
+ # TODO:
15
+ # - lists
16
+ # formatted with indents
17
+ # in all lines except for the first one
18
+ #
19
+ # Also...
20
+ # - preserve indents
21
+ # - of the entire list
22
+ #
23
+ # And...
24
+ # - ensure
25
+ # that
26
+ # - it all works
27
+ # with nested lists
28
+
29
+
30
+ _re_indent_match = _re.compile(r"(\t*)( +)(\t*)(.*?)$").match
31
+ _re_tab_indent_match = _re.compile(r"(\t+)(.*?)$").match
32
+ _re_list_line_match = _re.compile(
33
+ r"(\s*)("
34
+ r"[-*•]+"
35
+ r"|"
36
+ r"[a-zA-Z]\s*[.)]"
37
+ r"|"
38
+ r"[0-9+]\s*[.)]"
39
+ r")\s+"
40
+ ).match
41
+
42
+
43
+ def _recover_tab_indents(line: str, tab_size: int):
44
+ """Turn indenting spaces back to tabs using regexp. Half-tab indents are rounded."""
45
+ assert bool(line) and isinstance(line, str)
46
+
47
+ n_tabs = 0.0
48
+
49
+ match = _re_indent_match(line)
50
+ while match:
51
+ pre_tabs, spaces, post_tabs, line = match.groups()
52
+ n_tabs_from_spaces = float(len(spaces)) / tab_size + 0.00001
53
+ n_post_tabs = len(post_tabs)
54
+ if n_post_tabs > 0:
55
+ # There are tabs after spaces. Don't preserve the fractional spaces-indent, truncate it:
56
+ n_tabs_from_spaces = int(n_tabs_from_spaces)
57
+ n_tabs += len(pre_tabs) + n_tabs_from_spaces + n_post_tabs
58
+ match = _re_indent_match(line)
59
+
60
+ if n_tabs < 0.5:
61
+ return line
62
+
63
+ tabs_prefix = '\t' * int(n_tabs + 0.50001)
64
+ return f"{tabs_prefix}{line}"
65
+
66
+
67
+ def _join_paragraph_and_format_tabs(paragraph: _t.List[str], tab_size: int):
68
+ """
69
+ Given "continuous" paragraph (i.e., with no empty newlines between chunks), recover tabs for each chunk
70
+ and join them together into a single actual line.
71
+ Works as a generator to account for blocks with different indents - to make each its own line.
72
+ """
73
+ pending_indent = 0
74
+ pending_chunks: _t.List[str] = list()
75
+
76
+ def join_pending_chunks() -> str:
77
+ return "{}{}".format('\t' * pending_indent, ' '.join(pending_chunks))
78
+
79
+ for chunk in paragraph:
80
+ chunk = _recover_tab_indents(chunk, tab_size)
81
+
82
+ cur_indent = 0
83
+ match = _re_tab_indent_match(chunk)
84
+ if match:
85
+ tab_indent, chunk = match.groups() # We've detected indent. Now, get rid of it.
86
+ cur_indent = len(tab_indent)
87
+
88
+ match_list_line = _re_list_line_match(chunk)
89
+ # In case of a bulleted/numbered list, we'll need to start a new block, too.
90
+ if cur_indent == pending_indent and not match_list_line:
91
+ pending_chunks.append(chunk)
92
+ continue
93
+
94
+ # Indent mismatch or a list line:
95
+ # we're either ended one block or entered another. Either way, the previous block ends.
96
+ if pending_chunks:
97
+ yield join_pending_chunks()
98
+ pending_chunks = list()
99
+ assert not pending_chunks
100
+ pending_chunks.append(chunk)
101
+ pending_indent = cur_indent
102
+
103
+ if pending_chunks:
104
+ yield join_pending_chunks()
105
+
106
+
107
+ def _formatted_paragraphs_gen(doc: str, tab_size: int):
108
+ """
109
+ Generator, which splits docstring into lines and transforms them into an actual printable output:
110
+ - From each bulk of empty lines, the first one is skipped...
111
+ - ... thus, non-empty lines are joined into continuous paragraphs.
112
+ - Recover tabs in the beginning oh lines (``inspect.cleandoc()`` converts them into spaces).
113
+ """
114
+ if not doc:
115
+ return
116
+ doc = str(doc)
117
+ if not doc.strip():
118
+ return
119
+
120
+ tab_size = max(int(tab_size), 1)
121
+
122
+ cur_paragraph: _t.List[str] = list()
123
+
124
+ for line in doc.splitlines():
125
+ line: str = line.rstrip()
126
+ if line:
127
+ cur_paragraph.append(line)
128
+ continue
129
+
130
+ assert not line
131
+ if cur_paragraph:
132
+ for block in _join_paragraph_and_format_tabs(cur_paragraph, tab_size):
133
+ yield block
134
+ cur_paragraph = list()
135
+ # Just skip the current empty line entirely - do nothing with it.
136
+ continue
137
+
138
+ # We're in a chain of empty lines, and we've already skipped the first one. Preserve the remaining ones:
139
+ yield ''
140
+
141
+ # Return the last paragraph post-loop:
142
+ if cur_paragraph:
143
+ for block in _join_paragraph_and_format_tabs(cur_paragraph, tab_size):
144
+ yield block
145
+
146
+
147
+ def format_docstring(doc: str, tab_size: int = 8) -> str:
148
+ """
149
+ Turn a pre-cleaned-up docstring (with tabs as spaces and newlines mid-sentence)
150
+ into an actually printable output:
151
+ - mid-paragraph new lines are replaced with spaces...
152
+ - ... while still keeping indented blocks separate.
153
+
154
+ Remember to pass a pre-cleaned-up docstring - i.e., with one of:
155
+ - format_docstring(inspect.cleandoc(__doc__))
156
+ - format_docstring(inspect.getdoc(class_or_function))
157
+ """
158
+ return '\n'.join(_formatted_paragraphs_gen(doc, tab_size))
159
+
160
+
161
+ def format_object_docstring(_obj, tab_size: int = 8) -> str:
162
+ """Find the object's docstring and format it with ``format_docstring()``"""
163
+ doc = getdoc(_obj)
164
+ if not doc:
165
+ return ''
166
+ # noinspection PyArgumentList
167
+ return format_docstring(doc, tab_size=tab_size)
@@ -1,3 +1,3 @@
1
1
  # encoding: utf-8
2
2
 
3
- VERSION = "0.0.2"
3
+ VERSION = "1.0.1"
@@ -1,18 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: docstring-to-text
3
- Version: 0.0.2
4
- Summary: A simple pip package converting docstrings into clean text (proper paragraphs and indents)
5
- Project-URL: Source Code, https://github.com/Lex-DRL/Py-docstring-to-text
6
- Project-URL: Issues, https://github.com/Lex-DRL/Py-docstring-to-text/issues
7
- Author: Lex Darlog (Lex-DRL)
8
- License-Expression: MPL-2.0
9
- License-File: LICENSE.md
10
- Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
11
- Classifier: Operating System :: OS Independent
12
- Classifier: Programming Language :: Python :: 3
13
- Requires-Python: >=3.7
14
- Description-Content-Type: text/markdown
15
-
16
- # docstring-to-text
17
-
18
- A simple pip package converting docstrings into clean text (proper paragraphs and indents)
@@ -1,3 +0,0 @@
1
- # docstring-to-text
2
-
3
- A simple pip package converting docstrings into clean text (proper paragraphs and indents)
@@ -1,6 +0,0 @@
1
- # encoding: utf-8
2
- """
3
- """
4
-
5
- from .__package_meta import VERSION
6
- from .__package_meta import VERSION as __version__