/usr/share/pyshared/d_rats/platform.py is in d-rats 0.3.3-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | #!/usr/bin/python
#
# Copyright 2008 Dan Smith <dsmith@danplanet.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import glob
import commands
import subprocess
import urllib
def find_me():
return sys.modules["d_rats.platform"].__file__
class Platform(object):
# pylint: disable-msg=R0201
def __init__(self, basepath):
self._base = basepath
self._source_dir = os.path.abspath(".")
self._connected = True
def __str__(self):
l = ["Platform %s:" % str(self.__class__.__name__)]
l.append(" base: %s" % self.config_dir())
l.append(" source_dir: %s" % self.source_dir())
l.append(" OS version: %s" % self.os_version_string())
return os.linesep.join(l)
def config_dir(self):
return self._base
def source_dir(self):
return self._source_dir
def log_dir(self):
logdir = os.path.join(self.config_dir(), "logs")
if not os.path.isdir(logdir):
os.mkdir(logdir)
return logdir
def filter_filename(self, filename):
return filename
def log_file(self, filename):
filename = self.filter_filename(filename + ".txt").replace(" ", "_")
return os.path.join(self.log_dir(), filename)
def config_file(self, filename):
return os.path.join(self.config_dir(),
self.filter_filename(filename))
def open_text_file(self, path):
raise NotImplementedError("The base class can't do that")
def open_html_file(self, path):
raise NotImplementedError("The base class can't do that")
def list_serial_ports(self):
return []
def default_dir(self):
return "."
def gui_open_file(self, start_dir=None):
import gtk
dlg = gtk.FileChooserDialog("Select a file to open",
None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
if start_dir and os.path.isdir(start_dir):
dlg.set_current_folder(start_dir)
res = dlg.run()
fname = dlg.get_filename()
dlg.destroy()
if res == gtk.RESPONSE_OK:
return fname
else:
return None
def gui_save_file(self, start_dir=None, default_name=None):
import gtk
dlg = gtk.FileChooserDialog("Save file as",
None,
gtk.FILE_CHOOSER_ACTION_SAVE,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_SAVE, gtk.RESPONSE_OK))
if start_dir and os.path.isdir(start_dir):
dlg.set_current_folder(start_dir)
if default_name:
dlg.set_current_name(default_name)
res = dlg.run()
fname = dlg.get_filename()
dlg.destroy()
if res == gtk.RESPONSE_OK:
return fname
else:
return None
def gui_select_dir(self, start_dir=None):
import gtk
dlg = gtk.FileChooserDialog("Choose folder",
None,
gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_SAVE, gtk.RESPONSE_OK))
if start_dir and os.path.isdir(start_dir):
dlg.set_current_folder(start_dir)
res = dlg.run()
fname = dlg.get_filename()
dlg.destroy()
if res == gtk.RESPONSE_OK and os.path.isdir(fname):
return fname
else:
return None
def os_version_string(self):
return "Unknown Operating System"
def run_sync(self, command):
pipe = subprocess.Popen(command, stdout=subprocess.PIPE)
data = pipe.stdout.read()
return 0, data
def retrieve_url(self, url):
if self._connected:
return urllib.urlretrieve(url)
raise Exception("Not connected")
def set_connected(self, connected):
self._connected = connected
def play_sound(self, soundfile):
print "Sound is unsupported on this platform!"
class UnixPlatform(Platform):
def __init__(self, basepath):
if not basepath:
basepath = os.path.abspath(os.path.join(self.default_dir(),
".d-rats"))
if not os.path.isdir(basepath):
os.mkdir(basepath)
Platform.__init__(self, basepath)
def source_dir(self):
if "site-packages" in find_me():
return "/usr/share/d-rats"
elif "dist-packages" in find_me():
return "/usr/share/d-rats"
elif "pymodules" in find_me():
return "/usr/share/d-rats"
elif "pyshared" in find_me():
return "/usr/share/d-rats"
elif "/usr/share/d-rats" in find_me():
return "/usr/share/d-rats"
else:
return self._source_dir
def default_dir(self):
return os.path.abspath(os.getenv("HOME"))
def filter_filename(self, filename):
return filename.replace("/", "")
def _unix_doublefork_run(self, *args):
pid1 = os.fork()
if pid1 == 0:
pid2 = os.fork()
if pid2 == 0:
print "Exec'ing %s" % str(args)
os.execlp(args[0], *args)
else:
sys.exit(0)
else:
os.waitpid(pid1, 0)
print "Exec child exited"
def open_text_file(self, path):
self._unix_doublefork_run("gedit", path)
def open_html_file(self, path):
self._unix_doublefork_run("firefox", path)
def list_serial_ports(self):
return sorted(glob.glob("/dev/ttyS*") + glob.glob("/dev/ttyUSB*"))
def os_version_string(self):
# pylint: disable-msg=W0703
try:
issue = file("/etc/issue.net", "r")
ver = issue.read().strip()
issue.close()
ver = "%s - %s" % (os.uname()[0], ver)
except Exception:
ver = " ".join(os.uname())
return ver
def run_sync(self, command):
return commands.getstatusoutput(command)
def play_sound(self, soundfile):
import ossaudiodev
import sndhdr
try:
(t, r, c, f, b) = sndhdr.what(soundfile)
except Exception, e:
print "Unable to determine sound header of %s: %s" % (soundfile, e)
return
if t != "wav":
print "Unable to play non-wav file %s" % soundfile
return
if b != 16:
print "Unable to support strange non-16-bit audio (%i)" % b
return
dev = None
try:
dev = ossaudiodev.open("w")
dev.setfmt(ossaudiodev.AFMT_S16_LE)
dev.channels(c)
dev.speed(r)
f = file(soundfile, "rb")
dev.write(f.read())
f.close()
dev.close()
except Exception, e:
print "Error playing sound %s: %s" % (soundfile, e)
if dev:
dev.close()
class MacOSXPlatform(UnixPlatform):
def __init__(self, basepath):
# We need to make sure DISPLAY is set
if not os.environ.has_key("DISPLAY"):
print "Forcing DISPLAY for MacOS"
os.environ["DISPLAY"] = ":0"
os.environ["PANGO_RC_FILE"] = "../Resources/etc/pango/pangorc"
UnixPlatform.__init__(self, basepath)
def open_html_file(self, path):
self._unix_doublefork_run("open", path)
def open_text_file(self, path):
macos_textedit = "/Applications/TextEdit.app/Contents/MacOS/TextEdit"
self._unix_doublefork_run(macos_textedit, path)
def list_serial_ports(self):
keyspan = glob.glob("/dev/cu.KeySerial*")
prolific = glob.glob("/dev/tty.usbserial*")
return sorted(keyspan + prolific)
def os_version_string(self):
return "MacOS X"
def source_dir(self):
if "site-packages" in find_me():
return "../Resources"
else:
return self._source_dir
class Win32Platform(Platform):
def __init__(self, basepath=None):
if not basepath:
appdata = os.getenv("APPDATA")
if not appdata:
appdata = "C:\\"
basepath = os.path.abspath(os.path.join(appdata, "D-RATS"))
if not os.path.isdir(basepath):
os.mkdir(basepath)
Platform.__init__(self, basepath)
def default_dir(self):
return os.path.abspath(os.path.join(os.getenv("USERPROFILE"),
"Desktop"))
def filter_filename(self, filename):
for char in "/\\:*?\"<>|":
filename = filename.replace(char, "")
return filename
def open_text_file(self, path):
subprocess.Popen(["notepad", path])
return
def open_html_file(self, path):
subprocess.Popen(["explorer", path])
def list_serial_ports(self):
import win32file
import win32con
ports = []
for i in range(1, 257):
try:
portname = "COM%i" % i
mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE
port = \
win32file.CreateFile(portname,
mode,
win32con.FILE_SHARE_READ,
None,
win32con.OPEN_EXISTING,
0,
None)
ports.append(portname)
win32file.CloseHandle(port)
port = None
except Exception:
pass
return ports
def gui_open_file(self, start_dir=None):
# pylint: disable-msg=W0703,W0613
import win32gui
try:
fname, _, _ = win32gui.GetOpenFileNameW()
except Exception, e:
print "Failed to get filename: %s" % e
return None
return str(fname)
def gui_save_file(self, start_dir=None, default_name=None):
# pylint: disable-msg=W0703,W0613
import win32gui
try:
fname, _, _ = win32gui.GetSaveFileNameW(File=default_name)
except Exception, e:
print "Failed to get filename: %s" % e
return None
return str(fname)
def gui_select_dir(self, start_dir=None):
# pylint: disable-msg=W0703,W0613
from win32com.shell import shell
try:
pidl, _, _ = shell.SHBrowseForFolder()
fname = shell.SHGetPathFromIDList(pidl)
except Exception, e:
print "Failed to get directory: %s" % e
return None
return str(fname)
def os_version_string(self):
import win32api
vers = { 4: "Windows 2000",
5: "Windows XP",
6: "Windows Vista",
7: "Windows 7",
}
(pform, _, build, _, _) = win32api.GetVersionEx()
return vers.get(pform, "Win32 (Unknown %i:%i)" % (pform, build))
def play_sound(self, soundfile):
import winsound
winsound.PlaySound(soundfile, winsound.SND_FILENAME)
def _get_platform(basepath):
if os.name == "nt":
return Win32Platform(basepath)
elif sys.platform == "darwin":
return MacOSXPlatform(basepath)
else:
return UnixPlatform(basepath)
PLATFORM = None
def get_platform(basepath=None):
#pylint: disable-msg=W0602
global PLATFORM
if not PLATFORM:
PLATFORM = _get_platform(basepath)
return PLATFORM
if __name__ == "__main__":
def do_test():
__pform = get_platform()
print "Config dir: %s" % __pform.config_dir()
print "Default dir: %s" % __pform.default_dir()
print "Log file (foo): %s" % __pform.log_file("foo")
print "Serial ports: %s" % __pform.list_serial_ports()
print "OS Version: %s" % __pform.os_version_string()
#__pform.open_text_file("d-rats.py")
#print "Open file: %s" % __pform.gui_open_file()
#print "Save file: %s" % __pform.gui_save_file(default_name="Foo.txt")
#print "Open folder: %s" % __pform.gui_select_dir("/tmp")
do_test()
|