[go: up one dir, main page]

Menu

[b34c80]: / common.py  Maximize  Restore  History

Download this file

204 lines (157 with data), 6.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
import sys, os
from urllib2 import urlopen, URLError, HTTPError
from wx.lib.docview import PathOnly
import wxversion
wxversion.select(['2.6', '2.7', '2.8'])
import wx
import language
# Set the encoding to unicode
if (sys.getdefaultencoding() != 'utf-8'):
reload(sys)
sys.setdefaultencoding('utf-8')
wx.SetDefaultPyEncoding('UTF-8')
RELEASE=False
maj_version = 0
mid_version = 7
min_version = 10
# For testing release
if (not RELEASE):
min_version += 0.5
debreate_version = u'%s.%s.%s' % (maj_version, mid_version, min_version)
db_version = (maj_version, mid_version, min_version)
db_here = PathOnly(__file__).decode(u'utf-8')
db_website = u'http://debreate.sourceforge.net'
maj_pyversion = sys.version_info[0]
mid_pyversion = sys.version_info[1]
min_pyversion = sys.version_info[2]
python_version = u'%s.%s.%s' % (maj_pyversion, mid_pyversion, min_pyversion)
print("Python version: %s" % python_version)
print("wxPython version: %s.%s.%s" % (wx.MAJOR_VERSION, wx.MINOR_VERSION, wx.RELEASE_VERSION))
print("Debreate version: %s.%s.%s" % (maj_version, mid_version, min_version))
##################
### FUNCTIONS ###
##################
def RequirePython(version):
error = 'Incompatible python version'
t = type(version)
if t == type(''):
if version == python_version[0:3]:
return
raise ValueError(error)
elif t == type([]) or t == type(()):
if python_version[0:3] in version:
return
raise ValueError(error)
raise ValueError('Wrong type for argument 1 of RequirePython(version)')
def GetCurrentVersion():
try:
request = urlopen(u'%s/current.txt' % (db_website))
version = request.readlines()[0]
version = version.split('.')
if ('\n' in version[-1]):
# Remove newline character
version[-1] = version[-1][:-1]
# Convert to integer
for v in range(0, len(version)):
version[v] = int(version[v])
# Change container to tuple and return it
version = (version[0], version[1], version[2])
return version
except URLError, err:
#err = unicode(err)
return err
### -*- Execute commands with sudo privileges -*- ###
def RunSudo(password, command):
command = u'echo %s | sudo -S %s ; echo $?' % (password, command)
wx.SafeYield()
output = os.popen(command).read()
err = int(output.split(u'\n')[-2])
if (err):
return False
return True
### -*- Function to check for installed executables -*- ###
def CommandExists(command):
try:
subprocess.Popen(command.split(u' ')[0].split(u' '))
exists = True
print u'First subprocess: %s' % (exists)
except OSError:
exists = os.path.isfile(command)
print u'os.path: %s' % (exists)
if exists:
subprocess.Popen((command))
print u'Second subprocess: %s' % (exists)
return exists
################
### CLASSES ###
################
### -*- A very handy widget that captures stdout and stderr to a wx.TextCtrl -*- ###
class OutputLog(wx.TextCtrl):
def __init__(self, parent, id=-1):
wx.TextCtrl.__init__(self, parent, id, style=wx.TE_MULTILINE|wx.TE_READONLY)
self.SetBackgroundColour(u'black')
self.SetForegroundColour(u'white')
self.stdout = sys.stdout
self.stderr = sys.stderr
def write(self, string):
self.AppendText(string)
def ToggleOutput(self, event=None):
if (sys.stdout == self):
sys.stdout = self.stdout
sys.stderr = self.stderr
else:
sys.stdout = self
sys.stdout = self
### -*- Dialog for overwrite prompt of a text area -*- ###
class OverwriteDialog(wx.Dialog):
def __init__(self, parent, id=-1, title=_(u'Overwrite?'), message=u''):
wx.Dialog.__init__(self, parent, id, title)
self.message = wx.StaticText(self, -1, message)
self.button_overwrite = wx.Button(self, ID_OVERWRITE, _(u'Overwrite'))
self.button_append = wx.Button(self, ID_APPEND, _(u'Append'))
self.button_cancel = wx.Button(self, wx.ID_CANCEL)
### -*- Button events -*- ###
wx.EVT_BUTTON(self.button_overwrite, ID_OVERWRITE, self.OnButton)
wx.EVT_BUTTON(self.button_append, ID_APPEND, self.OnButton)
hsizer = wx.BoxSizer(wx.HORIZONTAL)
hsizer.Add(self.button_overwrite, 0, wx.LEFT|wx.RIGHT, 5)
hsizer.Add(self.button_append, 0, wx.LEFT|wx.RIGHT, 5)
hsizer.Add(self.button_cancel, 0, wx.LEFT|wx.RIGHT, 5)
vsizer = wx.BoxSizer(wx.VERTICAL)
vsizer.Add(self.message, 1, wx.ALIGN_CENTER|wx.ALL, 5)
vsizer.Add(hsizer, 0, wx.ALIGN_RIGHT|wx.TOP|wx.BOTTOM, 5)
self.SetAutoLayout(True)
self.SetSizerAndFit(vsizer)
self.Layout()
def OnButton(self, event):
id = event.GetEventObject().GetId()
self.EndModal(id)
def GetMessage(self, event=None):
return self.message.GetLabel()
### -*- Object for Dropping Text Files -*-
class SingleFileTextDropTarget(wx.FileDropTarget):
def __init__(self, obj):
wx.FileDropTarget.__init__(self)
self.obj = obj
def OnDropFiles(self, x, y, filenames):
if len(filenames) > 1:
raise BaseError(_(u'Too many files'))
text = open(filenames[0]).read()
try:
if (not TextIsEmpty(self.obj.GetValue())):
overwrite = OverwriteDialog(self.obj, message = _(u'The text area is not empty!'))
id = overwrite.ShowModal()
if (id == ID_OVERWRITE):
self.obj.SetValue(text)
elif (id == ID_APPEND):
self.obj.SetInsertionPoint(-1)
self.obj.WriteText(text)
else:
self.obj.SetValue(text)
except UnicodeDecodeError:
wx.MessageDialog(None, _(u'Error decoding file'), _(u'Error'), wx.OK).ShowModal()
### -*- Checks if Text Control is Empty -*- ###
def TextIsEmpty(text):
text = u''.join(u''.join(text.split(u' ')).split(u'\n'))
return (text == u'')