chromeos-config: Pass schema to CrosConfigValidator constructor

Rather than passing the schema to the Start() method, pass it to the
constructor. We are not planning to pass different schemas, and this
allows us to return a CrosConfigValidator with the schema set up and
ready to use.

Add a GetValidator() function for this. This will be called by
libcros_config_host to access schema information.

BUG=chromium:769575
TEST=PYTHONPATH=~/cosarm \
   python cros_config_host_py/libcros_config_host_unittest.py;
PYTHONPATH=~/cosarm \
   python validate/validate_config_unittest.py

Change-Id: I06ce2683b7fa4309d6bc9e19825fdb2384d0cf7c
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/729135
Reviewed-by: Jason Clinton <jclinton@chromium.org>
diff --git a/chromeos-config/validate/validate_config.py b/chromeos-config/validate/validate_config.py
index cdada16..25971b0 100755
--- a/chromeos-config/validate/validate_config.py
+++ b/chromeos-config/validate/validate_config.py
@@ -82,7 +82,7 @@
 
 class CrosConfigValidator(object):
   """Validator for the master configuration"""
-  def __init__(self, raise_on_error):
+  def __init__(self, schema, raise_on_error):
     """Master configuration validator.
 
     Properties:
@@ -98,6 +98,7 @@
     self._errors = []
     self._fdt = None
     self._raise_on_error = raise_on_error
+    self._schema = schema
     self.model_list = []
     self.submodel_list = {}
 
@@ -299,7 +300,7 @@
                  "Phandle '%s' sku-id %d must target a model or submodel'" %
                  (prop.name, sku_id))
 
-  def Start(self, fname, schema):
+  def Start(self, fname):
     """Start validating a master configuration file
 
     Args:
@@ -328,7 +329,7 @@
           self.submodel_list[model.name] = []
 
       # Validate the entire master configuration
-      self._ValidateTree(self._fdt.GetRoot(), schema)
+      self._ValidateTree(self._fdt.GetRoot(), self._schema)
     finally:
       if tmpfile:
         os.unlink(tmpfile.name)
@@ -462,6 +463,15 @@
 ])
 
 
+def GetValidator():
+  """Get a schema validator for use by another module
+
+  Returns:
+    CrosConfigValidator object
+  """
+  return CrosConfigValidator(SCHEMA, raise_on_error=True)
+
+
 def Main(argv):
   """Main program for validator
 
@@ -472,10 +482,10 @@
     argv: Arguments to the problem (excluding argv[0])
   """
   args = ParseArgv(argv)
-  validator = CrosConfigValidator(args.raise_on_error)
+  validator = CrosConfigValidator(SCHEMA, args.raise_on_error)
   found_errors = False
   for fname in args.config:
-    errors = validator.Start(fname, SCHEMA)
+    errors = validator.Start(fname)
     if errors:
       found_errors = True
       print('%s:' % fname)
diff --git a/chromeos-config/validate/validate_config_unittest.py b/chromeos-config/validate/validate_config_unittest.py
index 030cc9f..182c39c 100755
--- a/chromeos-config/validate/validate_config_unittest.py
+++ b/chromeos-config/validate/validate_config_unittest.py
@@ -382,13 +382,14 @@
 class UnitTests(cros_test_lib.TestCase):
   """Unit tests for CrosConfigValidator"""
   def setUp(self):
-    self.val = validate_config.CrosConfigValidator(False)
+    self.val = validate_config.CrosConfigValidator(validate_config.SCHEMA,
+                                                   False)
 
   def Run(self, dts_source):
     dts = tempfile.NamedTemporaryFile(suffix='.dts', delete=False)
     dts.write(dts_source)
     dts.close()
-    errors = self.val.Start(dts.name, validate_config.SCHEMA)
+    errors = self.val.Start(dts.name)
     if errors:
       return errors
     if dts: