View Single Post
Old 03-12-2024, 02:28 PM   #12
lomkiri
Groupie
lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.
 
lomkiri's Avatar
 
Posts: 169
Karma: 1497966
Join Date: Jul 2021
Device: N/A
Quote:
Originally Posted by theducks View Post
Maybe a fork of the Language cleaner plugin
That replaces $#+% words with others
I don't know this plugin, and I don't know witch solution is the easiest, but anyway, since I was writing my function while you were writing your message, here it is (it still is simple):

Quote:
Originally Posted by moldy View Post
Thank you Lokiri - that would be very helpful if you did that and it will probably save me a lot of time tomorrow.
Put your json file in the config-folder of calibre.
If you make a "replace all", it will load the file only once, during the first passage.

Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    """ 
    Replace John|Paul|George|Ringo by Mick, Kieth, Ronnie or Charlie, 
    using a dict {"John": "Mick", ...}
    This dict is loaded from a json file, plaaced in the config folder of calibre.
    """
    from calibre.utils.config import JSONConfig
    m = match[0]

    # first passage
    # Load the dict from a json file and store it in the persistent dict "data"
    # (see the help of calibre for more details about the dict "data")
    if number == 1:
        fname = 'beastones.json'
        data['equiv'] = JSONConfig(fname)
        if not data['equiv']:
            print(f'Problem loading {fname}, no treatment will be done')
            
    # normal treatment
    return data['equiv'].get(m, m) 	# if the json file was not found, no changes are made

Last edited by lomkiri; 03-13-2024 at 06:27 AM. Reason: comments added in the code
lomkiri is offline   Reply With Quote