bare-script 0.9.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.
- bare-script-0.9.0/LICENSE +21 -0
- bare-script-0.9.0/PKG-INFO +189 -0
- bare-script-0.9.0/README.rst +166 -0
- bare-script-0.9.0/pyproject.toml +2 -0
- bare-script-0.9.0/setup.cfg +39 -0
- bare-script-0.9.0/src/bare_script/__init__.py +33 -0
- bare-script-0.9.0/src/bare_script/__main__.py +12 -0
- bare-script-0.9.0/src/bare_script/bare.py +109 -0
- bare-script-0.9.0/src/bare_script/baredoc.py +137 -0
- bare-script-0.9.0/src/bare_script/library.py +1912 -0
- bare-script-0.9.0/src/bare_script/model.py +257 -0
- bare-script-0.9.0/src/bare_script/options.py +108 -0
- bare-script-0.9.0/src/bare_script/parser.py +680 -0
- bare-script-0.9.0/src/bare_script/runtime.py +345 -0
- bare-script-0.9.0/src/bare_script/value.py +199 -0
- bare-script-0.9.0/src/bare_script.egg-info/PKG-INFO +189 -0
- bare-script-0.9.0/src/bare_script.egg-info/SOURCES.txt +20 -0
- bare-script-0.9.0/src/bare_script.egg-info/dependency_links.txt +1 -0
- bare-script-0.9.0/src/bare_script.egg-info/entry_points.txt +3 -0
- bare-script-0.9.0/src/bare_script.egg-info/requires.txt +1 -0
- bare-script-0.9.0/src/bare_script.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Craig A. Hobbs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: bare-script
|
|
3
|
+
Version: 0.9.0
|
|
4
|
+
Summary: bare-script
|
|
5
|
+
Home-page: https://github.com/craigahobbs/bare-script
|
|
6
|
+
Author: Craig A. Hobbs
|
|
7
|
+
Author-email: craigahobbs@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Keywords: bare-script
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Utilities
|
|
20
|
+
Description-Content-Type: text/x-rst
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: schema-markdown>=1.2.0
|
|
23
|
+
|
|
24
|
+
bare-script
|
|
25
|
+
===========
|
|
26
|
+
|
|
27
|
+
.. |badge-status| image:: https://img.shields.io/pypi/status/bare-script
|
|
28
|
+
:alt: PyPI - Status
|
|
29
|
+
:target: https://pypi.python.org/pypi/bare-script/
|
|
30
|
+
|
|
31
|
+
.. |badge-version| image:: https://img.shields.io/pypi/v/bare-script
|
|
32
|
+
:alt: PyPI
|
|
33
|
+
:target: https://pypi.python.org/pypi/bare-script/
|
|
34
|
+
|
|
35
|
+
.. |badge-license| image:: https://img.shields.io/github/license/craigahobbs/bare-script-py
|
|
36
|
+
:alt: GitHub
|
|
37
|
+
:target: https://github.com/craigahobbs/bare-script-py/blob/main/LICENSE
|
|
38
|
+
|
|
39
|
+
.. |badge-python| image:: https://img.shields.io/pypi/pyversions/bare-script
|
|
40
|
+
:alt: PyPI - Python Version
|
|
41
|
+
:target: https://www.python.org/downloads/
|
|
42
|
+
|
|
43
|
+
|badge-status| |badge-version| |badge-license| |badge-python|
|
|
44
|
+
|
|
45
|
+
BareScript is a light-weight scripting and expression language.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
Links
|
|
49
|
+
-----
|
|
50
|
+
|
|
51
|
+
- `The BareScript Language <https://craigahobbs.github.io/bare-script/language/>`__
|
|
52
|
+
- `The BareScript Library <https://craigahobbs.github.io/bare-script-py/library/>`__
|
|
53
|
+
- `The BareScript Expression Library <https://craigahobbs.github.io/bare-script-py/library/expression.html>`__
|
|
54
|
+
- `API Documentation <https://craigahobbs.github.io/bare-script-py/>`__
|
|
55
|
+
- `Source code <https://github.com/craigahobbs/bare-script-py>`__
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
Executing BareScript
|
|
59
|
+
--------------------
|
|
60
|
+
|
|
61
|
+
To execute a BareScript script, parse the script using the
|
|
62
|
+
`parse_script <https://craigahobbs.github.io/bare-script-py/scripts.html#parse-script>`__
|
|
63
|
+
function. Then execute the script using the
|
|
64
|
+
`execute_script <https://craigahobbs.github.io/bare-script-py/scripts.html#execute-script>`__
|
|
65
|
+
function. For example:
|
|
66
|
+
|
|
67
|
+
>>> from bare_script import execute_script, parse_script
|
|
68
|
+
...
|
|
69
|
+
>>> # Parse the script
|
|
70
|
+
... script = parse_script('''\
|
|
71
|
+
... # Double a number
|
|
72
|
+
... function double(n):
|
|
73
|
+
... return n * 2
|
|
74
|
+
... endfunction
|
|
75
|
+
...
|
|
76
|
+
... return N + ' times 2 is ' + double(N)
|
|
77
|
+
... ''')
|
|
78
|
+
...
|
|
79
|
+
>>> # Execute the script
|
|
80
|
+
... globals = {'N': 10}
|
|
81
|
+
>>> print(execute_script(script, {'globals': globals}))
|
|
82
|
+
10 times 2 is 20
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
The BareScript Library
|
|
86
|
+
^^^^^^^^^^^^^^^^^^^^^^
|
|
87
|
+
|
|
88
|
+
`The BareScript Library <https://craigahobbs.github.io/bare-script-py/library/>`__
|
|
89
|
+
includes a set of built-in functions for mathematical operations, object manipulation, array
|
|
90
|
+
manipulation, regular expressions, HTTP fetch and more. The following example demonstrates the use
|
|
91
|
+
of the
|
|
92
|
+
`systemFetch <https://craigahobbs.github.io/bare-script-py/library/#var.vGroup='System'&systemfetch>`__,
|
|
93
|
+
`objectGet <https://craigahobbs.github.io/bare-script-py/library/#var.vGroup='Object'&objectget>`__, and
|
|
94
|
+
`arrayLength <https://craigahobbs.github.io/bare-script-py/library/#var.vGroup='Array'&arraylength>`__
|
|
95
|
+
functions.
|
|
96
|
+
|
|
97
|
+
>>> import urllib.request
|
|
98
|
+
...
|
|
99
|
+
>>> from bare_script import execute_script, fetch_http, parse_script
|
|
100
|
+
...
|
|
101
|
+
>>> # Parse the script
|
|
102
|
+
... script = parse_script('''\
|
|
103
|
+
... # Fetch the BareScript library documentation JSON
|
|
104
|
+
... docs = jsonParse(systemFetch('https://craigahobbs.github.io/bare-script-py/library/library.json'))
|
|
105
|
+
...
|
|
106
|
+
... # Return the number of library functions
|
|
107
|
+
... return 'The BareScript Library has ' + arrayLength(objectGet(docs, 'functions')) + ' functions'
|
|
108
|
+
... ''')
|
|
109
|
+
...
|
|
110
|
+
>>> # Execute the script
|
|
111
|
+
... print(execute_script(script, {'fetchFn': fetch_http}))
|
|
112
|
+
The BareScript Library has 106 functions
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
Evaluating BareScript Expressions
|
|
116
|
+
---------------------------------
|
|
117
|
+
|
|
118
|
+
To evaluate a
|
|
119
|
+
`BareScript expression <https://craigahobbs.github.io/bare-script/language/#expressions>`__,
|
|
120
|
+
parse the expression using the
|
|
121
|
+
`parse_expression <https://craigahobbs.github.io/bare-script-py/expressions.html#parse-expression>`__
|
|
122
|
+
function. Then evaluate the expression using the
|
|
123
|
+
`evaluate_expression <https://craigahobbs.github.io/bare-script-py/expressions.html#evaluate-expression>`__
|
|
124
|
+
function.
|
|
125
|
+
|
|
126
|
+
Expression evaluation includes the
|
|
127
|
+
`BareScript Expression Library <https://craigahobbs.github.io/bare-script-py/library/expression.html>`__,
|
|
128
|
+
a set of built-in, spreadsheet-like functions.
|
|
129
|
+
|
|
130
|
+
For example:
|
|
131
|
+
|
|
132
|
+
>>> from bare_script import evaluate_expression, parse_expression
|
|
133
|
+
...
|
|
134
|
+
>>> # Parse the expression
|
|
135
|
+
... expr = parse_expression('2 * max(a, b, c)')
|
|
136
|
+
...
|
|
137
|
+
>>> # Evaluate the expression
|
|
138
|
+
... variables = {'a': 1, 'b': 2, 'c': 3}
|
|
139
|
+
>>> print(evaluate_expression(expr, None, variables))
|
|
140
|
+
6.0
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
The BareScript Command-Line Interface (CLI)
|
|
144
|
+
-------------------------------------------
|
|
145
|
+
|
|
146
|
+
You can run BareScript from the command line using the BareScript CLI, "bare". BareScript script
|
|
147
|
+
files use the ".bare" file extension.
|
|
148
|
+
|
|
149
|
+
.. code-block:: sh
|
|
150
|
+
|
|
151
|
+
bare script.bare
|
|
152
|
+
|
|
153
|
+
**Note:** In the BareScript CLI, import statements and the
|
|
154
|
+
`systemFetch <https://craigahobbs.github.io/bare-script-py/library/#var.vGroup='System'&systemfetch>`__
|
|
155
|
+
function read non-URL paths from the local file system.
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
MarkdownUp, a Markdown Viewer with BareScript
|
|
159
|
+
---------------------------------------------
|
|
160
|
+
|
|
161
|
+
`MarkdownUp <https://craigahobbs.github.io/markdown-up/>`__
|
|
162
|
+
is a Markdown Viewer that executes BareScript embedded within Markdown documents.
|
|
163
|
+
`MarkdownUp <https://craigahobbs.github.io/markdown-up/>`__
|
|
164
|
+
extends its
|
|
165
|
+
`standard library <https://craigahobbs.github.io/markdown-up/library/>`__
|
|
166
|
+
with functions for dynamically rendering Markdown text, drawing SVG images, etc.
|
|
167
|
+
|
|
168
|
+
For example:
|
|
169
|
+
|
|
170
|
+
.. code-block:: markdown
|
|
171
|
+
|
|
172
|
+
# Markdown Application
|
|
173
|
+
|
|
174
|
+
This is a Markdown document with embedded BareScript:
|
|
175
|
+
|
|
176
|
+
~~~ markdown-script
|
|
177
|
+
markdownPrint('Hello, Markdown!')
|
|
178
|
+
~~~
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
Development
|
|
182
|
+
-----------
|
|
183
|
+
|
|
184
|
+
This package is developed using `python-build <https://github.com/craigahobbs/python-build#readme>`__.
|
|
185
|
+
It was started using `python-template <https://github.com/craigahobbs/python-template#readme>`__ as follows:
|
|
186
|
+
|
|
187
|
+
.. code-block:: sh
|
|
188
|
+
|
|
189
|
+
template-specialize python-template/template/ bare-script/ -k package bare-script -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs'
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
bare-script
|
|
2
|
+
===========
|
|
3
|
+
|
|
4
|
+
.. |badge-status| image:: https://img.shields.io/pypi/status/bare-script
|
|
5
|
+
:alt: PyPI - Status
|
|
6
|
+
:target: https://pypi.python.org/pypi/bare-script/
|
|
7
|
+
|
|
8
|
+
.. |badge-version| image:: https://img.shields.io/pypi/v/bare-script
|
|
9
|
+
:alt: PyPI
|
|
10
|
+
:target: https://pypi.python.org/pypi/bare-script/
|
|
11
|
+
|
|
12
|
+
.. |badge-license| image:: https://img.shields.io/github/license/craigahobbs/bare-script-py
|
|
13
|
+
:alt: GitHub
|
|
14
|
+
:target: https://github.com/craigahobbs/bare-script-py/blob/main/LICENSE
|
|
15
|
+
|
|
16
|
+
.. |badge-python| image:: https://img.shields.io/pypi/pyversions/bare-script
|
|
17
|
+
:alt: PyPI - Python Version
|
|
18
|
+
:target: https://www.python.org/downloads/
|
|
19
|
+
|
|
20
|
+
|badge-status| |badge-version| |badge-license| |badge-python|
|
|
21
|
+
|
|
22
|
+
BareScript is a light-weight scripting and expression language.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
Links
|
|
26
|
+
-----
|
|
27
|
+
|
|
28
|
+
- `The BareScript Language <https://craigahobbs.github.io/bare-script/language/>`__
|
|
29
|
+
- `The BareScript Library <https://craigahobbs.github.io/bare-script-py/library/>`__
|
|
30
|
+
- `The BareScript Expression Library <https://craigahobbs.github.io/bare-script-py/library/expression.html>`__
|
|
31
|
+
- `API Documentation <https://craigahobbs.github.io/bare-script-py/>`__
|
|
32
|
+
- `Source code <https://github.com/craigahobbs/bare-script-py>`__
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
Executing BareScript
|
|
36
|
+
--------------------
|
|
37
|
+
|
|
38
|
+
To execute a BareScript script, parse the script using the
|
|
39
|
+
`parse_script <https://craigahobbs.github.io/bare-script-py/scripts.html#parse-script>`__
|
|
40
|
+
function. Then execute the script using the
|
|
41
|
+
`execute_script <https://craigahobbs.github.io/bare-script-py/scripts.html#execute-script>`__
|
|
42
|
+
function. For example:
|
|
43
|
+
|
|
44
|
+
>>> from bare_script import execute_script, parse_script
|
|
45
|
+
...
|
|
46
|
+
>>> # Parse the script
|
|
47
|
+
... script = parse_script('''\
|
|
48
|
+
... # Double a number
|
|
49
|
+
... function double(n):
|
|
50
|
+
... return n * 2
|
|
51
|
+
... endfunction
|
|
52
|
+
...
|
|
53
|
+
... return N + ' times 2 is ' + double(N)
|
|
54
|
+
... ''')
|
|
55
|
+
...
|
|
56
|
+
>>> # Execute the script
|
|
57
|
+
... globals = {'N': 10}
|
|
58
|
+
>>> print(execute_script(script, {'globals': globals}))
|
|
59
|
+
10 times 2 is 20
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
The BareScript Library
|
|
63
|
+
^^^^^^^^^^^^^^^^^^^^^^
|
|
64
|
+
|
|
65
|
+
`The BareScript Library <https://craigahobbs.github.io/bare-script-py/library/>`__
|
|
66
|
+
includes a set of built-in functions for mathematical operations, object manipulation, array
|
|
67
|
+
manipulation, regular expressions, HTTP fetch and more. The following example demonstrates the use
|
|
68
|
+
of the
|
|
69
|
+
`systemFetch <https://craigahobbs.github.io/bare-script-py/library/#var.vGroup='System'&systemfetch>`__,
|
|
70
|
+
`objectGet <https://craigahobbs.github.io/bare-script-py/library/#var.vGroup='Object'&objectget>`__, and
|
|
71
|
+
`arrayLength <https://craigahobbs.github.io/bare-script-py/library/#var.vGroup='Array'&arraylength>`__
|
|
72
|
+
functions.
|
|
73
|
+
|
|
74
|
+
>>> import urllib.request
|
|
75
|
+
...
|
|
76
|
+
>>> from bare_script import execute_script, fetch_http, parse_script
|
|
77
|
+
...
|
|
78
|
+
>>> # Parse the script
|
|
79
|
+
... script = parse_script('''\
|
|
80
|
+
... # Fetch the BareScript library documentation JSON
|
|
81
|
+
... docs = jsonParse(systemFetch('https://craigahobbs.github.io/bare-script-py/library/library.json'))
|
|
82
|
+
...
|
|
83
|
+
... # Return the number of library functions
|
|
84
|
+
... return 'The BareScript Library has ' + arrayLength(objectGet(docs, 'functions')) + ' functions'
|
|
85
|
+
... ''')
|
|
86
|
+
...
|
|
87
|
+
>>> # Execute the script
|
|
88
|
+
... print(execute_script(script, {'fetchFn': fetch_http}))
|
|
89
|
+
The BareScript Library has 106 functions
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
Evaluating BareScript Expressions
|
|
93
|
+
---------------------------------
|
|
94
|
+
|
|
95
|
+
To evaluate a
|
|
96
|
+
`BareScript expression <https://craigahobbs.github.io/bare-script/language/#expressions>`__,
|
|
97
|
+
parse the expression using the
|
|
98
|
+
`parse_expression <https://craigahobbs.github.io/bare-script-py/expressions.html#parse-expression>`__
|
|
99
|
+
function. Then evaluate the expression using the
|
|
100
|
+
`evaluate_expression <https://craigahobbs.github.io/bare-script-py/expressions.html#evaluate-expression>`__
|
|
101
|
+
function.
|
|
102
|
+
|
|
103
|
+
Expression evaluation includes the
|
|
104
|
+
`BareScript Expression Library <https://craigahobbs.github.io/bare-script-py/library/expression.html>`__,
|
|
105
|
+
a set of built-in, spreadsheet-like functions.
|
|
106
|
+
|
|
107
|
+
For example:
|
|
108
|
+
|
|
109
|
+
>>> from bare_script import evaluate_expression, parse_expression
|
|
110
|
+
...
|
|
111
|
+
>>> # Parse the expression
|
|
112
|
+
... expr = parse_expression('2 * max(a, b, c)')
|
|
113
|
+
...
|
|
114
|
+
>>> # Evaluate the expression
|
|
115
|
+
... variables = {'a': 1, 'b': 2, 'c': 3}
|
|
116
|
+
>>> print(evaluate_expression(expr, None, variables))
|
|
117
|
+
6.0
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
The BareScript Command-Line Interface (CLI)
|
|
121
|
+
-------------------------------------------
|
|
122
|
+
|
|
123
|
+
You can run BareScript from the command line using the BareScript CLI, "bare". BareScript script
|
|
124
|
+
files use the ".bare" file extension.
|
|
125
|
+
|
|
126
|
+
.. code-block:: sh
|
|
127
|
+
|
|
128
|
+
bare script.bare
|
|
129
|
+
|
|
130
|
+
**Note:** In the BareScript CLI, import statements and the
|
|
131
|
+
`systemFetch <https://craigahobbs.github.io/bare-script-py/library/#var.vGroup='System'&systemfetch>`__
|
|
132
|
+
function read non-URL paths from the local file system.
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
MarkdownUp, a Markdown Viewer with BareScript
|
|
136
|
+
---------------------------------------------
|
|
137
|
+
|
|
138
|
+
`MarkdownUp <https://craigahobbs.github.io/markdown-up/>`__
|
|
139
|
+
is a Markdown Viewer that executes BareScript embedded within Markdown documents.
|
|
140
|
+
`MarkdownUp <https://craigahobbs.github.io/markdown-up/>`__
|
|
141
|
+
extends its
|
|
142
|
+
`standard library <https://craigahobbs.github.io/markdown-up/library/>`__
|
|
143
|
+
with functions for dynamically rendering Markdown text, drawing SVG images, etc.
|
|
144
|
+
|
|
145
|
+
For example:
|
|
146
|
+
|
|
147
|
+
.. code-block:: markdown
|
|
148
|
+
|
|
149
|
+
# Markdown Application
|
|
150
|
+
|
|
151
|
+
This is a Markdown document with embedded BareScript:
|
|
152
|
+
|
|
153
|
+
~~~ markdown-script
|
|
154
|
+
markdownPrint('Hello, Markdown!')
|
|
155
|
+
~~~
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
Development
|
|
159
|
+
-----------
|
|
160
|
+
|
|
161
|
+
This package is developed using `python-build <https://github.com/craigahobbs/python-build#readme>`__.
|
|
162
|
+
It was started using `python-template <https://github.com/craigahobbs/python-template#readme>`__ as follows:
|
|
163
|
+
|
|
164
|
+
.. code-block:: sh
|
|
165
|
+
|
|
166
|
+
template-specialize python-template/template/ bare-script/ -k package bare-script -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs'
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = bare-script
|
|
3
|
+
version = 0.9.0
|
|
4
|
+
url = https://github.com/craigahobbs/bare-script
|
|
5
|
+
author = Craig A. Hobbs
|
|
6
|
+
author_email = craigahobbs@gmail.com
|
|
7
|
+
license = MIT
|
|
8
|
+
description = bare-script
|
|
9
|
+
long_description = file:README.rst
|
|
10
|
+
long_description_content_type = text/x-rst
|
|
11
|
+
keywords = bare-script
|
|
12
|
+
classifiers =
|
|
13
|
+
Development Status :: 5 - Production/Stable
|
|
14
|
+
Intended Audience :: Developers
|
|
15
|
+
License :: OSI Approved :: MIT License
|
|
16
|
+
Operating System :: OS Independent
|
|
17
|
+
Programming Language :: Python :: 3.8
|
|
18
|
+
Programming Language :: Python :: 3.9
|
|
19
|
+
Programming Language :: Python :: 3.10
|
|
20
|
+
Programming Language :: Python :: 3.11
|
|
21
|
+
Programming Language :: Python :: 3.12
|
|
22
|
+
Topic :: Utilities
|
|
23
|
+
|
|
24
|
+
[options]
|
|
25
|
+
packages = bare_script
|
|
26
|
+
package_dir =
|
|
27
|
+
= src
|
|
28
|
+
install_requires =
|
|
29
|
+
schema-markdown >= 1.2.0
|
|
30
|
+
|
|
31
|
+
[options.entry_points]
|
|
32
|
+
console_scripts =
|
|
33
|
+
bare = bare_script.bare:main
|
|
34
|
+
baredoc = bare_script.baredoc:main
|
|
35
|
+
|
|
36
|
+
[egg_info]
|
|
37
|
+
tag_build =
|
|
38
|
+
tag_date = 0
|
|
39
|
+
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Licensed under the MIT License
|
|
2
|
+
# https://github.com/craigahobbs/bare-script-py/blob/main/LICENSE
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
bare-script package
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .library import \
|
|
9
|
+
EXPRESSION_FUNCTIONS, \
|
|
10
|
+
SCRIPT_FUNCTIONS
|
|
11
|
+
|
|
12
|
+
from .model import \
|
|
13
|
+
BARE_SCRIPT_TYPES, \
|
|
14
|
+
lint_script, \
|
|
15
|
+
validate_expression, \
|
|
16
|
+
validate_script
|
|
17
|
+
|
|
18
|
+
from .options import \
|
|
19
|
+
fetch_http, \
|
|
20
|
+
fetch_read_only, \
|
|
21
|
+
fetch_read_write, \
|
|
22
|
+
log_print, \
|
|
23
|
+
url_file_relative
|
|
24
|
+
|
|
25
|
+
from .parser import \
|
|
26
|
+
BareScriptParserError, \
|
|
27
|
+
parse_expression, \
|
|
28
|
+
parse_script
|
|
29
|
+
|
|
30
|
+
from .runtime import \
|
|
31
|
+
BareScriptRuntimeError, \
|
|
32
|
+
evaluate_expression, \
|
|
33
|
+
execute_script
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Licensed under the MIT License
|
|
2
|
+
# https://github.com/craigahobbs/bare-script-py/blob/main/LICENSE
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
bare-script command-line interface (CLI) main module
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
from functools import partial
|
|
10
|
+
import sys
|
|
11
|
+
import time
|
|
12
|
+
|
|
13
|
+
from .model import lint_script
|
|
14
|
+
from .options import fetch_read_write, log_print, url_file_relative
|
|
15
|
+
from .parser import parse_script
|
|
16
|
+
from .runtime import execute_script
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def main(argv=None):
|
|
20
|
+
"""
|
|
21
|
+
BareScript command-line interface (CLI) main entry point
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
# Command line arguments
|
|
25
|
+
parser = argparse.ArgumentParser(prog='bare', description='The BareScript command-line interface')
|
|
26
|
+
parser.add_argument('filename', nargs='*', action=_ScriptAction, help='files to process')
|
|
27
|
+
parser.add_argument('-c', '--code', nargs=1, action=_ScriptAction, help='execute the BareScript code')
|
|
28
|
+
parser.add_argument('-d', '--debug', action='store_true', help='enable debug mode')
|
|
29
|
+
parser.add_argument('-s', '--static', action='store_true', help='perform static analysis')
|
|
30
|
+
parser.add_argument('-v', '--var', nargs=2, action='append', metavar=('VAR', 'EXPR'),
|
|
31
|
+
help='set a global variable to an expression value')
|
|
32
|
+
args = parser.parse_args(args=argv)
|
|
33
|
+
|
|
34
|
+
status_code = 0
|
|
35
|
+
current_file = None
|
|
36
|
+
try:
|
|
37
|
+
# Parse and execute all source files in order
|
|
38
|
+
globals_ = dict(args.var) if args.var is not None else {}
|
|
39
|
+
for file_, source in args.files:
|
|
40
|
+
current_file = file_
|
|
41
|
+
|
|
42
|
+
# Parse the source
|
|
43
|
+
script = parse_script(source if source is not None else fetch_read_write({'url': file_}))
|
|
44
|
+
|
|
45
|
+
# Run the bare-script linter?
|
|
46
|
+
if args.static or args.debug:
|
|
47
|
+
warnings = lint_script(script)
|
|
48
|
+
warning_prefix = f'BareScript: Static analysis "{file_}" ...'
|
|
49
|
+
if not warnings:
|
|
50
|
+
print(f'{warning_prefix} OK')
|
|
51
|
+
else:
|
|
52
|
+
print(f'{warning_prefix} {len(warnings)} warning{"s" if len(warnings) > 1 else ""}:')
|
|
53
|
+
for warning in warnings:
|
|
54
|
+
print(f'BareScript: {warning}')
|
|
55
|
+
if args.static:
|
|
56
|
+
# pylint: disable=broad-exception-raised
|
|
57
|
+
raise Exception('Static analysis failed')
|
|
58
|
+
if args.static:
|
|
59
|
+
continue
|
|
60
|
+
|
|
61
|
+
# Execute the script
|
|
62
|
+
time_begin = time.time()
|
|
63
|
+
result = execute_script(script, {
|
|
64
|
+
'debug': args.debug or False,
|
|
65
|
+
'fetchFn': fetch_read_write,
|
|
66
|
+
'globals': globals_,
|
|
67
|
+
'logFn': log_print,
|
|
68
|
+
'systemPrefix': 'https://craigahobbs.github.io/markdown-up/include/',
|
|
69
|
+
'urlFn': partial(url_file_relative, file_)
|
|
70
|
+
})
|
|
71
|
+
if isinstance(result, (int, float)) and int(result) == result and 0 <= result <= 255:
|
|
72
|
+
status_code = int(result)
|
|
73
|
+
else:
|
|
74
|
+
status_code = 1 if result else 0
|
|
75
|
+
|
|
76
|
+
# Log script execution end with timing
|
|
77
|
+
if args.debug:
|
|
78
|
+
time_end = time.time()
|
|
79
|
+
print(f'BareScript: Script executed in {1000 * (time_end - time_begin):.1f} milliseconds')
|
|
80
|
+
|
|
81
|
+
# Stop on error status code
|
|
82
|
+
if status_code != 0:
|
|
83
|
+
break
|
|
84
|
+
|
|
85
|
+
except Exception as e: # pylint: disable=broad-exception-caught
|
|
86
|
+
if current_file is not None:
|
|
87
|
+
print(f'{current_file}:')
|
|
88
|
+
print(str(e))
|
|
89
|
+
status_code = 1
|
|
90
|
+
|
|
91
|
+
# Return the status code
|
|
92
|
+
sys.exit(status_code)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# Custom argparse action for script file arguments
|
|
96
|
+
class _ScriptAction(argparse.Action):
|
|
97
|
+
|
|
98
|
+
def __call__(self, parser, namespace, values, option_string=None):
|
|
99
|
+
files = getattr(namespace, 'files', [])
|
|
100
|
+
if option_string in ['-c', '--code']:
|
|
101
|
+
code_count = getattr(namespace, 'code_count', 0)
|
|
102
|
+
code_count += 1
|
|
103
|
+
setattr(namespace, 'code_count', code_count)
|
|
104
|
+
for value in values:
|
|
105
|
+
files.append((f'-c {code_count}', value))
|
|
106
|
+
elif option_string is None:
|
|
107
|
+
for value in values:
|
|
108
|
+
files.append((value, None))
|
|
109
|
+
setattr(namespace, 'files', files)
|