eric6/Utilities/__init__.py

Tue, 10 Sep 2019 19:30:07 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 10 Sep 2019 19:30:07 +0200
changeset 7229
53054eb5b15a
parent 7201
6b42677d7043
child 7259
7c017076c12e
permissions
-rw-r--r--

Removed obsolete "from __future__ import ..." statements.

0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6596
diff changeset
3 # Copyright (c) 2003 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
3670
f0cb7579c0b4 Finished renaming eric5 for PyQt5 to eric6.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
7 Package implementing various functions/classes needed everywhere within eric6.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2319
diff changeset
10
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import os
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import sys
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
13 import codecs
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import re
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 import fnmatch
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 import glob
114
f6e7cec11168 Changed the getUserName() function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 97
diff changeset
17 import getpass
7098
fbdf76af88ed Utilities: added a function to search the directory of a given volume name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
18 import ctypes
fbdf76af88ed Utilities: added a function to search the directory of a given volume name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
19 import subprocess
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
21
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
22 def __showwarning(message, category, filename, lineno, file=None, line=""):
141
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
23 """
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
24 Module function to raise a SyntaxError for a SyntaxWarning.
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
25
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
26 @param message warning object
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
27 @param category type object of the warning
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
28 @param filename name of the file causing the warning (string)
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
29 @param lineno line number causing the warning (integer)
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
30 @param file file to write the warning message to (ignored)
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
31 @param line line causing the warning (ignored)
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
32 @raise SyntaxError
5726
e1dbd217214a Fixed a few source docu issues (forgotten signals and exceptions).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5708
diff changeset
33 """ # __IGNORE_WARNING_D252__ __IGNORE_WARNING_D253__
141
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
34 if category is SyntaxWarning:
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
35 err = SyntaxError(str(message))
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
36 err.filename = filename
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
37 err.lineno = lineno
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
38 raise err
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
39
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 import warnings
141
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
41 warnings.showwarning = __showwarning
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3628
diff changeset
45 from PyQt5.QtCore import QRegExp, QDir, QProcess, Qt, QByteArray, \
682
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
46 qVersion, PYQT_VERSION_STR, QCoreApplication, QCryptographicHash
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3628
diff changeset
47 from PyQt5.Qsci import QSCINTILLA_VERSION_STR, QsciScintilla
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
882
34b86be88bf0 Redid the native file dialog code to be future proof.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 832
diff changeset
49 # import these methods into the Utilities namespace
3209
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3173
diff changeset
50 from Globals import ( # __IGNORE_WARNING__
6289
f481df37413c Extended the error report to send the desktop environment in use (Linux only).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6247
diff changeset
51 isWindowsPlatform, isLinuxPlatform, isMacPlatform, desktopName,
f481df37413c Extended the error report to send the desktop environment in use (Linux only).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6247
diff changeset
52 getConfigDir, setConfigDir, getPythonModulesDirectory,
f481df37413c Extended the error report to send the desktop environment in use (Linux only).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6247
diff changeset
53 getPyQt5ModulesDirectory, getQtBinariesPath, getPyQtToolsPath,
f481df37413c Extended the error report to send the desktop environment in use (Linux only).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6247
diff changeset
54 qVersionTuple)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
55
b5c84934de9c Renamed E4Gui to E5Gui.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 53
diff changeset
56 from E5Gui.E5Application import e5App
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 from UI.Info import Program, Version
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 import Preferences
5616
adcffadf4962 Reworked some __IGNORE_WARNING__ comments to be more specific.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5604
diff changeset
61 from Plugins.CheckerPlugins.SyntaxChecker.SyntaxCheck import (
adcffadf4962 Reworked some __IGNORE_WARNING__ comments to be more specific.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5604
diff changeset
62 # __IGNORE_WARNING__
3484
645c12de6b0c Merge with default branch.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456 3446
diff changeset
63 normalizeCode)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
3670
f0cb7579c0b4 Finished renaming eric5 for PyQt5 to eric6.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
65 from eric6config import getConfig
795
917f1945355c Added a syntax checker function for Python 2 files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
66
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 configDir = None
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
69 codingBytes_regexps = [
6075
281a47a8be9d Extended the search for a 'coding' statement from the first two lines to the first 5 lines because some scripts include empty lines between the she-bang line and the coding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
70 (5, re.compile(br'''coding[:=]\s*([-\w_.]+)''')),
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
71 (1, re.compile(br'''<\?xml.*\bencoding\s*=\s*['"]([-\w_.]+)['"]\?>''')),
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
72 ]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
73 coding_regexps = [
6075
281a47a8be9d Extended the search for a 'coding' statement from the first two lines to the first 5 lines because some scripts include empty lines between the she-bang line and the coding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
74 (5, re.compile(r'''coding[:=]\s*([-\w_.]+)''')),
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
75 (1, re.compile(r'''<\?xml.*\bencoding\s*=\s*['"]([-\w_.]+)['"]\?>''')),
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
76 ]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
77
3030
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
78 supportedCodecs = [
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
79 'utf-8',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
80 'iso8859-1', 'iso8859-15', 'iso8859-2', 'iso8859-3',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
81 'iso8859-4', 'iso8859-5', 'iso8859-6', 'iso8859-7',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
82 'iso8859-8', 'iso8859-9', 'iso8859-10', 'iso8859-11',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
83 'iso8859-13', 'iso8859-14', 'iso8859-16', 'latin-1',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
84 'koi8-r', 'koi8-u',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
85 'utf-16', 'utf-32',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
86 'cp037', 'cp424', 'cp437', 'cp500', 'cp737', 'cp775',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
87 'cp850', 'cp852', 'cp855', 'cp856', 'cp857', 'cp860',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
88 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', 'cp866',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
89 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
90 'cp1006', 'cp1026', 'cp1140', 'cp1250', 'cp1251',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
91 'cp1252', 'cp1253', 'cp1254', 'cp1255', 'cp1256',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
92 'cp1257', 'cp1258',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
93 'gb2312', 'gb18030',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
94 'ascii'
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
95 ]
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
97
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
98 class CodingError(Exception):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
99 """
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
100 Class implementing an exception, which is raised, if a given coding is
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
101 incorrect.
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
102 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
103 def __init__(self, coding):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
104 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
105 Constructor
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
106
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
107 @param coding coding to include in the message (string)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
108 """
3030
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
109 self.errorMessage = QCoreApplication.translate(
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
110 "CodingError",
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
111 "The coding '{0}' is wrong for the given text.").format(coding)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
112
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
113 def __repr__(self):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
114 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3525
diff changeset
115 Special method returning a representation of the exception.
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
116
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
117 @return string representing the error message
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
118 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
119 return str(self.errorMessage)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
120
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
121 def __str__(self):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
122 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3525
diff changeset
123 Special method returning a string representation of the exception.
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
124
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
125 @return string representing the error message
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
126 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
127 return str(self.errorMessage)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
128
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
129
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
130 def get_codingBytes(text):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
131 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
132 Function to get the coding of a bytes text.
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
133
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
134 @param text bytes text to inspect (bytes)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
135 @return coding string
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
136 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
137 lines = text.splitlines()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
138 for coding in codingBytes_regexps:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
139 coding_re = coding[1]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
140 head = lines[:coding[0]]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
141 for l in head:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
142 m = coding_re.search(l)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
143 if m:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
144 return str(m.group(1), "ascii").lower()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
145 return None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
146
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
147
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
148 def get_coding(text):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
149 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
150 Function to get the coding of a text.
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
151
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
152 @param text text to inspect (string)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
153 @return coding string
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
154 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
155 lines = text.splitlines()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
156 for coding in coding_regexps:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
157 coding_re = coding[1]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
158 head = lines[:coding[0]]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
159 for l in head:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
160 m = coding_re.search(l)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
161 if m:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
162 return m.group(1).lower()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
163 return None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
164
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
165
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
166 def readEncodedFile(filename):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
167 """
2763
e4794166ad70 Fixed a serious typo.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2741
diff changeset
168 Function to read a file and decode its contents into proper text.
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
169
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
170 @param filename name of the file to read (string)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
171 @return tuple of decoded text and encoding (string, string)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
172 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
173 f = open(filename, "rb")
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
174 text = f.read()
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
175 f.close()
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
176 return decode(text)
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
177
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
178
682
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
179 def readEncodedFileWithHash(filename):
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
180 """
2763
e4794166ad70 Fixed a serious typo.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2741
diff changeset
181 Function to read a file, calculate a hash value and decode its contents
682
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
182 into proper text.
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
183
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
184 @param filename name of the file to read (string)
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
185 @return tuple of decoded text, encoding and hash value (string, string,
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
186 string)
682
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
187 """
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
188 f = open(filename, "rb")
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
189 text = f.read()
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
190 f.close()
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
191 hashStr = str(QCryptographicHash.hash(
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
192 QByteArray(text), QCryptographicHash.Md5).toHex(), encoding="ASCII")
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
193 return decode(text) + (hashStr, )
682
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
194
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
195
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
196 def decode(text):
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
197 """
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
198 Function to decode some byte text into a string.
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
199
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
200 @param text byte text to decode (bytes)
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
201 @return tuple of decoded text and encoding (string, string)
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
202 """
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
203 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
204 if text.startswith(BOM_UTF8):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
205 # UTF-8 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
206 return str(text[len(BOM_UTF8):], 'utf-8'), 'utf-8-bom'
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
207 elif text.startswith(BOM_UTF16):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
208 # UTF-16 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
209 return str(text[len(BOM_UTF16):], 'utf-16'), 'utf-16'
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
210 elif text.startswith(BOM_UTF32):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
211 # UTF-32 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
212 return str(text[len(BOM_UTF32):], 'utf-32'), 'utf-32'
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
213 coding = get_codingBytes(text)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
214 if coding:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
215 return str(text, coding), coding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
216 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
217 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
218
159
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
219 # Assume UTF-8
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
220 try:
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
221 return str(text, 'utf-8'), 'utf-8-guessed'
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
222 except (UnicodeError, LookupError):
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
223 pass
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
224
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
225 guess = None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
226 if Preferences.getEditor("AdvancedEncodingDetection"):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
227 # Try the universal character encoding detector
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
228 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
229 import ThirdParty.CharDet.chardet
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
230 guess = ThirdParty.CharDet.chardet.detect(text)
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
231 if guess and guess['confidence'] > 0.95 and \
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
232 guess['encoding'] is not None:
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
233 codec = guess['encoding'].lower()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
234 return str(text, codec), '{0}-guessed'.format(codec)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
235 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
236 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
237 except ImportError:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
238 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
239
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
240 # Try default encoding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
241 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
242 codec = Preferences.getEditor("DefaultEncoding")
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
243 return str(text, codec), '{0}-default'.format(codec)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
244 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
245 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
246
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
247 if Preferences.getEditor("AdvancedEncodingDetection"):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
248 # Use the guessed one even if confifence level is low
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
249 if guess and guess['encoding'] is not None:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
250 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
251 codec = guess['encoding'].lower()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
252 return str(text, codec), '{0}-guessed'.format(codec)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
253 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
254 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
255
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
256 # Assume UTF-8 loosing information
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
257 return str(text, "utf-8", "ignore"), 'utf-8-ignore'
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
258
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
259
3394
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
260 def readEncodedFileWithEncoding(filename, encoding):
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
261 """
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
262 Function to read a file and decode its contents into proper text.
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
263
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
264 @param filename name of the file to read (string)
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
265 @keyparam encoding encoding to be used to read the file (string)
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
266 @return tuple of decoded text and encoding (string, string)
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
267 """
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
268 f = open(filename, "rb")
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
269 text = f.read()
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
270 f.close()
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
271 if encoding:
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
272 try:
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
273 return str(text, encoding), '{0}-selected'.format(encoding)
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
274 except (UnicodeError, LookupError):
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
275 pass
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
276 # Try default encoding
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
277 try:
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
278 codec = Preferences.getEditor("DefaultEncoding")
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
279 return str(text, codec), '{0}-default'.format(codec)
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
280 except (UnicodeError, LookupError):
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
281 pass
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
282 # Assume UTF-8 loosing information
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
283 return str(text, "utf-8", "ignore"), 'utf-8-ignore'
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
284 else:
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
285 return decode(text)
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
286
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
287
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
288 def writeEncodedFile(filename, text, origEncoding, forcedEncoding=""):
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
289 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
290 Function to write a file with properly encoded text.
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
291
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
292 @param filename name of the file to read
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
293 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
294 @param text text to be written
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
295 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
296 @param origEncoding type of the original encoding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
297 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
298 @param forcedEncoding encoding to be used for writing, if no coding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
299 line is present
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
300 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
301 @return encoding used for writing the file
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
302 @rtype str
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
303 """
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
304 etext, encoding = encode(text, origEncoding, forcedEncoding=forcedEncoding)
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
305
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
306 f = open(filename, "wb")
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
307 f.write(etext)
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
308 f.close()
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
309
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
310 return encoding
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
311
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
312
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
313 def encode(text, origEncoding, forcedEncoding=""):
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
314 """
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
315 Function to encode text into a byte text.
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
316
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
317 @param text text to be encoded
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
318 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
319 @param origEncoding type of the original encoding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
320 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
321 @param forcedEncoding encoding to be used for writing, if no coding line
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
322 is present
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
323 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
324 @return tuple of encoded text and encoding used
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
325 @rtype tuple of (bytes, str)
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
326 @exception CodingError raised to indicate an invalid encoding
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
327 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
328 encoding = None
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
329 if origEncoding == 'utf-8-bom':
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
330 etext, encoding = BOM_UTF8 + text.encode("utf-8"), 'utf-8-bom'
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
331 else:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
332 # Try declared coding spec
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
333 coding = get_coding(text)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
334 if coding:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
335 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
336 etext, encoding = text.encode(coding), coding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
337 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
338 # Error: Declared encoding is incorrect
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
339 raise CodingError(coding)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
340 else:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
341 if forcedEncoding:
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
342 try:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
343 etext, encoding = (
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
344 text.encode(forcedEncoding), forcedEncoding)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
345 except (UnicodeError, LookupError):
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
346 # Error: Forced encoding is incorrect, ignore it
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
347 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
348
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
349 if encoding is None:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
350 # Try the original encoding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
351 if origEncoding and origEncoding.endswith(
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
352 ('-selected', '-default', '-guessed', '-ignore')):
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
353 coding = origEncoding\
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
354 .replace("-selected", "")\
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
355 .replace("-default", "")\
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
356 .replace("-guessed", "")\
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
357 .replace("-ignore", "")
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
358 try:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
359 etext, encoding = text.encode(coding), coding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
360 except (UnicodeError, LookupError):
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
361 pass
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
362
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
363 if encoding is None:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
364 # Try configured default
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
365 try:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
366 codec = Preferences.getEditor("DefaultEncoding")
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
367 etext, encoding = text.encode(codec), codec
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
368 except (UnicodeError, LookupError):
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
369 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
370
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
371 if encoding is None:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
372 # Try saving as ASCII
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
373 try:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
374 etext, encoding = text.encode('ascii'), 'ascii'
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
375 except UnicodeError:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
376 pass
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
377
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
378 if encoding is None:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
379 # Save as UTF-8 without BOM
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
380 etext, encoding = text.encode('utf-8'), 'utf-8'
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
381
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
382 return etext, encoding
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
383
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
384
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
385 def decodeString(text):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
386 """
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
387 Function to decode a string containing Unicode encoded characters.
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
388
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
389 @param text text containing encoded chars (string)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
390 @return decoded text (string)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
391 """
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
392 buf = b""
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
393 index = 0
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
394 while index < len(text):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
395 if text[index] == "\\":
6596
2d82cd481adc Utilities: fixed an issue related to a forgotten conversion of str to bytes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6545
diff changeset
396 qb = QByteArray.fromHex(text[index:index + 4].encode())
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
397 buf += bytes(qb)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
398 index += 4
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
399 else:
2716
98c11482598e Python2 compatible decodeString (used e.g. in menu item Manage SSL Certificates)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2679
diff changeset
400 buf += codecs.encode(text[index], "utf-8")
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
401 index += 1
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
402 buf = buf.replace(b"\x00", b"")
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
403 return decodeBytes(buf)
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
404
1112
8a7d1b9d18db Improved code quality by getting rid of star imports. That way pyflakes can do its job. A few bugs fixed found by flakes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1109
diff changeset
405
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
406 def decodeBytes(buffer):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
407 """
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
408 Function to decode some byte text into a string.
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
409
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
410 @param buffer byte buffer to decode (bytes)
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
411 @return decoded text (string)
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
412 """
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
413 # try UTF with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
414 try:
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
415 if buffer.startswith(BOM_UTF8):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
416 # UTF-8 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
417 return str(buffer[len(BOM_UTF8):], encoding='utf-8')
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
418 elif buffer.startswith(BOM_UTF16):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
419 # UTF-16 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
420 return str(buffer[len(BOM_UTF16):], encoding='utf-16')
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
421 elif buffer.startswith(BOM_UTF32):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
422 # UTF-32 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
423 return str(buffer[len(BOM_UTF32):], encoding='utf-32')
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
424 except (UnicodeError, LookupError):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
425 pass
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
426
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
427 # try UTF-8
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
428 try:
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
429 return str(buffer, encoding="utf-8")
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
430 except UnicodeError:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
431 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
432
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
433 # try codec detection
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
434 try:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
435 import ThirdParty.CharDet.chardet
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
436 guess = ThirdParty.CharDet.chardet.detect(buffer)
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
437 if guess and guess['encoding'] is not None:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
438 codec = guess['encoding'].lower()
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
439 return str(buffer, encoding=codec)
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
440 except (UnicodeError, LookupError):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
441 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
442 except ImportError:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
443 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
444
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
445 return str(buffer, encoding="utf-8", errors="ignore")
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
446
2084
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
447
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
448 def readStringFromStream(stream):
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
449 """
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
450 Module function to read a string from the given stream.
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
451
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
452 @param stream data stream opened for reading (QDataStream)
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
453 @return string read from the stream (string)
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
454 """
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
455 data = stream.readString()
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
456 if data is None:
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
457 data = b""
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
458 return data.decode('utf-8')
2084
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
459
d3f083dd0222 Made the fiveth set of Qt5 compatibility changes (some of the previous changes are backed out due to changes in PyQt4).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2079
diff changeset
460
2079
febae63bb821 Extended the html_encode function a bit to prepare to use it instead of Qt.escape (step 3 of Qt5 compatibility changes).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2030
diff changeset
461 _escape = re.compile("[&<>\"'\u0080-\uffff]")
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 _escape_map = {
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 "&": "&amp;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 "<": "&lt;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 ">": "&gt;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 '"': "&quot;",
2079
febae63bb821 Extended the html_encode function a bit to prepare to use it instead of Qt.escape (step 3 of Qt5 compatibility changes).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2030
diff changeset
468 "'": "&#x27;",
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 }
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
471
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
472 def escape_entities(m, escmap=_escape_map):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 Function to encode html entities.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 @param m the match object
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
477 @param escmap the map of entities to encode
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 @return the converted text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 char = m.group()
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
481 text = escmap.get(char)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 if text is None:
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
483 text = "&#{0:d};".format(ord(char))
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
486
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 def html_encode(text, pattern=_escape):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 Function to correctly encode a text for html.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 @param text text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 @param pattern search pattern for text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 @return the encoded text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 if not text:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 text = pattern.sub(escape_entities, text)
12
1d8dd9706f46 First commit after changing to Python 3.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7
diff changeset
498 return text
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499
56
3bd61d38c924 Fixed a problem in Utilities related to HTML encoding of unicode characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 55
diff changeset
500 _uescape = re.compile('[\u0080-\uffff]')
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
502
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 def escape_uentities(m):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 Function to encode html entities.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 @param m the match object
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 @return the converted text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 char = m.group()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
511 text = "&#{0:d};".format(ord(char))
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
514
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 def html_uencode(text, pattern=_uescape):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 Function to correctly encode a unicode text for html.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 @param text text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 @param pattern search pattern for text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 @return the encoded text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 if not text:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 text = pattern.sub(escape_uentities, text)
12
1d8dd9706f46 First commit after changing to Python 3.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7
diff changeset
526 return text
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527
6247
5c677a7f7d51 Corrected some code style issues detected by the new pycodestyle version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6105
diff changeset
528 _uunescape = re.compile(r'&#\d+;')
1732
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
529
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
530
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
531 def unescape_uentities(m):
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
532 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
533 Function to decode html entities.
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
534
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
535 @param m the match object
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
536 @return the converted text (string)
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
537 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
538 char = m.group()
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
539 ordinal = int(char[2:-1])
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
540 return chr(ordinal)
1732
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
541
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
542
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
543 def html_udecode(text, pattern=_uunescape):
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
544 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
545 Function to correctly decode a html text to a unicode text.
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
546
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
547 @param text text to be decoded (string)
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
548 @param pattern search pattern for text to be decoded (string)
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
549 @return the decoded text (string)
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
550 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
551 if not text:
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
552 return ""
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
553 text = pattern.sub(unescape_uentities, text)
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
554 return text
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
555
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
556
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557 def convertLineEnds(text, eol):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 Function to convert the end of line characters.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561 @param text text to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 @param eol new eol setting (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
563 @return text with converted eols (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 if eol == '\r\n':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 regexp = re.compile(r"""(\r(?!\n)|(?<!\r)\n)""")
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
567 return regexp.sub(lambda m, eol='\r\n': eol, text)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568 elif eol == '\n':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 regexp = re.compile(r"""(\r\n|\r)""")
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
570 return regexp.sub(lambda m, eol='\n': eol, text)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 elif eol == '\r':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 regexp = re.compile(r"""(\r\n|\n)""")
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
573 return regexp.sub(lambda m, eol='\r': eol, text)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
574 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
576
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
577
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 def linesep():
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579 """
6105
cbd34d558bd9 Finished implementing support for EditorConfig for the IDE editor component and the standalone editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6099
diff changeset
580 Function to return the line separator used by the editor.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582 @return line separator used by the editor (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584 eolMode = Preferences.getEditor("EOLMode")
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585 if eolMode == QsciScintilla.EolUnix:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 return "\n"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 elif eolMode == QsciScintilla.EolMac:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588 return "\r"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 return "\r\n"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
592
788
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
593 def extractFlags(text):
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
594 """
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
595 Function to extract eric specific flags out of the given text.
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
596
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
597 Flags are contained in comments and are introduced by 'eflag:'.
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
598 The rest of the line is interpreted as 'key = value'. value is
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
599 analyzed for being an integer or float value. If that fails, it
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
600 is assumed to be a string. If a key does not contain a '='
788
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
601 character, it is assumed to be a boolean flag. Flags are expected
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
602 at the very end of a file. The search is ended, if a line without
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
603 the 'eflag:' marker is found.
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
604
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
605 @param text text to be scanned (string)
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
606 @return dictionary of string, boolean, complex, float and int
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
607 """
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
608 flags = {}
7192
a22eee00b052 Started removing runtime support for Python2 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7186
diff changeset
609 if isinstance(text, str):
2574
9d6b6cf31ec6 Fix for SyntaxCheckDialog and eflags evaluation.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2572
diff changeset
610 lines = text.rstrip().splitlines()
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
611 else:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
612 lines = text
788
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
613 for line in reversed(lines):
3099
d5dcda74d839 Fix the syntaxcheck if only one file is tested. Fix seldom bug if last line has unicode characters in it.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3065
diff changeset
614 try:
d5dcda74d839 Fix the syntaxcheck if only one file is tested. Fix seldom bug if last line has unicode characters in it.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3065
diff changeset
615 index = line.index("eflag:")
d5dcda74d839 Fix the syntaxcheck if only one file is tested. Fix seldom bug if last line has unicode characters in it.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3065
diff changeset
616 except ValueError:
788
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
617 # no flag found, don't look any further
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
618 break
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
619
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
620 flag = line[index + 6:].strip()
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
621 if "=" in flag:
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
622 key, value = flag.split("=", 1)
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
623 key = key.strip()
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
624 value = value.strip()
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
625
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
626 if value.lower() in ["true", "false", "yes", "no", "ok"]:
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
627 # it is a flag
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
628 flags[key] = value.lower() in ["true", "yes", "ok"]
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
629 continue
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
630
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
631 try:
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
632 # interpret as int first
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
633 value = int(value)
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
634 except ValueError:
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
635 try:
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
636 # interpret as float next
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
637 value = float(value)
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
638 except ValueError:
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
639 pass
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
640
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
641 flags[key] = value
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
642 else:
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
643 # treat it as a boolean
4514
e410e76d6538 Added capability to disable code style checks using an eflags: comment like 'eflags: noqa = M601, M702'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4384
diff changeset
644 if flag[0] == "-":
e410e76d6538 Added capability to disable code style checks using an eflags: comment like 'eflags: noqa = M601, M702'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4384
diff changeset
645 # false flags start with '-'
e410e76d6538 Added capability to disable code style checks using an eflags: comment like 'eflags: noqa = M601, M702'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4384
diff changeset
646 flags[flag[1:]] = False
e410e76d6538 Added capability to disable code style checks using an eflags: comment like 'eflags: noqa = M601, M702'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4384
diff changeset
647 else:
e410e76d6538 Added capability to disable code style checks using an eflags: comment like 'eflags: noqa = M601, M702'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4384
diff changeset
648 flags[flag] = True
788
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
649
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
650 return flags
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
651
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
652
1166
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
653 def extractFlagsFromFile(filename):
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
654 """
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
655 Function to extract eric specific flags out of the given file.
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
656
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
657 @param filename name of the file to be scanned (string)
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
658 @return dictionary of string, boolean, complex, float and int
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
659 """
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
660 try:
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
661 source, encoding = readEncodedFile(filename)
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
662 except (UnicodeError, IOError):
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
663 return {}
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
664
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
665 return extractFlags(source)
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
666
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
667
5586
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
668 def extractLineFlags(line, startComment="#", endComment="", flagsLine=False):
1308
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
669 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
670 Function to extract flags starting and ending with '__' from a line
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
671 comment.
1308
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
672
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
673 @param line line to extract flags from (string)
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
674 @keyparam startComment string identifying the start of the comment (string)
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
675 @keyparam endComment string identifying the end of a comment (string)
5586
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
676 @keyparam flagsLine flag indicating to check for a flags only line (bool)
1308
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
677 @return list containing the extracted flags (list of strings)
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
678 """
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
679 flags = []
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
680
5586
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
681 if not flagsLine or (
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
682 flagsLine and line.strip().startswith(startComment)):
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
683 pos = line.rfind(startComment)
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
684 if pos >= 0:
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
685 comment = line[pos + len(startComment):].strip()
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
686 if endComment:
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
687 endPos = line.rfind(endComment)
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
688 if endPos >= 0:
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
689 comment = comment[:endPos]
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
690 flags = [f.strip() for f in comment.split()
0e5421d679e7 Added capability to place line flags (e.g. __IGNORE...) on the line following the one to be ignored.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5421
diff changeset
691 if (f.startswith("__") and f.endswith("__"))]
1308
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
692 return flags
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
693
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
694
5020
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
695 def filterAnsiSequences(txt):
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
696 """
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
697 Function to filter out ANSI escape sequences (color only).
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
698
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
699 @param txt text to be filtered
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
700 @type str
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
701 @return text without ANSI escape sequences
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
702 @rtype str
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
703 """
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
704 ntxt = txt[:]
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
705 while True:
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
706 start = ntxt.find("\33[") # find escape character
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
707 if start == -1:
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
708 break
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
709 end = ntxt.find("m", start)
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
710 if end == -1:
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
711 break
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
712 ntxt = ntxt[:start] + ntxt[end + 1:]
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
713
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
714 return ntxt
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
715
d0afdfd8e45b Added capability to filter ANSI escape sequences out of output strings. This is limited to color sequences at the moment.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4869
diff changeset
716
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
717 def toNativeSeparators(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719 Function returning a path, that is using native separator characters.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721 @param path path to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 @return path with converted separator characters (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 return QDir.toNativeSeparators(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
726
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727 def fromNativeSeparators(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 Function returning a path, that is using "/" separator characters.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 @param path path to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 @return path with converted separator characters (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 return QDir.fromNativeSeparators(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
736
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737 def normcasepath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
738 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
739 Function returning a path, that is normalized with respect to its case
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
740 and references.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743 @return case normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745 return os.path.normcase(os.path.normpath(path))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
746
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
747
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
748 def normabspath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 Function returning a normalized, absolute path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 return os.path.abspath(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
757
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
758 def normcaseabspath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
760 Function returning an absolute path, that is normalized with respect to
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
761 its case and references.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
762
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
763 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
765 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766 return os.path.normcase(os.path.abspath(path))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
768
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 def normjoinpath(a, *p):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771 Function returning a normalized path of the joined parts passed into it.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773 @param a first path to be joined (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774 @param p variable number of path parts to be joind (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 @return normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
776 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 return os.path.normpath(os.path.join(a, *p))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
779
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 def normabsjoinpath(a, *p):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
782 Function returning a normalized, absolute path of the joined parts passed
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
783 into it.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 @param a first path to be joined (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 @param p variable number of path parts to be joind (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 return os.path.abspath(os.path.join(a, *p))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
791
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
792 def relpath(path, start=os.path.curdir):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
794 Return a relative version of a path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796 @param path path to make relative (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797 @param start path to make relative from (string)
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
798 @return relative path (string)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
799 @exception ValueError raised to indicate an invalid path
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
800 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
801 if not path:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802 raise ValueError("no path specified")
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
804 start_list = os.path.abspath(start).split(os.path.sep)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
805 path_list = os.path.abspath(path).split(os.path.sep)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
806
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
807 # Work out how much of the filepath is shared by start and path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
808 i = len(os.path.commonprefix([start_list, path_list]))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
809
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
810 rel_list = [os.path.pardir] * (len(start_list) - i) + path_list[i:]
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
811 if not rel_list:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
812 return os.path.curdir
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813 return os.path.join(*rel_list)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
814
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
815
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
816 def isinpath(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
817 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
818 Function to check for an executable file.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
819
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820 @param file filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821 @return flag to indicate, if the executable file is accessible
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822 via the searchpath defined by the PATH environment variable.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
823 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
824 if os.path.isabs(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
825 return os.access(file, os.X_OK)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826
1329
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
827 if os.path.exists(os.path.join(os.curdir, file)):
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
828 return os.access(os.path.join(os.curdir, file), os.X_OK)
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
829
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
830 path = getEnvironmentEntry('PATH')
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
831
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
832 # environment variable not defined
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
833 if path is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
834 return False
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
835
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
836 dirs = path.split(os.pathsep)
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
837 for directory in dirs:
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
838 if os.access(os.path.join(directory, file), os.X_OK):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
839 return True
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
840
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
841 return False
3833
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
842
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
843
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
844 def startswithPath(path, start):
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
845 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
846 Function to check, if a path starts with a given start path.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
847
3833
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
848 @param path path to be checked (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
849 @param start start path (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
850 @return flag indicating that the path starts with the given start
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
851 path (boolean)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
852 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
853 if start:
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
854 if path == start:
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
855 return True
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
856 elif normcasepath(toNativeSeparators(path)).startswith(
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
857 normcasepath(toNativeSeparators(start + "/"))):
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
858 return True
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
859 else:
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
860 return False
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
861 else:
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
862 return False
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
863
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
864
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
865 def relativePath(path, start):
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
866 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
867 Function to convert a file path to a path relative to a start path.
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
868
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
869 @param path file or directory name to convert (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
870 @param start start path (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
871 @return relative path or unchanged path, if path does not start with
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
872 the start path (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
873 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
874 if startswithPath(path, start):
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
875 if path == start:
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
876 return ""
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
877 else:
5385
b1ed8297b183 Fix for Utilities.relativePath() not considering the path to make relative to ending in '/' or '\'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5331
diff changeset
878 if start.endswith(("/", "\\")):
b1ed8297b183 Fix for Utilities.relativePath() not considering the path to make relative to ending in '/' or '\'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5331
diff changeset
879 return path[len(start):]
b1ed8297b183 Fix for Utilities.relativePath() not considering the path to make relative to ending in '/' or '\'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5331
diff changeset
880 else:
b1ed8297b183 Fix for Utilities.relativePath() not considering the path to make relative to ending in '/' or '\'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5331
diff changeset
881 return path[len(start) + 1:]
3833
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
882 else:
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
883 return path
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
884
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
885
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
886 def relativeUniversalPath(path, start):
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
887 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
888 Function to convert a file path to a path relative to a start path
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
889 with universal separators.
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
890
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
891 @param path file or directory name to convert (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
892 @param start start path (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
893 @return relative path or unchanged path, if path does not start with
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
894 the start path with universal separators (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
895 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
896 return fromNativeSeparators(relativePath(path, start))
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
897
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
898
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
899 def absolutePath(path, start):
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
900 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
901 Public method to convert a path relative to a start path to an
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
902 absolute path.
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
903
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
904 @param path file or directory name to convert (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
905 @param start start path (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
906 @return absolute path (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
907 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
908 if not os.path.isabs(path):
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
909 path = os.path.join(start, path)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
910 return path
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
911
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
912
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
913 def absoluteUniversalPath(path, start):
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
914 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
915 Public method to convert a path relative to a start path with
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
916 universal separators to an absolute path.
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
917
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
918 @param path file or directory name to convert (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
919 @param start start path (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
920 @return absolute path with native separators (string)
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
921 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
922 if not os.path.isabs(path):
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
923 path = toNativeSeparators(os.path.join(start, path))
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
924 return path
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
925
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
926
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
927 def getExecutablePath(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
928 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
929 Function to build the full path of an executable file from the environment.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
930
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
931 @param file filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
932 @return full executable name, if the executable file is accessible
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
933 via the searchpath defined by the PATH environment variable, or an
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
934 empty string otherwise.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
935 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
936 if os.path.isabs(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
937 if os.access(file, os.X_OK):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
938 return file
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
939 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
940 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
941
1329
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
942 cur_path = os.path.join(os.curdir, file)
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
943 if os.path.exists(cur_path):
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
944 if os.access(cur_path, os.X_OK):
1474
1eaec11a0078 Fixed a few PEP-8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1465
diff changeset
945 return cur_path
1329
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
946
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
947 path = os.getenv('PATH')
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
948
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
949 # environment variable not defined
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
950 if path is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
951 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
952
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
953 dirs = path.split(os.pathsep)
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
954 for directory in dirs:
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
955 exe = os.path.join(directory, file)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
956 if os.access(exe, os.X_OK):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
957 return exe
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
958
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
959 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
960
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
961
2553
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
962 def getExecutablePaths(file):
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
963 """
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
964 Function to build all full path of an executable file from the environment.
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
965
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
966 @param file filename of the executable (string)
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
967 @return list of full executable names (list of strings), if the executable
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
968 file is accessible via the searchpath defined by the PATH environment
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
969 variable, or an empty list otherwise.
2553
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
970 """
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
971 paths = []
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
972
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
973 if os.path.isabs(file):
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
974 if os.access(file, os.X_OK):
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
975 return [file]
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
976 else:
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
977 return []
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
978
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
979 cur_path = os.path.join(os.curdir, file)
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
980 if os.path.exists(cur_path):
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
981 if os.access(cur_path, os.X_OK):
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
982 paths.append(cur_path)
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
983
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
984 path = os.getenv('PATH')
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
985
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
986 # environment variable not defined
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
987 if path is not None:
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
988 dirs = path.split(os.pathsep)
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
989 for directory in dirs:
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
990 exe = os.path.join(directory, file)
2553
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
991 if os.access(exe, os.X_OK) and exe not in paths:
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
992 paths.append(exe)
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
993
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
994 return paths
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
995
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
996
5331
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
997 def getWindowsExecutablePath(file):
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
998 """
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
999 Function to build the full path of an executable file from the environment
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1000 on Windows platforms.
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1001
6495
6e73d31af3af Changed the extension of the generated script files for Windows from '.bat' to '.cmd'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6477
diff changeset
1002 First an executable with the extension .exe is searched for, thereafter
6e73d31af3af Changed the extension of the generated script files for Windows from '.bat' to '.cmd'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6477
diff changeset
1003 such with the extensions .cmd or .bat and finally the given file name as
6e73d31af3af Changed the extension of the generated script files for Windows from '.bat' to '.cmd'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6477
diff changeset
1004 is. The first match is returned.
5331
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1005
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1006 @param file filename of the executable to check (string)
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1007 @return full executable name, if the executable file is accessible
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1008 via the searchpath defined by the PATH environment variable, or an
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1009 empty string otherwise.
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1010 """
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1011 if os.path.isabs(file):
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1012 if os.access(file, os.X_OK):
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1013 return file
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1014 else:
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1015 return ""
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1016
6495
6e73d31af3af Changed the extension of the generated script files for Windows from '.bat' to '.cmd'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6477
diff changeset
1017 filenames = [file + ".exe", file + ".cmd", file + ".bat", file]
5331
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1018
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1019 for filename in filenames:
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1020 cur_path = os.path.join(os.curdir, filename)
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1021 if os.path.exists(cur_path):
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1022 if os.access(cur_path, os.X_OK):
5421
ce62341e81b6 Little fix determining the PyQt tools path on Windows, if the tools are installed in parallel to eric.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
1023 return os.path.abspath(cur_path)
5331
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1024
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1025 path = os.getenv('PATH')
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1026
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1027 # environment variable not defined
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1028 if path is None:
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1029 return ""
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1030
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1031 dirs = path.split(os.pathsep)
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
1032 for directory in dirs:
5331
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1033 for filename in filenames:
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
1034 exe = os.path.join(directory, filename)
5331
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1035 if os.access(exe, os.X_OK):
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1036 return exe
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1037
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1038 return ""
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1039
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1040
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1041 def isExecutable(exe):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1042 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1043 Function to check, if a file is executable.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1044
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1045 @param exe filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1046 @return flag indicating executable status (boolean)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1047 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1048 return os.access(exe, os.X_OK)
5708
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1049
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1050
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1051 def isDrive(path):
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1052 """
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1053 Function to check, if a path is a Windows drive.
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1054
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1055 @param path path name to be checked
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1056 @type str
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1057 @return flag indicating a Windows drive
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1058 @rtype bool
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1059 """
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1060 isDrive = False
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1061 drive, directory = os.path.splitdrive(path)
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1062 if drive and len(drive) == 2 and drive.endswith(":") and \
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1063 directory in ["", "\\", "/"]:
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1064 isDrive = True
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1065
9b01b4004314 Fixed an issue in the file browser model caused some error message being shown upon startup in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5696
diff changeset
1066 return isDrive
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1067
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1068
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1069 def samepath(f1, f2):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1070 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1071 Function to compare two paths.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1072
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1073 @param f1 first path for the compare (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1074 @param f2 second path for the compare (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1075 @return flag indicating whether the two paths represent the
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1076 same path on disk.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1077 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1078 if f1 is None or f2 is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1079 return False
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1080
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
1081 if normcaseabspath(os.path.realpath(f1)) == \
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
1082 normcaseabspath(os.path.realpath(f2)):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1083 return True
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1084
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1085 return False
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1086
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1087
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1088 def samefilepath(f1, f2):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1089 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1090 Function to compare two paths. Strips the filename.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1091
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1092 @param f1 first filepath for the compare (string)
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1093 @param f2 second filepath for the compare (string)
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1094 @return flag indicating whether the two paths represent the
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1095 same path on disk.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1096 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1097 if f1 is None or f2 is None:
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1098 return False
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1099
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1100 if (normcaseabspath(os.path.dirname(os.path.realpath(f1))) ==
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3060
diff changeset
1101 normcaseabspath(os.path.dirname(os.path.realpath(f2)))):
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3060
diff changeset
1102 return True
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1103
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1104 return False
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1105
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1106 try:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1107 EXTSEP = os.extsep
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1108 except AttributeError:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1109 EXTSEP = "."
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1110
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1111
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1112 def splitPath(name):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1113 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1114 Function to split a pathname into a directory part and a file part.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1115
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1116 @param name path name (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1117 @return a tuple of 2 strings (dirname, filename).
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1118 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1119 if os.path.isdir(name):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1120 dn = os.path.abspath(name)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1121 fn = "."
de9c2efb9d02 Started porting eric4 to Python3
&