Skip to content

Commit

Permalink
Rudimentary Command Line Interface (#25)
Browse files Browse the repository at this point in the history
* Add a rudamentary CLI

* Update README to prefer CLI for generating stubs.
  • Loading branch information
BrianPugh authored Oct 27, 2023
1 parent ffa06ca commit 08890e4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ uStubby is targeted to run on Python 3.7, but should run on versions 3.6 or grea
### Installing

Currently, there are no external dependencies for running uStubby.
Clone the repository using git and just put it on the path to install.
Alternatively, install from PyPI with
Install from PyPI with:
```bash
pip install ustubby
```
Alternatively, clone the repository using git and just put it on the path to install.

## Usage
This example follows generating the template as shown [here](http://docs.micropython.org/en/latest/develop/cmodules.html#basic-example)
Expand All @@ -39,7 +39,14 @@ def add_ints(a: int, b: int) -> int:
:param b:
:return:a + b"""
```
We can then convert this into the appropriate c stub by running
We can then convert this into the appropriate c stub via the CLI:
```bash
# This will generate the file "example.c"
ustubby example.py
```

Alternatively, you can invoke the python interface in a script:

```python
import ustubby
import example
Expand Down Expand Up @@ -150,13 +157,7 @@ def foo(*args, **kwargs):
MP_DEFINE_CONST_FUN_OBJ_KW
```
Each successively increasing the boiler plate to conveniently accessing the variables.
Using the same code to parse it
```python
import ustubby
import example

print(ustubby.stub_module(example))
```
<details><summary>Output</summary><p>

```c
Expand Down
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def get_dependencies():
long_description_content_type="text/markdown",
package_dir={'': 'src'},
packages=find_packages('src'),
entry_points={
'console_scripts': [
'ustubby = ustubby.__main__:main'
]
},
url="https://github.com/pazzarpj/micropython-ustubby",
version=get_version(),
python_requires='>=3.6',
Expand Down
1 change: 0 additions & 1 deletion src/ustubby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ def ret_val_init(ret_type):


def ret_val_return(ret_type):
print('ret_type', ret_type)
return return_handler[ret_type]


Expand Down
58 changes: 58 additions & 0 deletions src/ustubby/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import argparse
import importlib
import sys
from pathlib import Path

import ustubby


def main() -> int:
parser = argparse.ArgumentParser(description="Converts a python file into micropython c extension stubs.")
parser.add_argument("input", type=Path,
help="Python file to convert.")
parser.add_argument("-o", "--output", type=Path, default=None,
help="Output C file. Defaults to \"${input}.c\".")
parser.add_argument("--overwrite", action="store_true",
help="Overwrite output file if it already exists.")
args = parser.parse_args()

########################################
# Preprocess and error-check arguments #
########################################
if not args.input.exists():
print(f"{args.input} does not exist.")
return 1

if args.input.suffix != ".py":
print(f"{args.input} is not a \".py\" file.")
return 1

args.input = args.input.expanduser().resolve()

if args.output is None:
args.output = args.input.with_suffix(".c")

if args.output.suffix != ".c":
print(f"{args.output} is not a \".c\" file.")
return 1

if args.output.exists() and not args.overwrite:
print(f"{args.output} already exists.")
return 1

###################
# Execute ustubby #
###################
sys.path.insert(0, str(args.input.parent))

module = importlib.import_module(args.input.stem)

c_output = ustubby.stub_module(module)

args.output.parent.mkdir(exist_ok=True, parents=True)
args.output.write_text(c_output)

return 0

if __name__ == "__main__":
sys.exit(main())

0 comments on commit 08890e4

Please sign in to comment.