PluginToolGenerateHash.py

changeset 45
60ef6c28f7fb
parent 44
4a8ec9716e40
child 47
aee34fc5bccc
equal deleted inserted replaced
44:4a8ec9716e40 45:60ef6c28f7fb
4 # 4 #
5 5
6 """ 6 """
7 Module implementing the 'Generate Hash' tool plug-in. 7 Module implementing the 'Generate Hash' tool plug-in.
8 """ 8 """
9
10 from __future__ import unicode_literals
11 try:
12 str = unicode # __IGNORE_WARNING__
13 except NameError:
14 pass
15 9
16 import os 10 import os
17 import hashlib 11 import hashlib
18 12
19 from PyQt5.QtCore import QObject, QTranslator 13 from PyQt5.QtCore import QObject, QTranslator
25 # Start-Of-Header 19 # Start-Of-Header
26 name = "Generate Hash Tool Plug-in" 20 name = "Generate Hash Tool Plug-in"
27 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 21 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
28 autoactivate = True 22 autoactivate = True
29 deactivateable = True 23 deactivateable = True
30 version = "2.1.3" 24 version = "3.0.0"
31 className = "ToolGenerateHashPlugin" 25 className = "ToolGenerateHashPlugin"
32 packageName = "ToolGenerateHash" 26 packageName = "ToolGenerateHash"
33 shortDescription = "Generate a hash for a selectable file or directory" 27 shortDescription = "Generate a hash for a selectable file or directory"
34 longDescription = \ 28 longDescription = (
35 """Plug-in to generate a hash for a selectable file or directory. The"""\ 29 """Plug-in to generate a hash for a selectable file or directory. The"""
36 """ hash string will be inserted at the cursor position of the current"""\ 30 """ hash string will be inserted at the cursor position of the current"""
37 """ editor. The menu will be disabled, if no editor is open.""" 31 """ editor. The menu will be disabled, if no editor is open."""
32 )
38 needsRestart = False 33 needsRestart = False
39 pyqtApi = 2 34 pyqtApi = 2
40 python2Compatible = True
41 # End-Of-Header 35 # End-Of-Header
42 36
43 error = "" 37 error = ""
44 38
45 39
317 E5FileDialog.Options(E5FileDialog.Option(0))) 311 E5FileDialog.Options(E5FileDialog.Option(0)))
318 if folder and os.path.isdir(folder): 312 if folder and os.path.isdir(folder):
319 fails = 0 313 fails = 0
320 hashes = [] 314 hashes = []
321 for name in os.listdir(folder): 315 for name in os.listdir(folder):
322 if not name.startswith(".") and \ 316 if (
323 os.path.isfile(os.path.join(folder, name)): 317 not name.startswith(".") and
318 os.path.isfile(os.path.join(folder, name))
319 ):
324 try: 320 try:
325 f = open(os.path.join(folder, name), "rb") 321 f = open(os.path.join(folder, name), "rb")
326 hashStr = self.Hashes[act.data()](f.read()).hexdigest() 322 hashStr = self.Hashes[act.data()](f.read()).hexdigest()
327 f.close() 323 f.close()
328 hashes.append((name, hashStr)) 324 hashes.append((name, hashStr))
335 self.tr("""<p>The hash for some files could not""" 331 self.tr("""<p>The hash for some files could not"""
336 """ be generated.</p>""") 332 """ be generated.</p>""")
337 ) 333 )
338 else: 334 else:
339 editor = e5App().getObject("ViewManager").activeWindow() 335 editor = e5App().getObject("ViewManager").activeWindow()
340 if (editor.getLanguage() in ['Python', 'Python2'] and
341 "from __future__ import unicode_literals"
342 not in editor.text()):
343 prefix = 'u'
344 else:
345 prefix = ''
346
347 line, index = editor.getCursorPosition() 336 line, index = editor.getCursorPosition()
348 indLevel = (editor.indentation(line) // 337 indLevel = (editor.indentation(line) //
349 editor.indentationWidth()) 338 editor.indentationWidth())
350 if editor.indentationsUseTabs(): 339 if editor.indentationsUseTabs():
351 indString = '\t' 340 indString = '\t'
352 else: 341 else:
353 indString = editor.indentationWidth() * ' ' 342 indString = editor.indentationWidth() * ' '
354 indent = (indLevel + 1) * indString 343 indent = (indLevel + 1) * indString
355 code = ["["] 344 code = ["["]
356 for name, hashStr in hashes: 345 for name, hashStr in hashes:
357 code.append("{0}({1}'{2}', '{3}'),".format( 346 code.append("{0}('{1}', '{2}'),".format(
358 indent, prefix, name, hashStr)) 347 indent, name, hashStr))
359 code.append("{0}]".format(indLevel * indString)) 348 code.append("{0}]".format(indLevel * indString))
360 349
361 self.__insertHash(os.linesep.join(code)) 350 self.__insertHash(os.linesep.join(code))
362 351
363 # 352 #

eric ide

mercurial