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.
This commit is contained in:
Sean Dague 2017-01-16 16:38:07 -05:00
parent 671f526e41
commit 36b1e0ff34
3 changed files with 82 additions and 4 deletions

75
devstack/cmd.py Normal file
View File

@ -0,0 +1,75 @@
# Copyright 2017 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import sys
import devstack.dsconf
def iniset(inifile, args):
inifile.set(args.section, args.name, args.value)
def inirm(inifile, args):
inifile.remove(args.section, args.name)
def inicomment(inifile, args):
inifile.comment(args.section, args.name)
def iniuncomment(inifile, args):
inifile.uncomment(args.section, args.name)
def parse_args(argv):
parser = argparse.ArgumentParser(prog='dsconf')
subparsers = parser.add_subparsers(title='commands',
help='sub-command help')
parser_iniset = subparsers.add_parser('iniset', help='iniset help')
parser_iniset.set_defaults(func=iniset)
parser_iniset.add_argument('inifile', help='name of file')
parser_iniset.add_argument('section', help='name of section')
parser_iniset.add_argument('name', help='name')
parser_iniset.add_argument('value', help='value')
parser_inicomment = subparsers.add_parser('inicomment', help='inicomment help')
parser_inicomment.set_defaults(func=inicomment)
parser_inicomment.add_argument('inifile', help='name of file')
parser_inicomment.add_argument('section', help='name of section')
parser_inicomment.add_argument('name', help='name')
parser_iniuncomment = subparsers.add_parser('iniuncomment', help='iniuncomment help')
parser_iniuncomment.set_defaults(func=iniuncomment)
parser_iniuncomment.add_argument('inifile', help='name of file')
parser_iniuncomment.add_argument('section', help='name of section')
parser_iniuncomment.add_argument('name', help='name')
parser_inirm = subparsers.add_parser('inirm', help='inirm help')
parser_inirm.set_defaults(func=inirm)
parser_inirm.add_argument('inifile', help='name of file')
parser_inirm.add_argument('section', help='name of section')
parser_inirm.add_argument('name', help='name')
return parser.parse_args()
def main(argv=None):
args = parse_args(argv or sys.argv)
if hasattr(args, 'inifile'):
f = devstack.dsconf.IniFile(args.inifile)
args.func(f, args)
return

View File

@ -13,11 +13,9 @@ classifier =
License :: OSI Approved :: Apache Software License
Operating System :: POSIX :: Linux
Programming Language :: Python
Programming Language :: Python :: 2
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3
Programming Language :: Python :: 3.3
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
[files]
packages =
@ -31,6 +29,10 @@ all_files = 1
[upload_sphinx]
upload-dir = doc/build/html
[entry_points]
console_scripts =
dsconf = devstack.cmd:main
[compile_catalog]
directory = devstack/locale
domain = devstack

View File

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