Improved compability for gevent (version 1.3.x & Windows)

Bugfix gdrive naming of client_secrets.json
Bugfix handling of gdrive with working folder not cps root folder
pull/519/merge
OzzieIsaacs 6 years ago
parent c66bb54478
commit 7d3fadb685

1
.gitignore vendored

@ -28,3 +28,4 @@ settings.yaml
gdrive_credentials gdrive_credentials
vendor vendor
client_secrets.json

@ -12,7 +12,7 @@ sys.path.append(os.path.join(base_path, 'vendor'))
from cps import web from cps import web
try: try:
from gevent.wsgi import WSGIServer from gevent.pywsgi import WSGIServer
gevent_present = True gevent_present = True
except ImportError: except ImportError:
from tornado.wsgi import WSGIContainer from tornado.wsgi import WSGIContainer

@ -154,7 +154,7 @@ class Singleton:
@Singleton @Singleton
class Gauth: class Gauth:
def __init__(self): def __init__(self):
self.auth = GoogleAuth(settings_file='settings.yaml') self.auth = GoogleAuth(settings_file=os.path.join(config.get_main_dir,'settings.yaml'))
@Singleton @Singleton
@ -254,7 +254,8 @@ if config.config_log_level == logging.DEBUG:
def is_gdrive_ready(): def is_gdrive_ready():
return os.path.exists('settings.yaml') and os.path.exists('gdrive_credentials') return os.path.exists(os.path.join(config.get_main_dir,'settings.yaml')) and \
os.path.exists(os.path.join(config.get_main_dir,'gdrive_credentials'))
@babel.localeselector @babel.localeselector
@ -1540,7 +1541,7 @@ def authenticate_google_drive():
def google_drive_callback(): def google_drive_callback():
auth_code = request.args.get('code') auth_code = request.args.get('code')
credentials = Gauth.Instance().auth.flow.step2_exchange(auth_code) credentials = Gauth.Instance().auth.flow.step2_exchange(auth_code)
with open('gdrive_credentials', 'w') as f: with open(os.path.join(config.get_main_dir,'gdrive_credentials'), 'w') as f:
f.write(credentials.to_json()) f.write(credentials.to_json())
return redirect(url_for('configuration')) return redirect(url_for('configuration'))
@ -1550,7 +1551,7 @@ def google_drive_callback():
@admin_required @admin_required
def watch_gdrive(): def watch_gdrive():
if not config.config_google_drive_watch_changes_response: if not config.config_google_drive_watch_changes_response:
with open('client_secret.json', 'r') as settings: with open(os.path.join(config.get_main_dir,'client_secrets.json'), 'r') as settings:
filedata = json.load(settings) filedata = json.load(settings)
if filedata['web']['redirect_uris'][0].endswith('/'): if filedata['web']['redirect_uris'][0].endswith('/'):
filedata['web']['redirect_uris'][0] = filedata['web']['redirect_uris'][0][:-((len('/gdrive/callback')+1))] filedata['web']['redirect_uris'][0] = filedata['web']['redirect_uris'][0][:-((len('/gdrive/callback')+1))]
@ -2517,13 +2518,13 @@ def configuration_helper(origin):
if gdrive_support == False: if gdrive_support == False:
gdriveError = _('Import of optional GDrive requirements missing') gdriveError = _('Import of optional GDrive requirements missing')
else: else:
if not os.path.isfile('client_secret.json'): if not os.path.isfile(os.path.join(config.get_main_dir,'client_secrets.json')):
gdriveError = _('client_secret.json is missing or not readable') gdriveError = _('client_secrets.json is missing or not readable')
else: else:
with open('client_secret.json', 'r') as settings: with open(os.path.join(config.get_main_dir,'client_secrets.json'), 'r') as settings:
filedata=json.load(settings) filedata=json.load(settings)
if not 'web' in filedata: if not 'web' in filedata:
gdriveError = _('client_secret.json is not configured for web application') gdriveError = _('client_secrets.json is not configured for web application')
filedata = None filedata = None
if request.method == "POST": if request.method == "POST":
to_save = request.form.to_dict() to_save = request.form.to_dict()
@ -2537,18 +2538,21 @@ def configuration_helper(origin):
if filedata: if filedata:
if filedata['web']['redirect_uris'][0].endswith('/'): if filedata['web']['redirect_uris'][0].endswith('/'):
filedata['web']['redirect_uris'][0] = filedata['web']['redirect_uris'][0][:-1] filedata['web']['redirect_uris'][0] = filedata['web']['redirect_uris'][0][:-1]
with open('settings.yaml', 'w') as f: with open(os.path.join(config.get_main_dir,'settings.yaml'), 'w') as f:
yaml = "client_config_backend: settings\nclient_config:\n" \ yaml = "client_config_backend: settings\nclient_config_file: %(client_file)s\n" \
"client_config:\n" \
" client_id: %(client_id)s\n client_secret: %(client_secret)s\n" \ " client_id: %(client_id)s\n client_secret: %(client_secret)s\n" \
" redirect_uri: %(redirect_uri)s\n\nsave_credentials: True\n" \ " redirect_uri: %(redirect_uri)s\n\nsave_credentials: True\n" \
"save_credentials_backend: file\nsave_credentials_file: gdrive_credentials\n\n" \ "save_credentials_backend: file\nsave_credentials_file: %(credential)s\n\n" \
"get_refresh_token: True\n\noauth_scope:\n" \ "get_refresh_token: True\n\noauth_scope:\n" \
" - https://www.googleapis.com/auth/drive\n" " - https://www.googleapis.com/auth/drive\n"
f.write(yaml % {'client_id': filedata['web']['client_id'], f.write(yaml % {'client_file': os.path.join(config.get_main_dir,'client_secrets.json'),
'client_id': filedata['web']['client_id'],
'client_secret': filedata['web']['client_secret'], 'client_secret': filedata['web']['client_secret'],
'redirect_uri': filedata['web']['redirect_uris'][0]}) 'redirect_uri': filedata['web']['redirect_uris'][0],
'credential': os.path.join(config.get_main_dir,'gdrive_credentials')})
else: else:
flash(_(u'client_secret.json is not configured for web application'), category="error") flash(_(u'client_secrets.json is not configured for web application'), category="error")
return render_title_template("config_edit.html", content=config, origin=origin, return render_title_template("config_edit.html", content=config, origin=origin,
gdrive=gdrive_support, gdriveError=gdriveError, gdrive=gdrive_support, gdriveError=gdriveError,
goodreads=goodreads_support, title=_(u"Basic Configuration")) goodreads=goodreads_support, title=_(u"Basic Configuration"))
@ -3308,7 +3312,7 @@ def upload():
file_size = os.path.getsize(saved_filename) file_size = os.path.getsize(saved_filename)
if meta.cover is None: if meta.cover is None:
has_cover = 0 has_cover = 0
basedir = os.path.dirname(__file__) basedir = config.get_main_dir # os.path.dirname(__file__)
copyfile(os.path.join(basedir, "static/generic_cover.jpg"), os.path.join(filepath, "cover.jpg")) copyfile(os.path.join(basedir, "static/generic_cover.jpg"), os.path.join(filepath, "cover.jpg"))
else: else:
has_cover = 1 has_cover = 1
@ -3382,13 +3386,16 @@ def upload():
return redirect(url_for("index")) return redirect(url_for("index"))
def start_gevent(): def start_gevent():
from gevent.wsgi import WSGIServer from gevent.pywsgi import WSGIServer
global gevent_server global gevent_server
try: try:
ssl_args=dict() ssl_args=dict()
if ub.config.get_config_certfile() and ub.config.get_config_keyfile(): if ub.config.get_config_certfile() and ub.config.get_config_keyfile():
ssl_args = {"certfile": ub.config.get_config_certfile(), ssl_args = {"certfile": ub.config.get_config_certfile(),
"keyfile": ub.config.get_config_keyfile()} "keyfile": ub.config.get_config_keyfile()}
if os.name == 'nt':
gevent_server = WSGIServer(('0.0.0.0', ub.config.config_port), app, **ssl_args)
else:
gevent_server = WSGIServer(('', ub.config.config_port), app, **ssl_args) gevent_server = WSGIServer(('', ub.config.config_port), app, **ssl_args)
gevent_server.serve_forever() gevent_server.serve_forever()
except SocketError: except SocketError:

@ -92,7 +92,7 @@ The Drive API should now be setup and ready to use, so we need to integrate it i
2. Tick Use Google Drive 2. Tick Use Google Drive
3. Click the "Submit" button 3. Click the "Submit" button
4. Now select Authenticate Google Drive 4. Now select Authenticate Google Drive
5. This should redirect you to Google to allow it top use your Drive, and then redirect you back to the config page 5. This should redirect you to Google. After allowing it to use your Drive, it redirects you back to the config page
6. Select the folder that is the root of your calibre library on Gdrive ("Google drive Calibre folder") 6. Select the folder that is the root of your calibre library on Gdrive ("Google drive Calibre folder")
7. Click the "Submit" button 7. Click the "Submit" button
8. Google Drive should now be connected and be used to get images and download Epubs. The metadata.db is stored in the calibre library location 8. Google Drive should now be connected and be used to get images and download Epubs. The metadata.db is stored in the calibre library location

Loading…
Cancel
Save