Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Various functions to install `SciTE <http://www.scintilla.org/SciTE.html>`_. 

4""" 

5from __future__ import print_function 

6import sys 

7import re 

8import os 

9 

10from ..installhelper.install_cmd_helper import unzip_files 

11from .install_custom import download_page, download_from_sourceforge, download_file 

12from ..installhelper.link_shortcuts import add_shortcut_to_desktop, suffix 

13 

14if sys.version_info[0] == 2: 

15 from codecs import open 

16 

17 

18def IsSciteInstalled(dest_folder): 

19 """ 

20 check if Scite was already installed 

21 

22 @param dest_folder where it was installed 

23 @return boolean 

24 """ 

25 if sys.platform.startswith("win"): 

26 file = os.path.join(dest_folder, "wscite", "SciTE.exe") 

27 return os.path.exists(file) 

28 else: 

29 raise NotImplementedError("not available on platform " + sys.platform) 

30 

31 

32def install_scite(dest_folder=".", fLOG=print, install=True, change_python_path=False, version=None): 

33 """ 

34 install `SciTE <http://www.scintilla.org/SciTE.html>`_ (only on Windows) 

35 

36 @param dest_folder where to download the setup 

37 @param fLOG logging function 

38 @param install install (otherwise only download) 

39 @param change_python_path update python path as well (if *install* is True) 

40 @param version version to install (unused) 

41 @return temporary file 

42 

43 .. exref:: 

44 :title: install SciTE 

45 

46 The function downloads the latest version of SciTE. 

47 It also changes some settings for Python (no tabs, Courier New as a police). 

48 

49 :: 

50 

51 install_scite("my_folder_for_scite") 

52 

53 .. versionchanged:: 1.1 

54 Parameter *change_python_path* was added. 

55 """ 

56 if version is not None: 

57 raise ValueError("cannot specify a version") 

58 if IsSciteInstalled(dest_folder): 

59 return os.path.join( 

60 os.path.abspath(dest_folder), "wscite", "SciTE.exe") 

61 

62 if not sys.platform.startswith("win"): 

63 raise NotImplementedError( 

64 "SciTE can only be installed on Windows at the moment") 

65 

66 url = "http://www.scintilla.org/SciTEDownload.html" 

67 page = download_page(url) 

68 

69 rel = re.compile("Release ([0-9.]+)") 

70 rel = rel.findall(page) 

71 if len(rel) == 0: 

72 raise Exception("unable to find the release version") 

73 rel = rel[0] 

74 fLOG("[pymy] SciTE, release version ", rel) 

75 

76 reg = re.compile("<a href=\\\"(.*zip.*)\\\">full download</a>") 

77 find = reg.findall(page) 

78 if len(find) != 1: 

79 raise Exception("unable to find the file to download at " + 

80 url + "\nfound: " + str(len(find)) + "\n" + "\n".join(find)) 

81 

82 # should be something like http://www.scintilla.org/wscite356.zip 

83 newurl = find[0] 

84 outfile = os.path.join(dest_folder, "scite.zip") 

85 if not os.path.exists(outfile): 

86 download_file(newurl, outfile) 

87 

88 if os.path.exists(outfile): 

89 file = outfile 

90 else: 

91 newurl = "http://sourceforge.net/projects/scintilla/files/SciTE/{0}/wscite{1}.zip/download?use_mirror=autoselect".format( 

92 rel, 

93 rel.replace( 

94 ".", 

95 "")) 

96 fLOG("[pymy] SciTE, download from ", newurl) 

97 file = download_from_sourceforge( 

98 newurl, 

99 outfile, 

100 fLOG=fLOG, 

101 temp_folder=dest_folder) 

102 

103 if install: 

104 unzip_files(file, whereTo=dest_folder, fLOG=fLOG) 

105 modify_scite_properties(sys.executable if change_python_path else None, 

106 os.path.join(dest_folder, "wscite")) 

107 return os.path.join(os.path.abspath(dest_folder), "wscite", "SciTE.exe") 

108 else: 

109 return outfile 

110 

111 

112def modify_scite_properties(python_path, scite_path): 

113 """ 

114 modifies the scite properties 

115 

116 @param python_path python path 

117 @param scite_path scrite path 

118 

119 Avoid tabulations, change the path to the interpreter. 

120 

121 .. versionchanged:: 1.1 

122 If *python_path* is None, the function does not change it. 

123 """ 

124 if python_path is not None: 

125 # we change the path 

126 config = os.path.join(scite_path, "python.properties") 

127 with open(config, "r") as f: 

128 content = f.read() 

129 

130 # we change the executable 

131 lines = content.split("\n") 

132 for i, line in enumerate(lines): 

133 if "command.go.*.py=" in line: 

134 lines[ 

135 i] = ' command.go.*.py={0} -u "$(FileNameExt)"'.format(python_path) 

136 elif "command.go.*.pyw=" in line: 

137 lines[ 

138 i] = ' command.go.*.pyw={0} -u "$(FileNameExt)"'.format(python_path) 

139 content = "\n".join(lines) 

140 with open(config, "w") as f: 

141 f.write(content) 

142 

143 # we change the options 

144 config = os.path.join(scite_path, "SciTEGlobal.properties") 

145 with open(config, "r", encoding="utf8", errors="ignore") as f: 

146 content = f.read() 

147 content = content.replace("tabsize=8", "tabsize=4") 

148 content = content.replace("indent.size=8", "indent.size=4") 

149 content = content.replace("use.tabs=1", "use.tabs=0") 

150 content = content.replace("font:Verdana,", "font:Consolas,") 

151 with open(config, "w", encoding="utf8") as f: 

152 f.write(content) 

153 

154 

155def add_shortcut_to_desktop_for_scite(scite): 

156 """ 

157 create a shortcut on your desktop 

158 

159 @param scite scite location (SciTE.exe 

160 @return filename 

161 """ 

162 ver = suffix() 

163 return add_shortcut_to_desktop(scite, "SciTE." + ver, "SciTE." + ver)