Browse Source

Support launching via Chrome native messaging

master
Johann Schmitz 1 year ago
parent
commit
88f1fef2ba
Signed by: ercpe <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
4 changed files with 72 additions and 3 deletions
  1. +21
    -2
      esgp/__main__.py
  2. +42
    -0
      esgp/settings.py
  3. +4
    -1
      esgp/ui.py
  4. +5
    -0
      launch.sh

+ 21
- 2
esgp/__main__.py View File

@@ -14,7 +14,9 @@
#
# 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 json
import logging
import struct
from argparse import ArgumentParser
import daiquiri
import sys
@@ -24,20 +26,37 @@ from PyQt5.QtWidgets import QApplication
from esgp.config import Configuration
from esgp.ui import MainWindow


def _read_chrome_native_message():
logger = logging.getLogger(__file__)
text_length_bytes = sys.stdin.read(4)
text_length = struct.unpack("i", bytes(text_length_bytes, 'utf-8'))[0]
text_decoded = sys.stdin.read(text_length)
return json.loads(text_decoded)


if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('-d', '--domain')
parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Enable verbose logging (default: %(default)s)')
parser.add_argument('-v', '--verbose', action='store_true', default=False,
help='Enable verbose logging (default: %(default)s)')
parser.add_argument('arg', nargs='?')

args = parser.parse_args()

daiquiri.setup(level=logging.DEBUG if args.verbose else logging.WARNING)
url = None
if (args.arg or "").startswith('chrome-extension://'):
# started from chrome extension
url = _read_chrome_native_message()['url']

config = Configuration()
config.read()

app = QApplication(sys.argv)
main_window = MainWindow(config, args)
main_window = MainWindow(config, url, args)
main_window.show()

sys.exit(app.exec_())

+ 42
- 0
esgp/settings.py View File

@@ -13,12 +13,18 @@
#
# 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 json
import os
import sys

from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant, QModelIndex, QRegExp
from PyQt5.QtGui import QIntValidator, QRegExpValidator
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QFrame, QLabel, QTableView, QLineEdit, \
QStyledItemDelegate, QPushButton, QComboBox, QHBoxLayout, QTableWidget, QAbstractItemView, QHeaderView

import logging

logger = logging.getLogger(__name__)

class DomainSettingsTableModel(QAbstractTableModel):
@@ -128,10 +134,46 @@ class SettingsDialog(QDialog):

layout.addWidget(self.build_domain_settings_ui())
layout.addLayout(self.build_chrome_extension_layout())
save_button = QPushButton('Save')
save_button.clicked.connect(self.save_and_close)
layout.addWidget(save_button)
def build_chrome_extension_layout(self):
l = QVBoxLayout()
register_button = QPushButton('Register as a native messaging host in Chrome/Chromium')
register_button.clicked.connect(self.register_as_chrome_messaging_host)
l.addWidget(register_button)
return l
def register_as_chrome_messaging_host(self):
for profile_directory in [ '~/.config/google-chrome/', '~/.config/chromium/' ]:
#NativeMessagingHosts/
directory = os.path.expanduser(profile_directory)
if not os.path.exists(directory):
logger.info("Skipping configuration in %s (not found)", directory)
continue
path = os.path.join(directory, 'NativeMessagingHosts', 'de.ercpe.esgp.json')
if not os.path.exists(os.path.dirname(path)):
os.mkdir(os.path.dirname(path))
with open(path, 'w') as o:
cfg = {
"name": "de.ercpe.esgp",
"description": "eSGP",
"path": sys.argv[0],
"type": "stdio",
"allowed_origins": [
"chrome-extension://odhpmlfckigpfekmnaldaimknpdbodlo/"
]
}

o.write(json.dumps(cfg))

def build_domain_settings_ui(self):
# per-domain settings
domain_settings_layout = QVBoxLayout()


+ 4
- 1
esgp/ui.py View File

@@ -32,7 +32,7 @@ logger = logging.getLogger(__name__)

class MainWindow(QDialog):
def __init__(self, config, cmdargs, *args, **kwargs):
def __init__(self, config, url, cmdargs, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.config = config
@@ -49,6 +49,9 @@ class MainWindow(QDialog):
self.build_ui()
self.set_default_settings()
self.show()
if url:
self.domain_pasted(url)

def build_ui(self):
self.setWindowTitle('eSGP')


+ 5
- 0
launch.sh View File

@@ -0,0 +1,5 @@
#!/bin/bash

source ~/dev/.virtualenv/esgp/bin/activate
cd $(dirname $0)
cat /dev/stdin | python3 -m esgp $*

Loading…
Cancel
Save