terms.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. import yaml
  17. logger = logging.getLogger(__name__)
  18. class Terms(object):
  19. def __init__(self, yamlObj):
  20. self._rawTerms = yamlObj
  21. def getMasterVersion(self):
  22. return None if self._rawTerms is None else self._rawTerms['master_version']
  23. def getForClient(self):
  24. policies = {}
  25. if self._rawTerms is not None:
  26. for docName, doc in self._rawTerms['docs'].items():
  27. policies[docName] = {
  28. 'version': doc['version'],
  29. }
  30. policies[docName].update(doc['langs'])
  31. return { 'policies': policies }
  32. def getUrlSet(self):
  33. urls = set()
  34. if self._rawTerms is not None:
  35. for docName, doc in self._rawTerms['docs'].items():
  36. for langName, lang in doc['langs'].items():
  37. urls.add(lang['url'])
  38. return urls
  39. def urlListIsSufficient(self, urls):
  40. agreed = set()
  41. urlset = set(urls)
  42. if self._rawTerms is not None:
  43. for docName, doc in self._rawTerms['docs'].items():
  44. for lang in doc['langs'].values():
  45. if lang['url'] in urlset:
  46. agreed.add(docName)
  47. break
  48. required = set(self._rawTerms['docs'].keys())
  49. return agreed == required
  50. def get_terms(sydent):
  51. """Read and parse terms as specified in the config.
  52. :returns Terms
  53. """
  54. try:
  55. termsYaml = None
  56. termsPath = sydent.cfg.get('general', 'terms.path')
  57. if termsPath == '':
  58. return Terms(None)
  59. with open(termsPath) as fp:
  60. termsYaml = yaml.full_load(fp)
  61. if 'master_version' not in termsYaml:
  62. raise Exception("No master version")
  63. if 'docs' not in termsYaml:
  64. raise Exception("No 'docs' key in terms")
  65. for docName, doc in termsYaml['docs'].items():
  66. if 'version' not in doc:
  67. raise Exception("'%s' has no version" % (docName,))
  68. if 'langs' not in doc:
  69. raise Exception("'%s' has no langs" % (docName,))
  70. for langKey, lang in doc['langs'].items():
  71. if 'name' not in lang:
  72. raise Exception("lang '%s' of doc %s has no name" % (langKey, docName))
  73. if 'url' not in lang:
  74. raise Exception("lang '%s' of doc %s has no url" % (langKey, docName))
  75. return Terms(termsYaml)
  76. except Exception:
  77. logger.exception("Couldn't read terms file '%s'", sydent.cfg.get('general', 'terms.path'))