简体   繁体   English

如何在Calibre插件中存储用户设置?

[英]How one can store user's settings in Calibre's plugin?

I am developing a Calibre's plugin and I want to store user settings (eg if the plugin should make a post-import conversion from one format to another - in my case: pdf to djvu ). 我正在开发Calibre的插件,并且想存储用户设置(例如,如果插件应将导入后的一种格式转换为另一种格式-在我的情况下为pdfdjvu )。

How can I store user settings? 如何存储用户设置? Does Calibre have a build-in method to do this? Calibre是否有内置方法可以做到这一点?

For example I have a dictionary prefs_org_dict with keys and values representing preferences set by an user. 例如,我有一个字典prefs_org_dict其中包含表示用户设置的首选项的键和值。 How can I store this data reliably and read it later? 如何可靠地存储这些数据并在以后读取?

The method suggested by the manual is to create the JSONConfig object and store user preferences in it. 手册建议的方法是创建JSONConfig对象并将用户首选项存储在其中。

Basically just: 基本上只是:

import os
from calibre.utils.config import JSONConfig

prefs = JSONConfig(os.path('plugins', 'PLUGINNAME'))
# JSONConfig inherits after Dict so use it as dictionary

for key, val in prefs_org_dict.iteritems():
    prefs[key] = val
prefs.commit() # explanation in 3rd section of this post

But this method has some caveats: 但是此方法有一些警告:

  1. Name of the settings file. 设置文件的名称。 Paraphrasing manual: 释义手册:

    Remember that this name (ie 'plugins/PLUGINNAME' ) is also in a global namespace, so make it as unique as possible. 请记住,该名称(即'plugins/PLUGINNAME' )也位于全局名称空间中,因此请使其尽可能唯一。 You should always prefix your config file name with plugins/ , so as to ensure you don't accidentally clobber a calibre config file. 您应该始终在配置文件名前添加plugins/ ,以确保不会意外破坏口径的配置文件。

    After you save anything to this object you can see your PLUGINNAME.json file inside Calibre's plugin's config folder (for Windows: %APPDATA%\\calibre\\plugins ) (you can get this path programmatically using: from calibre.utils.config import config_dir and appending /plugins ). 将任何内容保存到该对象后,您可以在Calibre插件的config文件夹中看到您的PLUGINNAME.json文件(对于Windows: %APPDATA%\\calibre\\plugins )(可以使用以下方式以编程方式获取此路径: from calibre.utils.config import config_dir和附加/plugins )。

  2. Default settings. 默认设置。

    prefs.defaults is a dictionary which value is returned if given key doesn't exist in your prefs object. prefs.defaults是一个字典,如果给定键在prefs对象中不存在,则返回该值。 So you can create some default values for your plugin's settings, eg: 因此,您可以为插件的设置创建一些默认值,例如:

     prefs.defaults['postimport'] = False 

    The main problem is when you trying to use other dict methods like .values() , .items() or .iteritems() they return "real" prefs , not defaults, ie for our example, if prefs['postimport'] was not further defined: 主要问题是,当您尝试使用其他dict方法(例如.values() .items().iteritems()它们返回的是“真实的” prefs ,而不是默认值,例如,对于我们的示例,如果prefs['postimport']为没有进一步定义:

     >>> prefs.defaults['postimport'] False >>> prefs.defaults.items() [('postimport', False)] >>> prefs['postimport'] False >>> prefs.items() [] 
  3. Committing nested dict. 提交嵌套字典。

    If you want to use JSONConfig object as real .json storage you probably want to use nested dictionaries. 如果你想使用JSONConfig对象作为真正的.json存储,您可能需要使用嵌套的字典。 For example: 例如:

     prefs['pdf'] = {} prefs['pdf']['convert'] = True 

    But if you set (or delete) value to nested dictionary prefs['pdf'] it will not be saved to .json file. 但是,如果您将嵌套字典prefs['pdf']值设置(或删除), 则不会将其保存到.json文件。 You have to: 你必须:

     prefs.commit() 

    to save data to file after setting them to nested dict. 将数据设置为嵌套字典后将其保存到文件。

  4. Constraints of JSON format. JSON格式的约束。

    A few of Python features cannot be translated to JSON format, eg JSON has not tuples, so json.dumps translates tuples to arrays. json.dumps Python功能无法转换为JSON格式,例如JSON没有元组,因此json.dumps 元组转换为数组。 Also in Python you can have an every hashable object (eg tuple, or frozenset) as a key. 同样在Python中,您可以将每个可哈希对象(例如,元组或Frozenset)作为键。 JSON accepts only strings. JSON仅接受字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM