preparse 0.0.0.dev0__tar.gz → 0.0.1.dev0__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.
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: preparse
3
- Version: 0.0.0.dev0
4
- Summary: preparse
3
+ Version: 0.0.1.dev0
4
+ Summary: preparse arguments
5
5
  Author-email: Johannes <johannes-programming@mailfence.com>
6
6
  License: The MIT License (MIT)
7
7
 
@@ -23,54 +23,27 @@ License: The MIT License (MIT)
23
23
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
24
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
25
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
- SOFTWARE.
27
-
26
+ SOFTWARE.
28
27
  Project-URL: Documentation, https://pypi.org/project/preparse
29
28
  Project-URL: Download, https://pypi.org/project/preparse/#files
29
+ Project-URL: Index, https://pypi.org/project/preparse
30
30
  Project-URL: Source, https://github.com/johannes-programming/preparse
31
- Classifier: Development Status :: 1 - Planning
31
+ Project-URL: Website, https://preparse.johannes-programming.online/
32
+ Classifier: Development Status :: 2 - Pre-Alpha
32
33
  Classifier: License :: OSI Approved :: MIT License
33
34
  Classifier: Programming Language :: Python
34
35
  Classifier: Programming Language :: Python :: 3
35
36
  Classifier: Programming Language :: Python :: 3 :: Only
36
- Requires-Python: >=3.12.5
37
+ Requires-Python: >=3.8
37
38
  Description-Content-Type: text/x-rst
38
39
  License-File: LICENSE.txt
40
+ Requires-Dist: click>=8.1.7
41
+ Requires-Dist: datarepr>=0.0.4
42
+ Requires-Dist: makefunc>=0.0.3
43
+ Requires-Dist: makeprop>=0.1.0
39
44
 
40
45
  ========
41
46
  preparse
42
47
  ========
43
48
 
44
- Overview
45
- --------
46
-
47
- preparse
48
-
49
- Installation
50
- ------------
51
-
52
- To install ``preparse``, you can use ``pip``. Open your terminal and run:
53
-
54
- .. code-block:: bash
55
-
56
- pip install preparse
57
-
58
- License
59
- -------
60
-
61
- This project is licensed under the MIT License.
62
-
63
- Links
64
- -----
65
-
66
- * `Documentation <https://pypi.org/project/preparse>`_
67
- * `Download <https://pypi.org/project/preparse/#files>`_
68
- * `Source <https://github.com/johannes-programming/preparse>`_
69
-
70
- Credits
71
- -------
72
-
73
- * Author: Johannes
74
- * Email: `johannes-programming@mailfence.com <mailto:johannes-programming@mailfence.com>`_
75
-
76
- Thank you for using ``preparse``!
49
+ Visit the website https://preparse.johannes-programming.online for more information.
@@ -0,0 +1,5 @@
1
+ ========
2
+ preparse
3
+ ========
4
+
5
+ Visit the website https://preparse.johannes-programming.online for more information.
@@ -9,19 +9,24 @@ authors = [
9
9
  { email = "johannes-programming@mailfence.com", name = "Johannes" },
10
10
  ]
11
11
  classifiers = [
12
- "Development Status :: 1 - Planning",
12
+ "Development Status :: 2 - Pre-Alpha",
13
13
  "License :: OSI Approved :: MIT License",
14
14
  "Programming Language :: Python",
15
15
  "Programming Language :: Python :: 3",
16
16
  "Programming Language :: Python :: 3 :: Only",
17
17
  ]
18
- dependencies = []
19
- description = "preparse"
18
+ dependencies = [
19
+ "click>=8.1.7",
20
+ "datarepr>=0.0.4",
21
+ "makefunc>=0.0.3",
22
+ "makeprop>=0.1.0",
23
+ ]
24
+ description = "preparse arguments"
20
25
  keywords = []
21
26
  name = "preparse"
22
27
  readme = "README.rst"
