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.

97 lines
3.5 KiB

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