eric6/Utilities/__init__.py

Sat, 19 Dec 2020 15:22:26 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 19 Dec 2020 15:22:26 +0100
changeset 7900
72b88fb20261
parent 7849
70e464748aaa
child 7907
7991ea245c20
permissions
-rw-r--r--

Corrected the use of '@keyparam' in the source documentation.

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
7360
9190402e4505 Updated copyright for 2020.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7329
diff changeset
3 # Copyright (c) 2003 - 2020 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
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import os
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 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
12 import codecs
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import re
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import fnmatch
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 import glob
114
f6e7cec11168 Changed the getUserName() function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 97
diff changeset
16 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
17 import ctypes
7628
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7599
diff changeset
18 import subprocess # secok
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
20
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
21 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
22 """
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
23 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
24
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
25 @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
26 @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
27 @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
28 @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
29 @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
30 @param line line causing the warning (ignored)
7599
d6efb1e15a5b Utilities: changed a doc string to not need a code style warning suppression anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7595
diff changeset
31 @raise err exception of type SyntaxError
d6efb1e15a5b Utilities: changed a doc string to not need a code style warning suppression anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7595
diff changeset
32 """
141
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
33 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
34 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
35 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
36 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
37 raise err
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
38
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 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
40 warnings.showwarning = __showwarning
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
44 from PyQt5.QtCore import (
7775
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7729
diff changeset
45 qVersion, PYQT_VERSION_STR, QDir, QProcess, QByteArray, QCoreApplication,
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7729
diff changeset
46 QCryptographicHash
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
47 )
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',
7841
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
81
7843
0a569bf8ca45 Utilities: fine tuned the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7841
diff changeset
82 'iso-8859-1', 'iso-8859-2', 'iso-8859-3',
0a569bf8ca45 Utilities: fine tuned the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7841
diff changeset
83 'iso-8859-4', 'iso-8859-5', 'iso-8859-6', 'iso-8859-7',
0a569bf8ca45 Utilities: fine tuned the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7841
diff changeset
84 'iso-8859-8', 'iso-8859-9', 'iso-8859-10', 'iso-8859-11',
0a569bf8ca45 Utilities: fine tuned the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7841
diff changeset
85 'iso-8859-13', 'iso-8859-14', 'iso-8859-15', 'iso-8859-16',
7841
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
86 'latin-1',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
87
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
88 'koi8-r', 'koi8-t', 'koi8-u',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
89
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
90 'utf-7',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
91 'utf-16', 'utf-16-be', 'utf-16-le',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
92 'utf-32', 'utf-32-be', 'utf-32-le',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
93
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
94 'cp037', 'cp273', 'cp424', 'cp437', 'cp500', 'cp720',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
95 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
96 'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
97 'cp864', 'cp865', 'cp866', 'cp869', 'cp874', 'cp875',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
98 'cp932', 'cp949', 'cp950', 'cp1006', 'cp1026', 'cp1125',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
99 'cp1140',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
100
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
101 'windows-1250', 'windows-1251', 'windows-1252', 'windows-1253',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
102 'windows-1254', 'windows-1255', 'windows-1256', 'windows-1257',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
103 'windows-1258',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
104
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
105 'gb2312', 'hz', 'gb18030', 'gbk',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
106
7843
0a569bf8ca45 Utilities: fine tuned the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7841
diff changeset
107 'iso-2022-jp', 'iso-2022-jp-1', 'iso-2022-jp-2', 'iso-2022-jp-2004',
0a569bf8ca45 Utilities: fine tuned the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7841
diff changeset
108 'iso-2022-jp-3', 'iso-2022-jp-ext', 'iso-2022-kr',
7841
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
109
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
110 'mac-cyrillic', 'mac-greek', 'mac-iceland', 'mac-latin2',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
111 'mac-roman', 'mac-turkish',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
112
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
113 'ascii',
1bb64cb3c4a8 Utilities: extended the list of supported codecs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
114 'big5-tw', 'big5-hkscs',
3030
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
115 ]
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
117
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
118 class CodingError(Exception):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
119 """
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
120 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
121 incorrect.
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
122 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
123 def __init__(self, coding):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
124 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
125 Constructor
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
126
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2770
diff changeset
127 @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
128 """
3030
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
129 self.errorMessage = QCoreApplication.translate(
4a0a82ddd9d2 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2997
diff changeset
130 "CodingError",
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
131 "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
132
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
133 def __repr__(self):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
134 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3525
diff changeset
135 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
136
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
137 @return string representing the error message
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
138 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
139 return str(self.errorMessage)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
140
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
141 def __str__(self):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
142 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3525
diff changeset
143 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
144
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
145 @return string representing the error message
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
146 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
147 return str(self.errorMessage)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
148
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
149
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
150 def get_codingBytes(text):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
151 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
152 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
153
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
154 @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
155 @return coding string
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
156 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
157 lines = text.splitlines()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
158 for coding in codingBytes_regexps:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
159 coding_re = coding[1]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
160 head = lines[:coding[0]]
7628
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7599
diff changeset
161 for line in head:
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7599
diff changeset
162 m = coding_re.search(line)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
163 if m:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
164 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
165 return None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
166
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
167
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
168 def get_coding(text):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
169 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
170 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
171
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
172 @param text text to inspect (string)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
173 @return coding string
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
174 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
175 lines = text.splitlines()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
176 for coding in coding_regexps:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
177 coding_re = coding[1]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
178 head = lines[:coding[0]]
7628
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7599
diff changeset
179 for line in head:
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7599
diff changeset
180 m = coding_re.search(line)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
181 if m:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
182 return m.group(1).lower()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
183 return None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
184
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
185
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
186 def readEncodedFile(filename):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
187 """
2763
e4794166ad70 Fixed a serious typo.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2741
diff changeset
188 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
189
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
190 @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
191 @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
192 """
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7775
diff changeset
193 with open(filename, "rb") as f:
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7775
diff changeset
194 text = f.read()
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
195 return decode(text)
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
196
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
197
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
198 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
199 """
2763
e4794166ad70 Fixed a serious typo.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2741
diff changeset
200 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
201 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
202
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
203 @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
204 @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
205 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
206 """
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7775
diff changeset
207 with open(filename, "rb") as f:
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7775
diff changeset
208 text = f.read()
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
209 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
210 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
211 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
212
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
213
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
214 def decode(text):
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
215 """
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
216 Function to decode some byte text into a string.
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
217
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
218 @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
219 @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
220 """
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
221 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
222 if text.startswith(BOM_UTF8):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
223 # UTF-8 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
224 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
225 elif text.startswith(BOM_UTF16):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
226 # UTF-16 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
227 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
228 elif text.startswith(BOM_UTF32):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
229 # UTF-32 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
230 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
231 coding = get_codingBytes(text)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
232 if coding:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
233 return str(text, coding), coding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
234 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
235 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
236
159
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
237 # 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
238 try:
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
239 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
240 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
241 pass
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
242
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
243 guess = None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
244 if Preferences.getEditor("AdvancedEncodingDetection"):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
245 # Try the universal character encoding detector
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
246 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
247 import ThirdParty.CharDet.chardet
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
248 guess = ThirdParty.CharDet.chardet.detect(text)
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
249 if (
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
250 guess and
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
251 guess['confidence'] > 0.95 and
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
252 guess['encoding'] is not None
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
253 ):
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
254 codec = guess['encoding'].lower()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
255 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
256 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
257 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
258 except ImportError:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
259 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
260
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
261 # Try default encoding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
262 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
263 codec = Preferences.getEditor("DefaultEncoding")
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
264 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
265 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
266 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
267
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
268 if Preferences.getEditor("AdvancedEncodingDetection"):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
269 # 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
270 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
271 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
272 codec = guess['encoding'].lower()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
273 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
274 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
275 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
276
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
277 # Assume UTF-8 loosing information
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
278 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
279
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
280
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
281 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
282 """
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 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
284
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 @param filename name of the file to read (string)
7900
72b88fb20261 Corrected the use of '@keyparam' in the source documentation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7849
diff changeset
286 @param encoding encoding to be used to read the file (string)
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
287 @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
288 """
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7775
diff changeset
289 with open(filename, "rb") as f:
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7775
diff changeset
290 text = f.read()
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
291 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
292 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
293 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
294 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
295 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
296 # 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
297 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
298 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
299 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
300 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
301 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
302 # 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
303 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
304 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
305 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
306
d1d4d79b4f11 Added a context menu entry to reload a file with a given encoding.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3206
diff changeset
307
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
308 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
309 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
310 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
311
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
312 @param filename name of the file to read
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
313 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
314 @param text text to be written
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
315 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
316 @param origEncoding type of the original encoding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
317 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
318 @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
319 line is present
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
320 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
321 @return encoding used for writing the file
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
322 @rtype str
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
323 """
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
324 etext, encoding = encode(text, origEncoding, forcedEncoding=forcedEncoding)
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
325
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7775
diff changeset
326 with open(filename, "wb") as f:
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7775
diff changeset
327 f.write(etext)
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
328
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
329 return encoding
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
330
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
331
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
332 def encode(text, origEncoding, forcedEncoding=""):
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
333 """
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
334 Function to encode text into a byte text.
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
335
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
336 @param text text to be encoded
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
337 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
338 @param origEncoding type of the original encoding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
339 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
340 @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
341 is present
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
342 @type str
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
343 @return tuple of encoded text and encoding used
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
344 @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
345 @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
346 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
347 encoding = None
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
348 if origEncoding == 'utf-8-bom':
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
349 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
350 else:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
351 # Try declared coding spec
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
352 coding = get_coding(text)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
353 if coding:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
354 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
355 etext, encoding = text.encode(coding), coding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
356 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
357 # Error: Declared encoding is incorrect
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
358 raise CodingError(coding)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
359 else:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
360 if forcedEncoding:
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
361 try:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
362 etext, encoding = (
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
363 text.encode(forcedEncoding), forcedEncoding)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
364 except (UnicodeError, LookupError):
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
365 # 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
366 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
367
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
368 if encoding is None:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
369 # Try the original encoding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
370 if origEncoding and origEncoding.endswith(
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
371 ('-selected', '-default', '-guessed', '-ignore')):
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
372 coding = (
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
373 origEncoding
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
374 .replace("-selected", "")
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
375 .replace("-default", "")
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
376 .replace("-guessed", "")
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
377 .replace("-ignore", "")
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
378 )
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
379 try:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
380 etext, encoding = text.encode(coding), coding
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
381 except (UnicodeError, LookupError):
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
382 pass
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
383
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
384 if encoding is None:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
385 # Try configured default
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
386 try:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
387 codec = Preferences.getEditor("DefaultEncoding")
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
388 etext, encoding = text.encode(codec), codec
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
389 except (UnicodeError, LookupError):
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
390 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
391
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
392 if encoding is None:
6099
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
393 # Try saving as ASCII
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
394 try:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
395 etext, encoding = text.encode('ascii'), 'ascii'
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
396 except UnicodeError:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
397 pass
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
398
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
399 if encoding is None:
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
400 # Save as UTF-8 without BOM
a7fecbc392d7 Started implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6080
diff changeset
401 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
402
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
403 return etext, encoding
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
404
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
405
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
406 def decodeString(text):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
407 """
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
408 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
409
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
410 @param text text containing encoded chars (string)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
411 @return decoded text (string)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
412 """
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
413 buf = b""
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
414 index = 0
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
415 while index < len(text):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
416 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
417 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
418 buf += bytes(qb)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
419 index += 4
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
420 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
421 buf += codecs.encode(text[index], "utf-8")
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
422 index += 1
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
423 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
424 return decodeBytes(buf)
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
425
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
426
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
427 def decodeBytes(buffer):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
428 """
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
429 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
430
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
431 @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
432 @return decoded text (string)
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
433 """
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
434 # try UTF with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
435 try:
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
436 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
437 # UTF-8 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
438 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
439 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
440 # UTF-16 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
441 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
442 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
443 # UTF-32 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
444 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
445 except (UnicodeError, LookupError):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
446 pass
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
447
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
448 # try UTF-8
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
449 try:
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
450 return str(buffer, encoding="utf-8")
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
451 except UnicodeError:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
452 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
453
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
454 # try codec detection
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
455 try:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
456 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
457 guess = ThirdParty.CharDet.chardet.detect(buffer)
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
458 if guess and guess['encoding'] is not None:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
459 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
460 return str(buffer, encoding=codec)
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
461 except (UnicodeError, LookupError):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
462 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
463 except ImportError:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
464 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
465
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
466 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
467
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
468
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
469 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
470 """
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
471 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
472
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
473 @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
474 @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
475 """
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
476 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
477 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
478 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
479 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
480
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
481
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
482 _escape = re.compile("[&<>\"'\u0080-\uffff]")
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 _escape_map = {
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 "&": "&amp;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486 "<": "&lt;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 ">": "&gt;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 '"': "&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
489 "'": "&#x27;",
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 }
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
492
5604
b047181a4a33 Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
493 def escape_entities(m, escmap=_escape_map):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 Function to encode html entities.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 @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
498 @param escmap the map of entities to encode
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 @return the converted text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 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
502 text = escmap.get(char)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 if text is None:
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
504 text = "&#{0:d};".format(ord(char))
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
507
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 def html_encode(text, pattern=_escape):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 Function to correctly encode a text for html.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 @param text text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 @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
514 @return the encoded text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 if not text:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 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
519 return text
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520
56
3bd61d38c924 Fixed a problem in Utilities related to HTML encoding of unicode characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 55
diff changeset
521 _uescape = re.compile('[\u0080-\uffff]')
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
523
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 def escape_uentities(m):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 Function to encode html entities.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 @param m the match object
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 @return the converted text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531 char = m.group()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
532 text = "&#{0:d};".format(ord(char))
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
535
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 def html_uencode(text, pattern=_uescape):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 Function to correctly encode a unicode text for html.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 @param text text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 @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
542 @return the encoded text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 if not text:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 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
547 return text
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548
6247
5c677a7f7d51 Corrected some code style issues detected by the new pycodestyle version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6105
diff changeset
549 _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
550
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
551
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 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
553 """
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 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
555
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 @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
557 @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
558 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
559 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
560 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
561 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
562
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
563
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
564 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
565 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
566 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
567
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
568 @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
569 @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
570 @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
571 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
572 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
573 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
574 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
575 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
576
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
577
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 def convertLineEnds(text, eol):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 Function to convert the end of line characters.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582 @param text text to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 @param eol new eol setting (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584 @return text with converted eols (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 if eol == '\r\n':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 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
588 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
589 elif eol == '\n':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 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
591 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
592 elif eol == '\r':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
593 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
594 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
595 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
598
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 def linesep():
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 """
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
601 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
602
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 @return line separator used by the editor (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605 eolMode = Preferences.getEditor("EOLMode")
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606 if eolMode == QsciScintilla.EolUnix:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 return "\n"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 elif eolMode == QsciScintilla.EolMac:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609 return "\r"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611 return "\r\n"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
613
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 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
615 """
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
616 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
617
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 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
619 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
620 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
621 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
622 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
623 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
624 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
625
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
626 @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
627 @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
628 """
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 = {}
7192
a22eee00b052 Started removing runtime support for Python2 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7186
diff changeset
630 if isinstance(text, str):
2574
9d6b6cf31ec6 Fix for SyntaxCheckDialog and eflags evaluation.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2572
diff changeset
631 lines = text.rstrip().splitlines()
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
632 else:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
633 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
634 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
635 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
636 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
637 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
638 # 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
639 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
640
5b1b59777460 Added a feature where flags can be given at the end of a source file. The only flag supported is 'FileType' to overwrite the filetype detection mechanism.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 768
diff changeset
641 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
642 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
643 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
644 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
645 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
646
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
647 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
648 # 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
649 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
650 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
651
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 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
653 # 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
654 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
655 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
656 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
657 # 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
658 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
659 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
660 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
661
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
662 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
663 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
664 # 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
665 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
666 # 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
667 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
668 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
669 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
670
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
671 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
672
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
673
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
674 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
675 """
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
676 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
677
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
678 @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
679 @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
680 """
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
681 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
682 source, encoding = readEncodedFile(filename)
7836
2f0d208b8137 Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7835
diff changeset
683 except (UnicodeError, OSError):
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
684 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
685
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
686 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
687
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
688
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
689 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
690 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
691 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
692 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
693
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
694 @param line line to extract flags from (string)
7900
72b88fb20261 Corrected the use of '@keyparam' in the source documentation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7849
diff changeset
695 @param startComment string identifying the start of the comment (string)
72b88fb20261 Corrected the use of '@keyparam' in the source documentation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7849
diff changeset
696 @param endComment string identifying the end of a comment (string)
72b88fb20261 Corrected the use of '@keyparam' in the source documentation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7849
diff changeset
697 @param 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
698 @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
699 """
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
700 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
701
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
702 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
703 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
704 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
705 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
706 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
707 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
708 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
709 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
710 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
711 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
712 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
713 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
714
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
715
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
716 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
717 """
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
718 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
719
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
720 @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
721 @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
722 @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
723 @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
724 """
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
725 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
726 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
727 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
728 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
729 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
730 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
731 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
732 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
733 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
734
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
735 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
736
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
737
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
738 def toNativeSeparators(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
739 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
740 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
741
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 @param path path to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743 @return path with converted separator characters (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745 return QDir.toNativeSeparators(path)
7835
0835ed67714b Changed code slightly to work with the upcoming qt5-applications package done by Kyle Altendorf.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
746
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
747
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
748 def fromNativeSeparators(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 Function returning a path, that is using "/" separator characters.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 @param path path to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 @return path with converted separator characters (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 return QDir.fromNativeSeparators(path)
7835
0835ed67714b Changed code slightly to work with the upcoming qt5-applications package done by Kyle Altendorf.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
756
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
757
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
758 def normcasepath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
760 Function returning 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
761 and references.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
762
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
763 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764 @return case normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
765 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766 return os.path.normcase(os.path.normpath(path))
7835
0835ed67714b Changed code slightly to work with the upcoming qt5-applications package done by Kyle Altendorf.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
767
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
768
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 def normcaseabspath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
771 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
772 its case and references.
0
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 path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
776 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 return os.path.normcase(os.path.abspath(path))
7835
0835ed67714b Changed code slightly to work with the upcoming qt5-applications package done by Kyle Altendorf.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
778
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
779
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 def normjoinpath(a, *p):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782 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
783
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784 @param a first path to be joined (string)
7849
70e464748aaa Utilities: removed some obsolete functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7843
diff changeset
785 @param p variable number of path parts to be joined (string)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 @return normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 return os.path.normpath(os.path.join(a, *p))
7835
0835ed67714b Changed code slightly to work with the upcoming qt5-applications package done by Kyle Altendorf.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
789
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
790
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791 def normabsjoinpath(a, *p):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
792 """
2997
7f0ef975da9e Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
793 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
794 into it.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796 @param a first path to be joined (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797 @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
798 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
800 return os.path.abspath(os.path.join(a, *p))
7835
0835ed67714b Changed code slightly to work with the upcoming qt5-applications package done by Kyle Altendorf.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
801
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
802
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803 def isinpath(file):
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 Function to check for an executable file.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
806
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
807 @param file filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
808 @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
809 via the searchpath defined by the PATH environment variable.
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 if os.path.isabs(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
812 return os.access(file, os.X_OK)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813
1329
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
814 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
815 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
816
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
817 path = getEnvironmentEntry('PATH')
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 # environment variable not defined
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820 if path is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821 return False
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
823 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
824 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
825 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
826 return True
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
827
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
828 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
829
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
830
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
831 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
832 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
833 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
834
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
835 @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
836 @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
837 @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
838 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
839 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
840 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
841 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
842 return True
7849
70e464748aaa Utilities: removed some obsolete functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7843
diff changeset
843 elif normcasepath(path).startswith(
70e464748aaa Utilities: removed some obsolete functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7843
diff changeset
844 normcasepath(start + "/")):
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
845 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
846 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
847 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
848 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
849 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
850
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
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 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
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 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
855 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
856
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 @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
858 @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
859 @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
860 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
861 """
7849
70e464748aaa Utilities: removed some obsolete functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7843
diff changeset
862 return fromNativeSeparators(os.path.relpath(path, start))
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
863
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
864
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
865 def 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
866 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
867 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
868 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
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 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
873 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
874 if not os.path.isabs(path):
7849
70e464748aaa Utilities: removed some obsolete functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7843
diff changeset
875 path = os.path.normpath(os.path.join(start, path))
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
876 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
877
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
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
879 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
880 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
881 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
882 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
883
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 @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
885 @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
886 @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
887 """
64b5f5fa3b00 Changed multi project files to contain relative paths to the contained project files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3670
diff changeset
888 if not os.path.isabs(path):
7849
70e464748aaa Utilities: removed some obsolete functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7843
diff changeset
889 path = toNativeSeparators(os.path.normpath(os.path.join(start, path)))
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
890 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
891
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
892
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
893 def getExecutablePath(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
894 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
895 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
896
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
897 @param file filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
898 @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
899 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
900 empty string otherwise.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
901 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
902 if os.path.isabs(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
903 if os.access(file, os.X_OK):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
904 return file
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
905 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
906 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
907
1329
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
908 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
909 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
910 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
911 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
912
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
913 path = os.getenv('PATH')
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
914
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
915 # environment variable not defined
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
916 if path is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
917 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
918
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
919 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
920 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
921 exe = os.path.join(directory, file)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
922 if os.access(exe, os.X_OK):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
923 return exe
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
924
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
925 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
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
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
928 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
929 """
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
930 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
931
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
932 @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
933 @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
934 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
935 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
936 """
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
937 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
938
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
939 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
940 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
941 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
942 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
943 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
944
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
945 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
946 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
947 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
948 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
949
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
950 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
951
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
952 # 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
953 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
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)
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
957 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
958 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
959
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
960 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
961
2ef00ee163a8 Added a function to Utilities to determine all possible full executable paths using the PATH environment variable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2319
diff changeset
962
5331
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
963 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
964 """
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
965 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
966 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
967
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
968 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
969 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
970 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
971
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
972 @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
973 @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
974 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
975 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
976 """
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
977 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
978 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
979 return file
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
980 else:
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
981 return ""
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
982
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
983 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
984
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
985 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
986 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
987 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
988 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
989 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
990
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
991 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
992
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
993 # 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
994 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
995 return ""
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
996
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
997 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
998 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
999 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
1000 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
1001 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
1002 return exe
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1003
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1004 return ""
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1005
8d607cd52f26 Fixed an issue locating the right PyQt5 tools executables on Windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5020
diff changeset
1006
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1007 def isExecutable(exe):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1008 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1009 Function to check, if a file is executable.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1010
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1011 @param exe filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1012 @return flag indicating executable status (boolean)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1013 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1014 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
1015
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
1016
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
1017 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
1018 """
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
1019 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
1020
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
1021 @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
1022 @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
1023 @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
1024 @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
1025 """
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
1026 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
1027 drive, directory = os.path.splitdrive(path)
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1028 if (
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1029 drive and
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1030 len(drive) == 2 and
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1031 drive.endswith(":") and
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1032 directory in ["", "\\", "/"]
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1033 ):
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
1034 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
1035
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
1036 return isDrive
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1037
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1038
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1039 def samepath(f1, f2):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1040 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1041 Function to compare two paths.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1042
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1043 @param f1 first path for the compare (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1044 @param f2 second path for the compare (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1045 @return flag indicating whether the two paths represent the
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1046 same path on disk.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1047 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1048 if f1 is None or f2 is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1049 return False
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1050
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1051 if (
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1052 normcaseabspath(os.path.realpath(f1)) ==
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1053 normcaseabspath(os.path.realpath(f2))
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
1054 ):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1055 return True
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1056
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1057 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
1058
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1059
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1060 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
1061 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1062 Function to compare two paths. Strips the filename.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1063
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
1064 @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
1065 @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
1066 @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
1067 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
1068 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1069 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
1070 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
1071
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1072 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
1073 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
1074 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
1075
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
1076 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
1077
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1078 try:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1079 EXTSEP = os.extsep
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1080 except AttributeError:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1081 EXTSEP = "."
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1082
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1083
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1084 def splitPath(name):
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 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
1087
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1088 @param name path name (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1089 @return a tuple of 2 strings (dirname, filename).
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1090 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1091 if os.path.isdir(name):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1092 dn = os.path.abspath(name)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1093 fn = "."
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1094 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1095 dn, fn = os.path.split(name)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1096 return (dn, fn)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1097
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1098
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1099 def joinext(prefix, ext):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1100 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1101 Function to join a file extension to a path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1102
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1103 The leading "." of ext is replaced by a platform specific extension
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1104 separator if necessary.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1105
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1106 @param prefix the basepart of the filename (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1107 @param ext the extension part (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1108 @return the complete filename (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1109 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1110 if ext[0] != ".":
3621
15f23ed3f216 Fixed a few source code style issues found by the updated pe8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3591
diff changeset
1111 ext = ".{0}".format(ext)
15f23ed3f216 Fixed a few source code style issues found by the updated pe8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3591
diff changeset
1112 # require leading separator to match os.path.splitext
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1113 return prefix + EXTSEP + ext[1:]
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1114
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1115
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1116 def compactPath(path, width, measure=len):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1117 """