Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4249)

Unified Diff: build/gn_helpers.py

Issue 1847333005: Add FromGNArgs() to gn_helpers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix_gn_helpers_bools
Patch Set: allow lists in args Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | build/gn_helpers_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/gn_helpers.py
diff --git a/build/gn_helpers.py b/build/gn_helpers.py
index 6a61a0877f04e79ad200637515cf8efd245b711d..6aeddae5fd853c7e58def9254e5a24a289853710 100644
--- a/build/gn_helpers.py
+++ b/build/gn_helpers.py
@@ -103,6 +103,27 @@ def FromGNString(input):
return parser.Parse()
+def FromGNArgs(input):
+ """Converts a string with a bunch of gn arg assignments into a Python dict.
+
+ Given a whitespace-separated list of
+
+ <ident> = (integer | string | boolean | <list of the former>)
+
+ gn assignments, this returns a Python dict, i.e.:
+
+ FromGNArgs("foo=true\nbar=1\n") -> { 'foo': True, 'bar': 1 }.
+
+ Only simple types and lists supported; variables, structs, calls
+ and other, more complicated things are not.
+
+ This routine is meant to handle only the simple sorts of values that
+ arise in parsing --args.
+ """
+ parser = GNValueParser(input)
+ return parser.ParseArgs()
+
+
def UnescapeGNString(value):
"""Given a string with GN escaping, returns the unescaped string.
@@ -174,6 +195,27 @@ class GNValueParser(object):
self.input[self.cur:])
return result
+ def ParseArgs(self):
+ """Converts a whitespace-separated list of ident=literals to a dict.
+
+ See additional usage notes on FromGNArgs, above.
+ """
+ d = {}
+
+ self.ConsumeWhitespace()
+ while not self.IsDone():
+ ident = self._ParseIdent()
+ self.ConsumeWhitespace()
+ if self.input[self.cur] != '=':
+ raise GNException("Unexpected token: " + self.input[self.cur:])
+ self.cur += 1
+ self.ConsumeWhitespace()
+ val = self._ParseAllowTrailing()
+ self.ConsumeWhitespace()
+ d[ident] = val
+
+ return d
+
def _ParseAllowTrailing(self):
"""Internal version of Parse that doesn't check for trailing stuff."""
self.ConsumeWhitespace()
@@ -194,6 +236,24 @@ class GNValueParser(object):
else:
raise GNException("Unexpected token: " + self.input[self.cur:])
+ def _ParseIdent(self):
+ id = ''
+
+ next_char = self.input[self.cur]
+ if not next_char.isalpha() and not next_char=='_':
+ raise GNException("Expected an identifier: " + self.input[self.cur:])
+
+ id += next_char
+ self.cur += 1
+
+ next_char = self.input[self.cur]
+ while next_char.isalpha() or next_char.isdigit() or next_char=='_':
+ id += next_char
+ self.cur += 1
+ next_char = self.input[self.cur]
+
+ return id
+
def ParseNumber(self):
self.ConsumeWhitespace()
if self.IsDone():
« no previous file with comments | « no previous file | build/gn_helpers_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698