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