Skip to content

Commit

Permalink
Merge branch 'master' into wallpaper
Browse files Browse the repository at this point in the history
  • Loading branch information
Frewacom committed Sep 4, 2020
2 parents e33a5d5 + 71b66d0 commit ff3b098
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 69 deletions.
50 changes: 34 additions & 16 deletions pywalfox/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import atexit
import logging
import argparse
import subprocess
Expand All @@ -14,10 +15,29 @@
from .channel.unix.client import Client

parser = argparse.ArgumentParser(description='Pywalfox - Native messaging host')
parser.add_argument('action', nargs='?', default=None, help='available options are setup, update, daemon, log and uninstall')
parser.add_argument('--verbose', dest='verbose', action='store_true', help='runs the daemon in verbose mode with debugging output')
parser.add_argument('-p', '--print', dest='print_mode', action='store_true', help='prints the debugging output instead of writing to logfile')
parser.add_argument('-v', '--version', dest='version', action='store_true', help='displays the current version of the daemon')
setup_group = parser.add_argument_group('install/uninstall')
start_group = parser.add_argument_group('start')
parser.add_argument('action',
nargs='?',
default=None,
metavar='ACTION',
help='available actions are install, start, update, log and uninstall')
parser.add_argument('-v', '--version',
dest='version',
action='store_true',
help='displays the current version of the daemon')
start_group.add_argument('-p', '--print',
dest='print_mode',
action='store_true',
help='writes debugging output from the native messaging host to stdout')
start_group.add_argument('--verbose',
dest='verbose',
action='store_true',
help='runs the native messaging host in verbose mode')
setup_group.add_argument('-g', '--global',
dest='global_install',
action='store_true',
help='installs/uninstalls the native host manifest globally')

def get_python_version():
"""Gets the current python version and checks if it is supported."""
Expand All @@ -34,10 +54,11 @@ def get_python_version():
def send_update_action():
"""Sends the update command to the socket server."""
client = Client()
connected = client.start()

if connected is True:
client.send_message('update')
for host in client.hosts:
connected = client.connect(host)
if connected is True:
client.send_message('update')

def open_log_file():
"""Opens the daemon log file in an editor."""
Expand All @@ -57,11 +78,9 @@ def print_version():

def run_daemon():
"""Starts the daemon."""
python_version = get_python_version()

daemon = Daemon(python_version.major)
daemon = Daemon(get_python_version().major)
atexit.register(daemon.close)
daemon.start()
daemon.close()

def handle_args(args):
"""Handles CLI arguments."""
Expand All @@ -73,23 +92,22 @@ def handle_args(args):
send_update_action()
sys.exit(0)

if args.action == 'daemon':
if args.action == 'start':
setup_logging(args.verbose, args.print_mode)
run_daemon()
sys.exit(0)

if args.action == 'log':
open_log_file()
sys.exit(0)

if args.action == 'setup':
if args.action == 'install':
from pywalfox.install import start_setup
start_setup(True)
start_setup(args.global_install)
sys.exit(0)

if args.action == 'uninstall':
from pywalfox.install import start_uninstall
start_uninstall(True)
start_uninstall(args.global_install)
sys.exit(0)

# If no action was specified
Expand Down
19 changes: 16 additions & 3 deletions pywalfox/assets/css/userChrome.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#main-window {
--pywalfox-font-size: 13px;
--pywalfox-font-size-sm: calc(var(--pywalfox-font-size) * 0.9);
--pywalfox-background: var(--lwt-toolbarbutton-active-background);
--pywalfox-background: var(--lwt-accent-color);
--pywalfox-background-light: var(--arrowpanel-background);
--pywalfox-text: var(--arrowpanel-color);
--pywalfox-text-focus: var(--toolbar-color);
--pywalfox-unselected-tab-opacity: 0.8;
--pywalfox-darker-background: rgba(0, 0, 0, 0.4);
--pywalfox-padding: 4px 8px;
Expand All @@ -24,7 +25,8 @@ button, search-textbox, menuseparator {
/* Background color on hover in right-click context menus */
menu[_moz-menuactive="true"], menuitem[_moz-menuactive="true"] {
-moz-appearance: none !important;
background-color: rgba(249, 249, 250, 0.1) !important;
background-color: var(--pywalfox-background) !important;
color: var(--pywalfox-text-focus) !important;
padding: 4px 4px !important;
}

Expand Down Expand Up @@ -69,5 +71,16 @@ textbox, panelview, .tabbrowser-tab, #sidebar-header,
panelmultiview {
font-size: var(--pywalfox-font-size-sm) !important;
}
/********************* END PYWALFOX CUSTOM CSS *********************/

/* Change the grey background color seen e.g. when opening a bookmark in a newtab */
#tabbrowser-tabpanels {
background-color: var(--pywalfox-background) !important;
}

