Enable customize hw_property from Acloud config.

Fix pylint error: The if expression can be replaced with 'bool(test)'
(simplifiable-if-expression)

Bug: 174124129
Test: acloud-dev create --hw-property cpu:2 -vv
Change-Id: I9dd40af4cd4f4f6f60f2551021138e52cafedceb
diff --git a/create/avd_spec.py b/create/avd_spec.py
index d0eb849..1232cf3 100644
--- a/create/avd_spec.py
+++ b/create/avd_spec.py
@@ -265,17 +265,23 @@
 
         This method will initialize _hw_property in the following
         manner:
-        1. Get default hw properties from config.
-        2. Override by hw_property args.
+        1. Get default hw properties from flavor.
+        2. Override hw properties from config.
+        3. Override by hw_property args.
 
         Args:
             args: Namespace object from argparse.parse_args.
         """
-        self._cfg.OverrideHwProperty(self._flavor, self._instance_type)
         self._hw_property = {}
-        self._hw_property = self._ParseHWPropertyStr(self._cfg.hw_property)
+        default_property = self._cfg.GetDefaultHwProperty(self._flavor,
+                                                          self._instance_type)
+        self._hw_property = self._ParseHWPropertyStr(default_property)
         logger.debug("Default hw property for [%s] flavor: %s", self._flavor,
                      self._hw_property)
+        if self._cfg.hw_property:
+            cfg_hw_property = self._ParseHWPropertyStr(self._cfg.hw_property)
+            logger.debug("Hw property from config: %s", cfg_hw_property)
+            self._hw_property.update(cfg_hw_property)
 
         if args.hw_property:
             arg_hw_property = self._ParseHWPropertyStr(args.hw_property)
diff --git a/public/config.py b/public/config.py
index 9fe5f83..85d7d43 100755
--- a/public/config.py
+++ b/public/config.py
@@ -289,23 +289,26 @@
 
             self.launch_args = " ".join(scrubbed_args)
 
-    def OverrideHwProperty(self, flavor, instance_type=None):
-        """Override hw configuration values.
+    def GetDefaultHwProperty(self, flavor, instance_type=None):
+        """Get default hw configuration values.
 
         HwProperty will be overrided according to the change of flavor and
         instance type. The format of key is flavor or instance_type-flavor.
         e.g: 'phone' or 'local-phone'.
-        If the giving key is not found, set hw configuration with a default
+        If the giving key is not found, get hw configuration with a default
         phone property.
 
         Args:
             flavor: String of flavor name.
             instance_type: String of instance type.
+
+        Returns:
+            String of device hardware property, it would be like
+            "cpu:4,resolution:720x1280,dpi:320,memory:4g".
         """
         hw_key = ("%s-%s" % (instance_type, flavor)
                   if instance_type == constants.INSTANCE_TYPE_LOCAL else flavor)
-        self.hw_property = self.common_hw_property_map.get(
-            hw_key, _DEFAULT_HW_PROPERTY)
+        return self.common_hw_property_map.get(hw_key, _DEFAULT_HW_PROPERTY)
 
     def Verify(self):
         """Verify configuration fields."""
@@ -322,7 +325,7 @@
 
     def SupportRemoteInstance(self):
         """Return True if gcp project is provided in config."""
-        return True if self.project else False
+        return bool(self.project)
 
 
 class AcloudConfigManager(object):
diff --git a/public/config_test.py b/public/config_test.py
index cd0cf95..cbe7a4b 100644
--- a/public/config_test.py
+++ b/public/config_test.py
@@ -109,10 +109,23 @@
   key: "phone"
   value: "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"
 }
+
+common_hw_property_map {
+  key: "auto"
+  value: "cpu:4,resolution:1280x800,dpi:160,memory:4g"
+}
 """
 
     def setUp(self):
         self.config_file = mock.MagicMock()
+        # initial config with test config.
+        self.config_file.read.return_value = self.INTERNAL_CONFIG
+        internal_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
+            self.config_file, internal_config_pb2.InternalConfig)
+        self.config_file.read.return_value = self.USER_CONFIG
+        usr_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
+            self.config_file, user_config_pb2.UserConfig)
+        self.cfg = config.AcloudConfig(usr_cfg, internal_cfg)
 
     # pylint: disable=no-member
     def testLoadUserConfig(self):
@@ -253,7 +266,8 @@
         # hw property
         self.assertEqual(
             {key: val for key, val in six.iteritems(cfg.common_hw_property_map)},
-            {"phone": "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"})
+            {"phone": "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g",
+             "auto": "cpu:4,resolution:1280x800,dpi:160,memory:4g"})
 
     def testLoadConfigFails(self):
         """Test loading a bad file."""
@@ -264,40 +278,41 @@
 
     def testOverrideWithHWProperty(self):
         """Test override hw property by flavor type."""
-        # initial config with test config.
-        self.config_file.read.return_value = self.INTERNAL_CONFIG
-        internal_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
-            self.config_file, internal_config_pb2.InternalConfig)
-        self.config_file.read.return_value = self.USER_CONFIG
-        usr_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
-            self.config_file, user_config_pb2.UserConfig)
-        cfg = config.AcloudConfig(usr_cfg, internal_cfg)
-
         # test override with an exist flavor.
-        cfg.hw_property = None
+        self.cfg.hw_property = None
         args = mock.MagicMock()
         args.flavor = "phone"
         args.which = "create"
-        cfg.OverrideWithArgs(args)
-        self.assertEqual(cfg.hw_property,
+        self.cfg.OverrideWithArgs(args)
+        self.assertEqual(self.cfg.hw_property,
                          "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g")
 
         # test override with a nonexistent flavor.
-        cfg.hw_property = None
+        self.cfg.hw_property = None
         args = mock.MagicMock()
         args.flavor = "non-exist-flavor"
         args.which = "create"
-        cfg.OverrideWithArgs(args)
-        self.assertEqual(cfg.hw_property, "")
+        self.cfg.OverrideWithArgs(args)
+        self.assertEqual(self.cfg.hw_property, "")
 
         # test override zone.
-        cfg.zone = "us-central1-f"
+        self.cfg.zone = "us-central1-f"
         args = mock.MagicMock()
         args.which = "create"
         args.flavor = "phone"
         args.zone = "us-central1-b"
-        cfg.OverrideWithArgs(args)
-        self.assertEqual(cfg.zone, "us-central1-b")
+        self.cfg.OverrideWithArgs(args)
+        self.assertEqual(self.cfg.zone, "us-central1-b")
+
+    def testGetDefaultHwProperty(self):
+        """Test GetDefaultHwProperty."""
+        # test with "phone" flavor
+        expected = "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"
+        self.assertEqual(expected, self.cfg.GetDefaultHwProperty("phone"))
+
+        # test with "auto" flavor
+        expected = "cpu:4,resolution:1280x800,dpi:160,memory:4g"
+        self.assertEqual(expected, self.cfg.GetDefaultHwProperty("auto"))
 
 
 if __name__ == "__main__":