eric6/Utilities/__init__.py

Sat, 31 Aug 2019 12:58:11 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 31 Aug 2019 12:58:11 +0200
branch
without_py2_and_pyqt4
changeset 7192
a22eee00b052
parent 7186
ccd55666e9d2
child 7196
ab0a91b82b37
permissions
-rw-r--r--

Started removing runtime support for Python2 and PyQt4.

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
3145
a9de05d4a22f # __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3144
diff changeset
10 from __future__ import unicode_literals
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
11
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import os
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 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
14 import codecs
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 import re
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 import fnmatch
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 import glob
114
f6e7cec11168 Changed the getUserName() function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 97
diff changeset
18 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
19 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
20 import subprocess
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
22
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
23 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
24 """
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
25 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
26
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
27 @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
28 @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
29 @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
30 @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
31 @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
32 @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
33 @raise SyntaxError
5726
e1dbd217214a Fixed a few source docu issues (forgotten signals and exceptions).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5708
diff changeset
34 """ # __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
35 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
36 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
37 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
38 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
39 raise err
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
40
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 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
42 warnings.showwarning = __showwarning
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3628
diff changeset
46 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
47 qVersion, PYQT_VERSION_STR, QCoreApplication, QCryptographicHash
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3628
diff changeset
48 from PyQt5.Qsci import QSCINTILLA_VERSION_STR, QsciScintilla
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
882
34b86be88bf0 Redid the native file dialog code to be future proof.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 832
diff changeset
50 # 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
51 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
52 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
53 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
54 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
55 qVersionTuple)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
55
b5c84934de9c Renamed E4Gui to E5Gui.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 53
diff changeset
57 from E5Gui.E5Application import e5App
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 from UI.Info import Program, Version
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 import Preferences
5616
adcffadf4962 Reworked some __IGNORE_WARNING__ comments to be more specific.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5604
diff changeset
62 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
63 # __IGNORE_WARNING__
3484
645c12de6b0c Merge with default branch.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456 3446
diff changeset
64 normalizeCode)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
3670
f0cb7579c0b4 Finished renaming eric5 for PyQt5 to eric6.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
66 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
67
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 configDir = None
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
70 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
71 (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
72 (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
73 ]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
74 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
75 (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
76 (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
77 ]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
78
3030
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
79 supportedCodecs = [
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
80 'utf-8',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
81 '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
82 '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
83 '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
84 '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
85 'koi8-r', 'koi8-u',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
86 'utf-16', 'utf-32',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
87 'cp037', 'cp424', 'cp437', 'cp500', 'cp737', 'cp775',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
88 'cp850', 'cp852', 'cp855', 'cp856', 'cp857', 'cp860',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
89 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', 'cp866',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
90 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
91 'cp1006', 'cp1026', 'cp1140', 'cp1250', 'cp1251',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
92 'cp1252', 'cp1253', 'cp1254', 'cp1255', 'cp1256',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
93 'cp1257', 'cp1258',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
94 'gb2312', 'gb18030',
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
95 'ascii'
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
96 ]
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
98
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
99 class CodingError(Exception):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
100 """
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
101 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
102 incorrect.
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
103 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
104 def __init__(self, coding):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
105 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
106 Constructor
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
107
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
108 @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
109 """
3030
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
110 self.errorMessage = QCoreApplication.translate(
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
111 "CodingError",
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
112 "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
113
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
114 def __repr__(self):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
115 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3525
diff changeset
116 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
117
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
118 @return string representing the error message
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
119 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
120 return str(self.errorMessage)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
121
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
122 def __str__(self):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
123 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3525
diff changeset
124 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
125
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
126 @return string representing the error message
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
127 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
128 return str(self.errorMessage)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
129
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
130
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
131 def get_codingBytes(text):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
132 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
133 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
134
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
135 @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
136 @return coding string
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
137 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
138 lines = text.splitlines()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
139 for coding in codingBytes_regexps:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
140 coding_re = coding[1]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
141 head = lines[:coding[0]]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
142 for l in head:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
143 m = coding_re.search(l)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
144 if m:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
145 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
146 return None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
147
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
148
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
149 def get_coding(text):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
150 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
151 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
152
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
153 @param text text to inspect (string)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
154 @return coding string
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
155 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
156 lines = text.splitlines()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
157 for coding in coding_regexps:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
158 coding_re = coding[1]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
159 head = lines[:coding[0]]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
160 for l in head:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
161 m = coding_re.search(l)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
162 if m:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
163 return m.group(1).lower()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
164 return None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
165
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
166
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
167 def readEncodedFile(filename):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
168 """
2763
e4794166ad70 Fixed a serious typo.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2741
diff changeset
169 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
170
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
171 @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
172 @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
173 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
174 f = open(filename, "rb")
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
175 text = f.read()
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
176 f.close()
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
177 return decode(text)
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
178
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
179
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
180 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
181 """
2763
e4794166ad70 Fixed a serious typo.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2741
diff changeset
182 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
183 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
184
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
185 @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
186 @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
187 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
188 """
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 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
190 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
191 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
192 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
193 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
194 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
195
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
196
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
197 def decode(text):
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
198 """
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
199 Function to decode some byte text into a string.
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
200
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
201 @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
202 @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
203 """
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
204 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
205 if text.startswith(BOM_UTF8):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
206 # UTF-8 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
207 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
208 elif text.startswith(BOM_UTF16):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
209 # UTF-16 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
210 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
211 elif text.startswith(BOM_UTF32):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
212 # UTF-32 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
213 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
214 coding = get_codingBytes(text)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
215 if coding:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
216 return str(text, coding), coding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
217 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
218 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
219
159
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
220 # 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
221 try:
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
222 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
223 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
224 pass
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
225
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
226 guess = None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
227 if Preferences.getEditor("AdvancedEncodingDetection"):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
228 # Try the universal character encoding detector
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
229 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
230 import ThirdParty.CharDet.chardet
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
231 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
232 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
233 guess['encoding'] is not None:
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
234 codec = guess['encoding'].lower()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
235 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
236 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
237 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
238 except ImportError:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
239 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
240
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
241 # Try default encoding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
242 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
243 codec = Preferences.getEditor("DefaultEncoding")
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
244 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
245 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
246 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
247
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
248 if Preferences.getEditor("AdvancedEncodingDetection"):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
249 # 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
250 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
251 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
252 codec = guess['encoding'].lower()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
253 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
254 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
255 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
256
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
257 # Assume UTF-8 loosing information
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
258 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
259
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
260
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
261 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
262 """
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 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
264
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 @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
266 @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
267 @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
268 """
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 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
270 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
271 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
272 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
273 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
274 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
275 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
276 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
277 # 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
278 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
279 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
280 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
281 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
282 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
283 # 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
284 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
285 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
286 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
287
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
288
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
289 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
290 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
291 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
292
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
293 @param filename name of the file to read
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
294 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
295 @param text text to be written
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
296 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
297 @param origEncoding type of the original encoding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
298 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
299 @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
300 line is present
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
301 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
302 @return encoding used for writing the file
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
303 @rtype str
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
304 """
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
305 etext, encoding = encode(text, origEncoding, forcedEncoding=forcedEncoding)
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
306
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
307 f = open(filename, "wb")
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
308 f.write(etext)
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
309 f.close()
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
310
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
311 return encoding
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
312
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
313
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
314 def encode(text, origEncoding, forcedEncoding=""):
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
315 """
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
316 Function to encode text into a byte text.
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
317
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
318 @param text text to be encoded
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
319 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
320 @param origEncoding type of the original encoding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
321 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
322 @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
323 is present
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
324 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
325 @return tuple of encoded text and encoding used
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
326 @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
327 @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
328 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
329 encoding = None
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
330 if origEncoding == 'utf-8-bom':
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
331 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
332 else:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
333 # Try declared coding spec
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
334 coding = get_coding(text)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
335 if coding:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
336 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
337 etext, encoding = text.encode(coding), coding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
338 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
339 # Error: Declared encoding is incorrect
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
340 raise CodingError(coding)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
341 else:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
342 if forcedEncoding:
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
343 try:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
344 etext, encoding = (
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
345 text.encode(forcedEncoding), forcedEncoding)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
346 except (UnicodeError, LookupError):
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
347 # 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
348 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
349
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
350 if encoding is None:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
351 # Try the original encoding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
352 if origEncoding and origEncoding.endswith(
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
353 ('-selected', '-default', '-guessed', '-ignore')):
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
354 coding = origEncoding\
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
355 .replace("-selected", "")\
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
356 .replace("-default", "")\
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
357 .replace("-guessed", "")\
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
358 .replace("-ignore", "")
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
359 try:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
360 etext, encoding = text.encode(coding), coding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
361 except (UnicodeError, LookupError):
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
362 pass
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
363
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
364 if encoding is None:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
365 # Try configured default
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
366 try:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
367 codec = Preferences.getEditor("DefaultEncoding")
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
368 etext, encoding = text.encode(codec), codec
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
369 except (UnicodeError, LookupError):
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
370 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
371
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
372 if encoding is None:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
373 # Try saving as ASCII
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
374 try:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
375 etext, encoding = text.encode('ascii'), 'ascii'
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
376 except UnicodeError:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
377 pass
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
378
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
379 if encoding is None:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
380 # Save as UTF-8 without BOM
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
381 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
382
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
383 return etext, encoding
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
384
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
385
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
386 def decodeString(text):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
387 """
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
388 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
389
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
390 @param text text containing encoded chars (string)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
391 @return decoded text (string)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
392 """
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
393 buf = b""
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
394 index = 0
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
395 while index < len(text):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
396 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
397 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
398 buf += bytes(qb)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
399 index += 4
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
400 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
401 buf += codecs.encode(text[index], "utf-8")
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
402 index += 1
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
403 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
404 return decodeBytes(buf)
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
405
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
406
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
407 def decodeBytes(buffer):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
408 """
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
409 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
410
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
411 @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
412 @return decoded text (string)
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
413 """
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
414 # try UTF with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
415 try:
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
416 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
417 # UTF-8 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
418 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
419 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
420 # UTF-16 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
421 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
422 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
423 # UTF-32 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
424 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
425 except (UnicodeError, LookupError):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
426 pass
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
427
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
428 # try UTF-8
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
429 try:
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
430 return str(buffer, encoding="utf-8")
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
431 except UnicodeError:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
432 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
433
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
434 # try codec detection
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
435 try:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
436 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
437 guess = ThirdParty.CharDet.chardet.detect(buffer)
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
438 if guess and guess['encoding'] is not None:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
439 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
440 return str(buffer, encoding=codec)
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
441 except (UnicodeError, LookupError):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
442 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
443 except ImportError:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
444 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
445
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
446 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
447
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
448
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 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
450 """
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 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
452
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 @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
454 @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
455 """
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 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
457 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
458 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
459 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
460
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
461
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
462 _escape = re.compile("[&<>\"'\u0080-\uffff]")
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 _escape_map = {
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 "&": "&amp;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 "<": "&lt;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 ">": "&gt;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 '"': "&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
469 "'": "&#x27;",
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 }
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
472
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
473 def escape_entities(m, escmap=_escape_map):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 Function to encode html entities.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 @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
478 @param escmap the map of entities to encode
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 @return the converted text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 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
482 text = escmap.get(char)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 if text is None:
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
484 text = "&#{0:d};".format(ord(char))
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
487
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 def html_encode(text, pattern=_escape):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 Function to correctly encode a text for html.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 @param text text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 @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
494 @return the encoded text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 if not text:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 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
499 return text
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500
56
3bd61d38c924 Fixed a problem in Utilities related to HTML encoding of unicode characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 55
diff changeset
501 _uescape = re.compile('[\u0080-\uffff]')
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
503
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 def escape_uentities(m):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506 Function to encode html entities.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 @param m the match object
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 @return the converted text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 char = m.group()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
512 text = "&#{0:d};".format(ord(char))
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
515
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 def html_uencode(text, pattern=_uescape):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 Function to correctly encode a unicode text for html.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 @param text text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 @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
522 @return the encoded text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 if not text:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 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
527 return text
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528
6247
5c677a7f7d51 Corrected some code style issues detected by the new pycodestyle version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6105
diff changeset
529 _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
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
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 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
533 """
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 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
535
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 @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
537 @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
538 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
539 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
540 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
541 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
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
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 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
545 """
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 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
547
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 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
549 @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
550 @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
551 """
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 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
553 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
554 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
555 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
556
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
557
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 def convertLineEnds(text, eol):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560 Function to convert the end of line characters.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 @param text text to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
563 @param eol new eol setting (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 @return text with converted eols (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 if eol == '\r\n':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 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
568 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
569 elif eol == '\n':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 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
571 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
572 elif eol == '\r':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
573 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
574 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
575 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
576 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
578
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579 def linesep():
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 """
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
581 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
582
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 @return line separator used by the editor (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585 eolMode = Preferences.getEditor("EOLMode")
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 if eolMode == QsciScintilla.EolUnix:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 return "\n"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588 elif eolMode == QsciScintilla.EolMac:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 return "\r"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 return "\r\n"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
593
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
594 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
595 """
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 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
597
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 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
599 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
600 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
601 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
602 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
603 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
604 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
605
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 @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
607 @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
608 """
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
609 flags = {}
7192
a22eee00b052 Started removing runtime support for Python2 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7186
diff changeset
610 if isinstance(text, str):
2574
9d6b6cf31ec6 Fix for SyntaxCheckDialog and eflags evaluation.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2572
diff changeset
611 lines = text.rstrip().splitlines()
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
612 else:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
613 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
614 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
615 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
616 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
617 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
618 # 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
619 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
620
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 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
622 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
623 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
624 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
625 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
626
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 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
628 # 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
629 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
630 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
631
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 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
633 # 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
634 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
635 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
636 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
637 # 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
638 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
639 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
640 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
641
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 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
643 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
644 # 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
645 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
646 # 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
647 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
648 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
649 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
650
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 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
652
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
653
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
654 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
655 """
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 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
657
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 @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
659 @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
660 """
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 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
662 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
663 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
664 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
665
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 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
667
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
668
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
669 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
670 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
671 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
672 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
673
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 @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
675 @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
676 @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
677 @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
678 @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
679 """
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 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
681
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
682 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
683 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
684 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
685 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
686 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
687 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
688 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
689 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
690 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
691 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
692 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
693 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
694
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
695
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
696 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
697 """
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 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
699
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 @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
701 @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
702 @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
703 @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
704 """
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 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
706 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
707 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
708 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
709 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
710 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
711 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
712 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
713 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
714
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 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
716
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
717
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718 def toNativeSeparators(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720 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
721
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 @param path path to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 @return path with converted separator characters (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 return QDir.toNativeSeparators(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
727
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 def fromNativeSeparators(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730 Function returning a path, that is using "/" separator characters.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 @param path path to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733 @return path with converted separator characters (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735 return QDir.fromNativeSeparators(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
737
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
738 def normcasepath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
739 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
740 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
741 and references.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744 @return case normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
746 return os.path.normcase(os.path.normpath(path))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
747
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
748
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 def normabspath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751 Function returning a normalized, absolute path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756 return os.path.abspath(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
757
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
758
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759 def normcaseabspath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
760 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
761 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
762 its case and references.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
763
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
765 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767 return os.path.normcase(os.path.abspath(path))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
769
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 def normjoinpath(a, *p):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 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
773
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774 @param a first path to be joined (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 @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
776 @return normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 return os.path.normpath(os.path.join(a, *p))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
780
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 def normabsjoinpath(a, *p):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
783 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
784 into it.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 @param a first path to be joined (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787 @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
788 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790 return os.path.abspath(os.path.join(a, *p))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
792
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
793 def relpath(path, start=os.path.curdir):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
794 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795 Return a relative version of a path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797 @param path path to make relative (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 @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
799 @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
800 @exception ValueError raised to indicate an invalid path
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
801 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802 if not path:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803 raise ValueError("no path specified")
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
804
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
805 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
806 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
807
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
808 # 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
809 i = len(os.path.commonprefix([start_list, path_list]))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
810
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
811 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
812 if not rel_list:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813 return os.path.curdir
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
814 return os.path.join(*rel_list)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
815
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
816
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
817 def isinpath(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
818 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
819 Function to check for an executable file.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821 @param file filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822 @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
823 via the searchpath defined by the PATH environment variable.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
824 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
825 if os.path.isabs(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826 return os.access(file, os.X_OK)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
827
1329
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
828 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
829 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
830
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
831 path = getEnvironmentEntry('PATH')
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
832
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
833 # environment variable not defined
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
834 if path is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
835 return False
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
836
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
837 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
838 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
839 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
840 return True
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
841
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
842 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
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
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 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
846 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
847 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
848
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
849 @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
850 @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
851 @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
852 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
853 """
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 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 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
856 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
857 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
858 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
859 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
860 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
861 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
862 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
863 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
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
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 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
867 """
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 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
869
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 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
871 @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
872 @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
873 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
874 """
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 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
876 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
877 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
878 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
879 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
880 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
881 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
882 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
883 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
884 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
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
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 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
888 """
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 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
890 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
891
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 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
893 @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
894 @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
895 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
896 """
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 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
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
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 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
901 """
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 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
903 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
904
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 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
906 @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
907 @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
908 """
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 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
910 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
911 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
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
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 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
915 """
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 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
917 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
918
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 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
920 @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
921 @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
922 """
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 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
924 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
925 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
926
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
927
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
928 def getExecutablePath(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
929 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
930 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
931
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
932 @param file filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
933 @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
934 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
935 empty string otherwise.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
936 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
937 if os.path.isabs(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
938 if os.access(file, os.X_OK):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
939 return file
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
940 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
941 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
942
1329
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
943 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
944 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
945 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
946 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
947
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
948 path = os.getenv('PATH')
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
949
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
950 # environment variable not defined
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
951 if path is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
952 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
953
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
954 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
955 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
956 exe = os.path.join(directory, file)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
957 if os.access(exe, os.X_OK):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
958 return exe
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
959
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
960 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
961
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
962
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
963 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
964 """
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 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
966
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
967 @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
968 @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
969 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
970 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
971 """
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 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
973
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.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
975 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
976 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
977 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
978 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
979
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 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
981 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
982 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
983 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
984
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 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
986
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 # 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
988 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
989 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
990 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
991 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
992 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
993 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
994
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 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
996
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
997
5331
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
998 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
999 """
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1000 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
1001 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
1002
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
1003 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
1004 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
1005 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
1006
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1007 @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
1008 @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
1009 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
1010 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
1011 """
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.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
1013 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
1014 return file
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1015 else:
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1016 return ""
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1017
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
1018 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
1019
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1020 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
1021 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
1022 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
1023 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
1024 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
1025
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1026 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
1027
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1028 # 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
1029 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
1030 return ""
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1031
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1032 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
1033 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
1034 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
1035 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
1036 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
1037 return exe
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1038
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1039 return ""
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1040
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1041
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1042 def isExecutable(exe):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1043 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1044 Function to check, if a file is executable.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1045
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1046 @param exe filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1047 @return flag indicating executable status (boolean)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1048 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1049 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
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
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 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
1053 """
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 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
1055
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 @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
1057 @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
1058 @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
1059 @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
1060 """
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 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
1062 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
1063 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
1064 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
1065 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
1066
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
1067 return isDrive
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1068
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1069
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1070 def samepath(f1, f2):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1071 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1072 Function to compare two paths.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1073
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1074 @param f1 first path for the compare (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1075 @param f2 second path for the compare (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1076 @return flag indicating whether the two paths represent the
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1077 same path on disk.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1078 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1079 if f1 is None or f2 is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1080 return False
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1081
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
1082 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
1083 normcaseabspath(os.path.realpath(f2)):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1084 return True
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1085
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1086 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
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
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 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
1090 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1091 Function to compare two paths. Strips the filename.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1092
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
1093 @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
1094 @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
1095 @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
1096 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
1097 """
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 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
1099 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
1100
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1101 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
1102 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
1103 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
1104
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 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
1106
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1107 try:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1108 EXTSEP = os.extsep
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1109 except AttributeError:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1110 EXTSEP = "."
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1111
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1112
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1113 def splitPath(name):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1114 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1115 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
1116
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1117 @param name path name (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1118 @return a tuple of 2 strings (dirname, filename).
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1119 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1120 if os.path.isdir(name):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1121 dn = os.path.abspath(name)