diff --git a/README.rst b/README.rst index fea33f7..275bf6c 100644 --- a/README.rst +++ b/README.rst @@ -30,3 +30,13 @@ next summit is for "Rocky":: +---------+-------+ | Summits | 13 | +---------+-------+ + +Can also be used for counting PTGs. For example:: + + $ openstack ptg count pike rocky + + +-------+-------+ + | Field | Value | + +-------+-------+ + | PTGs | 3 | + +-------+-------+ diff --git a/setup.cfg b/setup.cfg index fc431cb..571a7d6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,6 +22,8 @@ packages = openstack.cli = summit count = summit_counter.main:SummitCount summit list = summit_counter.main:SummitList + ptg count = summit_counter.ptg:PtgCount + ptg list = summit_counter.ptg:PtgList [wheel] universal = 1 diff --git a/summit_counter/ptg.py b/summit_counter/ptg.py new file mode 100644 index 0000000..30a6e3e --- /dev/null +++ b/summit_counter/ptg.py @@ -0,0 +1,66 @@ +# 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. + +from cliff import lister +from cliff import show + +_PTGS = [ + ('Pike', 'Atlanta'), + ('Queens', 'Denver'), + ('Rocky', 'Dublin'), +] + + +class PtgCount(show.ShowOne): + + # Set the command so that when we run through + # python-openstackclient we do not require authentication. + auth_required = False + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + choices = [s[0].lower() for s in _PTGS] + parser.add_argument( + 'first', + help='Your first PTG', + choices=choices, + ) + parser.add_argument( + 'current', + help='The current PTG', + choices=choices, + nargs='?', + default=choices[-1], + ) + return parser + + def _name_to_num(self, name): + return ord(name.lower()[0]) - ord('a') + 1 + + def take_action(self, parsed_args): + first = self._name_to_num(parsed_args.first) + current = self._name_to_num(parsed_args.current) + return (('PTGs',), (current - first + 1,)) + + +class PtgList(lister.Lister): + + # Set the command so that when we run through + # python-openstackclient we do not require authentication. + auth_required = False + + def take_action(self, parsed_args): + columns = ('Number', 'Name', 'Location') + summits = [] + for i, s in enumerate(_PTGS, 1): + summits.append((i,) + s) + return (columns, summits)