23
- requires-python = ">=3.12.5"
24
- version = "0.0.0.dev0"
28
+ requires-python = ">=3.8"
29
+ version = "0.0.1.dev0"
25
30
 
26
31
  [project.license]
27
32
  file = "LICENSE.txt"
@@ -29,4 +34,6 @@ file = "LICENSE.txt"
29
34
  [project.urls]
30
35
  Documentation = "https://pypi.org/project/preparse"
31
36
  Download = "https://pypi.org/project/preparse/#files"
37
+ Index = "https://pypi.org/project/preparse"
32
38
  Source = "https://github.com/johannes-programming/preparse"
39
+ Website = "https://preparse.johannes-programming.online/"
@@ -0,0 +1,2 @@
1
+ from preparse.core import *
2
+ from preparse.tests import *
@@ -0,0 +1,298 @@
1
+ import dataclasses
2
+ import enum
3
+ import functools
4
+ import operator
5
+ import os
6
+ import sys
7
+ import types
8
+ import warnings
9
+ from typing import *
10
+
11
+ import click as cl
12
+ from datarepr import datarepr
13
+ from makefunc import makefunc
14
+ from makeprop import makeprop
15
+
16
+ __all__ = ["PreParser"]
17
+
18
+
19
+ class Abbrev(enum.IntEnum):
20
+ REJECT = 0
21
+ COMPLETE = 1
22
+ KEEP = 2
23
+
24
+
25
+ class Nargs(enum.IntEnum):
26
+ NO_ARGUMENT = 0
27
+ REQUIRED_ARGUMENT = 1
28
+ OPTIONAL_ARGUMENT = 2
29
+
30
+
31
+ @dataclasses.dataclass(kw_only=True)
32
+ class PreParser:
33
+ def __init__(
34
+ self,
35
+ optdict: Any = None,
36
+ prog: Any = None,
37
+ abbrev: Any = Abbrev.COMPLETE,
38
+ permutate: Any = True,
39
+ posix: Any = "infer",
40
+ ):
41
+ self._optdict = dict()
42
+ self.optdict = optdict
43
+ self.prog = prog
44
+ self.abbrev = abbrev
45
+ self.permutate = permutate
46
+ self.posix = posix
47
+
48
+ def __repr__(self) -> str:
49
+ return datarepr(type(self).__name__, **self.todict())
50
+
51
+ @makeprop()
52
+ def abbrev(self, value):
53
+ return Abbrev(value)
54
+
55
+ def click(self, *, cmd=True, ctx=True):
56
+ return Click(parser=self, cmd=cmd, ctx=ctx)
57
+
58
+ def clickCommand(self, cmd: cl.Command):
59
+ optdict = dict()
60
+ for p in cmd.params:
61
+ if not isinstance(p, cl.Option):
62
+ continue
63
+ if p.is_flag or p.nargs == 0:
64
+ optn = Nargs.NO_ARGUMENT
65
+ elif p.nargs == 1:
66
+ optn = Nargs.REQUIRED_ARGUMENT
67
+ else:
68
+ optn = Nargs.OPTIONAL_ARGUMENT
69
+ for o in p.opts:
70
+ optdict[str(o)] = optn
71
+ self.optdict.clear()
72
+ self.optdict.update(optdict)
73
+
74
+ def clickContext(self, ctx: cl.Context):
75
+ self.prog = ctx.info_name
76
+
77
+ def copy(self):
78
+ return type(self)(**self.todict())
79
+
80
+ @makeprop()
81
+ def optdict(self, value):
82
+ if value is None:
83
+ self._optdict.clear()
84
+ return self._optdict
85
+ value = dict(value)
86
+ self._optdict.clear()
87
+ self._optdict.update(value)
88
+ return self._optdict
89
+
90
+ def parse_args(self, args: Optional[Iterable] = None) -> List[str]:
91
+ if args is None:
92
+ args = sys.argv[1:]
93
+ return _Parsing(
94
+ parser=self.copy(),
95
+ args=list(args),
96
+ ).ans
97
+
98
+ @makeprop()
99
+ def permutate(self, value):
100
+ return bool(value)
101
+
102
+ @makeprop()
103
+ def posix(self, value):
104
+ if value == "infer":
105
+ value = os.environ.get("POSIXLY_CORRECT")
106
+ value = bool(value)
107
+ return value
108
+
109
+ @makeprop()
110
+ def prog(self, value):
111
+ if value is None:
112
+ value = os.path.basename(sys.argv[0])
113
+ return str(value)
114
+
115
+ def todict(self) -> dict:
116
+ return dict(
117
+ optdict=self.optdict,
118
+ prog=self.prog,
119
+ abbrev=self.abbrev,
120
+ permutate=self.permutate,
121
+ posix=self.posix,
122
+ )
123
+
124
+ def warn(self, message):
125
+ warnings.warn("%s: %s" % (self.prog, message))
126
+
127
+ def warnAboutUnrecognizedOption(self, option):
128
+ self.warn("unrecognized option %r" % option)
129
+
130
+ def warnAboutInvalidOption(self, option):
131
+ self.warn("invalid option -- %r" % option)
132
+
133
+ def warnAboutAmbigousOption(self, option, possibilities):
134
+ msg = "option %r is ambiguous; possibilities:" % option
135
+ for x in possibilities:
136
+ msg += " %r" % x
137
+ self.warn(msg)
138
+
139
+ def warnAboutNotAllowedArgument(self, option):
140
+ self.warn("option %r doesn't allow an argument" % option)
141
+
142
+ def warnAboutRequiredArgument(self, option):
143
+ self.warn("option requires an argument -- %r" % option)
144
+
145
+
146
+ @dataclasses.dataclass
147
+ class Click:
148
+ parser: Any
149
+ cmd: Any
150
+ ctx: Any
151
+
152
+ def __call__(self, target: Any):
153
+ if isinstance(target, types.FunctionType):
154
+ return self._f(target)
155
+ elif isinstance(target, types.MethodType):
156
+ return self._m(target)
157
+ elif isinstance(target, type):
158
+ return self._t(target)
159
+ else:
160
+ return self._o(target)
161
+
162
+ def _f(self, target):
163
+ @functools.wraps(target)
164
+ def ans(cmd, ctx, args):
165
+ p = self.parser.copy()
166
+ if self.cmd:
167
+ p = p.clickCommand(cmd)
168
+ if self.ctx:
169
+ p = p.clickContext(ctx)
170
+ return target(cmd, ctx, p.parse_args(args))
171
+
172
+ return ans
173
+
174
+ def _m(self, target):
175
+ func = self._f(target.__func__)
176
+ ans = types.MethodType(func, target.__self__)
177
+ return ans
178
+
179
+ def _t(self, target):
180
+ target.parse_args = self._f(target)
181
+ return target
182
+
183
+ def _o(self, target):
184
+ target.parse_args = self._m(target)
185
+ return target
186
+
187
+
188
+ @dataclasses.dataclass
189
+ class _Parsing:
190
+ parser: PreParser
191
+ args: list[str]
192
+
193
+ def __post_init__(self):
194
+ self.ans = list()
195
+ self.spec = list()
196
+ optn = 0
197
+ while self.args:
198
+ optn = self.tick(optn)
199
+ if optn == 1:
200
+ self.parser.warnAboutRequiredArgument(self.ans[-1])
201
+ self.ans += self.spec
202
+
203
+ @functools.cached_property
204
+ def islongonly(self):
205
+ for k in self.optdict.keys():
206
+ if len(k) < 3:
207
+ continue
208
+ if k.startswith("--"):
209
+ continue
210
+ if not k.startswith("-"):
211
+ continue
212
+ return True
213
+ return False
214
+
215
+ @functools.cached_property
216
+ def optdict(self):
217
+ ans = dict()
218
+ for k, v in self.parser.items():
219
+ ans[str(k)] = Nargs(v)
220
+ return ans
221
+
222
+ def possibilities(self, opt):
223
+ if opt in self.optdict.keys():
224
+ return [opt]
225
+ if self.parser.abbrev == Abbrev.REJECT:
226
+ return list()
227
+ ans = list()
228
+ for k in self.optdict.keys():
229
+ if k.startswith(opt):
230
+ ans.append(k)
231
+ return ans
232
+
233
+ def tick(self, optn):
234
+ arg = self.args.pop(0)
235
+ if optn == "break":
236
+ self.spec.append(arg)
237
+ return "break"
238
+ if optn == 1:
239
+ self.ans.append(arg)
240
+ return 0
241
+ elif arg == "--":
242
+ self.ans.append("--")
243
+ return "break"
244
+ elif arg.startswith("-") and arg != "-":
245
+ if arg.startswith("--") or self.islongonly:
246
+ return self.tick_long(arg)
247
+ else:
248
+ return self.tick_short(arg)
249
+ else:
250
+ if self.parser.posix:
251
+ self.spec.append(arg)
252
+ return "break"
253
+ elif self.parser.permutate:
254
+ self.spec.append(arg)
255
+ return 0
256
+ else:
257
+ self.ans.append(arg)
258
+ return 0
259
+
260
+ def tick_long(self, arg: str):
261
+ try:
262
+ i = arg.index("=")
263
+ except ValueError:
264
+ i = len(arg)
265
+ opt = arg[:i]
266
+ possibilities = self.possibilities(opt)
267
+ if len(possibilities) == 0:
268
+ self.parser.warnAboutUnrecognizedOption(arg)
269
+ self.ans.append(arg)
270
+ return 0
271
+ if len(possibilities) > 1:
272
+ self.parser.warnAboutAmbigousOption(arg, possibilities)
273
+ self.ans.append(arg)
274
+ return 0
275
+ opt = possibilities[0]
276
+ if self.parser.abbrev == Abbrev.COMPLETE:
277
+ self.ans.append(opt + arg[i:])
278
+ else:
279
+ self.ans.append(arg)
280
+ if "=" in arg:
281
+ if self.optdict[opt] == 0:
282
+ self.parser.warnAboutNotAllowedArgument(opt)
283
+ return 0
284
+ else:
285
+ return self.optdict[opt]
286
+
287
+ def tick_short(self, arg: str):
288
+ self.ans.append(arg)
289
+ for i in range(1 - len(arg), 0):
290
+ optn = self.optdict.get("-" + arg[i])
291
+ if optn is None:
292
+ self.parser.warnAboutInvalidOption(arg[i])
293
+ optn = 0
294
+ if i != -1 and optn != 0:
295
+ return 0
296
+ if i == -1 and optn == 1:
297
+ return 1
298
+ return 0
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: preparse
3
- Version: 0.0.0.dev0
4
- Summary: preparse
3
+ Version: 0.0.1.dev0
4
+ Summary: preparse arguments
5
5
  Author-email: Johannes <johannes-programming@mailfence.com>
