/usr/share/pyshared/hotot/utils.py is in hotot-gtk 1:0.9.8.14-2.
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 | # -*- coding: UTF-8 -*-
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
import subprocess
import os
import sys
from webbrowser import _iscommand as is_command
import gtk
import mimetypes, mimetools
import urllib
import config
import locale
try: import i18n
except: from gettext import gettext as _
supported_locate = {
'en_US': 'en'
, 'zh_CN': 'zh_CN'
, 'ja_JP': 'ja'
, 'fr_FR': 'fr'
, 'es_ES': 'es'
, 'pt_BR': 'pt_BR'
}
_browser = ''
def open_webbrowser(uri):
'''open a URI in the registered default application
'''
## for proxychains
os.environ['LD_PRELOAD'] = ' '.join(
[ ld for ld in os.environ.get('LD_PRELOAD', '').split(' ') if 'libproxychains.so' not in ld ]
)
browser = 'xdg-open'
if sys.platform[:3] == "win":
browser = 'start'
subprocess.Popen([browser, uri])
def webkit_set_proxy_uri(scheme = None, host = None, port = None, user = None, passwd = None):
from ctypes import CDLL, c_void_p, c_char_p, c_int
try:
if os.name == 'nt':
libgobject = CDLL('libgobject-2.0-0.dll')
libsoup = CDLL('libsoup-2.4-1.dll')
libwebkit = CDLL('libwebkit-1.0-2.dll')
else:
libgobject = CDLL('libgobject-2.0.so.0')
libsoup = CDLL('libsoup-2.4.so.1')
try:
libwebkit = CDLL('libwebkitgtk-1.0.so.0')
except:
libwebkit = CDLL('libwebkit-1.0.so.2')
pass
get_session = libwebkit.webkit_get_default_session
get_session.restype = c_void_p
session = get_session()
g_object_set = libgobject.g_object_set
if session == 0:
return 1
g_object_set.argtypes = [ c_void_p, c_char_p, c_int, c_void_p ]
g_object_set(session, "max-conns", 20, None)
g_object_set(session, "max-conns-per-host", 5, None)
if not scheme:
return 1
elif ":" in scheme:
soup_uri_new = libsoup.soup_uri_new
soup_uri_new.restype = c_void_p
soup_uri_new.argtypes = [ c_char_p ]
proxy_uri = soup_uri_new(str(scheme))
g_object_set.argtypes = [ c_void_p, c_char_p, c_void_p, c_void_p ]
g_object_set(session, "proxy-uri", proxy_uri, None)
elif host:
soup_uri_new = libsoup.soup_uri_new
soup_uri_new.restype = c_void_p
soup_uri_new.argtypes = [ c_char_p ]
proxy_uri = soup_uri_new("http://127.0.0.1")
if proxy_uri == 0:
return 1
soup_uri_set_scheme = libsoup.soup_uri_set_scheme
soup_uri_set_scheme.argtypes = [ c_void_p, c_char_p ]
soup_uri_set_scheme(proxy_uri, str(scheme))
soup_uri_set_host = libsoup.soup_uri_set_host
soup_uri_set_host.argtypes = [ c_void_p, c_char_p ]
soup_uri_set_host(proxy_uri, str(host))
if port:
soup_uri_set_port = libsoup.soup_uri_set_port
soup_uri_set_port.argtypes = [ c_void_p, c_int ]
soup_uri_set_port(proxy_uri, int(port))
if user:
soup_uri_set_user = libsoup.soup_uri_set_user
soup_uri_set_user.argtypes = [ c_void_p, c_char_p ]
soup_uri_set_user(proxy_uri, str(user))
if passwd:
soup_uri_set_password = libsoup.soup_uri_set_password
soup_uri_set_password.argtypes = [ c_void_p, c_char_p ]
soup_uri_set_password(proxy_uri, str(passwd))
g_object_set.argtypes = [ c_void_p, c_char_p, c_void_p, c_void_p ]
g_object_set(session, "proxy-uri", proxy_uri, None)
return 0
except:
exctype, value = sys.exc_info()[:2]
print 'error: webkit_set_proxy_uri: (%s, %s)' % (exctype,value)
return 1
def open_file_chooser_dialog():
sel_file = None
fc_dlg = gtk.FileChooserDialog(title='Open ... '
, parent=None
, action=gtk.FILE_CHOOSER_ACTION_OPEN
, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN,gtk.RESPONSE_OK))
fc_dlg.set_default_response(gtk.RESPONSE_OK)
resp = fc_dlg.run()
if resp == gtk.RESPONSE_OK:
sel_file = fc_dlg.get_filename()
fc_dlg.destroy()
gtk.gdk.threads_leave()
return sel_file
def encode_multipart_formdata(fields, files):
BOUNDARY = mimetools.choose_boundary()
CRLF = '\r\n'
L = []
total_size = 0
L = []
for key, value in fields.items():
key, value = str(key).encode('utf8'), str(value).encode('utf8')
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
L.append(value)
for pair in files:
key, filename = pair[0].encode('utf8'), pair[1].encode('utf8')
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"; filename="%s"' %
(key, 'hotot.png'));
L.append('Content-Type: %s' % get_content_type(filename))
L.append('')
L.append(file(filename).read())
total_size += os.path.getsize(filename)
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join(L)
headers = {'content-type':'multipart/form-data; boundary=%s' % BOUNDARY
, 'content-length': str(len(body))};
return headers, body
def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
def get_ui_object(name):
for base in config.DATA_DIRS:
fullpath = os.path.join(base, name)
if os.path.exists(fullpath):
return fullpath
def get_extra_exts():
import glob
exts = []
files = glob.glob(os.path.join(config.CONF_DIR, config.EXT_DIR_NAME) + '/*')
ext_dirs = filter(lambda x: os.path.isdir(x), files)
for dir in ext_dirs:
ext_js = os.path.join(dir, 'entry.js')
if os.path.exists(ext_js):
exts.append('file://%s' % ext_js)
return exts
def get_extra_themes():
import glob
themes = []
files = glob.glob(os.path.join(config.CONF_DIR, config.THEME_DIR_NAME) + '/*')
theme_dirs = filter(lambda x: os.path.isdir(x), files)
for dir in theme_dirs:
info_file = os.path.join(dir, 'info.json')
style_file = os.path.join(dir, 'style.css')
if os.path.exists(info_file) and os.path.exists(style_file):
themes.append('file://%s' % dir)
return themes
def get_extra_fonts():
font_list = [ff.get_name() for ff in
gtk.gdk.pango_context_get().list_families()]
font_list.sort()
for font in font_list:
try:
font.decode('ascii')
except:
font_list.remove(font)
font_list.insert(0, font)
return font_list
def get_locale():
try:
lang, encode = locale.getdefaultlocale()
except:
lang = 'en'
if lang in supported_locate:
return supported_locate[lang]
return 'en'
def get_file_path_from_dnd_dropped_uri(uri):
path = ""
if uri.startswith('file:\\\\\\'): # windows
path = uri[8:] # 8 is len('file:///')
elif uri.startswith('file://'): # nautilus, rox
path = uri[7:] # 7 is len('file://')
elif uri.startswith('file:'): # xffm
path = uri[5:] # 5 is len('file:')
path = urllib.url2pathname(path) # escape special chars
path = path.strip('\r\n\x00') # remove \r\n and NULL
return path
|