summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Magnusson <ulfalizer@gmail.com>2012-12-11 09:42:12 +0100
committerUlf Magnusson <ulfalizer@gmail.com>2012-12-11 09:42:12 +0100
commitdf05d2f0251bea2c7842bebc1d2c316e3e4b8fdd (patch)
treec8772f77b82c5f545828926f5a1ea3b6350c9aa3
parent65639b40bd90d79afc1ffdcada926c99e6954102 (diff)
Add a grep'ing example.
-rw-r--r--examples/help_grep.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/examples/help_grep.py b/examples/help_grep.py
new file mode 100644
index 0000000..93b086a
--- /dev/null
+++ b/examples/help_grep.py
@@ -0,0 +1,52 @@
+# Does a case-insensitive search for a string in the help texts for symbols and
+# choices and the titles of menus and comments. Prints the matching items
+# together with their locations and the matching text. Used like
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/help_grep.py SCRIPT_ARG=<search text>
+
+import kconfiglib
+import sys
+
+if len(sys.argv) < 3:
+ print 'Pass search string with SCRIPT_ARG="search string"'
+ sys.exit(1)
+search_string = sys.argv[2].lower()
+
+conf = kconfiglib.Config(sys.argv[1])
+
+for item in conf.get_symbols() +\
+ conf.get_choices() + conf.get_menus() + conf.get_comments():
+ if item.is_symbol() or item.is_choice():
+ text = item.get_help()
+ elif item.is_menu():
+ text = item.get_title()
+ else:
+ # Comment
+ text = item.get_text()
+
+ # Case-insensitive search
+ if text is not None and search_string in text.lower():
+ if item.is_symbol() or item.is_choice():
+ # Indent lines in help text. (There might be a nicer way. :)
+ text = "\n".join([" " + s for s in text.splitlines()])
+
+ # Don't worry about symbols/choices defined in multiple locations to
+ # keep things simple
+ fname, linenr = item.get_def_locations()[0]
+ if item.is_symbol():
+ print "config {0} at {1}:{2}:\n{3}".\
+ format(item.get_name(), fname, linenr, text)
+ elif item.is_choice():
+ print "choice at {0}:{1}:\n{2}".\
+ format(fname, linenr, text)
+
+ else:
+ # Menu or comment
+ fname, linenr = item.get_location()
+ if item.is_menu():
+ print 'menu "{0}" at {1}:{2}'.\
+ format(text, fname, linenr)
+ else:
+ # Comment
+ print 'comment "{0}" at {1}:{2}'.\
+ format(text, fname, linenr)