Utilities/__init__.py

Mon, 25 Mar 2013 03:11:06 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Mon, 25 Mar 2013 03:11:06 +0100
branch
Py2 comp.
changeset 2525
8b507a9a2d40
parent 2319
116c547dedce
child 2571
e6bb19eb87ea
permissions
-rw-r--r--

Script changes: Future import added, super calls modified and unicode behavior for str.

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
2302
f29e9405c851 Updated copyright for 2013.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2239
diff changeset
3 # Copyright (c) 2003 - 2013 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 """
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
7 Package implementing various functions/classes needed everywhere within eric5.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2319
diff changeset
10 from __future__ import unicode_literals # __IGNORE_WARNING__
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2319
diff changeset
11 try:
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2319
diff changeset
12 str = unicode
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2319
diff changeset
13 except (NameError):
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2319
diff changeset
14 pass
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2319
diff changeset
15
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 import os
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 import sys
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 import re
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 import fnmatch
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 import glob
114
f6e7cec11168 Changed the getUserName() function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 97
diff changeset
21 import getpass
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
23
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
24 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
25 """
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
26 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
27
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
28 @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
29 @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
30 @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
31 @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
32 @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
33 @param line line causing the warning (ignored)
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
34 @raise SyntaxError
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
35 """
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
36 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
37 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
38 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
39 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
40 raise err
c76f1ec28766 Added code to handle syntax warnings during the compile much better.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 114
diff changeset
41
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 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
43 warnings.showwarning = __showwarning
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46
682
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
47 from PyQt4.QtCore import QRegExp, QDir, QProcess, Qt, QByteArray, \
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
48 qVersion, PYQT_VERSION_STR, QCoreApplication, QCryptographicHash
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 from PyQt4.Qsci import QSCINTILLA_VERSION_STR, QsciScintilla
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50
882
34b86be88bf0 Redid the native file dialog code to be future proof.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 832
diff changeset
51 from Globals import isWindowsPlatform, isLinuxPlatform, isMacPlatform # __IGNORE_WARNING__
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2085
diff changeset
52 from Globals import getConfigDir, setConfigDir # __IGNORE_WARNING__
882
34b86be88bf0 Redid the native file dialog code to be future proof.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 832
diff changeset
53 # import these methods into the Utilities namespace
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54
55
b5c84934de9c Renamed E4Gui to E5Gui.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 53
diff changeset
55 from E5Gui.E5Application import e5App
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 from UI.Info import Program, Version
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 import Preferences
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
795
917f1945355c Added a syntax checker function for Python 2 files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
61 from eric5config import getConfig
917f1945355c Added a syntax checker function for Python 2 files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
62
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 configDir = None
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
65 codingBytes_regexps = [
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
66 (2, re.compile(br'''coding[:=]\s*([-\w_.]+)''')),
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
67 (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
68 ]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
69 coding_regexps = [
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
70 (2, re.compile(r'''coding[:=]\s*([-\w_.]+)''')),
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
71 (1, re.compile(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
72 ]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
73
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
74 supportedCodecs = ['utf-8',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
75 'iso8859-1', 'iso8859-15', 'iso8859-2', 'iso8859-3',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
76 'iso8859-4', 'iso8859-5', 'iso8859-6', 'iso8859-7',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
77 'iso8859-8', 'iso8859-9', 'iso8859-10', 'iso8859-11',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
78 'iso8859-13', 'iso8859-14', 'iso8859-16', 'latin-1',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
79 'koi8-r', 'koi8-u',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
80 'utf-16', 'utf-32',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
81 'cp037', 'cp424', 'cp437', 'cp500', 'cp737', 'cp775',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
82 'cp850', 'cp852', 'cp855', 'cp856', 'cp857', 'cp860',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
83 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', 'cp866',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
84 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
85 'cp1006', 'cp1026', 'cp1140', 'cp1250', 'cp1251',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
86 'cp1252', 'cp1253', 'cp1254', 'cp1255', 'cp1256',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
87 'cp1257', 'cp1258',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
88 'gb2312', 'gb18030',
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 'ascii']
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
91
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
92 class CodingError(Exception):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
93 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
94 Class implementing an exception, which is raised, if a given coding is incorrect.
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
95 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
96 def __init__(self, coding):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
97 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
98 Constructor
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
99 """
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
100 self.errorMessage = QCoreApplication.translate("CodingError",
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
101 "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
102
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
103 def __repr__(self):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
104 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
105 Private method returning a representation of the exception.
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
106
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
107 @return string representing the error message
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
108 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
109 return str(self.errorMessage)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
110
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
111 def __str__(self):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
112 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
113 Private method returning a string representation of the exception.
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
114
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
115 @return string representing the error message
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
116 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
117 return str(self.errorMessage)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
118
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
119
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
120 def get_codingBytes(text):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
121 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
122 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
123
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
124 @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
125 @return coding string
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
126 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
127 lines = text.splitlines()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
128 for coding in codingBytes_regexps:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
129 coding_re = coding[1]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
130 head = lines[:coding[0]]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
131 for l in head:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
132 m = coding_re.search(l)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
133 if m:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
134 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
135 return None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
136
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
137
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
138 def get_coding(text):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
139 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
140 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
141
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
142 @param text text to inspect (string)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
143 @return coding string
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 lines = text.splitlines()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
146 for coding in coding_regexps:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
147 coding_re = coding[1]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
148 head = lines[:coding[0]]
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
149 for l in head:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
150 m = coding_re.search(l)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
151 if m:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
152 return m.group(1).lower()
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
153 return None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
154
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
155
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
156 def readEncodedFile(filename):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
157 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
158 Function to read a file and decode it's contents into proper text.
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
159
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
160 @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
161 @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
162 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
163 f = open(filename, "rb")
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
164 text = f.read()
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
165 f.close()
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
166 return decode(text)
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
167
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
168
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
169 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
170 """
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
171 Function to read a file, calculate a hash value and decode it's contents
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
172 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
173
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
174 @param filename name of the file to read (string)
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
175 @return tuple of decoded text, encoding and hash value (string, string, string)
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
176 """
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
177 f = open(filename, "rb")
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
178 text = f.read()
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
179 f.close()
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
180 hash = str(QCryptographicHash.hash(QByteArray(text), QCryptographicHash.Md5).toHex(),
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
181 encoding="ASCII")
682
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
182 return decode(text) + (hash, )
91114a975eda Added a check to the find and replace dialog to make sure, that no external changes were done between the find run and the replace run.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 565
diff changeset
183
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
184
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
185 def decode(text):
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
186 """
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
187 Function to decode some byte text into a string.
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
188
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
189 @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
190 @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
191 """
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
192 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
193 if text.startswith(BOM_UTF8):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
194 # UTF-8 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
195 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
196 elif text.startswith(BOM_UTF16):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
197 # UTF-16 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
198 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
199 elif text.startswith(BOM_UTF32):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
200 # UTF-32 with BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
201 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
202 coding = get_codingBytes(text)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
203 if coding:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
204 return str(text, coding), coding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
205 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
206 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
207
159
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
208 # 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
209 try:
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
210 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
211 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
212 pass
2e79b9c72e59 Changed order of encodings detection to give UTF-8 a chance first.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 141
diff changeset
213
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
214 guess = None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
215 if Preferences.getEditor("AdvancedEncodingDetection"):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
216 # Try the universal character encoding detector
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
217 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
218 import ThirdParty.CharDet.chardet
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
219 guess = ThirdParty.CharDet.chardet.detect(text)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
220 if guess and guess['confidence'] > 0.95 and guess['encoding'] is not None:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
221 codec = guess['encoding'].lower()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
222 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
223 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
224 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
225 except ImportError:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
226 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
227
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
228 # Try default encoding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
229 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
230 codec = Preferences.getEditor("DefaultEncoding")
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
231 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
232 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
233 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
234
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
235 if Preferences.getEditor("AdvancedEncodingDetection"):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
236 # 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
237 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
238 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
239 codec = guess['encoding'].lower()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
240 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
241 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
242 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
243
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
244 # Assume UTF-8 loosing information
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
245 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
246
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
247
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
248 def writeEncodedFile(filename, text, orig_coding):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
249 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
250 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
251
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
252 @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
253 @param text text to be written (string)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
254 @param orig_coding type of the original encoding (string)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
255 @return encoding used for writing the file (string)
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
256 """
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
257 etext, encoding = encode(text, orig_coding)
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
258
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
259 f = open(filename, "wb")
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
260 f.write(etext)
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
261 f.close()
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
262
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
263 return encoding
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
264
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
265
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
266 def encode(text, orig_coding):
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
267 """
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
268 Function to encode text into a byte text.
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
269
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
270 @param text text to be encoded (string)
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
271 @param orig_coding type of the original encoding (string)
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
272 @return tuple of encoded text and encoding used (bytes, string)
45
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
273 """
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
274 encoding = None
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
275 if orig_coding == 'utf-8-bom':
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
276 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
277 else:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
278 # Try declared coding spec
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
279 coding = get_coding(text)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
280 if coding:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
281 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
282 etext, encoding = text.encode(coding), coding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
283 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
284 # Error: Declared encoding is incorrect
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
285 raise CodingError(coding)
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
286 else:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
287 if orig_coding and orig_coding.endswith(
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
288 ('-selected', '-default', '-guessed', '-ignore')):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
289 coding = orig_coding\
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
290 .replace("-selected", "")\
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
291 .replace("-default", "")\
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
292 .replace("-guessed", "")\
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
293 .replace("-ignore", "")
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
294 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
295 etext, encoding = text.encode(coding), coding
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
296 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
297 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
298
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
299 if encoding is None:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
300 # Try configured default
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
301 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
302 codec = Preferences.getEditor("DefaultEncoding")
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
303 etext, encoding = text.encode(codec), codec
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
304 except (UnicodeError, LookupError):
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
305 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
306
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
307 if encoding is None:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
308 # Try saving as ASCII
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
309 try:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
310 etext, encoding = text.encode('ascii'), 'ascii'
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
311 except UnicodeError:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
312 pass
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
313
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
314 if encoding is None:
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
315 # Save as UTF-8 without BOM
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
316 etext, encoding = text.encode('utf-8'), 'utf-8'
9a18f4dbb493 Did a lot of encoding related stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 41
diff changeset
317
46
b09750fd2a06 Added utility functions to encode/decode some text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 45
diff changeset
318 return etext, encoding
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
319
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
320
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
321 def decodeString(text):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
322 """
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
323 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
324
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
325 @param text text containing encoded chars (string)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
326 @return decoded text (string)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
327 """
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
328 buf = b""
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
329 index = 0
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
330 while index < len(text):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
331 if text[index] == "\\":
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
332 qb = QByteArray.fromHex(text[index:index + 4])
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
333 buf += bytes(qb)
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
334 index += 4
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
335 else:
1659
d5215347c209 Fixed an issue in Utilities.decodeString().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
336 buf += bytes(text[index], encoding="utf-8")
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
337 index += 1
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
338 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
339 return decodeBytes(buf)
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
340
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
341
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
342 def decodeBytes(buffer):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
343 """
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
344 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
345
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
346 @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
347 @return decoded text (string)
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
348 """
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
349 # try UTF with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
350 try:
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
351 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
352 # UTF-8 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
353 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
354 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
355 # UTF-16 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
356 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
357 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
358 # UTF-32 with BOM
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
359 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
360 except (UnicodeError, LookupError):
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
361 pass
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
362
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
363 # try UTF-8
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
364 try:
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
365 return str(buffer, encoding="utf-8")
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
366 except UnicodeError:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
367 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
368
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
369 # try codec detection
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
370 try:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
371 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
372 guess = ThirdParty.CharDet.chardet.detect(buffer)
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
373 if guess and guess['encoding'] is not None:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
374 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
375 return str(buffer, encoding=codec)
753
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
376 except (UnicodeError, LookupError):
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
377 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
378 except ImportError:
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
379 pass
e19a516f0a97 Continued implementing the certificate manager.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 682
diff changeset
380
1109
9552b5e16a07 Added some more search engines to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 988
diff changeset
381 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
382
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
383
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
384 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
385 """
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
386 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
387
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
388 @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
389 @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
390 """
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
391 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
392 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
393 data = b""
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
394 return data.decode()
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
395
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
396
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
397 _escape = re.compile("[&<>\"'\u0080-\uffff]")
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 _escape_map = {
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 "&": "&amp;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 "<": "&lt;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 ">": "&gt;",
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 '"': "&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
404 "'": "&#x27;",
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 }
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
407
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 def escape_entities(m, map=_escape_map):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 Function to encode html entities.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 @param m the match object
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 @param map the map of entities to encode
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 @return the converted text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 char = m.group()
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 text = map.get(char)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 if text is None:
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
419 text = "&#{0:d};".format(ord(char))
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
422
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 def html_encode(text, pattern=_escape):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 Function to correctly encode a text for html.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 @param text text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 @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
429 @return the encoded text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 if not text:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 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
434 return text
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435
56
3bd61d38c924 Fixed a problem in Utilities related to HTML encoding of unicode characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 55
diff changeset
436 _uescape = re.compile('[\u0080-\uffff]')
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
438
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 def escape_uentities(m):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 Function to encode html entities.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 @param m the match object
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 @return the converted text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 char = m.group()
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
447 text = "&#{0:d};".format(ord(char))
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
450
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 def html_uencode(text, pattern=_uescape):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 Function to correctly encode a unicode text for html.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 @param text text to be encoded (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 @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
457 @return the encoded text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 if not text:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 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
462 return text
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463
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
464 _uunescape = re.compile('&#\d+;')
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
465
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
466
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
467 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
468 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
469 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
470
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
471 @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
472 @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
473 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
474 char = m.group()
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
475 ord = int(char[2:-1])
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
476 return chr(ord)
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
477
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
478
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
479 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
480 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
481 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
482
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
483 @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
484 @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
485 @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
486 """
b140a24e147a Rewrote the reader for HTML bookmark files to cope with the various dates.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1659
diff changeset
487 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
488 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
489 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
490 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
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
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 def convertLineEnds(text, eol):
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 convert the end of line characters.
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 text text to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 @param eol new eol setting (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 @return text with converted eols (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 if eol == '\r\n':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502 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
503 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
504 elif eol == '\n':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 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
506 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
507 elif eol == '\r':
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 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
509 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
510 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
513
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 def linesep():
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 Function to return the lineseparator used by the editor.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 @return line separator used by the editor (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 eolMode = Preferences.getEditor("EOLMode")
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 if eolMode == QsciScintilla.EolUnix:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 return "\n"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 elif eolMode == QsciScintilla.EolMac:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 return "\r"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 return "\r\n"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
528
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
529 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
530 """
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
531 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
532
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
533 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
534 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
535 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
536 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
537 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
538 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
539 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
540
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
541 @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
542 @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
543 """
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
544 flags = {}
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
545 if isinstance(text, str):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
546 lines = text.splitlines()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
547 else:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 805
diff changeset
548 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
549 for line in reversed(lines):
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
550 index = line.find("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
551 if index == -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
552 # 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
553 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
554
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
555 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
556 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
557 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
558 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
559 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
560
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
561 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
562 # 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
563 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
564 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
565
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
566 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
567 # 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
568 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
569 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
570 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
571 # 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
572 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
573 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
574 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
575
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
576 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
577 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
578 # treat it as a boolean
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
579 flags[flag] = True
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
580
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
581 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
582
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
583
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
584 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
585 """
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
586 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
587
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
588 @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
589 @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
590 """
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
591 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
592 source, encoding = readEncodedFile(filename)
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
593 except (UnicodeError, IOError):
a94b0a2fafd7 Extended the unit test interface to support skipped tests, expected failures and unexpected successes and to better cope with Python2 scripts when run from the eric IDE.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1142
diff changeset
594 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
595
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
596 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
597
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
598
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
599 def extractLineFlags(line, startComment="#", endComment=""):
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
600 """
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
601 Function to extract flags starting and ending with '__' from a line comment.
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
602
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
603 @param line line to extract flags from (string)
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
604 @keyparam startComment string identifying the start of the comment (string)
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
605 @keyparam endComment string identifying the end of a comment (string)
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
606 @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
607 """
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
608 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
609
1309
6caa231421cf Fixed a silly issue in the new code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1308
diff changeset
610 pos = line.rfind(startComment)
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
611 if pos >= 0:
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
612 comment = line[pos + len(startComment):].strip()
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
613 if endComment:
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
614 comment = comment.replace("endComment", "")
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
615 flags = [f.strip() for f in comment.split()
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
616 if (f.startswith("__") and f.endswith("__"))]
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
617 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
618
2b602a1521e7 Made the check for line flags like __IGNORE_WARNING__ more flexible and extensible.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1190
diff changeset
619
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 def toNativeSeparators(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622 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
623
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 @param path path to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
625 @return path with converted separator characters (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627 return QDir.toNativeSeparators(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
629
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 def fromNativeSeparators(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 Function returning a path, that is using "/" separator characters.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634 @param path path to be converted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635 @return path with converted separator characters (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 return QDir.fromNativeSeparators(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
639
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640 def normcasepath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 Function returning a path, that is normalized with respect to its case and references.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
645 @return case normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647 return os.path.normcase(os.path.normpath(path))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
649
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
650 def normabspath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
651 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
652 Function returning a normalized, absolute path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
654 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
656 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657 return os.path.abspath(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
659
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660 def normcaseabspath(path):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 """
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
662 Function returning an absolute path, that is normalized with respect to its case
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
663 and references.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
664
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 @param path file path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 return os.path.normcase(os.path.abspath(path))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
670
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 def normjoinpath(a, *p):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 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
674
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 @param a first path to be joined (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 @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
677 @return normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
679 return os.path.normpath(os.path.join(a, *p))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
681
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 def normabsjoinpath(a, *p):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684 Function returning a normalized, absolute path of the joined parts passed into it.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686 @param a first path to be joined (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 @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
688 @return absolute, normalized path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
689 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690 return os.path.abspath(os.path.join(a, *p))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
691
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
692
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
693 def relpath(path, start=os.path.curdir):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
694 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
695 Return a relative version of a path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
696
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
697 @param path path to make relative (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 @param start path to make relative from (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
699 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
700 if not path:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
701 raise ValueError("no path specified")
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
702
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
703 start_list = os.path.abspath(start).split(os.path.sep)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
704 path_list = os.path.abspath(path).split(os.path.sep)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
705
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
706 # Work out how much of the filepath is shared by start and path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 i = len(os.path.commonprefix([start_list, path_list]))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
708
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
709 rel_list = [os.path.pardir] * (len(start_list) - i) + path_list[i:]
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
710 if not rel_list:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
711 return os.path.curdir
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
712 return os.path.join(*rel_list)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
714
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
715 def isinpath(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
716 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
717 Function to check for an executable file.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719 @param file filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720 @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
721 via the searchpath defined by the PATH environment variable.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 if os.path.isabs(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 return os.access(file, os.X_OK)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725
1329
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
726 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
727 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
728
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 path = getEnvironmentEntry('PATH')
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 # environment variable not defined
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 if path is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733 return False
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735 dirs = path.split(os.pathsep)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736 for dir in dirs:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737 if os.access(os.path.join(dir, file), os.X_OK):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
738 return True
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 return False
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
742
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743 def getExecutablePath(file):
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 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
746
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
747 @param file filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
748 @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
749 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
750 empty string otherwise.
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 if os.path.isabs(file):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 if os.access(file, os.X_OK):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 return file
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
757
1329
ac21bb9cf8b9 Ammended 'isinpath' and 'getExecutablePath' to check the current directory as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1316
diff changeset
758 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
759 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
760 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
761 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
762
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
763 path = os.getenv('PATH')
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
765 # environment variable not defined
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766 if path is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 dirs = path.split(os.pathsep)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 for dir in dirs:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771 exe = os.path.join(dir, file)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 if os.access(exe, os.X_OK):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773 return exe
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
776
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
777
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 def isExecutable(exe):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 Function to check, if a file is executable.
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 @param exe filename of the executable to check (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783 @return flag indicating executable status (boolean)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 return os.access(exe, os.X_OK)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
787
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 def samepath(f1, f2):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790 Function to compare two paths.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
792 @param f1 first path for the compare (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793 @param f2 second path for the compare (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
794 @return flag indicating whether the two paths represent the
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795 same path on disk.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797 if f1 is None or f2 is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 return False
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 if normcaseabspath(os.path.realpath(f1)) == normcaseabspath(os.path.realpath(f2)):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
801 return True
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803 return False
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 try:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
806 EXTSEP = os.extsep
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
807 except AttributeError:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
808 EXTSEP = "."
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
809
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
810
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
811 def splitPath(name):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
812 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813 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
814
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
815 @param name path name (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
816 @return a tuple of 2 strings (dirname, filename).
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
817 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
818 if os.path.isdir(name):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
819 dn = os.path.abspath(name)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820 fn = "."
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822 dn, fn = os.path.split(name)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
823 return (dn, fn)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
824
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
825
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826 def joinext(prefix, ext):
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 Function to join a file extension to a path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
829
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
830 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
831 separator if necessary.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
832
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
833 @param prefix the basepart of the filename (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
834 @param ext the extension part (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
835 @return the complete filename (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
836 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
837 if ext[0] != ".":
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
838 ext = ".{0}".format(ext) # require leading separator, to match os.path.splitext
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
839 return prefix + EXTSEP + ext[1:]
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
840
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
841
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
842 def compactPath(path, width, measure=len):
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
843 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
844 Function to return a compacted path fitting inside the given width.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
845
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
846 @param path path to be compacted (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
847 @param width width for the compacted path (integer)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
848 @param measure reference to a function used to measure the length of the string
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
849 @return compacted path (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
850 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
851 if measure(path) <= width:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
852 return path
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
853
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
854 ellipsis = '...'
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
855
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
856 head, tail = os.path.split(path)
343
f23d1272de6b Changed algorithm for compactPath() slightly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 177
diff changeset
857 mid = len(head) // 2
f23d1272de6b Changed algorithm for compactPath() slightly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 177
diff changeset
858 head1 = head[:mid]
f23d1272de6b Changed algorithm for compactPath() slightly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 177
diff changeset
859 head2 = head[mid:]
f23d1272de6b Changed algorithm for compactPath() slightly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 177
diff changeset
860 while head1:
f23d1272de6b Changed algorithm for compactPath() slightly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 177
diff changeset
861 # head1 is same size as head2 or one shorter
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
862 path = os.path.join("{0}{1}{2}".format(head1, ellipsis, head2), tail)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
863 if measure(path) <= width:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
864 return path
343
f23d1272de6b Changed algorithm for compactPath() slightly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 177
diff changeset
865 head1 = head1[:-1]
f23d1272de6b Changed algorithm for compactPath() slightly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 177
diff changeset
866 head2 = head2[1:]
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
867 path = os.path.join(ellipsis, tail)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
868 if measure(path) <= width:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
869 return path
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
870 while tail:
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
871 path = "{0}{1}".format(ellipsis, tail)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
872 if measure(path) <= width:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
873 return path
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
874 tail = tail[1:]
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
875 return ""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
876
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
877
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
878 def direntries(path, filesonly=False, pattern=None, followsymlinks=True, checkStop=None):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
879 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
880 Function returning a list of all files and directories.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
881
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
882 @param path root of the tree to check
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
883 @param filesonly flag indicating that only files are wanted
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
884 @param pattern a filename pattern to check against
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
885 @param followsymlinks flag indicating whether symbolic links
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
886 should be followed
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
887 @param checkStop function to be called to check for a stop
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
888 @return list of all files and directories in the tree rooted
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
889 at path. The names are expanded to start with path.
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
890 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
891 if filesonly:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
892 files = []
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
893 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
894 files = [path]
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
895 try:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
896 entries = os.listdir(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
897 for entry in entries:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
898 if checkStop and checkStop():
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
899 break
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
900
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
901 if entry in ['CVS', 'cvs',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
902 '.svn', '_svn',
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
903 '.hg', '_hg',
177
c822ccc4d138 Added some code to tune performance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 159
diff changeset
904 '.ropeproject', '_ropeproject',
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
905 '.eric5project', '_eric5project',
177
c822ccc4d138 Added some code to tune performance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 159
diff changeset
906 '.issues', '_issues']:
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
907 continue
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
908
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
909 fentry = os.path.join(path, entry)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
910 if pattern and \
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
911 not os.path.isdir(fentry) and \
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
912 not fnmatch.fnmatch(entry, pattern):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
913 # entry doesn't fit the given pattern
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
914 continue
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
915
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
916 if os.path.isdir(fentry):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
917 if os.path.islink(fentry) and not followsymlinks:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
918 continue
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
919 files += direntries(fentry, filesonly, pattern, followsymlinks, checkStop)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
920 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
921 files.append(fentry)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
922 except OSError:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
923 pass
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
924 except UnicodeDecodeError:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
925 pass
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
926 return files
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
927
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
928
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
929 def getDirs(path, excludeDirs):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
930 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
931 Function returning a list of all directories below path.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
932
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
933 @param path root of the tree to check
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
934 @param excludeDirs basename of directories to ignore
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
935 @return list of all directories found
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
936 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
937 try:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
938 names = os.listdir(path)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
939 except EnvironmentError:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
940 return
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
941
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
942 dirs = []
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
943 for name in names:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
944 if os.path.isdir(os.path.join(path, name)) and \
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
945 not os.path.islink(os.path.join(path, name)):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
946 exclude = 0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
947 for e in excludeDirs:
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
948 if name.split(os.sep, 1)[0] == e:
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
949 exclude = 1
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
950 break
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
951 if not exclude:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
952 dirs.append(os.path.join(path, name))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
953
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
954 for name in dirs[:]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
955 if not os.path.islink(name):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
956 dirs = dirs + getDirs(name, excludeDirs)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
957
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
958 return dirs
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
959
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
960
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
961 def getTestFileName(fn):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
962 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
963 Function to build the filename of a unittest file.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
964
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
965 The filename for the unittest file is built by prepending
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
966 the string "test" to the filename passed into this function.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
967
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
968 @param fn filename basis to be used for the unittest filename (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
969 @return filename of the corresponding unittest file (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
970 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
971 dn, fn = os.path.split(fn)
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
972 return os.path.join(dn, "test{0}".format(fn))
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
973
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
974
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
975 def parseOptionString(s):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
976 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
977 Function used to convert an option string into a list of options.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
978
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
979 @param s option string (string or string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
980 @return list of options (list of strings)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
981 """
768
df2dbf3072be Fixed an issue with blanks in pathname of eric plug-ins when developing them.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 753
diff changeset
982 rx = QRegExp(r"""\s([\w=/-]*"[^"]+"|[\w=/-]*'[^']+'|[^\s]+)""")
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
983 s = re.sub(r"%[A-Z%]", _percentReplacementFunc, s)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
984 return parseString(s, rx)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
985
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
986
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
987 def parseEnvironmentString(s):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
988 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
989 Function used to convert an environment string into a list of environment settings.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
990
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
991 @param s environment string (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
992 @return list of environment settings (list of strings)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
993 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
994 rx = QRegExp(r"""\s(\w+\+?=[^\s]+|\w+="[^"]+"|\w+='[^']+')""")
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
995 return parseString(s, rx)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
996
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
997
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
998 def parseString(s, rx):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
999 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1000 Function used to convert a string into a list.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1001
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1002 @param s string to be parsed (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1003 @param rx regex defining the parse pattern (QRegExp)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1004 @return list of parsed data (list of strings)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1005 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1006 olist = []
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1007 if not s.startswith(' '):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1008 # prepare the string to fit our pattern
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1009 s = ' ' + s
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 pos = rx.indexIn(s)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1012 while pos != -1:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1013 cs = rx.cap(1)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1014 if cs.startswith('"') or cs.startswith("'"):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1015 cs = cs[1:-1]
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1016 olist.append(cs)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1017 pos += rx.matchedLength()
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1018 pos = rx.indexIn(s, pos)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1019
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1020 return olist
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1021
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1022
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1023 def _percentReplacementFunc(matchobj):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1024 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1025 Protected function called for replacing % codes.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1026
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1027 @param matchobj matchobject for the code
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1028 @return replacement string
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1029 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1030 return getPercentReplacement(matchobj.group(0))
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1031
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1032
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1033 def getPercentReplacement(code):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1034 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1035 Function to get the replacement for code.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1036
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1037 @param code code indicator (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1038 @return replacement string (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1039 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1040 if code in ["C", "%C"]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1041 # column of the cursor of the current editor
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
1042 aw = e5App().getObject("ViewManager").activeWindow()
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1043 if aw is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1044 column = -1
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1045 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1046 column = aw.getCursorPosition()[1]
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
1047 return "{0:d}".format(column)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1048 elif code in ["D", "%D"]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1049 # directory of active editor
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
1050 aw = e5App().getObject("ViewManager").activeWindow()
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1051 if aw is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1052 dn = "not_available"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1053 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1054 fn = aw.getFileName()
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1055 if fn is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1056 dn = "not_available"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1057 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1058 dn = os.path.dirname(fn)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1059 return dn
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1060 elif code in ["F", "%F"]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1061 # filename (complete) of active editor
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
1062 aw = e5App().getObject("ViewManager").activeWindow()
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1063 if aw is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1064 fn = "not_available"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1065 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1066 fn = aw.getFileName()
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1067 if fn is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1068 fn = "not_available"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1069 return fn
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1070 elif code in ["H", "%H"]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1071 # home directory
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1072 return getHomeDir()
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1073 elif code in ["L", "%L"]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1074 # line of the cursor of the current editor
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
1075 aw = e5App().getObject("ViewManager").activeWindow()
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1076 if aw is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1077 line = 0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1078 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1079 line = aw.getCursorPosition()[0] + 1
432
497e188ee86e Did some more string format conversions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
1080 return "{0:d}".format(line)
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1081 elif code in ["P", "%P"]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1082 # project path
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
1083 projectPath = e5App().getObject("Project").getProjectPath()
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1084 if not projectPath:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1085 projectPath = "not_available"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1086 return projectPath
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1087 elif code in ["S", "%S"]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1088 # selected text of the current editor
53
c3eb7cc1ff8b Renamed e5App to e5App.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 46
diff changeset
1089 aw = e5App().getObject("ViewManager").activeWindow()
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1090 if aw is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1091 text = "not_available"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1092 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1093 text = aw.selectedText()
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1094 return text
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1095 elif code in ["U", "%U"]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1096 # username
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1097 un = getUserName()
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1098 if un is None:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1099 return code
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1100 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1101 return un
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1102 elif code in ["%", "%%"]:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1103 # the percent sign
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1104 return "%"
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1105 else:
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1106 # unknown code, just return it
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1107 return code
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1108
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1109
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1110 def getPercentReplacementHelp():
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1111 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1112 Function to get the help text for the supported %-codes.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1113
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1114 @returns help text (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1115 """
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1116 return QCoreApplication.translate("Utilities",
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1117 """<p>You may use %-codes as placeholders in the string."""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1118 """ Supported codes are:"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1119 """<table>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1120 """<tr><td>%C</td><td>column of the cursor of the current editor</td></tr>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1121 """<tr><td>%D</td><td>directory of the current editor</td></tr>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1122 """<tr><td>%F</td><td>filename of the current editor</td></tr>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1123 """<tr><td>%H</td><td>home directory of the current user</td></tr>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1124 """<tr><td>%L</td><td>line of the cursor of the current editor</td></tr>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1125 """<tr><td>%P</td><td>path of the current project</td></tr>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1126 """<tr><td>%S</td><td>selected text of the current editor</td></tr>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1127 """<tr><td>%U</td><td>username of the current user</td></tr>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1128 """<tr><td>%%</td><td>the percent sign</td></tr>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1129 """</table>"""
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1130 """</p>""")
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1131
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1132
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1133 def getUserName():
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1134 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1135 Function to get the user name.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1136
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1137 @return user name (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1138 """
114
f6e7cec11168 Changed the getUserName() function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 97
diff changeset
1139 user = getpass.getuser()
f6e7cec11168 Changed the getUserName() function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 97
diff changeset
1140
f6e7cec11168 Changed the getUserName() function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 97
diff changeset
1141 if not user and isWindowsPlatform():
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1142 return win32_GetUserName()
2227
b7aceb255831 First commit of the simple IRC client for eric. It is usable but not yet complete.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2194
diff changeset
1143
b7aceb255831 First commit of the simple IRC client for eric. It is usable but not yet complete.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2194
diff changeset
1144 return user
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1145
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 915
diff changeset
1146
2239
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1147 def getRealName():
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1148 """
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1149 Function to get the real name of the user.
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1150
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1151 @return real name of the user (string)
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1152 """
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1153 if isWindowsPlatform():
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1154 return win32_getRealName()
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1155 else:
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1156 import pwd
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1157 user = getpass.getuser()
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2227
diff changeset
1158 return pwd.getpwnam(user).pw_gecos
a47b50e80a20 Continued with the IRC management.
Detlev Offenbach <detlev@die-offenbachs.de>