Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 01-01-2022, 09:35 AM   #1
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,774
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
API for plugins to create custom columns

Working with @davidfor, I added an API that plugin writers can use to create custom columns for the plugin's use. See the class CreateNewCustomColumn in calibre.gui2.preferences.create_custom_column. It is documented there.

The class is in calibre source now.

Notes:
  • The custom column(s) are actually created the next time calibre starts. The column(s) don't exist until then. The plugin configurator must deal with that in some way.
  • You can create columns only once per calibre instance. Further attempts to create columns or run calibre preferences will fail until calibre is restarted. If you use calibre preferences to create/edit/delete a column then this API will refuse to create new columns until calibre is restarted.
  • This API doesn't support editing an existing custom column. Use calibre preferences to do that.

EDIT: I have improved the documentation for the class and methods to make it clearer that more than one column can be created, and to better explain the parameters. I will submit it to Kovid if/when I have more feedback.

The documentation as of 2/Jan/21 is:
Spoiler:
Code:
    Provide an API to create new custom columns.

    Usage:
        from calibre.gui2.preferences.create_custom_column import CreateNewCustomColumn
        creator = CreateNewCustomColumn(gui)
        if creator.must_restart():
                ...
        else:
            result = creator.create_column(....)
            if result[0] == creator.Result.COLUMN_ADDED:

    The parameter 'gui' passed when creating a class instance is the main
    calibre gui (calibre.gui2.ui.get_gui())

    Use the create_column(...) method to open a dialog to create a new custom
    column with given lookup_name, column_heading, datatype, and is_multiple.
    You can create as many columns as you wish with a single instance of the
    CreateNewCustomColumn class. Subsequent class instances will refuse to
    create columns until calibre is restarted, as will calibre Preferences.

    The lookup name must begin with a '#'. All remaining characters must be
    lower case letters, digits or underscores. The character after the '#' must
    be a letter. The lookup name must not end with the suffix '_index'.

    The datatype must be one of calibre's custom column types: 'bool',
    'comments', 'composite', 'datetime', 'enumeration', 'float', 'int',
    'rating', 'series', or 'text'. The datatype can't be changed in the dialog.

    is_multiple tells calibre that the column contains multiple values -- is
    tags-like. The value True is allowed only for 'composite' and 'text' types.

    If generate_unused_lookup_name is False then the provided lookup_name and
    column_heading must not already exist. If generate_unused_lookup_name is
    True then if necessary the method will add the suffix '_n' to the provided
    lookup_name to allocate an unused lookup_name, where 'n' is an integer.
    The same processing is applied to column_heading to make it is unique, using
    the same suffix used for the lookup name if possible. In either case the
    user can change the column heading in the dialog.

    Set freeze_lookup_name to False if you want to allow the user choose a
    different lookup name. The user will not be allowed to choose the lookup
    name of an existing column. The provided lookup_name and column_heading
    either must not exist or generate_unused_lookup_name must be True,
    regardless of the value of freeze_lookup_name.

    The 'display' parameter is used to pass item- and type-specific information
    for the column. It is a dict. The easiest way to see the current values for
    'display' for a particular column is to create a column like you want then
    look for the lookup name in the file metadata_db_prefs_backup.json. You must
    restart calibre twice after creating a new column before its information
    will appear in that file.

    The key:value pairs for each type are as follows. Note that this
    list might be incorrect. As said above, the best way to get current values
    is to create a similar column and look at the values in 'display'.
      all types:
        'default_value': a string representation of the default value for the
                         column. Permitted values are type specific
        'description': a string containing the column's description
      comments columns:
        'heading_position': a string specifying where a comment heading goes:
                            hide, above, side
        'interpret_as': a string specifying the comment's purpose:
                        html, short-text, long-text, markdown
      composite columns:
        'composite_template': a string containing the template for the composite column
        'composite_sort': a string specifying how the composite is to be sorted
        'make_category': True or False -- whether the column is shown in the tag browser
        'contains_html': True or False -- whether the column is interpreted as HTML
        'use_decorations': True or False -- should check marks be displayed
      datetime columns:
        'date_format': a string specifying the display format
      enumerated columns
        'enum_values': a string containing comma-separated valid values for an enumeration
        'enum_colors': a string containing comma-separated colors for an enumeration
        'use_decorations': True or False -- should check marks be displayed
      float and int columns:
        'number_format': the format to apply for the column
      rating columns:
        'allow_half_stars': True or False -- are half-stars allowed
      text columns:
        'is_names': True or False -- whether the items are comma or ampersand separated
        'use_decorations': True or False -- should check marks be displayed

    This method returns a tuple (Result.enum_value, message). If tuple[0] is
    Result.COLUMN_ADDED then the message is the lookup name including the '#'.
    Otherwise it is a potentially localized error message.

    You or the user must restart calibre for the column(s) to be actually added.

    Result.EXCEPTION_RAISED is returned if the create dialog raises an exception.
    This can happen if the display contains illegal values, for example a string
    where a boolean is required. The string is the exception text. Run calibre
    in debug mode to see the entire traceback.

    The method returns Result.MUST_RESTART if further calibre configuration has
    been blocked. You can check for this situation in advance by calling
    must_restart().

