localize.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python3
  2. # reTux File Localizer
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import os
  17. import shutil
  18. import sys
  19. import tkinter
  20. if getattr(sys, "frozen", False):
  21. __file__ = sys.executable
  22. FILEDIR = os.path.dirname(__file__)
  23. CONFIG = os.path.join(
  24. os.getenv("XDG_CONFIG_HOME", os.path.join(os.path.expanduser("~"),
  25. ".config")), "retux")
  26. LOCAL = os.path.join(
  27. os.getenv("XDG_DATA_HOME", os.path.join(os.path.expanduser("~"), ".local",
  28. "share")), "retux")
  29. if __name__ == '__main__':
  30. if not os.path.exists(LOCAL):
  31. os.makedirs(LOCAL)
  32. tkwindow = tkinter.Tk()
  33. tkwindow.withdraw()
  34. fnames = tkinter.filedialog.askopenfilenames(
  35. filetypes=[("all files", ".*")], initialdir=FILEDIR)
  36. for fname in fnames:
  37. rp = os.path.relpath(fname, FILEDIR)
  38. if not rp.startswith(os.pardir):
  39. cd = os.path.dirname(os.path.join(LOCAL, rp))
  40. if not os.path.exists(cd):
  41. os.makedirs(cd)
  42. new_name = os.path.join(cd, os.path.basename(fname))
  43. if os.path.isfile(new_name):
  44. os.remove(new_name)
  45. elif os.path.isdir(new_name):
  46. shutil.rmtree(new_name)
  47. shutil.move(fname, cd)
  48. tkinter.messagebox.showinfo(
  49. "Message", 'Moved "{}" to "{}"'.format(fname, cd))
  50. else:
  51. tkinter.messagebox.showinfo(
  52. "Message",
  53. '"{}" was not localized (invalid location)'.format(fname))
  54. tkwindow.destroy()