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

Unified Diff: Source/core/scripts/in_file.py

Issue 14696003: in_file.py should be able to parse "in" files (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 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 | Source/core/scripts/in_file_unittest.py » ('j') | Source/core/scripts/in_file_unittest.py » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/scripts/in_file.py
diff --git a/Source/core/scripts/in_file.py b/Source/core/scripts/in_file.py
index c49ea7188ebd358b5b6b7780910d06c9238016f1..fa7106461ae3bb21df9380a6d51cb03decbe20a6 100644
--- a/Source/core/scripts/in_file.py
+++ b/Source/core/scripts/in_file.py
@@ -48,24 +48,51 @@ import os
#
# Parsing produces an array of dictionaries:
# [ { 'name' : 'name1', 'arg' :' value', arg2=['value2', 'value3'] }
+
+def _is_comment(line):
+ return line.startswith("//") or line.startswith("#")
+
class InFile(object):
- def __init__(self, lines, defaults):
- lines = map(str.strip, lines)
- lines = filter(lambda line: line and not line.startswith("//"), lines)
- self.name_dictionaries = [self._parse_line(line, defaults) for line in lines]
+ def __init__(self, lines, defaults, default_parameters):
+ self.name_dictionaries = []
+ self.parameters = copy.deepcopy(default_parameters if default_parameters else {})
+ self._defaults = defaults
+ self._parse(map(str.strip, lines))
@classmethod
- def load_from_path(self, path, defaults):
+ def load_from_path(self, path, defaults, default_parameters):
with open(os.path.abspath(path)) as in_file:
- return InFile(in_file.readlines(), defaults)
+ return InFile(in_file.readlines(), defaults, default_parameters)
def _is_sequence(self, arg):
return (not hasattr(arg, "strip")
and hasattr(arg, "__getitem__")
or hasattr(arg, "__iter__"))
- def _parse_line(self, line, defaults):
- args = copy.deepcopy(defaults)
+ def _parse(self, lines):
+ parsing_parameters = True
+ for line in lines:
+ if _is_comment(line):
+ continue
+ if not line:
eseidel 2013/04/30 21:46:16 So a leading newline (between the comments and the
abarth-chromium 2013/04/30 21:50:44 I'm just matching the behavior of the Perl "in" fi
+ parsing_parameters = False
+ continue
+ if parsing_parameters:
+ self._parse_parameter(line)
+ else:
+ self.name_dictionaries.append(self._parse_line(line))
+
+ def _parse_parameter(self, line):
+ if '=' in line:
+ name, value = line.split('=')
+ else:
+ name, value = line, True
eseidel 2013/04/30 21:46:16 If we don't support bare parameters, then paramete
abarth-chromium 2013/04/30 21:50:44 The Perl supports parameters that lack a =. I don
+ if not name in self.parameters:
+ self._fatal("Unknown parameter: '%s' in line:\n%s\nKnown parameters: %s" % (name, line, self.parameters.keys()))
+ self.parameters[name] = value
+
+ def _parse_line(self, line):
+ args = copy.deepcopy(self._defaults)
parts = line.split(' ')
args['name'] = parts[0]
# re-join the rest of the line and split on ','
@@ -78,12 +105,15 @@ class InFile(object):
arg_name, arg_value = arg_string.split('=')
else:
arg_name, arg_value = arg_string, True
- if arg_name not in defaults:
- # FIXME: This should probably raise instead of exit(1)
- print "Unknown argument: '%s' in line:\n%s\nKnown arguments: %s" % (arg_name, line, defaults.keys())
- exit(1)
+ if arg_name not in self._defaults:
+ self._fatal("Unknown argument: '%s' in line:\n%s\nKnown arguments: %s" % (arg_name, line, self._defaults.keys()))
if self._is_sequence(args[arg_name]):
args[arg_name].append(arg_value)
else:
args[arg_name] = arg_value
return args
+
+ def _fatal(self, message):
+ # FIXME: This should probably raise instead of exit(1)
+ print message
+ exit(1)
« no previous file with comments | « no previous file | Source/core/scripts/in_file_unittest.py » ('j') | Source/core/scripts/in_file_unittest.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698