Skip to content

Commit 215e462

Browse files
committed
Merge branch 'master' of github.com:nicfit/nicfit.py
* 'master' of github.com:nicfit/nicfit.py: Some docs, and example commands.
2 parents f80f8dd + f49ac42 commit 215e462

File tree

8 files changed

+89
-17
lines changed

8 files changed

+89
-17
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ test: gettext
9090
pytest $(_PYTEST_OPTS) $(_PDB_OPTS) ${TEST_DIR}
9191

9292
test-all:
93+
for example in `ls ./examples/*.py`; do \
94+
echo "Runninig $$example..."; \
95+
./$$example > /dev/null ; \
96+
done
9397
tox
9498

9599
coverage: gettext

README.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
nicfit.py README
33
=================
44

5-
Common Python utils (App, logging, config, etc.)
5+
Basic building blocks for building Python applications.
66

77
Status
88
------
@@ -28,4 +28,11 @@ Status
2828
Features
2929
--------
3030

31+
* Common appplication skeletons (including async) providing common argument
32+
parsing add-ons and utilities, error handling boiler-plate, and application
33+
life-cycle hooks. Including intregrated `cookiecutter` that
34+
suuports updating to future versions with auto-merging.
35+
* Simple command API based on :mod:`argparse` and sub-parsers.
36+
* Integrated configuation file (based on configparser) utitiles.
37+
* Logging extras (e.g. verbose level) and command line configuration.
3138
* Free software: MIT license

cookiecutter/{{cookiecutter.project_name}}/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.idea
1+
.idea/
22
.cookiecutter.md5
33
_misc/
44

examples/asyncio_example.py

100644100755
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import nicfit
1+
#!/usr/bin/env python
2+
import time
23
from nicfit.aio import Application
34

45
async def _main(args):
5-
import time
66
print(args)
7-
time.sleep(5)
8-
return 5
7+
print("Sleeping 2...")
8+
time.sleep(2)
9+
print("Sleeping 0...")
10+
return 0
911

1012
def atexit():
1113
print("atexit")

examples/commands.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
import nicfit
3+
4+
@nicfit.Command.register
5+
class Hi(nicfit.Command):
6+
NAME = "hi"
7+
ALIAS = ["hello"]
8+
HELP = "A simple hello command."
9+
DESC = "This command can speak Spanish a command line option."
10+
11+
def _initArgParser(self, parser):
12+
parser.add_argument("--spanish", action="store_true",
13+
help="Respond in Spanish")
14+
15+
def _run(self):
16+
print("Hola" if self.args.spanish else "Hi")
17+
18+
@nicfit.Command.register
19+
class bye(nicfit.Command):
20+
HELP = "A simple hello command."
21+
def _run(self):
22+
print("Bye")
23+
24+
# Until Python 3.7 ArgumentParser.add_subparsers does not support the `required`
25+
# keyword, nicfit.ArgumentParser handles this.
26+
parser = nicfit.ArgumentParser()
27+
nicfit.Command.loadCommandMap(parser.add_subparsers(dest="cmd", required=True))
28+
args = parser.parse_args()
29+
args.command_func(args)
30+
31+
"""
32+
$ ./cmds2.py
33+
usage: cmds2.py [-h] {bye,hi} ...
34+
cmds2.py: error: the following arguments are required: cmd
35+
$ ./cmds2.py --help
36+
usage: cmds2.py [-h] {hi,bye} ...
37+
38+
positional arguments:
39+
{hi,bye}
40+
hi A simple hello command.
41+
bye A simple hello command.
42+
43+
optional arguments:
44+
-h, --help show this help message and exit
45+
$ ./cmds2.py hi --help
46+
usage: cmds2.py hi [-h] [--spanish]
47+
48+
This command can speak Spanish a command line option.
49+
50+
optional arguments:
51+
-h, --help show this help message and exit
52+
--spanish Respond in Spanish
53+
$ ./cmds2.py hi --spanish
54+
Hola
55+
"""

examples/curio_example.py

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import nicfit
1+
#!/usr/bin/env python
2+
import time
23
from nicfit.curio import Application
34

45
async def _main(args):
5-
import time
66
print(args)
7-
time.sleep(5)
8-
return 5
7+
time.sleep(2)
8+
return 0
99

1010
def atexit():
1111
print("atexit")

examples/example.py

100644100755
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1+
#!/usr/bin/env python
12
import time
23
import nicfit
34

45
def main(args):
5-
print(args)
6+
print("Parsed command line args:", args)
67
try:
7-
time.sleep(15)
8+
print("Sleeping 5, Ctrl-C to interupt...")
9+
time.sleep(10)
810
except KeyboardInterrupt:
9-
raise ValueError()
10-
return 1
11+
print("Returning 2...")
12+
return 2
1113
else:
14+
print("Returning 0...")
1215
return 0
1316

1417
def atexit():
15-
print("in case you care")
18+
print("atexit, in case you care")
1619

1720

1821
app = nicfit.Application(main, name="example", description="Just an example",
19-
version="1.0.0-alpha",
20-
atexit=atexit, pdb_opt=True)
22+
version="1.0a0", atexit=atexit, pdb_opt=True)
2123
app.arg_parser.add_argument("--example", help="Example cli")
2224
app.run()
2325
assert not"will not execute"

nicfit/_argparse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sys
22
import argparse
3+
import gettext
4+
_ = gettext.gettext
35

46

57
class ArgumentParser(argparse.ArgumentParser):

0 commit comments

Comments
 (0)