6
6
  License: The MIT License (MIT)
7
7
 
@@ -23,54 +23,27 @@ License: The MIT License (MIT)
23
23
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
24
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
25
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
- SOFTWARE.
27
-
26
+ SOFTWARE.
28
27
  Project-URL: Documentation, https://pypi.org/project/preparse
29
28
  Project-URL: Download, https://pypi.org/project/preparse/#files
29
+ Project-URL: Index, https://pypi.org/project/preparse
30
30
  Project-URL: Source, https://github.com/johannes-programming/preparse
31
- Classifier: Development Status :: 1 - Planning
31
+ Project-URL: Website, https://preparse.johannes-programming.online/
32
+ Classifier: Development Status :: 2 - Pre-Alpha
32
33
  Classifier: License :: OSI Approved :: MIT License
33
34
  Classifier: Programming Language :: Python
34
35
  Classifier: Programming Language :: Python :: 3
35
36
  Classifier: Programming Language :: Python :: 3 :: Only
36
- Requires-Python: >=3.12.5
37
+ Requires-Python: >=3.8
37
38
  Description-Content-Type: text/x-rst
38
39
  License-File: LICENSE.txt
40
+ Requires-Dist: click>=8.1.7
41
+ Requires-Dist: datarepr>=0.0.4
42
+ Requires-Dist: makefunc>=0.0.3
43
+ Requires-Dist: makeprop>=0.1.0
39
44
 
