You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.5 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import re
  20. import uuid
  21. import chardet
  22. from itertools import chain
  23. from lxml.etree import tostring
  24. from lxml.html import fromstring
  25. from odoo.tools import ustr
  26. from odoo.addons.muk_utils.tools.utils import safe_execute
  27. def debrand_documentation(text, value, expression):
  28. text = re.sub(
  29. r'https://www.{0}.com/documentation/'.format(expression),
  30. '{0}/documentation/'.format(value), text, flags=re.IGNORECASE
  31. )
  32. text = re.sub(
  33. r'https://www.{0}.com/page/docs/'.format(expression),
  34. '{0}/page/docs/'.format(value), text, flags=re.IGNORECASE
  35. )
  36. return text
  37. def debrand_link(text, value, expression):
  38. return re.sub(
  39. r'\b{0}.com\b'.format(expression),
  40. value, text, flags=re.IGNORECASE
  41. )
  42. def debrand_text(text, value, expression):
  43. cases = {
  44. expression: uuid.uuid4().hex,
  45. expression.upper(): uuid.uuid4().hex,
  46. expression.lower(): uuid.uuid4().hex,
  47. expression.capitalize(): uuid.uuid4().hex,
  48. }
  49. def init_no_debranding(match):
  50. text = match.group()
  51. for key in cases:
  52. text = re.sub(r'\b{0}\b'.format(key), cases[key], text)
  53. return text
  54. def post_no_debranding(text):
  55. for key in cases:
  56. text = text.replace(cases[key], key)
  57. return text
  58. if isinstance(value, dict):
  59. text = safe_execute(text, debrand_documentation, text, value.get('documentation'), expression)
  60. text = safe_execute(text, debrand_link, text, value.get('website'), expression)
  61. text = re.sub(
  62. r'<.*class=".*no_debranding.*".*>.*(\b{0}\b).*<.*>'.format(expression),
  63. init_no_debranding, text, flags=re.IGNORECASE
  64. )
  65. text = re.sub(
  66. r'\b(?<!\.){0}(?!\.\S|\s?=|\w|\[)\b'.format(expression),
  67. value.get('system_name') if isinstance(value, dict) else value,
  68. text, flags=re.IGNORECASE
  69. )
  70. return post_no_debranding(text)
  71. def debrand_with_check(text, value, expression):
  72. if not text or not re.search(r'\b{0}\b'.format(expression), text, re.IGNORECASE):
  73. return text
  74. return debrand_text(text, value, expression)
  75. def debrand(input, value, expression="odoo"):
  76. if isinstance(input, str):
  77. return ustr(debrand_with_check(input, value, expression))
  78. if isinstance(input, bytes):
  79. encoding = chardet.detect(input)['encoding'] or 'utf-8'
  80. return bytes(debrand(ustr(input, hint_encoding=encoding), value, expression), encoding)
  81. return input
  82. def safe_debrand(input, value, expression="odoo"):
  83. return safe_execute(input, debrand, input, value, expression)