/sort.py

https://github.com/dkujawski/utilities · Python · 86 lines · 52 code · 9 blank · 25 comment · 25 complexity · a84fdf9bbc43667f9feec704d5b0feb9 MD5 · raw file

  1. # By Jimmy Ruska
  2. # Sorts Desktop and Downloads folder by extension.
  3. # Made for easy modification. Tested: Windows/Ubuntu Python 2.7, 3.2.
  4. # Beware, it will also move folders and program Shortcuts.
  5. import ConfigParser
  6. import os
  7. import re
  8. cfg_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'sort.cfg')
  9. class FileSorter(object):
  10. """ By Jimmy Ruska
  11. Sorts Desktop and Downloads folder by extension.
  12. Made for easy modification. Tested: Windows/Ubuntu Python 2.7, 3.2.
  13. Beware, it will also move folders and program Shortcuts.
  14. Refactor, cleanup, and tests by Dave Kujawski
  15. Note: changes only tested on Kubuntu.
  16. """
  17. def __init__(self):
  18. """ read the config file
  19. """
  20. config = ConfigParser.ConfigParser()
  21. config.read(cfg_path)
  22. self.organize = dict()
  23. for k, v in config.items('organize'):
  24. self.organize[k] = v.split('.')
  25. self.ignore = list()
  26. for k, v in config.items('ignore'):
  27. for item in v.split(','):
  28. token = (k, item)
  29. self.ignore.append(token)
  30. self.dest = config.get('dirs', 'sorted')
  31. self.review_dirs = config.get('dirs', 'toreview').split(',')
  32. # sort list of dirs into folder final
  33. def sort(self):
  34. if not os.path.isdir(self.dest):
  35. os.mkdir(self.dest)
  36. # make base directories if they don't exist
  37. for key in self.organize:
  38. path = os.path.join(self.dest, key)
  39. if not os.path.isdir(path):
  40. os.makedirs(path)
  41. #loop through and sort all directories
  42. path_files = [[(d,z) for z in os.listdir(d)] for d in self.review_dirs]
  43. for path, file in sum(path_files,[]):
  44. target = os.path.join(path, file)
  45. if file in self.organize or self._exclude(file):
  46. """ skip anything that we are supposed to ignore
  47. """
  48. pass
  49. elif os.path.isdir(target) \
  50. and not os.path.exists(os.path.join(self.dest, 'dir', file)):
  51. """ if the target is a directory, move the directory only if it
  52. has not already been moved.
  53. """
  54. # TODO: tests do not cover this yet!
  55. os.rename(target, os.path.join(self.dest, 'dir', file))
  56. else:
  57. ext = file.rpartition(".")[2].lower()
  58. to = os.path.join(self.dest, self._grouping(ext), file)
  59. if not os.path.exists(to):
  60. os.rename(target, to)
  61. # Don't sort certain files like desktop.ini
  62. def _exclude(self, name):
  63. for op,check in self.ignore:
  64. if ((op=="re" and re.match(check,name))
  65. or (op=="match" and re.search(check,name))
  66. or (op=="exact" and check==name)):
  67. return True
  68. # Match file extensions to find group
  69. def _grouping(self, ext):
  70. for folder,exts in self.organize.items():
  71. if ext in exts:
  72. return folder
  73. return "other"
  74. if __name__ == '__main__':
  75. fs = FileSorter()
  76. fs.sort()