test suite reviews and discussions
 help / color / mirror / Atom feed
From: Yong Liu <yong.liu@intel.com>
To: dts@dpdk.org
Subject: [dts] [PATCH] Add parse option module in DTS tools
Date: Tue, 11 Aug 2015 09:46:07 +0800	[thread overview]
Message-ID: <1439257567-9798-1-git-send-email-yong.liu@intel.com> (raw)

From: Marvin Liu <yong.liu@intel.com>

This module support new simple way to interact with user. Now it support
several types of inputs like  "string", "int", "bool", "chice", "multichoice".
With these inputs, user can chose in several options or just keyboard the
input.

Signed-off-by: Marvin Liu <yong.liu@intel.com>

diff --git a/tools/parse_opt.py b/tools/parse_opt.py
new file mode 100755
index 0000000..e433d3f
--- /dev/null
+++ b/tools/parse_opt.py
@@ -0,0 +1,193 @@
+#!/usr/bin/python
+import sys
+import os.path
+import re
+
+
+def RED(text):
+    return "\x1B[" + "31;1m" + str(text) + "\x1B[" + "0m"
+
+
+def GREEN(text):
+    return "\x1B[" + "32;1m" + str(text) + "\x1B[" + "0m"
+
+
+OPTION_TYPES = ['string', 'ip', 'int', 'bool', 'choice', 'multichoice']
+
+
+class Option(object):
+
+    def __init__(self, **args):
+        self.args = args
+
+        if 'prompt' in args.keys():
+            self.prompt = args['prompt'] + ': '
+        else:
+            self.prompt = "Please input value: "
+        if 'type' in args.keys():
+            opt_type = args['type']
+            if opt_type not in OPTION_TYPES:
+                print RED('Invalid option type!!!')
+                raise ValueError
+            else:
+                self.opt_type = opt_type
+        else:
+            self.opt_type = 'string'
+        if self.opt_type == 'bool':
+            self.prompt += '[Yes/No]'
+
+        if 'help' in args.keys():
+            self.help_msg = args['help']
+        else:
+            self.help_msg = ''
+        if 'options' in args.keys():
+            self.opts = args['options']
+
+        if 'default' in args.keys():
+            self.default_value = args['default']
+            self.prompt += ' Default is [%s]' % self.default_value
+        else:
+            self.default_value = ''
+
+        if self.check_args() is False:
+            raise ValueError
+
+        self.prompt += '->'
+
+    def check_args(self):
+        if self.opt_type == 'choice':
+            if 'options' not in self.args.keys():
+                print RED("Choice option request options list!!!")
+                return False
+
+            if type(self.opts) != list:
+                return False
+
+        return True
+
+    def __print_options(self):
+        index = 0
+        for opt in self.opts:
+            print GREEN("%2d: %s" % (index, str(opt)))
+            index += 1
+
+    def parse_input(self):
+        parse_done = False
+
+        print GREEN(self.help_msg)
+
+        while not parse_done:
+            try:
+                if self.opt_type == 'choice' or self.opt_type == 'multichoice':
+                    self.__print_options()
+
+                input_val = raw_input(self.prompt)
+                if input_val == '':
+                    input_val = self.default_value
+                    print GREEN("Chose default [%s]" % input_val)
+
+                if self.opt_type == 'int':
+                    self.value = int(input_val)
+                elif self.opt_type == 'bool':
+                    opt = input_val.upper()
+                    if opt not in ['YES', 'NO']:
+                        raise ValueError
+                    if opt == 'YES':
+                        self.value = True
+                    elif opt == 'NO':
+                        self.value = False
+                elif self.opt_type == 'string':
+                    self.value = input_val
+                elif self.opt_type == 'choice':
+                    try:
+                        index = int(input_val)
+                    except:
+                        print RED("Invalid option index!!!")
+                        raise ValueError
+
+                    if index >= len(self.opts):
+                        print RED("Choice index should be 0-%d!!!"
+                                  % len(self.opts))
+                        raise ValueError
+                    self.value = self.opts[index]
+                    self.choice = index
+                elif self.opt_type == 'multichoice':
+                    if input_val == 'all':
+                        self.value = self.opts
+                        break
+                    try:
+                        sel_indexs = []
+                        sel_options = []
+                        input_val.strip()
+                        input_arrs = input_val.split(',')
+                        for sel_index in input_arrs:
+                            if '-' in sel_index:
+                                indexs = sel_index.split('-')
+                                start = int(indexs[0])
+                                end = int(indexs[1])
+                                if end <= start:
+                                    print RED("Choice end number must be "
+                                              "larger than start number!!!")
+                                    raise ValueError
+
+                                for index in range(start, end + 1):
+                                    sel_indexs.append(index)
+                            else:
+                                index = int(sel_index)
+
+                                sel_indexs.append(index)
+                        # sorted list
+                        sel_indexs = list(set(sorted(sel_indexs)))
+                        for index in sel_indexs:
+                            if index >= len(self.opts):
+                                print RED("Choice index should be 0-%d!!!"
+                                          % len(self.opts))
+                            sel_options.append(self.opts[index])
+                        self.value = sel_options
+                    except:
+                        print RED("Invalid option!!!")
+                        raise ValueError
+
+                elif self.opt_type == 'ip':
+                    ip_reg = r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}'
+                    m = re.match(ip_reg, input_val)
+                    if m:
+                        self.value = input_val
+                    else:
+                        raise ValueError
+
+            except Exception as e:
+                if type(e) is ValueError:
+                    print "Options parse failure"
+                    continue
+
+            parse_done = True
+
+        print ('')
+
+        return self.value
+
+
+if __name__ == "__main__":
+    option = {'prompt': 'first option',
+              'type': 'string', 'help': 'help message',
+              'default': 'DEFAULT'}
+    string_opt = Option(**option)
+    print string_opt.parse_input()
+    option = {'prompt': 'bool option', 'type': 'bool',
+              'help': 'bool option [Yes/No] only',
+              'default': 'No'}
+    bool_opt = Option(**option)
+    print bool_opt.parse_input()
+    option = {'prompt': 'choice option', 'type': 'choice',
+              'help': 'choice option in [1, 2 ,3]',
+              'options': ['option1', 'option2', 'option3'],
+              'default': '0'}
+    choice_opt = Option(**option)
+    print choice_opt.parse_input()
+    option = {'prompt': 'multichoice option', 'type': 'multichoice',
+              'help': 'mutli choice option in [1, 2 ,3]',
+              'options': ['option1', 'option2', 'option3'],
+              'default': '0'}
+    multichoice_opt = Option(**option)
+    print multichoice_opt.parse_input()
-- 
1.9.3

                 reply	other threads:[~2015-08-11  1:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1439257567-9798-1-git-send-email-yong.liu@intel.com \
    --to=yong.liu@intel.com \
    --cc=dts@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).