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

Unified Diff: scripts/slave/recipe_config_types.py

Issue 24737002: Add Paths as first-class types in configs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: Created 7 years, 3 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
Index: scripts/slave/recipe_config_types.py
diff --git a/scripts/slave/recipe_config_types.py b/scripts/slave/recipe_config_types.py
new file mode 100644
index 0000000000000000000000000000000000000000..08e9ff95fa1085e923a2291cfa4e99e7b9dbc2c5
--- /dev/null
+++ b/scripts/slave/recipe_config_types.py
@@ -0,0 +1,52 @@
+import re
+
+def ResetHandlers():
+ RecipeConfigType._HANDLER_MAP.clear() # pylint: disable=W0212
+
+
+def json_fixup(obj):
+ if isinstance(obj, RecipeConfigType):
+ return str(obj)
+ raise TypeError("%r is not JSON serializable" % obj)
+
+
+class RecipeConfigType(object):
agable 2013/09/26 21:46:02 docstrings docstrings docstrings.
iannucci 2013/09/27 02:08:20 Done.
+ _HANDLER_MAP = {}
+
+ def handle(self):
+ return self._HANDLER_MAP.get(self.__class__.__name__,
+ self.__class__.default_handler)(self)
+
+ @classmethod
+ def set_handler(cls, new_handler):
+ assert cls.__name__ not in cls._HANDLER_MAP, (
+ 'Handler already installed for %s' % cls)
+ cls._HANDLER_MAP[cls.__name__] = new_handler
+
+ def default_handler(self, *args, **kwargs):
agable 2013/09/26 21:46:02 default_tostring maybe? Not clear what it means to
iannucci 2013/09/27 02:08:20 how about default_tostring_fn . Makes sense to tal
+ raise NotImplementedError
+
+ def __str__(self):
+ return self.handle()
+
+
+class Path(RecipeConfigType):
+ BASE_RE = re.compile("[a-z][a-z_]*")
agable 2013/09/26 21:46:02 This RE seems incredibly restrictive. Also, (due t
iannucci 2013/09/27 02:08:20 docstrings should clear this up more, added commen
+
+ def __init__(self, base=None, *pieces, **kwargs):
+ super(Path, self).__init__()
agable 2013/09/26 21:46:02 Should pass kwargs to init. Otherwise there's no r
iannucci 2013/09/27 02:08:20 I would love to have wrapper=False at the end here
+ assert '..' not in pieces
+ assert self.BASE_RE.match(base)
+
+ self.base = base
+ self.pieces = pieces
+ self.wrapper = kwargs.get('wrapper', False)
agable 2013/09/26 21:46:02 Since there's only one wrapper function, and it is
iannucci 2013/09/27 02:08:20 Ok, changed this to a more generic fixed-function
+
+ def __call__(self, *pieces, **kwargs):
+ wrapper = kwargs.get('wrapper', self.wrapper)
+ new_pieces = filter(bool, self.pieces + pieces)
+ return Path(self.base, *new_pieces, wrapper=wrapper)
+
+ def default_handler(self):
+ suffix = ', wrapper=True' if self.wrapper else ''
+ return 'Path([%s], %r%s)' % (self.base.upper(), self.pieces, suffix)
agable 2013/09/26 21:58:08 Path([ROOT], piece, piece, piece) looks better tha
iannucci 2013/09/27 02:09:20 Since I changed the signature of this class, I dec

Powered by Google App Engine
This is Rietveld 408576698