config.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (c) 2021, Open Source Robotics Foundation
  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
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. # * Neither the name of the Willow Garage, Inc. nor the names of its
  13. # contributors may be used to endorse or promote products derived from
  14. # this software without specific prior written permission.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. # POSSIBILITY OF SUCH DAMAGE.
  27. import os
  28. import re
  29. import yaml
  30. from .apk import apk_base_url
  31. from .deb import deb_base_url
  32. from .pacman import pacman_base_url
  33. from .rpm import rpm_base_url
  34. DEFAULT_CONFIG_PATH = os.path.join(
  35. os.path.dirname(os.path.abspath(__file__)),
  36. 'config.yaml')
  37. def load_apk_base_url(loader, node):
  38. return apk_base_url(node.value)
  39. def load_deb_base_url(loader, node):
  40. base_url, comp = node.value.rsplit(' ', 1)
  41. return deb_base_url(base_url, comp)
  42. def load_pacman_base_url(loader, node):
  43. base_url, repo_name = node.value.rsplit(' ', 1)
  44. return pacman_base_url(base_url, repo_name)
  45. def load_rpm_base_url(loader, node):
  46. return rpm_base_url(node.value)
  47. def load_regex(loader, node):
  48. return re.compile(node.value)
  49. yaml.add_constructor(
  50. u'!apk_base_url', load_apk_base_url, Loader=yaml.SafeLoader)
  51. yaml.add_constructor(
  52. u'!deb_base_url', load_deb_base_url, Loader=yaml.SafeLoader)
  53. yaml.add_constructor(
  54. u'!pacman_base_url', load_pacman_base_url, Loader=yaml.SafeLoader)
  55. yaml.add_constructor(
  56. u'!rpm_base_url', load_rpm_base_url, Loader=yaml.SafeLoader)
  57. yaml.add_constructor(
  58. u'!regular_expression', load_regex, Loader=yaml.SafeLoader)
  59. def load_config(path=None):
  60. with open(path or DEFAULT_CONFIG_PATH) as f:
  61. return yaml.safe_load(f)