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.

98 lines
3.1 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 mimetypes
  23. import os
  24. import shutil
  25. import tempfile
  26. from odoo.tools.mimetypes import guess_mimetype
  27. def check_name(name):
  28. tmp_dir = tempfile.mkdtemp()
  29. try:
  30. open(os.path.join(tmp_dir, name), "a").close()
  31. except IOError:
  32. return False
  33. finally:
  34. shutil.rmtree(tmp_dir)
  35. return True
  36. def compute_name(name, suffix, escape_suffix):
  37. if escape_suffix:
  38. name, extension = os.path.splitext(name)
  39. return "{}({}){}".format(name, suffix, extension)
  40. else:
  41. return "{}({})".format(name, suffix)
  42. def unique_name(name, names, escape_suffix=False):
  43. if not name in names:
  44. return name
  45. else:
  46. suffix = 1
  47. name = compute_name(name, suffix, escape_suffix)
  48. while name in names:
  49. suffix += 1
  50. name = compute_name(name, suffix, escape_suffix)
  51. return name
  52. def unique_files(files):
  53. ufiles = []
  54. unames = []
  55. for file in files:
  56. uname = unique_name(file[0], unames, escape_suffix=True)
  57. ufiles.append((uname, file[1]))
  58. unames.append(uname)
  59. return ufiles
  60. def guess_extension(filename=None, mimetype=None, binary=None):
  61. extension = filename and os.path.splitext(filename)[1][1:].strip().lower()
  62. if not extension and mimetype:
  63. extension = mimetypes.guess_extension(mimetype)[1:].strip().lower()
  64. if not extension and binary:
  65. mimetype = guess_mimetype(binary, default="")
  66. extension = mimetypes.guess_extension(mimetype)[1:].strip().lower()
  67. return extension
  68. def ensure_path_directories(path):
  69. directory_path = os.path.dirname(path)
  70. if not os.path.exists(directory_path):
  71. os.makedirs(directory_path)
  72. def remove_empty_directories(path):
  73. if not os.path.isdir(path):
  74. return
  75. entries = os.listdir(path)
  76. if len(entries) > 0:
  77. for entry in entries:
  78. subpath = os.path.join(path, entry)
  79. if os.path.isdir(subpath):
  80. self._remove_empty_directories(subpath)
  81. else:
  82. os.rmdir(path)