40
45
  ========
41
46
  preparse
42
47
  ========
43
48
 
44
- Overview
45
- --------
46
-
47
- preparse
48
-
49
- Installation
50
- ------------
51
-
52
- To install ``preparse``, you can use ``pip``. Open your terminal and run:
53
-
54
- .. code-block:: bash
55
-
56
- pip install preparse
57
-
58
- License
59
- -------
60
-
61
- This project is licensed under the MIT License.
62
-
63
- Links
64
- -----
65
-
66
- * `Documentation <https://pypi.org/project/preparse>`_
67
- * `Download <https://pypi.org/project/preparse/#files>`_
68
- * `Source <https://github.com/johannes-programming/preparse>`_
69
-
70
- Credits
71
- -------
72
-
73
- * Author: Johannes
74
- * Email: `johannes-programming@mailfence.com <mailto:johannes-programming@mailfence.com>`_
75
-
76
- Thank you for using ``preparse``!
49
+ Visit the website https://preparse.johannes-programming.online for more information.
@@ -4,10 +4,11 @@ README.rst
4
4
  pyproject.toml
5
5
  setup.cfg
6
6
  src/preparse/__init__.py
7
- src/preparse/__main__.py
8
7
  src/preparse.egg-info/PKG-INFO
9
8
  src/preparse.egg-info/SOURCES.txt