Last edited by chaley; 01-02-2022 at 07:25 AM. Reason: New documentation
chaley is offline   Reply With Quote
Old 01-01-2022, 11:08 AM   #2
thiago.eec
Guru
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 946
Karma: 1183425
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
Hi, Chaley.

I have two questions:

1) What is the is_multiple argument used for? I could not understand this parameter.

2) When using calibre preferences, I can create multiple columns before restarting. Can't that be replicated with the API? I plan to use this on my Skoob Sync plugin, but the user must configure seven columns, hence seven restarts.

P.S.: Just noticed you said "create columns". Maybe I misunderstood your point on my second question. Can I create multiple columns at once? Maybe this is the reason for the is_multiple argument?

Last edited by thiago.eec; 01-01-2022 at 11:12 AM.
thiago.eec is offline   Reply With Quote
Advert
Old 01-01-2022, 11:24 AM   #3
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,774
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by thiago.eec View Post
Hi, Chaley.

I have two questions:

1) What is the is_multiple argument used for? I could not understand this parameter.
The is_multiple parameter tells calibre if text or composite columns hold a single value or are comma-separated (tags-like) columns. If is_multiple is True then the 'is_names' boolean in the display tells calibre if a text column is ampersand-separated instead of comma-separated (contains names ).
Quote:
2) When using calibre preferences, I can create multiple columns before restarting. Can't that be replicated with the API? I plan to use this on my Skoob Sync plugin, but the user must configure seven columns, hence seven restarts.
You can create as many columns as you want with a single instance of the class, as like this:
Code:
    creator = CreateNewCustomColumn(gui)
    result1 = creator.create('#col1', ...)
    --- check result1 for correctness ---
    result2 = creator.create('#col2', ...)
    --- check result2 for correctness ---
Any instance created after one is used to create columns and before calibre is restarted will refuse to create columns.

This system is similar to what you see with calibre preferences. You can create etc as many columns as you wish using one dialog, but you can't do it again until you restart.
chaley is offline   Reply With Quote
Old 01-01-2022, 11:50 AM   #4
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,774
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I have 'improved' the documentation for the class and methods to make it clearer that more than one column can be created, and to better explain the parameters. I will submit it to Kovid if/when I have more feedback.

EDIT: The documentation is now in the first post.

Last edited by chaley; 01-01-2022 at 01:06 PM. Reason: Moved documentation to the first post
chaley is offline   Reply With Quote
Old 01-02-2022, 12:55 AM   #5
davidfor
Grand Sorcerer
davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.
 
Posts: 24,906
Karma: 47303748
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
As @chaley said, I was testing his code and making changes. It is working well and I nearly have all the changes ready in the Kobo Utilities plugin.

@chaley: One thing we missed was duplicate column headings. When using "generate_unused_lookup_name=True", the supplied column heading is probably not unique. Appending the same number to the column heading is probably good enough for this.
davidfor is offline   Reply With Quote
Advert
Old 01-02-2022, 05:31 AM   #6
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,774
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by davidfor View Post
@chaley: One thing we missed was duplicate column headings. When using "generate_unused_lookup_name=True", the supplied column heading is probably not unique. Appending the same number to the column heading is probably good enough for this.
OK, will do.

And done.
I also added a method to return the column headings as a dict {heading:lookup_name}

Last edited by chaley; 01-02-2022 at 08:25 AM. Reason: Job done.
chaley is offline   Reply With Quote
Old 01-02-2022, 05:52 PM   #7
dunhill
Guru
dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.
 
dunhill's Avatar
 
Posts: 733
Karma: 228142
Join Date: Sep 2017
Location: Argentina
Device: moon+ reader, kindle paperwhite
Geniuses !! excellent work of both as always, I take off my hat
dunhill is offline   Reply With Quote
Old 01-02-2022, 08:57 PM   #8
davidfor
Grand Sorcerer
davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.
 
Posts: 24,906
Karma: 47303748
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
Quote:
Originally Posted by chaley View Post
OK, will do.
And it works.
Quote:
And done.
I also added a method to return the column headings as a dict {heading:lookup_name}
That's a good idea.
davidfor is offline   Reply With Quote
Old 01-05-2022, 03:12 PM   #9
thiago.eec
Guru
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 946
Karma: 1183425
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
Hi, @chaley and @davidfor.

Thank both of you, @chaley and @davidfor, for your efforts. This is really great. Got it working here for Skoob Sync after looking at implemented version by @davidfor.
thiago.eec is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Forgot how to create size columns Rellwood Library Management 2 08-06-2021 08:21 PM
Ratings Custom Columns Created From other Columns Tanjamuse Library Management 4 09-28-2020 12:57 AM
Is there any direct api to using built-in input and output plugins in new plugin? The_book Plugins 2 03-21-2020 09:31 AM
Custom sort order for custom columns? matjojo Library Management 5 11-24-2019 11:30 AM
Help Needed for Custom Columns Created From other Columns Tanjamuse Library Management 5 09-02-2018 06:19 PM


All times are GMT -4. The time now is 05:47 AM.


MobileRead.com is a privately owned, operated and funded community.