Refactor
This commit is contained in:
parent
fb2df21d01
commit
eedbbdc44e
3244
plugin/__init__.py
3244
plugin/__init__.py
File diff suppressed because it is too large
Load Diff
1
plugin/api/__init__.py
Normal file
1
plugin/api/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from . import gui
|
33
plugin/api/gui.py
Normal file
33
plugin/api/gui.py
Normal file
@ -0,0 +1,33 @@
|
||||
# Copyright 2016-2021 Alex Yatskov
|
||||
#
|
||||
# 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/>.
|
||||
|
||||
from . import util
|
||||
|
||||
|
||||
@util.api
|
||||
def guiBrowse(self, query=None):
|
||||
print(query)
|
||||
# browser = aqt.dialogs.open('Browser', self.window())
|
||||
# browser.activateWindow()
|
||||
#
|
||||
# if query is not None:
|
||||
# browser.form.searchEdit.lineEdit().setText(query)
|
||||
# if hasattr(browser, 'onSearch'):
|
||||
# browser.onSearch()
|
||||
# else:
|
||||
# browser.onSearchActivated()
|
||||
#
|
||||
# return list(map(int, browser.model.cards))
|
||||
|
61
plugin/api/util.py
Normal file
61
plugin/api/util.py
Normal file
@ -0,0 +1,61 @@
|
||||
# Copyright 2016-2021 Alex Yatskov
|
||||
#
|
||||
# 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 aqt
|
||||
|
||||
|
||||
def api(method):
|
||||
setattr(method, 'api', True)
|
||||
return method
|
||||
|
||||
|
||||
def window(self):
|
||||
return aqt.mw
|
||||
|
||||
|
||||
def reviewer():
|
||||
return window().reviewer
|
||||
|
||||
|
||||
def collection(self):
|
||||
return window().col
|
||||
|
||||
|
||||
def decks(self):
|
||||
return collection().decks
|
||||
|
||||
|
||||
def scheduler(self):
|
||||
return collection().sched
|
||||
|
||||
|
||||
def database(self):
|
||||
return collection().db
|
||||
|
||||
|
||||
def media(self):
|
||||
return collection().media
|
||||
|
||||
|
||||
def deckNames():
|
||||
return decks().allNames()
|
||||
|
||||
|
||||
class EditScope:
|
||||
def __enter__(self):
|
||||
window().requireReset()
|
||||
|
||||
def __exit__(self):
|
||||
window().maybeReset()
|
@ -13,19 +13,22 @@
|
||||
# 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 PyQt5
|
||||
|
||||
from . import web
|
||||
|
||||
|
||||
class ApiHost:
|
||||
def __init__(self, origins, key, interval, address, port):
|
||||
def __init__(self, origins, key, address, port):
|
||||
self.key = key
|
||||
self.modules = []
|
||||
|
||||
self.server = web.WebServer(self.handler, origins)
|
||||
self.server.bindAndListen(address, port)
|
||||
|
||||
self.timer = QTimer()
|
||||
self.timer.timeout.connect(self.advance)
|
||||
|
||||
def run(self, interval):
|
||||
self.timer = PyQt5.QtCore.QTimer()
|
||||
self.timer.timeout.connect(self.server.advance)
|
||||
self.timer.start(interval)
|
||||
|
||||
|
||||
@ -46,7 +49,7 @@ class ApiHost:
|
||||
method = None
|
||||
for module in self.modules:
|
||||
for methodName, methodInstance in inspect.getmembers(module, predicate=inspect.ismethod):
|
||||
if getattr(methodInstance, 'api', False):
|
||||
if methodName == action and getattr(methodInstance, 'api', False):
|
||||
method = methodInstance
|
||||
break
|
||||
|
||||
@ -57,8 +60,3 @@ class ApiHost:
|
||||
|
||||
except Exception as e:
|
||||
return {'error': str(e), 'result': None}
|
||||
|
||||
|
||||
def api(method):
|
||||
setattr(method, 'api', True)
|
||||
return decorator
|
||||
|
1
plugin/plugin
Symbolic link
1
plugin/plugin
Symbolic link
@ -0,0 +1 @@
|
||||
/home/alex/projects/anki-connect/plugin
|
48
plugin/settings.py
Normal file
48
plugin/settings.py
Normal file
@ -0,0 +1,48 @@
|
||||
# Copyright 2016-2021 Alex Yatskov
|
||||
#
|
||||
# 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 aqt
|
||||
|
||||
|
||||
def query(key):
|
||||
defaults = {
|
||||
'apiKey': None,
|
||||
'apiPollInterval': 25,
|
||||
'webBindAddress': os.getenv('ANKICONNECT_BIND_ADDRESS', '127.0.0.1'),
|
||||
'webBindPort': 8765,
|
||||
'webCorsOrigin': os.getenv('ANKICONNECT_CORS_ORIGIN'),
|
||||
'webCorsOriginList': [],
|
||||
'webTimeout': 10000,
|
||||
}
|
||||
|
||||
try:
|
||||
value = aqt.mw.addonManager.getConfig(__name__).get(key, defaults[key])
|
||||
except:
|
||||
raise Exception('setting {} not found'.format(key))
|
||||
|
||||
if key == 'webCorsOriginList':
|
||||
originOld = query('webCorsOrigin')
|
||||
if originOld:
|
||||
value.append(originOld)
|
||||
|
||||
value += [
|
||||
'http://127.0.0.1:',
|
||||
'http://localhost:',
|
||||
'chrome-extension://',
|
||||
'moz-extension://',
|
||||
]
|
||||
|
||||
return value
|
@ -83,7 +83,7 @@ def setting(key):
|
||||
try:
|
||||
return aqt.mw.addonManager.getConfig(__name__).get(key, defaults[key])
|
||||
except:
|
||||
raise Exception('setting {} not found'.format(key))
|
||||
raise Exception(f'setting {key} not found')
|
||||
|
||||
|
||||
#
|
||||
|
@ -157,15 +157,15 @@ class WebServer:
|
||||
origin = '*'
|
||||
allowed = True
|
||||
else:
|
||||
origin = request.headers.get('origin')
|
||||
allowed = origin in self.origins
|
||||
|
||||
if not allowed:
|
||||
origin = 'http://127.0.0.1'
|
||||
origin = request.headers.get('origin', 'http://127.0.0.1:')
|
||||
for prefix in self.origins:
|
||||
if origin.startswith(prefix):
|
||||
allowed = True
|
||||
break
|
||||
|
||||
try:
|
||||
call = json.loads(request.body)
|
||||
if call:
|
||||
if request.body:
|
||||
call = json.loads(request.body)
|
||||
call['allowed'] = allowed
|
||||
call['origin'] = origin
|
||||
body = json.dumps(self.handler(call))
|
||||
@ -182,7 +182,7 @@ class WebServer:
|
||||
['Content-Length', len(body.encode('utf-8'))]
|
||||
]
|
||||
|
||||
header = bytes()
|
||||
header = ''
|
||||
for key, value in headers:
|
||||
header += f'{key}: {value}\r\n'
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user