From 5973ff9d9ebfc60b922ffbe5278b4c3d25695a10 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sat, 11 Feb 2017 14:51:55 +0800 Subject: manifest.py: Try to parse given value to integer or float --- scripts/manifest.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/scripts/manifest.py b/scripts/manifest.py index bee8e5c..aba7a64 100755 --- a/scripts/manifest.py +++ b/scripts/manifest.py @@ -55,7 +55,7 @@ class Manifest: Set the value of the specified item in the manifest. (Will add a new item or update an existing item.) """ - self.manifest[key] = value + self.manifest[key] = self.parse_value(value) self.save() def add(self, key, value): @@ -67,7 +67,7 @@ class Manifest: if key in self.manifest: raise KeyError("manifest already has item: '%s'" % key) else: - self.manifest[key] = value + self.manifest[key] = self.parse_value(value) self.save() def update(self, key, value): @@ -77,7 +77,7 @@ class Manifest: If the specified item doesn't exist, raise a ``KeyError``. """ if key in self.manifest: - self.manifest[key] = value + self.manifest[key] = self.parse_value(value) self.save() else: raise KeyError("manifest doesn't have item: '%s'" % key) @@ -89,6 +89,20 @@ class Manifest: del self.manifest[key] self.save() + @staticmethod + def parse_value(value): + """ + Try to parse the value from string to integer or float. + """ + try: + v = int(value) + except ValueError: + try: + v = float(value) + except ValueError: + v = value + return v + def find_manifest(filename="manifest.yaml"): """ -- cgit v1.2.2