the other jimmy / mbed-sdk-tools

Fork of mbed-sdk-tools by mbed official

Revision:
16:a6285a7e5cc6
Parent:
14:ee1b877e6839
Child:
22:9e85236d8716
diff -r cd36228f7d73 -r a6285a7e5cc6 targets.py
--- a/targets.py	Thu Jul 14 20:51:33 2016 +0100
+++ b/targets.py	Thu Jul 14 21:29:46 2016 +0100
@@ -51,9 +51,9 @@
 caches = {}
 def cached(func):
     def wrapper(*args, **kwargs):
-        if not caches.has_key(func):
-            caches[func] = func(*args, **kwargs)
-        return caches[func]
+        if not caches.has_key((func.__name__, args)):
+            caches[(func.__name__, args)] = func(*args, **kwargs)
+        return caches[(func.__name__, args)]
     return wrapper
 
 class Target:
@@ -61,17 +61,24 @@
     # need to be computed differently than regular attributes
     __cumulative_attributes = ['extra_labels', 'macros', 'device_has', 'features']
 
-    # {target_name: target_instance} map for all the targets in the system
-    __target_map = {}
-
     # List of targets that were added dynamically using "add_py_targets" (see below)
     __py_targets = set()
 
+    # Location of the 'targets.json' file
+    __targets_json_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'targets.json')
+
     # Load the description of JSON target data
     @staticmethod
     @cached
     def get_json_target_data():
-        return json_file_to_dict(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'targets.json'))
+        return json_file_to_dict(Target.__targets_json_location)
+
+    # Set the location of the targets.json file
+    @staticmethod
+    def set_targets_json_location(location):
+        Target.__targets_json_location = location
+        # Invalidate caches, since the location of the JSON file changed
+        caches.clear()
 
     # Get the members of this module using Python's "inspect" module
     @staticmethod
@@ -203,10 +210,9 @@
 
     # Return the target instance starting from the target name
     @staticmethod
+    @cached
     def get_target(name):
-        if not Target.__target_map.has_key(name):
-            Target.__target_map[name] = Target(name)
-        return Target.__target_map[name]
+        return Target(name)
 
     def __init__(self, name):
         self.name = name
@@ -417,3 +423,15 @@
         for detect_code in target.detect_code:
             result[detect_code] = target.name
     return result
+
+# Sets the location of the JSON file that contains the targets
+def set_targets_json_location(location):
+    # First instruct Target about the new location
+    Target.set_targets_json_location(location)
+    # Then re-initialize TARGETS, TARGET_MAP and TARGET_NAMES
+    # The re-initialization does not create new variables, it keeps the old ones instead
+    # This ensures compatibility with code that does "from tools.targets import TARGET_NAMES"
+    TARGETS[:] = [Target.get_target(name) for name, value in Target.get_json_target_data().items() if value.get("public", True)]
+    TARGET_MAP.clear()
+    TARGET_MAP.update(dict([(t.name, t) for t in TARGETS]))
+    TARGET_NAMES[:] = TARGET_MAP.keys()