target_registry.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (c) 2015, Nordic Semiconductor
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. #
  7. # * Redistributions of source code must retain the above copyright notice, this
  8. # list of conditions and the following disclaimer.
  9. #
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. #
  14. # * Neither the name of Nordic Semiconductor ASA nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import re
  29. import os
  30. import json
  31. from abc import ABCMeta, abstractmethod
  32. class TargetDatabase(object):
  33. __metaclass__ = ABCMeta
  34. @abstractmethod
  35. def get_targets(self):
  36. pass
  37. @abstractmethod
  38. def get_target(self, target_id):
  39. pass
  40. @abstractmethod
  41. def refresh(self):
  42. pass
  43. @staticmethod
  44. def find_target(targets, target_id):
  45. for target in targets:
  46. if target["id"] == target_id:
  47. return target
  48. return None
  49. class EnvTargetDatabase(TargetDatabase):
  50. def __init__(self):
  51. self.targets = None
  52. def get_targets(self):
  53. if self.targets is None:
  54. self.targets = []
  55. for key, value in os.environ.iteritems():
  56. match = re.match("NORDICSEMI_TARGET_(?P<target>\d+)_(?P<key>[a-zA-Z_]+)", key)
  57. if match:
  58. key_value = match.groupdict()
  59. if "key" in key_value and "target" in key_value:
  60. target_id = int(key_value["target"])
  61. target = self.find_target(self.targets, target_id)
  62. if target is None:
  63. target = {"id": int(target_id)}
  64. self.targets.append(target)
  65. target[key_value["key"].lower()] = value
  66. return self.targets
  67. def refresh(self):
  68. self.targets = None
  69. def get_target(self, target_id):
  70. return self.find_target(self.get_targets(), target_id)
  71. class FileTargetDatabase(TargetDatabase):
  72. def __init__(self, filename):
  73. self.filename = filename
  74. self.targets = None
  75. def get_targets(self):
  76. if not self.targets:
  77. self.targets = json.load(open(self.filename, "r"))["targets"]
  78. return self.targets
  79. def get_target(self, target_id):
  80. return self.find_target(self.get_targets(), target_id)
  81. def refresh(self):
  82. self.targets = None
  83. class TargetRegistry(object):
  84. def __init__(self, target_db=EnvTargetDatabase()):
  85. self.target_db = target_db
  86. def find_one(self, target_id=None):
  87. if target_id:
  88. return self.target_db.get_target(target_id)
  89. else:
  90. return None
  91. def get_all(self):
  92. return self.target_db.get_targets()