mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Read and write CS settings from a file
This commit is contained in:
parent
52007b840d
commit
0a44163d82
@ -114,9 +114,8 @@ class ConnectionListener(Thread):
|
|||||||
remote = packet[1]
|
remote = packet[1]
|
||||||
content_server_port = b''
|
content_server_port = b''
|
||||||
try:
|
try:
|
||||||
from calibre.library.server import server_config as content_server_config
|
from calibre.srv.opts import server_config
|
||||||
content_server_port = \
|
content_server_port = str(server_config().port)
|
||||||
str(content_server_config().parse().port)
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
message = str(self.driver.ZEROCONF_CLIENT_STRING + b' (on ' +
|
message = str(self.driver.ZEROCONF_CLIENT_STRING + b' (on ' +
|
||||||
|
@ -559,9 +559,8 @@ class StanzaPage(QWizardPage, StanzaUI):
|
|||||||
def commit(self):
|
def commit(self):
|
||||||
p = self.set_port()
|
p = self.set_port()
|
||||||
if p is not None:
|
if p is not None:
|
||||||
from calibre.library.server import server_config
|
from calibre.srv.opts import change_settings
|
||||||
c = server_config()
|
change_settings(port=p)
|
||||||
c.set('port', p)
|
|
||||||
|
|
||||||
def set_port(self, *args):
|
def set_port(self, *args):
|
||||||
if not self.content_server.isChecked():
|
if not self.content_server.isChecked():
|
||||||
@ -895,6 +894,7 @@ def wizard(parent=None):
|
|||||||
w = Wizard(parent)
|
w = Wizard(parent)
|
||||||
return w
|
return w
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from calibre.gui2 import Application
|
from calibre.gui2 import Application
|
||||||
app = Application([])
|
app = Application([])
|
||||||
|
@ -6,11 +6,15 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
import errno, os
|
||||||
from itertools import izip_longest
|
from itertools import izip_longest
|
||||||
from collections import namedtuple, OrderedDict
|
from collections import namedtuple, OrderedDict
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
|
from calibre.constants import config_dir
|
||||||
|
from calibre.utils.lock import ExclusiveFile
|
||||||
|
|
||||||
Option = namedtuple('Option', 'name default longdoc shortdoc choices')
|
Option = namedtuple('Option', 'name default longdoc shortdoc choices')
|
||||||
|
|
||||||
|
|
||||||
@ -21,6 +25,7 @@ class Choices(frozenset):
|
|||||||
self.default = args[0]
|
self.default = args[0]
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
raw_options = (
|
raw_options = (
|
||||||
|
|
||||||
_('Path to the SSL certificate file'),
|
_('Path to the SSL certificate file'),
|
||||||
@ -133,8 +138,7 @@ raw_options = (
|
|||||||
_('Path to a file in which to store the user and password information. By default a'
|
_('Path to a file in which to store the user and password information. By default a'
|
||||||
' file in the calibre configuration directory is used.'),
|
' file in the calibre configuration directory is used.'),
|
||||||
|
|
||||||
_('Choose the type of authentication used'),
|
_('Choose the type of authentication used'), 'auth_mode', Choices('auto', 'basic', 'digest'),
|
||||||
'auth_mode', Choices('auto', 'basic', 'digest'),
|
|
||||||
_('Set the HTTP authentication mode used by the server. Set to "basic" if you are'
|
_('Set the HTTP authentication mode used by the server. Set to "basic" if you are'
|
||||||
' putting this server behind an SSL proxy. Otherwise, leave it as "auto", which'
|
' putting this server behind an SSL proxy. Otherwise, leave it as "auto", which'
|
||||||
' will use "basic" if SSL is configured otherwise it will use "digest".'),
|
' will use "basic" if SSL is configured otherwise it will use "digest".'),
|
||||||
@ -162,6 +166,7 @@ def grouper(n, iterable, fillvalue=None):
|
|||||||
args = [iter(iterable)] * n
|
args = [iter(iterable)] * n
|
||||||
return izip_longest(*args, fillvalue=fillvalue)
|
return izip_longest(*args, fillvalue=fillvalue)
|
||||||
|
|
||||||
|
|
||||||
for shortdoc, name, default, doc in grouper(4, raw_options):
|
for shortdoc, name, default, doc in grouper(4, raw_options):
|
||||||
choices = None
|
choices = None
|
||||||
if isinstance(default, Choices):
|
if isinstance(default, Choices):
|
||||||
@ -215,3 +220,68 @@ def opts_to_parser(usage):
|
|||||||
add_option(name, type=otype)
|
add_option(name, type=otype)
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_CONFIG = os.path.join(config_dir, 'server-config.txt')
|
||||||
|
|
||||||
|
|
||||||
|
def parse_config_file(path=DEFAULT_CONFIG):
|
||||||
|
try:
|
||||||
|
with ExclusiveFile(path) as f:
|
||||||
|
raw = f.read().decode('utf-8')
|
||||||
|
except EnvironmentError as err:
|
||||||
|
if err.errno != errno.ENOENT:
|
||||||
|
raise
|
||||||
|
raw = ''
|
||||||
|
ans = {}
|
||||||
|
for line in raw.splitlines():
|
||||||
|
line = line.strip()
|
||||||
|
if line.startswith('#'):
|
||||||
|
continue
|
||||||
|
key, rest = line.partition(' ')[::2]
|
||||||
|
opt = options.get(key)
|
||||||
|
if opt is None:
|
||||||
|
continue
|
||||||
|
val = rest
|
||||||
|
if isinstance(opt.default, (int, long, float)):
|
||||||
|
try:
|
||||||
|
val = type(opt.default)(rest)
|
||||||
|
except Exception:
|
||||||
|
raise ValueError('The value for %s: %s is not a valid number' % (key, rest))
|
||||||
|
elif opt.choices:
|
||||||
|
if rest not in opt.choices:
|
||||||
|
raise ValueError('The value for %s: %s is not valid' % (key, rest))
|
||||||
|
ans[key] = val
|
||||||
|
return Options(**ans)
|
||||||
|
|
||||||
|
|
||||||
|
def write_config_file(opts, path=DEFAULT_CONFIG):
|
||||||
|
changed = {name:getattr(opts, name) for name in options if getattr(opts, name) != options[name].default}
|
||||||
|
lines = []
|
||||||
|
for name in sorted(changed):
|
||||||
|
o = options[name]
|
||||||
|
lines.append('# ' + o.shortdoc)
|
||||||
|
if o.longdoc:
|
||||||
|
lines.append('# ' + o.longdoc)
|
||||||
|
lines.append('%s %s' % (name, changed[name]))
|
||||||
|
raw = '\n'.join(lines).encode('utf-8')
|
||||||
|
with ExclusiveFile(path) as f:
|
||||||
|
f.write(raw)
|
||||||
|
|
||||||
|
|
||||||
|
def server_config(refresh=False):
|
||||||
|
if refresh or not hasattr(server_config, 'ans'):
|
||||||
|
server_config.ans = parse_config_file()
|
||||||
|
return server_config.ans
|
||||||
|
|
||||||
|
|
||||||
|
def change_settings(**kwds):
|
||||||
|
new_opts = {}
|
||||||
|
opts = server_config()
|
||||||
|
for name in options:
|
||||||
|
if name in kwds:
|
||||||
|
new_opts[name] = kwds[name]
|
||||||
|
else:
|
||||||
|
new_opts[name] = getattr(opts, name)
|
||||||
|
new_opts = server_config.ans = Options(**new_opts)
|
||||||
|
write_config_file(new_opts)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user