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.

118 lines
3.7 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Utils
  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 os
  23. import re
  24. import io
  25. import sys
  26. import base64
  27. import shutil
  28. import urllib
  29. import logging
  30. import hashlib
  31. import binascii
  32. import tempfile
  33. import mimetypes
  34. import unicodedata
  35. from odoo.tools import human_size
  36. from odoo.tools.mimetypes import guess_mimetype
  37. _logger = logging.getLogger(__name__)
  38. #----------------------------------------------------------
  39. # File Helper
  40. #----------------------------------------------------------
  41. def slugify(value, lower=True):
  42. value = unicodedata.normalize('NFKD', value)
  43. value = value.encode('ascii', 'ignore').decode('ascii')
  44. value = value.lower() if lower else value
  45. value = re.sub('[^\w\s-]', '', value)
  46. value = re.sub('[-\s]+', '-', value)
  47. return value.strip()
  48. def check_name(name):
  49. tmp_dir = tempfile.mkdtemp()
  50. try:
  51. open(os.path.join(tmp_dir, name), 'a').close()
  52. except IOError:
  53. return False
  54. finally:
  55. shutil.rmtree(tmp_dir)
  56. return True
  57. def compute_name(name, suffix, escape_suffix):
  58. if escape_suffix:
  59. name, extension = os.path.splitext(name)
  60. return "%s(%s)%s" % (name, suffix, extension)
  61. else:
  62. return "%s(%s)" % (name, suffix)
  63. def unique_name(name, names, escape_suffix=False):
  64. if not name in names:
  65. return name
  66. else:
  67. suffix = 1
  68. name = compute_name(name, suffix, escape_suffix)
  69. while name in names:
  70. suffix += 1
  71. name = compute_name(name, suffix, escape_suffix)
  72. return name
  73. def unique_files(files):
  74. ufiles = []
  75. unames = []
  76. for file in files:
  77. uname = unique_name(file[0], unames, escape_suffix=True)
  78. ufiles.append((uname, file[1]))
  79. unames.append(uname)
  80. return ufiles
  81. def guess_extension(filename=None, mimetype=None, binary=None):
  82. extension = filename and os.path.splitext(filename)[1][1:].strip().lower()
  83. if not extension and mimetype:
  84. extension = mimetypes.guess_extension(mimetype)[1:].strip().lower()
  85. if not extension and binary:
  86. mimetype = guess_mimetype(binary, default="")
  87. extension = mimetypes.guess_extension(mimetype)[1:].strip().lower()
  88. return extension
  89. #----------------------------------------------------------
  90. # System Helper
  91. #----------------------------------------------------------
  92. def ensure_path_directories(path):
  93. directory_path = os.path.dirname(path)
  94. if not os.path.exists(directory_path):
  95. os.makedirs(directory_path)
  96. def remove_empty_directories(path):
  97. if not os.path.isdir(path):
  98. return
  99. entries = os.listdir(path)
  100. if len(entries) > 0:
  101. for entry in entries:
  102. subpath = os.path.join(path, entry)
  103. if os.path.isdir(subpath):
  104. self._remove_empty_directories(subpath)
  105. else:
  106. os.rmdir(path)