Utilities/__init__.py

branch
Py2 comp.
changeset 3484
645c12de6b0c
parent 3456
96232974dcdb
parent 3446
5a670e55adbb
child 3515
1b8381afe38f
equal deleted inserted replaced
3456:96232974dcdb 3484:645c12de6b0c
7 Package implementing various functions/classes needed everywhere within eric5. 7 Package implementing various functions/classes needed everywhere within eric5.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 try: 11 try:
12 str = unicode # __IGNORE_WARNING__ 12 str = unicode
13 import urllib 13 import urllib
14 14
15 def quote(url): 15 def quote(url):
16 """ 16 """
17 Replacement for the urllib.quote function because of unicode problems. 17 Replacement for the urllib.quote function because of unicode problems.
18 18
19 @param url text to quote (string) 19 @param url text to quote (string)
20 @return quoted url (string) 20 @return quoted url (string)
21 """ 21 """
22 return urllib.quote(url.encode('utf-8')) 22 return urllib.quote(url.encode('utf-8'))
23 except (NameError): 23 except NameError:
24 basestring = str 24 basestring = str
25 from urllib.parse import quote # __IGNORE_WARNING__ 25 from urllib.parse import quote # __IGNORE_WARNING__
26 26
27 import os 27 import os
28 import sys 28 import sys
69 from E5Gui.E5Application import e5App 69 from E5Gui.E5Application import e5App
70 70
71 from UI.Info import Program, Version 71 from UI.Info import Program, Version
72 72
73 import Preferences 73 import Preferences
74 from Plugins.CheckerPlugins.SyntaxChecker.SyntaxCheck import normalizeCode 74 from Plugins.CheckerPlugins.SyntaxChecker.SyntaxCheck import ( # __IGNORE_WARNING__
75 normalizeCode)
75 76
76 from eric5config import getConfig 77 from eric5config import getConfig
77 78
78 configDir = None 79 configDir = None
79 80
265 except (UnicodeError, LookupError): 266 except (UnicodeError, LookupError):
266 pass 267 pass
267 268
268 # Assume UTF-8 loosing information 269 # Assume UTF-8 loosing information
269 return str(text, "utf-8", "ignore"), 'utf-8-ignore' 270 return str(text, "utf-8", "ignore"), 'utf-8-ignore'
271
272
273 def readEncodedFileWithEncoding(filename, encoding):
274 """
275 Function to read a file and decode its contents into proper text.
276
277 @param filename name of the file to read (string)
278 @keyparam encoding encoding to be used to read the file (string)
279 @return tuple of decoded text and encoding (string, string)
280 """
281 f = open(filename, "rb")
282 text = f.read()
283 f.close()
284 if encoding:
285 try:
286 return str(text, encoding), '{0}-selected'.format(encoding)
287 except (UnicodeError, LookupError):
288 pass
289 # Try default encoding
290 try:
291 codec = Preferences.getEditor("DefaultEncoding")
292 return str(text, codec), '{0}-default'.format(codec)
293 except (UnicodeError, LookupError):
294 pass
295 # Assume UTF-8 loosing information
296 return str(text, "utf-8", "ignore"), 'utf-8-ignore'
297 else:
298 return decode(text)
270 299
271 300
272 def writeEncodedFile(filename, text, orig_coding): 301 def writeEncodedFile(filename, text, orig_coding):
273 """ 302 """
274 Function to write a file with properly encoded text. 303 Function to write a file with properly encoded text.
1296 return sys.hexversion >> 16 1325 return sys.hexversion >> 16
1297 1326
1298 1327
1299 def determinePythonVersion(filename, source, editor=None): 1328 def determinePythonVersion(filename, source, editor=None):
1300 """ 1329 """
1301 Determine the python version of a given file. 1330 Function to determine the python version of a given file.
1302 1331
1303 @param filename name of the file with extension (str) 1332 @param filename name of the file with extension (str)
1304 @param source of the file (str) 1333 @param source of the file (str)
1305 @keyparam editor if the file is opened already (Editor object) 1334 @keyparam editor reference to the editor, if the file is opened
1335 already (Editor object)
1306 @return Python version if file is Python2 or Python3 (int) 1336 @return Python version if file is Python2 or Python3 (int)
1307 """ 1337 """
1308 pyAssignment = {"Python": 2, "Python2": 2, "Python3": 3} 1338 pyAssignment = {"Python": 2, "Python2": 2, "Python3": 3}
1309 1339
1310 if not editor: 1340 if not editor:
1353 if pyVer == 0 and ext in py2Ext + py3Ext: 1383 if pyVer == 0 and ext in py2Ext + py3Ext:
1354 pyVer = sys.version_info[0] 1384 pyVer = sys.version_info[0]
1355 1385
1356 if editor and pyVer: 1386 if editor and pyVer:
1357 editor.filetype = "Python{0}".format(pyVer) 1387 editor.filetype = "Python{0}".format(pyVer)
1388
1358 return pyVer 1389 return pyVer
1359 1390
1360 1391
1361 ############################################################################### 1392 ###############################################################################
1362 # functions for environment handling 1393 # functions for environment handling
1703 GetUserNameEx(NameDisplay, None, size) 1734 GetUserNameEx(NameDisplay, None, size)
1704 1735
1705 nameBuffer = ctypes.create_unicode_buffer(size.contents.value) 1736 nameBuffer = ctypes.create_unicode_buffer(size.contents.value)
1706 GetUserNameEx(NameDisplay, nameBuffer, size) 1737 GetUserNameEx(NameDisplay, nameBuffer, size)
1707 return nameBuffer.value 1738 return nameBuffer.value
1739
1740 ###############################################################################
1741 # Javascript related functions below
1742 ###############################################################################
1743
1744
1745 def jsCheckSyntax(file, codestring=""):
1746 """
1747 Function to check a Javascript source file for syntax errors.
1748
1749 @param file source filename (string)
1750 @param codestring string containing the code to check (string)
1751 @return A tuple indicating status (True = an error was found), the
1752 file name, the line number and the error message (boolean, string,
1753 string, string). The values are only valid, if the status is True.
1754 """
1755 import jasy.js.parse.Parser as jsParser
1756 import jasy.js.tokenize.Tokenizer as jsTokenizer
1757
1758 if not codestring:
1759 try:
1760 codestring = readEncodedFile(file)[0]
1761 except (UnicodeDecodeError, IOError):
1762 return (False, None, None, None)
1763
1764 # normalize line endings
1765 codestring = codestring.replace("\r\n", "\n")
1766 codestring = codestring.replace("\r", "\n")
1767
1768 # ensure source ends with an eol
1769 if codestring and codestring[-1] != '\n':
1770 codestring = codestring + '\n'
1771
1772 try:
1773 jsParser.parse(codestring, file)
1774 except (jsParser.SyntaxError, jsTokenizer.ParseError) as exc:
1775 details = exc.args[0]
1776 error, details = details.splitlines()
1777 fn, line = details.strip().rsplit(":", 1)
1778 error = error.split(":", 1)[1].strip()
1779 return (True, fn, line, error)
1780
1781 return (False, None, None, None)

eric ide

mercurial