aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/lib/nrfutil/nordicsemi/utility
diff options
context:
space:
mode:
Diffstat (limited to 'circuitpython/lib/nrfutil/nordicsemi/utility')
-rw-r--r--circuitpython/lib/nrfutil/nordicsemi/utility/__init__.py29
-rw-r--r--circuitpython/lib/nrfutil/nordicsemi/utility/target_registry.py121
-rw-r--r--circuitpython/lib/nrfutil/nordicsemi/utility/tests/__init__.py29
-rw-r--r--circuitpython/lib/nrfutil/nordicsemi/utility/tests/test_target_registry.py90
-rw-r--r--circuitpython/lib/nrfutil/nordicsemi/utility/tests/test_targets.json17
5 files changed, 286 insertions, 0 deletions
diff --git a/circuitpython/lib/nrfutil/nordicsemi/utility/__init__.py b/circuitpython/lib/nrfutil/nordicsemi/utility/__init__.py
new file mode 100644
index 0000000..58c0272
--- /dev/null
+++ b/circuitpython/lib/nrfutil/nordicsemi/utility/__init__.py
@@ -0,0 +1,29 @@
+# Copyright (c) 2015, Nordic Semiconductor
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# * Neither the name of Nordic Semiconductor ASA nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.s
+
+"""Package marker file."""
diff --git a/circuitpython/lib/nrfutil/nordicsemi/utility/target_registry.py b/circuitpython/lib/nrfutil/nordicsemi/utility/target_registry.py
new file mode 100644
index 0000000..f87e006
--- /dev/null
+++ b/circuitpython/lib/nrfutil/nordicsemi/utility/target_registry.py
@@ -0,0 +1,121 @@
+# Copyright (c) 2015, Nordic Semiconductor
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# * Neither the name of Nordic Semiconductor ASA nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import re
+import os
+import json
+from abc import ABCMeta, abstractmethod
+
+
+class TargetDatabase(object):
+ __metaclass__ = ABCMeta
+
+ @abstractmethod
+ def get_targets(self):
+ pass
+
+ @abstractmethod
+ def get_target(self, target_id):
+ pass
+
+ @abstractmethod
+ def refresh(self):
+ pass
+
+ @staticmethod
+ def find_target(targets, target_id):
+ for target in targets:
+ if target["id"] == target_id:
+ return target
+
+ return None
+
+
+class EnvTargetDatabase(TargetDatabase):
+ def __init__(self):
+ self.targets = None
+
+ def get_targets(self):
+ if self.targets is None:
+ self.targets = []
+
+ for key, value in os.environ.iteritems():
+ match = re.match("NORDICSEMI_TARGET_(?P<target>\d+)_(?P<key>[a-zA-Z_]+)", key)
+
+ if match:
+ key_value = match.groupdict()
+ if "key" in key_value and "target" in key_value:
+ target_id = int(key_value["target"])
+
+ target = self.find_target(self.targets, target_id)
+
+ if target is None:
+ target = {"id": int(target_id)}
+ self.targets.append(target)
+
+ target[key_value["key"].lower()] = value
+
+ return self.targets
+
+ def refresh(self):
+ self.targets = None
+
+ def get_target(self, target_id):
+ return self.find_target(self.get_targets(), target_id)
+
+
+class FileTargetDatabase(TargetDatabase):
+ def __init__(self, filename):
+ self.filename = filename
+ self.targets = None
+
+ def get_targets(self):
+ if not self.targets:
+ self.targets = json.load(open(self.filename, "r"))["targets"]
+
+ return self.targets
+
+ def get_target(self, target_id):
+ return self.find_target(self.get_targets(), target_id)
+
+ def refresh(self):
+ self.targets = None
+
+
+class TargetRegistry(object):
+ def __init__(self, target_db=EnvTargetDatabase()):
+ self.target_db = target_db
+
+ def find_one(self, target_id=None):
+ if target_id:
+ return self.target_db.get_target(target_id)
+ else:
+ return None
+
+ def get_all(self):
+ return self.target_db.get_targets()
diff --git a/circuitpython/lib/nrfutil/nordicsemi/utility/tests/__init__.py b/circuitpython/lib/nrfutil/nordicsemi/utility/tests/__init__.py
new file mode 100644
index 0000000..8f8006e
--- /dev/null
+++ b/circuitpython/lib/nrfutil/nordicsemi/utility/tests/__init__.py
@@ -0,0 +1,29 @@
+# Copyright (c) 2015, Nordic Semiconductor
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# * Neither the name of Nordic Semiconductor ASA nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""Package marker file."""
diff --git a/circuitpython/lib/nrfutil/nordicsemi/utility/tests/test_target_registry.py b/circuitpython/lib/nrfutil/nordicsemi/utility/tests/test_target_registry.py
new file mode 100644
index 0000000..1d6df65
--- /dev/null
+++ b/circuitpython/lib/nrfutil/nordicsemi/utility/tests/test_target_registry.py
@@ -0,0 +1,90 @@
+# Copyright (c) 2015, Nordic Semiconductor
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# * Neither the name of Nordic Semiconductor ASA nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import os
+import unittest
+from nordicsemi.utility.target_registry import TargetRegistry, EnvTargetDatabase
+from nordicsemi.utility.target_registry import FileTargetDatabase
+
+
+class TestTargetRegistry(unittest.TestCase):
+ def setUp(self):
+ script_abspath = os.path.abspath(__file__)
+ script_dirname = os.path.dirname(script_abspath)
+ os.chdir(script_dirname)
+
+ # Setup the environment variables
+ os.environ["NORDICSEMI_TARGET_1_SERIAL_PORT"] = "COM1"
+ os.environ["NORDICSEMI_TARGET_1_PCA"] = "PCA10028"
+ os.environ["NORDICSEMI_TARGET_1_DRIVE"] = "D:\\"
+ os.environ["NORDICSEMI_TARGET_1_SEGGER_SN"] = "1231233333"
+
+ os.environ["NORDICSEMI_TARGET_2_SERIAL_PORT"] = "COM2"
+ os.environ["NORDICSEMI_TARGET_2_PCA"] = "PCA10028"
+ os.environ["NORDICSEMI_TARGET_2_DRIVE"] = "E:\\"
+ os.environ["NORDICSEMI_TARGET_2_SEGGER_SN"] = "3332222111"
+
+ def test_get_targets_from_file(self):
+ target_database = FileTargetDatabase("test_targets.json")
+ target_repository = TargetRegistry(target_db=target_database)
+
+ target = target_repository.find_one(target_id=1)
+ assert target is not None
+ assert target["drive"] == "d:\\"
+ assert target["serial_port"] == "COM7"
+ assert target["pca"] == "PCA10028"
+ assert target["segger_sn"] == "123123123123"
+
+ target = target_repository.find_one(target_id=2)
+ assert target is not None
+ assert target["drive"] == "e:\\"
+ assert target["serial_port"] == "COM8"
+ assert target["pca"] == "PCA10028"
+ assert target["segger_sn"] == "321321321312"
+
+ def test_get_targets_from_environment(self):
+ target_database = EnvTargetDatabase()
+ target_repository = TargetRegistry(target_db=target_database)
+
+ target = target_repository.find_one(target_id=1)
+ assert target is not None
+ assert target["drive"] == "D:\\"
+ assert target["serial_port"] == "COM1"
+ assert target["pca"] == "PCA10028"
+ assert target["segger_sn"] == "1231233333"
+
+ target = target_repository.find_one(target_id=2)
+ assert target is not None
+ assert target["drive"] == "E:\\"
+ assert target["serial_port"] == "COM2"
+ assert target["pca"] == "PCA10028"
+ assert target["segger_sn"] == "3332222111"
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/circuitpython/lib/nrfutil/nordicsemi/utility/tests/test_targets.json b/circuitpython/lib/nrfutil/nordicsemi/utility/tests/test_targets.json
new file mode 100644
index 0000000..2e1ab30
--- /dev/null
+++ b/circuitpython/lib/nrfutil/nordicsemi/utility/tests/test_targets.json
@@ -0,0 +1,17 @@
+{
+ "targets":
+ [{
+ "id": 1,
+ "drive": "d:\\",
+ "serial_port": "COM7",
+ "pca": "PCA10028",
+ "segger_sn": "123123123123"
+ },
+ {
+ "id": 2,
+ "drive": "e:\\",
+ "serial_port": "COM8",
+ "pca": "PCA10028",
+ "segger_sn": "321321321312"
+ }]
+} \ No newline at end of file