/* Theme the status panel at the bottom */
#statuspanel-label {
background: var(--pywalfox-background-light) !important;
border-color: var(--pywalfox-background) !important;
color: var(--pywalfox-text) !important;
}
/********************* END PYWALFOX CUSTOM CSS *********************/
2 changes: 1 addition & 1 deletion pywalfox/bin/main.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

pywalfox daemon
python -m pywalfox start || python3 -m pywalfox start || python2.7 -m pywalfox start
2 changes: 1 addition & 1 deletion pywalfox/bin/win.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@echo off

pywalfox daemon
python -m pywalfox start || python3 -m pywalfox start || python2.7 -m pywalfox start
54 changes: 49 additions & 5 deletions pywalfox/channel/connector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import socket
import logging
from ..config import UNIX_SOCKET_PATH, WIN_SOCKET_HOST

from ..config import UNIX_SOCKET_PATH, WIN_SOCKET_HOST, UNIX_SOCKET_PATH_ALT, WIN_SOCKET_HOST_ALT

class Connector:
"""
Expand All @@ -10,17 +10,61 @@ class Connector:
since UNIX-sockets are not properly supported on Windows.
:param platform_id str: the current platform identifier, e.g. win32
:param validate_host bool: check if the socket host is available before binding
"""
def __init__(self, platform_id):
def __init__(self, platform_id, validate_host=True):
if platform_id == 'win32':
self.host = WIN_SOCKET_HOST
if validate_host is True:
self.host = self.get_win_socket_host()

self.hosts = [WIN_SOCKET_HOST, WIN_SOCKET_HOST_ALT]
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
logging.debug('Setup socket server using AF_INET (win32)')
else:
self.host = UNIX_SOCKET_PATH
if validate_host is True:
self.host = self.get_unix_socket_path()

self.hosts = [UNIX_SOCKET_PATH, UNIX_SOCKET_PATH_ALT]
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
logging.debug('Setup socket server using AF_UNIX (linux/darwin)')

def get_unix_socket_path(self):
"""
Get an available path to bind the UNIX-socket to.
:return: the path to be used when binding the UNIX-socket
:rType: str
"""
if os.path.exists(UNIX_SOCKET_PATH):
logging.debug('Default UNIX-socket is already in use')
return UNIX_SOCKET_PATH_ALT

return UNIX_SOCKET_PATH

def get_win_socket_host(self):
"""
Get an available host and port to bind the UDP-socket to.
:return: the host and port to be used when binding the UDP-socket
:rType: (host, port)
"""
is_valid = True
test_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
test_socket.bind(WIN_SOCKET_HOST)
test_socket.close()
except OSError as e:
is_valid = False
if e.errno == 98: # errno 98 means that address is already bound
logging.debug('Default UDP-socket host is already in use')
else:
logging.error('Failed to test UDP-socket host availability: %s' % str(e))

if is_valid is True:
return WIN_SOCKET_HOST

return WIN_SOCKET_HOST_ALT

