summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Dague <sean@dague.net>2017-01-16 16:38:07 -0500
committerSean Dague <sean@dague.net>2017-01-16 16:38:07 -0500
commit36b1e0ff346ee674d5fde71f8517ed9b4f65eaa5 (patch)
tree4e98b0ba9d578c8357502f69d51ddc0dde2d8cbe
parent671f526e41228ad9b3e34d449f7df56cc9cfb64c (diff)
start adding parser, only support python3
python3 argparse adds native subparsers, which makes this much easier to build without external dependencies (which improves load and run time). 20ms for most operations means we can do 50 / sec. This will be a slow down from the awk approach, but for larger gains in readability.
-rw-r--r--devstack/cmd.py75
-rw-r--r--setup.cfg8
-rw-r--r--tox.ini3
3 files changed, 82 insertions, 4 deletions
diff --git a/devstack/cmd.py b/devstack/cmd.py
new file mode 100644
index 0000000..97149bd
--- /dev/null
+++ b/devstack/cmd.py
@@ -0,0 +1,75 @@
1# Copyright 2017 IBM
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import argparse
16import sys
17
18import devstack.dsconf
19
20
21def iniset(inifile, args):
22 inifile.set(args.section, args.name, args.value)
23
24
25def inirm(inifile, args):
26 inifile.remove(args.section, args.name)
27
28
29def inicomment(inifile, args):
30 inifile.comment(args.section, args.name)
31
32
33def iniuncomment(inifile, args):
34 inifile.uncomment(args.section, args.name)
35
36
37def parse_args(argv):
38 parser = argparse.ArgumentParser(prog='dsconf')
39 subparsers = parser.add_subparsers(title='commands',
40 help='sub-command help')
41
42 parser_iniset = subparsers.add_parser('iniset', help='iniset help')
43 parser_iniset.set_defaults(func=iniset)
44 parser_iniset.add_argument('inifile', help='name of file')
45 parser_iniset.add_argument('section', help='name of section')
46 parser_iniset.add_argument('name', help='name')
47 parser_iniset.add_argument('value', help='value')
48
49 parser_inicomment = subparsers.add_parser('inicomment', help='inicomment help')
50 parser_inicomment.set_defaults(func=inicomment)
51 parser_inicomment.add_argument('inifile', help='name of file')
52 parser_inicomment.add_argument('section', help='name of section')
53 parser_inicomment.add_argument('name', help='name')
54
55 parser_iniuncomment = subparsers.add_parser('iniuncomment', help='iniuncomment help')
56 parser_iniuncomment.set_defaults(func=iniuncomment)
57 parser_iniuncomment.add_argument('inifile', help='name of file')
58 parser_iniuncomment.add_argument('section', help='name of section')
59 parser_iniuncomment.add_argument('name', help='name')
60
61 parser_inirm = subparsers.add_parser('inirm', help='inirm help')
62 parser_inirm.set_defaults(func=inirm)
63 parser_inirm.add_argument('inifile', help='name of file')
64 parser_inirm.add_argument('section', help='name of section')
65 parser_inirm.add_argument('name', help='name')
66
67 return parser.parse_args()
68
69
70def main(argv=None):
71 args = parse_args(argv or sys.argv)
72 if hasattr(args, 'inifile'):
73 f = devstack.dsconf.IniFile(args.inifile)
74 args.func(f, args)
75 return
diff --git a/setup.cfg b/setup.cfg
index 3ec8f6d..213e948 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -13,11 +13,9 @@ classifier =
13 License :: OSI Approved :: Apache Software License 13 License :: OSI Approved :: Apache Software License
14 Operating System :: POSIX :: Linux 14 Operating System :: POSIX :: Linux
15 Programming Language :: Python 15 Programming Language :: Python
16 Programming Language :: Python :: 2
17 Programming Language :: Python :: 2.7
18 Programming Language :: Python :: 3 16 Programming Language :: Python :: 3
19 Programming Language :: Python :: 3.3
20 Programming Language :: Python :: 3.4 17 Programming Language :: Python :: 3.4
18 Programming Language :: Python :: 3.5
21 19
22[files] 20[files]
23packages = 21packages =
@@ -31,6 +29,10 @@ all_files = 1
31[upload_sphinx] 29[upload_sphinx]
32upload-dir = doc/build/html 30upload-dir = doc/build/html
33 31
32[entry_points]
33console_scripts =
34 dsconf = devstack.cmd:main
35
34[compile_catalog] 36[compile_catalog]
35directory = devstack/locale 37directory = devstack/locale
36domain = devstack 38domain = devstack
diff --git a/tox.ini b/tox.ini
index c888992..efec50e 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,6 +1,6 @@
1[tox] 1[tox]
2minversion = 2.0 2minversion = 2.0
3envlist = py34,py27,pypy,pep8 3envlist = py35,pep8
4skipsdist = True 4skipsdist = True
5 5
6[testenv] 6[testenv]
@@ -13,6 +13,7 @@ deps = -r{toxinidir}/test-requirements.txt
13commands = python setup.py test --slowest --testr-args='{posargs}' 13commands = python setup.py test --slowest --testr-args='{posargs}'
14 14
15[testenv:pep8] 15[testenv:pep8]
16basepython = python3
16commands = flake8 {posargs} 17commands = flake8 {posargs}
17 18
18[testenv:venv] 19[testenv:venv]