PageRenderTime 34ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/original/Compost.app/Contents/Resources/pyhredder.py

https://github.com/aleray/datateb
Python | 108 lines | 76 code | 15 blank | 17 comment | 2 complexity | 80442d56b9fdaf6fef6da2eef5c6bff6 MD5 | raw file
Possible License(s): GPL-3.0
  1. #!/usr/local/bin/python
  2. # -*- coding: utf-8 -*-
  3. from AppKit import NSSound
  4. from appscript import *
  5. from mactypes import Alias
  6. import osax
  7. import sys
  8. import time
  9. import os, sys
  10. import EasyDialogs
  11. def wrap(text, width):
  12. """
  13. A word-wrap function that preserves existing line breaks
  14. and most spaces in the text. Expects that existing line
  15. breaks are posix newlines (\n).
  16. """
  17. return reduce(
  18. lambda line,
  19. word,
  20. width=width: '%s%s%s' % (
  21. line,
  22. ' \n'[(len(line)-line.rfind('\n')-1 + len(word.split('\n',1)[0]) >= width)],
  23. word),
  24. text.split(' ')
  25. )
  26. def main():
  27. # Play a sound
  28. snd = "paper_shredder.wav"
  29. #os.system("/Users/aleray//bin/mplayer %s < /dev/tty1 >| ~/error.log" % snd)
  30. #os.system("/Users/aleray/bin/mplayer %s >| ~/error.log" % snd)
  31. #os.system("/Applications/VLC.app/Contents/MacOS/VLC -I ncurses %s &" % snd)
  32. sound = NSSound.alloc()
  33. sound.initWithContentsOfFile_byReference_('/Users/aleray/Desktop/Paper Shredder.app/Contents/Resources/paper_shredder.wav', True)
  34. sound.play()
  35. meter = EasyDialogs.ProgressBar('Shredding your file...',
  36. maxval=100,
  37. label='Starting',
  38. )
  39. for i in xrange(1, 101):
  40. phase = '%d %% Completed' % i
  41. print phase
  42. meter.label(phase)
  43. meter.inc()
  44. time.sleep(0.01)
  45. print 'Done with loop'
  46. time.sleep(1)
  47. del meter
  48. print 'The dialog should be gone now'
  49. # Nombre de caractères par ligne du fichier original, Nombre de caractère pas ligne des fichiers générés
  50. nc = 40
  51. nf = 5
  52. # Récupère le premier argument et le formate
  53. f = open(sys.argv[1],'r')
  54. g = f.read()
  55. f.close()
  56. g = wrap(g,nc)
  57. # À revoir car supprime n'importe quel type de fichier
  58. cmd = "rm %s" % sys.argv[1]
  59. os.system(cmd)
  60. # Récupère le chemin relatif de l'argument sans son extension
  61. basePath = os.path.splitext(sys.argv[1])[0]
  62. fileName = os.path.basename(sys.argv[1]).split(".")[0]
  63. fileExt = os.path.basename(sys.argv[1]).split(".")[1]
  64. destinationPath = "/Users/aleray/Recycled/Paper Shredder/"
  65. # liste des fichiers généré
  66. createdDocs = []
  67. # Découpe ligne par ligne le fichier original et cré des "bandes"
  68. for i in range(nc/nf):
  69. newContent=""
  70. for line in g.split("\n"):
  71. newContent += line[i*nf:(i*nf)+nf]+"\n"
  72. newPath = '%s%s_%s.%s' % (destinationPath, fileName, i, fileExt)
  73. createdDocs.append(newPath)
  74. newFile = open(newPath, 'w')
  75. newFile.write(newContent)
  76. te = app('TextEdit')
  77. te.activate()
  78. for i in range(len(createdDocs)):
  79. te.open(Alias(createdDocs[i]))
  80. te.windows[0].bounds.set([100+35*i, 100, 200+35*i, 800])
  81. # fait une petite pause avant de tout refermer
  82. time.sleep(2)
  83. for window in te.windows.get()[::-1]:
  84. window.close()
  85. # print EasyDialogs.Message('Your file have been succesfully destructed', ok='Continue')
  86. if __name__ == '__main__':
  87. main()