Browse Source

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.
Sean Dague 2 years ago
parent
commit
36b1e0ff34
3 changed files with 82 additions and 4 deletions
  1. 75
    0
      devstack/cmd.py
  2. 5
    3
      setup.cfg
  3. 2
    1
      tox.ini

+ 75
- 0
devstack/cmd.py View File

@@ -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
+
15
+import argparse
16
+import sys
17
+
18
+import devstack.dsconf
19
+
20
+
21
+def iniset(inifile, args):
22
+    inifile.set(args.section, args.name, args.value)
23
+
24
+
25
+def inirm(inifile, args):
26
+    inifile.remove(args.section, args.name)
27
+
28
+
29
+def inicomment(inifile, args):
30
+    inifile.comment(args.section, args.name)
31
+
32
+
33
+def iniuncomment(inifile, args):
34
+    inifile.uncomment(args.section, args.name)
35
+
36
+
37
+def 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
+
70
+def 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

+ 5
- 3
setup.cfg View File

@@ -13,11 +13,9 @@ classifier =
13 13
     License :: OSI Approved :: Apache Software License
14 14
     Operating System :: POSIX :: Linux
15 15
     Programming Language :: Python
16
-    Programming Language :: Python :: 2
17
-    Programming Language :: Python :: 2.7
18 16
     Programming Language :: Python :: 3
19
-    Programming Language :: Python :: 3.3
20 17
     Programming Language :: Python :: 3.4
18
+    Programming Language :: Python :: 3.5
21 19
 
22 20
 [files]
23 21
 packages =
@@ -31,6 +29,10 @@ all_files = 1
31 29
 [upload_sphinx]
32 30
 upload-dir = doc/build/html
33 31
 
32
+[entry_points]
33
+console_scripts =
34
+    dsconf = devstack.cmd:main
35
+
34 36
 [compile_catalog]
35 37
 directory = devstack/locale
36 38
 domain = devstack

+ 2
- 1
tox.ini View File

@@ -1,6 +1,6 @@
1 1
 [tox]
2 2
 minversion = 2.0
3
-envlist = py34,py27,pypy,pep8
3
+envlist = py35,pep8
4 4
 skipsdist = True
5 5
 
6 6
 [testenv]
@@ -13,6 +13,7 @@ deps = -r{toxinidir}/test-requirements.txt
13 13
 commands = python setup.py test --slowest --testr-args='{posargs}'
14 14
 
15 15
 [testenv:pep8]
16
+basepython = python3
16 17
 commands = flake8 {posargs}
17 18
 
18 19
 [testenv:venv]

Loading…
Cancel
Save