115 lines
2.6 KiB
Python
115 lines
2.6 KiB
Python
import traceback
|
|
|
|
from ctypes import cdll, create_string_buffer, byref
|
|
from platform import system
|
|
from collections import OrderedDict
|
|
from os.path import join, exists, getmtime, isfile
|
|
from urllib.parse import parse_qs
|
|
|
|
from IzzyLib import logging
|
|
from IzzyLib.cache import LRUCache
|
|
from IzzyLib.http import HttpClient
|
|
from gi.repository import Gio, GLib, Notify, WebKit2
|
|
|
|
from . import __version__
|
|
from .config import var
|
|
from .database import db
|
|
|
|
|
|
Notif = Notify.Notification
|
|
Client = HttpClient()
|
|
|
|
|
|
class ObjDict(object):
|
|
def __init__(self, d={}, ordered=False):
|
|
self.__dict__ = d if not ordered else OrderedDict(d)
|
|
self.items = self._values().items
|
|
self.values = self._values().values
|
|
self.keys = self._values().keys
|
|
self.update = self._values().update
|
|
self.pop = self._values().pop
|
|
|
|
def _values(self):
|
|
items = {}
|
|
|
|
for k, v in self.__dict__.items():
|
|
if k not in ['items', 'values', 'keys', 'update', 'pop', 'get']:
|
|
items[k] = v
|
|
|
|
return items
|
|
|
|
def get(self, key, backup=None):
|
|
return self.__dict__.get(key, backup)
|
|
|
|
|
|
def SetProcName(name='bsweb'):
|
|
## Currently only works on unix OSs with libc
|
|
if system() == 'Windows':
|
|
return
|
|
|
|
try:
|
|
libc = cdll.LoadLibrary('libc.so.6')
|
|
buff = create_string_buffer(len(name) + 1)
|
|
buff.value = name.encode("UTF-8")
|
|
ret = libc.prctl(15, byref(buff), 0, 0, 0)
|
|
|
|
except OSError:
|
|
logging.debug('Cannot find libc')
|
|
ret = 1
|
|
|
|
if ret != 0:
|
|
logging.error('Failed to set process title')
|
|
|
|
return ret
|
|
|
|
|
|
def ParseQuery(query):
|
|
return {key: value[0] for key, value in parse_qs(query).items()}
|
|
|
|
|
|
def HumanBytes(B):
|
|
B = float(B)
|
|
KB = float(1024)
|
|
MB = float(KB ** 2)
|
|
GB = float(KB ** 3)
|
|
TB = float(KB ** 4)
|
|
|
|
if B < KB:
|
|
return '{0} {1}'.format(B,'B')
|
|
elif KB <= B < MB:
|
|
return '{0:.2f} KiB'.format(B/KB)
|
|
elif MB <= B < GB:
|
|
return '{0:.2f} MiB'.format(B/MB)
|
|
elif GB <= B < TB:
|
|
return '{0:.2f} GiB'.format(B/GB)
|
|
elif TB <= B:
|
|
return '{0:.2f} TiB'.format(B/TB)
|
|
|
|
|
|
def FinishRequest(request, data, ctype='text/html'):
|
|
if isinstance(data, str):
|
|
data = data.encode('UTF-8')
|
|
|
|
bytestream = Gio.MemoryInputStream.new_from_bytes(GLib.Bytes(data))
|
|
request.finish(bytestream, len(data), ctype)
|
|
|
|
|
|
def SetMargin(widget):
|
|
widget.set_margin_top(5)
|
|
widget.set_margin_bottom(5)
|
|
widget.set_margin_left(5)
|
|
widget.set_margin_right(5)
|
|
|
|
|
|
## Doesn't seem to redirect to the url specified for some reason
|
|
def Error(webview, message, title='Error', redirect_url=None, timeout=5):
|
|
data = {
|
|
'error_message': message,
|
|
'redirect': True if redirect_url else False,
|
|
'redirect_url': redirect_url,
|
|
'timeout': timeout,
|
|
'title': title
|
|
}
|
|
|
|
webview.load_html(var.template.render('error.haml', data), 'pyweb://')
|