Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Sigil

Notices

Reply
 
Thread Tools Search this Thread
Old Yesterday, 02:42 PM   #1
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
Trying to use python function replace

Trying to use Python function replace to change strings from all caps to title case. This is the regular expression that I use and it appears to find what I want, but when I click on replace, nothing changes. This is the search that I use:

1\Name=title case
1\Find="(?<=>)([\\w —’]*)(?=</a)"
1\Replace=\\F<titlecase>
1\Controls=RX DN CF

Also, where are the Python functions stored on Windows 11?
jwes is offline   Reply With Quote
Old Yesterday, 03:11 PM   #2
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
They are stored in your Sigil Preferences folder.

I will run this test on my 2.7.0 build to see if I can replicate what you are seeing.
KevinH is offline   Reply With Quote
Old Yesterday, 04:37 PM   #3
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
Yes a recently introduced bug of some sort. The titlecase python function is somehow getting lost which breaks the apply_func_to_match_groups routine.

I will get you a fix or a workaround.
KevinH is offline   Reply With Quote
Old Yesterday, 04:39 PM   #4
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
Quote:
Originally Posted by KevinH View Post
They are stored in your Sigil Preferences folder.
If that is the folder I get when I click on Open preferences location, then there are no Python scripts but there is a file named replace_functions.json.
jwes is offline   Reply With Quote
Old Yesterday, 04:55 PM   #5
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 29,016
Karma: 210162574
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Yes. The user defined Python functions are stored in the json file in the user's preference directory. They are not kept in a Python script. The builtins like titlecase are part of the Sigil program directory. Namely titlecase.py, fr_utils.py and functionrep.py in the python3lib directory
DiapDealer is offline   Reply With Quote
Old Yesterday, 06:18 PM   #6
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
I put some logging code in titlecase.py and it's never called.
jwes is offline   Reply With Quote
Old Yesterday, 06:33 PM   #7
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
Yes because the routine that invokes it is broken. Somehow the SigilMatch object end() access method is being confused with the end attribute of the match object.

I do not know why that is showing up now and not earlier.

Either way, make the following change in functionsearch.py inside the SigilMatch object changing its "beg" and "end" attributes to mtchbeg and mtchend and updating the accessor methods which should prevent the name collision with the end() method and all should work.

So after modifications it should look like the following:


Code:
class SigilMatch(object):

    def __init__(self, text, groupslist):
        self.string = text
        self.groupslist = groupslist
        self.mtchbeg, self.mtchend = groupslist[0]

    def start(self):
        return self.mtchbeg

    def end(self):
        return self.mtchend

    def span(self, i):
        if i < 0 or i >= len(self.groupslist):
            raise IndexError("match group index not valid");
        return self.groupslist[i][0], self.groupslist[i][1]

    def group(self, i=0):
        if i < 0 or i >= len(self.groupslist):
            raise IndexError("match group index not valid");
        return self.string[self.groupslist[i][0] : self.groupslist[i][1]]

    def groups(self):
        results = []
        for i, v in enumerate(self.groupslist):
            if i > 0:
                results.append(self.string[v[0]: v[1]])
        return results
KevinH is offline   Reply With Quote
Old Yesterday, 06:34 PM   #8
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
Please give that a try and let me know how it goes.
KevinH is offline   Reply With Quote
Old Yesterday, 06:55 PM   #9
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
Quote:
Originally Posted by KevinH View Post
Please give that a try and let me know how it goes.
Still nothing. Here is the changed function:

Code:
class SigilMatch(object):

    def __init__(self, text, groupslist):
        self.string = text
        self.groupslist = groupslist
        self.mtchbeg, self.mtchend = groupslist[0]

    def start(self):
        return self.mtchbeg

    def end(self):
        return self.mtchend

    def span(self, i):
        if i < 0 or i >= len(self.groupslist):
            raise IndexError("match group index not valid");
        return self.groupslist[i][0], self.groupslist[i][1]

    def group(self, i=0):
        if i < 0 or i >= len(self.groupslist):
            raise IndexError("match group index not valid");
        return self.string[self.groupslist[i][0] : self.groupslist[i][1]]

    def groups(self):
        results = []
        for i, v in enumerate(self.groupslist):
            if i > 0:
                results.append(self.string[v[0]: v[1]])
        return results
jwes is offline   Reply With Quote
Old Yesterday, 07:26 PM   #10
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
That is strange, as this change allows all my tests with sub match groups in python function replace for titlecase to now work. Are you sure you are using this new functionsearch.py file?

Please try the simple example shown in the following images. The goal is to capture the text inside the h1 tag and run titlecase on it. Clicking the find button results in the following:

See before.png

Then click on the replace button and the fixed version of Sigil nicely titlecases the contents of the h1 tag.

See the after.png

This simple test did not work without the change I posted earlier. No idea why that attribute name and accessor function name collision did not happen in earlier testing but it did now until this fix.

I am going to push this change to master so it appears in the next release.

So please try a trivial test case like this one with your modified functionsearch.py put in the right place in the python3lib inside Sigil's program. It should all now work.
Attached Thumbnails
Click image for larger version

Name:	before.png
Views:	8
Size:	282.8 KB
ID:	220249   Click image for larger version

Name:	after.png
Views:	7
Size:	282.0 KB
ID:	220250  

Last edited by KevinH; Yesterday at 08:40 PM.
KevinH is offline   Reply With Quote
Old Yesterday, 07:30 PM   #11
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
The above is with the builtin titlecase function unmodified.
KevinH is offline   Reply With Quote
Old Yesterday, 09:14 PM   #12
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
It works now. I had to close and reopen sigil. Many thanks.
jwes is offline   Reply With Quote
Old Yesterday, 10:29 PM   #13
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
Glad to hear it works! Thanks for your bug report!
KevinH is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Volunteer to write new Chapter on Python Function Replace for Sigil User Guide KevinH Sigil 8 06-03-2025 08:11 PM
Python function terminated unexpectedly woodr2011 Library Management 12 11-04-2019 06:26 PM
Python function terminated stmbtbob Devices 2 05-08-2015 09:21 AM
Python Function Error LoveMissBubbly Calibre 5 09-21-2011 05:52 PM
txt2html function in python benluo Calibre 0 11-15-2010 10:27 AM


All times are GMT -4. The time now is 02:06 AM.


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