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.

94 lines
3.4 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. text = re.sub(
  39. r'(http(s)?:\/\/)?(www.)?{0}.com'.format(expression),
  40. value, text, flags=re.IGNORECASE
  41. )
  42. return text
  43. def debrand_text(text, value, expression):
  44. cases = {
  45. expression: uuid.uuid4().hex,
  46. expression.upper(): uuid.uuid4().hex,
  47. expression.lower(): uuid.uuid4().hex,
  48. expression.capitalize(): uuid.uuid4().hex,
  49. }
  50. def init_no_debranding(match):
  51. text = match.group()
  52. for key in cases:
  53. text = re.sub(r'\b{0}\b'.format(key), cases[key], text)
  54. return text
  55. def post_no_debranding(text):
  56. for key in cases:
  57. text = text.replace(cases[key], key)
  58. return text
  59. if isinstance(value, dict):
  60. text = safe_execute(text, debrand_documentation, text, value.get('documentation'), expression)
  61. text = safe_execute(text, debrand_link, text, value.get('website'), expression)
  62. text = re.sub(
  63. r'<.*class=".*no_debranding.*".*>.*(\b{0}\b).*<.*>'.format(expression),
  64. init_no_debranding, text, flags=re.IGNORECASE
  65. )
  66. text = re.sub(
  67. r'\b(?<!\.){0}(?!\.\S|\s?=|\w|\[)\b'.format(expression),
  68. value.get('system_name') if isinstance(value, dict) else value,
  69. text, flags=re.IGNORECASE
  70. )
  71. return post_no_debranding(text)
  72. def debrand_with_check(text, value, expression):
  73. if not text or not re.search(r'\b{0}\b'.format(expression), text, re.IGNORECASE):
  74. return text
  75. return debrand_text(text, value, expression)
  76. def debrand(input, value, expression="odoo"):
  77. if isinstance(input, str):
  78. return ustr(debrand_with_check(input, value, expression))
  79. if isinstance(input, bytes):
  80. encoding = chardet.detect(input)['encoding'] or 'utf-8'
  81. return bytes(debrand(ustr(input, hint_encoding=encoding), value, expression), encoding)
  82. return input
  83. def safe_debrand(input, value, expression="odoo"):
  84. return safe_execute(input, debrand, input, value, expression)