def encode_message(self, message):
"""
Encodes a message to be sent using the socket.
Expand Down
12 changes: 6 additions & 6 deletions pywalfox/channel/unix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
class Client(Connector):
"""UNIX-socket client used to communicate with the daemon."""
def __init__(self):
Connector.__init__(self, 'unix')
Connector.__init__(self, 'unix', False)

def start(self):
def connect(self, host):
"""
Connects to the UNIX-socket if it exists.
:return: if the connection to the socket was successfull
:rType: bool
"""
if os.path.exists(self.host):
if os.path.exists(host):
try:
self.socket.connect(self.host)
logging.debug('Successfully connected to UNIX socket at: %s' % self.host)
self.socket.connect(host)
logging.debug('Successfully connected to UNIX socket at: %s' % host)
return True
except OSError as e:
logging.error('Failed to connect to socket: %s' % e.strerror)
else:
logging.error('Could not find socket: %s' % self.host)
logging.debug('Could not find socket: %s' % host)

return False
12 changes: 11 additions & 1 deletion pywalfox/channel/unix/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ def start(self):
def close(self):
"""Unbinds the socket and deletes the file."""
self.socket.close()
os.remove(self.host)

try:
"""
UNIX-sockets can be overwritten by other processes even if another process
is already using it. This may lead to the file not existing and will
cause a crash if not handled properly.
"""
os.remove(self.host)
logging.debug('UNIX-socket deleted')
except OSError as e:
logging.debug('UNIX-socket has already been deleted, skipping')
10 changes: 5 additions & 5 deletions pywalfox/channel/win/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
class Client(Connector):
"""UDP-socket client used to communicate with the daemon."""
def __init__(self):
Connector.__init__(self, 'win32')
Connector.__init__(self, 'win32', False)

def start(self):
def connect(self, host):
"""
Connects to the UDP socket.
:return: if the connection to the socket was successfull
:rType: bool
"""
try:
self.socket.connect(self.host)
logging.debug('Successfully connected to UDP socket at: %s:%s' % (self.host[0], self.host[1]))
self.socket.connect(host)
logging.debug('Successfully connected to UDP socket at: %s:%s' % (host[0], host[1]))
return True
except Exception as e:
logging.error('Failed to connect to socket: %s' % str(e))
logging.debug('Failed to connect to socket: %s' % str(e))

return False
12 changes: 7 additions & 5 deletions pywalfox/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os

DAEMON_VERSION = '2.4'
DAEMON_VERSION = '2.7'

UNIX_SOCKET_PATH = '/tmp/pywalfox_socket'
UNIX_SOCKET_PATH_ALT = '/tmp/pywalfox_socket_alt'
WIN_SOCKET_HOST = ('127.0.0.1', 56744)
WIN_SOCKET_HOST_ALT = ('127.0.0.1', 56745)

SUPPORTED_BROWSERS = ['firefox']

Expand All @@ -12,19 +14,19 @@
PYWAL_COLORS_PATH = os.path.join(XDG_CACHE_DIR, 'wal/colors.json')

APP_PATH = os.path.dirname(os.path.abspath(__file__))
BIN_PATH_UNIX = os.path.join(APP_PATH, 'bin/main.sh')
BIN_PATH_WIN = os.path.join(APP_PATH, 'bin/win.bat')
CSS_PATH = os.path.join(APP_PATH, 'assets/css')
BIN_PATH_WIN = os.path.join(APP_PATH, 'bin/win.bat')
BIN_PATH_UNIX = os.path.join(APP_PATH, 'bin/main.sh')

FIREFOX_PROFILES_PATH_LINUX = os.path.join(HOME_PATH, '.mozilla/firefox')
FIREFOX_PROFILES_PATH_WIN = os.path.join(HOME_PATH, 'AppData/Roaming/Mozilla/Firefox')
FIREFOX_PROFILES_PATH_DARWIN = os.path.join(HOME_PATH, 'Library/Application Support/Firefox')

LOG_FILE_PATH = os.path.join(XDG_CACHE_DIR, 'pywalfox.log')
LOG_FILE_COUNT = 1
LOG_FILE_MAX_SIZE = 1000*200 # 0.2 mb
LOG_FILE_FORMAT = '[%(asctime)s] %(levelname)s:%(message)s'
LOG_FILE_DATE_FORMAT = '%m-%d-%Y %I:%M:%S'
LOG_FILE_FORMAT = '[%(asctime)s] %(levelname)s:%(message)s'
LOG_FILE_PATH = os.path.join(XDG_CACHE_DIR, 'pywalfox.log')

ACTIONS = {
'VERSION': 'debug:version',
Expand Down
17 changes: 8 additions & 9 deletions pywalfox/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,15 @@ def start(self):
"""Starts the daemon and listens for incoming messages."""
self.is_running = True
self.start_socket_server()
try:
while True:
message = self.messenger.get_message()
logging.debug('Received message from extension: %s' % message)
self.handle_message(message)
except KeyboardInterrupt:
return

while self.is_running:
message = self.messenger.get_message()
logging.debug('Received message from extension: %s' % message)
self.handle_message(message)

def close(self):
"""Application cleanup."""
self.socket_server.close()
logging.debug('Running cleanup')
self.is_running = False
logging.debug('Cleanup')
self.socket_server.close()
sys.exit(0)
Loading

0 comments on commit ff3b098

Please sign in to comment.