10
9
  src/preparse.egg-info/dependency_links.txt
10
+ src/preparse.egg-info/requires.txt
11
11
  src/preparse.egg-info/top_level.txt
12
+ src/preparse/core/__init__.py
12
13
  src/preparse/tests/__init__.py
13
14
  src/preparse/tests/test_1984.py
@@ -0,0 +1,4 @@
1
+ click>=8.1.7
2
+ datarepr>=0.0.4
3
+ makefunc>=0.0.3
4
+ makeprop>=0.1.0
@@ -1,37 +0,0 @@
1
- ========
2
- preparse
3
- ========
4
-
5
- Overview
6
- --------
7
-
8
- preparse
9
-
10
- Installation
11
- ------------
12
-
13
- To install ``preparse``, you can use ``pip``. Open your terminal and run:
14
-
15
- .. code-block:: bash
16
-
17
- pip install preparse
18
-
19
- License
20
- -------
21
-
22
- This project is licensed under the MIT License.
23
-
24
- Links
25
- -----
26
-
27
- * `Documentation <https://pypi.org/project/preparse>`_
28
- * `Download <https://pypi.org/project/preparse/#files>`_
29
- * `Source <https://github.com/johannes-programming/preparse>`_
30
-
31
- Credits
32
- -------
33
-
34
- * Author: Johannes
35
- * Email: `johannes-programming@mailfence.com <mailto:johannes-programming@mailfence.com>`_
36
-
37
- Thank you for using ``preparse``!
@@ -1,6 +0,0 @@
1
- def main(args=None):
2
- print("Hello World!")
3
-
4
-
5
- if __name__ == "__main__":
6
- main()
@@ -1,4 +0,0 @@
1
- from preparse import main
2
-
3
- if __name__ == "__main__":
4
- main()
File without changes
File without changes