Results 1 to 3 of 3

Thread: python script - Traits and ancillaries in epub form

  1. #1

    Default python script - Traits and ancillaries in epub form

    I got tired of trying and failing to read the awful M2TW tooltips and I want to be able just easily check them on my phone or whatever so I went completely bonkers and wrote a script.

    Sorry for the ad-hoc nonsense. The XML seems to not have everything and I have no idea how to properly write a parser. Seems to work so it's probably broken. Needs ebooklib and needs to be dropped in mods/ebii/data/text if you want to generate it yourself. Here's the output for 2.35A r2.

    Code:
    #!/usr/bin/env python3
    
    from ebooklib import epub
    from enum import Enum, auto
    
    def get_content2(fname):
        endtrait = '---------------\n'
    
        class Modes(Enum):
            NAMEID = auto()
            EFFECTSORDESC = auto()
            DESC = auto()
            EFFECTS = auto()
            DONE = auto()
            SKIP = auto()
    
        mode = Modes.NAMEID
    
        nameid = ''
        name = ''
        name_text = ''
        desc = ''
        effects = ''
    
        html = ''
        with open(fname, encoding='utf_16_le') as f:
            for line in f.readlines():
                line = line.replace(r'\n', '</p><p>')
                tokens = line.split()
                if mode != Modes.SKIP:
                    if mode == Modes.NAMEID:
                        if line[0] == '{':
                            if len(tokens) == 1:
                                # This probably shouldn't be included.
                                mode = Modes.SKIP
                            else:
                                nameid = tokens[0][1:-1]
                                name = line.replace(tokens[0], '').strip()
                                #print(f"{nameid} - {name} - {len(tokens)}")
                                if name == "GOD NOT DEFINED":
                                    mode = Modes.SKIP
                                mode = Modes.EFFECTSORDESC
                        if line == endtrait:
                            mode = Modes.DONE
                    elif mode == Modes.EFFECTSORDESC:
                        if '_effects_desc' in line:
                            mode = Modes.EFFECTS
                        elif '_desc' in line:
                            mode = Modes.DESC
                        elif line == endtrait:
                            mode = Modes.DONE
                    elif mode == Modes.DESC:
                        if line[0] == '{':
                            mode = Modes.EFFECTS
                        elif line == endtrait:
                            mode = Modes.DONE
                        else:
                            desc += line
                    elif mode == Modes.EFFECTS:
                        if line[0] == '{':
                            mode = Modes.DESC
                        elif line == endtrait:
                            mode = Modes.DONE
                        else:
                            effects += line
                if mode == Modes.DONE or mode == Modes.SKIP:
                    if mode != Modes.SKIP:
                        if desc or effects:
                            # Skip if neither desc or effects are defined
                            html += f'\n<h2>{name}</h2>\n<small><i>{nameid}</i></small>\n'
                            if desc:
                                html += f'<h3>Description</h3>\n{desc}\n'
                            if effects:
                                html += f'<h3>Effects</h3>\n<p>{effects}\n'
                    nameid = ''
                    name = ''
                    name_text = ''
                    desc = ''
                    effects = ''
                    mode = Modes.NAMEID
        return html
    
    if __name__ == "__main__":
        traits = get_content2('export_vnvs.txt')
        ancillaries = get_content2('export_ancillaries.txt')
        book = book = epub.EpubBook()
        book.set_title('Europa Barbarorum II - Traits and Ancillaries')
        book.add_metadata('DC', 'description', 'Traits and Ancillaries for easy reference')
        c1 = epub.EpubHtml(title='Ancillaries',
                           file_name='ancillaries.xhtml',
                           lang='en')
        c1.set_content(f"<html><body><h1>Ancillaries</h1>{ancillaries}</body></html>")
        c2 = epub.EpubHtml(title='Traits',
                           file_name='traits.xhtml',
                           lang='en')
        c2.set_content(f"<html><body><h1>Traits</h1>{traits}</body></html>")
        book.add_item(c1)
        book.add_item(c2)
        book.toc = [c1, c2]
        book.spine = [c1, c2]
        epub.write_epub('Europa Barbarorum II - Traits and Ancillaries.epub', book)

  2. #2

    Default Re: python script - Traits and ancillaries in epub form

    I can't edit posts yet I guess. The script has been tweaked slightly to work with R3. The Drive link at the top has also been updated for R3. Thanks mod team for a great update!

    Code:
    #!/usr/bin/env python3
    
    from ebooklib import epub
    from enum import Enum, auto
    
    def get_content2(fname):
        endtrait = '---------------\n'
    
        class Modes(Enum):
            NAMEID = auto()
            EFFECTSORDESC = auto()
            DESC = auto()
            EFFECTS = auto()
            DONE = auto()
            SKIP = auto()
    
        mode = Modes.NAMEID
    
        nameid = ''
        name = ''
        name_text = ''
        desc = ''
        effects = ''
    
        html = ''
        with open(fname, encoding='utf_16_le') as f:
            for line in f.readlines():
                line = line.replace(r'\n', '</p><p>')
                tokens = line.split()
                if mode != Modes.SKIP:
                    if mode == Modes.NAMEID:
                        if line[0] == '{':
                            tokens = line.split('}')
                            if len(tokens) == 1:
                                # This probably shouldn't be included.
                                mode = Modes.SKIP
                            else:
                                nameid = tokens[0] + '}'
                                name = tokens[1].strip()
                                #print(f"{nameid} - {name} - {len(tokens)}")
                                if name == "GOD NOT DEFINED":
                                    mode = Modes.SKIP
                                mode = Modes.EFFECTSORDESC
                        if line == endtrait:
                            mode = Modes.DONE
                    elif mode == Modes.EFFECTSORDESC:
                        if '_effects_desc' in line:
                            mode = Modes.EFFECTS
                        elif '_desc' in line:
                            mode = Modes.DESC
                        elif line == endtrait:
                            mode = Modes.DONE
                    elif mode == Modes.DESC:
                        if line[0] == '{':
                            mode = Modes.EFFECTS
                        elif line == endtrait:
                            mode = Modes.DONE
                        else:
                            desc += line
                    elif mode == Modes.EFFECTS:
                        if line[0] == '{':
                            mode = Modes.DESC
                        elif line == endtrait:
                            mode = Modes.DONE
                        else:
                            effects += line
                if mode == Modes.DONE or mode == Modes.SKIP:
                    if mode != Modes.SKIP:
                        if desc or effects:
                            # Skip if neither desc or effects are defined
                            html += f'\n<h2>{name}</h2>\n<small><i>{nameid}</i></small>\n'
                            if desc:
                                html += f'<h3>Description</h3>\n{desc}\n'
                            if effects:
                                html += f'<h3>Effects</h3>\n<p>{effects}\n'
                    nameid = ''
                    name = ''
                    name_text = ''
                    desc = ''
                    effects = ''
                    mode = Modes.NAMEID
        return html
    
    if __name__ == "__main__":
        traits = get_content2('export_vnvs.txt')
        ancillaries = get_content2('export_ancillaries.txt')
        book = book = epub.EpubBook()
        book.set_title('Europa Barbarorum II - Traits and Ancillaries')
        book.add_metadata('DC', 'description', 'Traits and Ancillaries for easy reference')
        c1 = epub.EpubHtml(title='Ancillaries',
                           file_name='ancillaries.xhtml',
                           lang='en')
        c1.set_content(f"<html><body><h1>Ancillaries</h1>{ancillaries}</body></html>")
        c2 = epub.EpubHtml(title='Traits',
                           file_name='traits.xhtml',
                           lang='en')
        c2.set_content(f"<html><body><h1>Traits</h1>{traits}</body></html>")
        book.add_item(c1)
        book.add_item(c2)
        book.toc = [c1, c2]
        book.spine = [c1, c2]
        epub.write_epub('Europa Barbarorum II - Traits and Ancillaries.epub', book)

  3. #3

    Default Re: python script - Traits and ancillaries in epub form

    I didn't realize but traits were completely broken in that version. I've fixed that in the script and also uploaded a new version in the drive link.

    Code:
    #!/usr/bin/env python3
    
    from ebooklib import epub
    from enum import Enum, auto
    
    class Modes(Enum):
        NAMEID = auto()
        EFFECTSORDESC = auto()
        DESC = auto()
        EFFECTS = auto()
        DONE = auto()
        SKIP = auto()
    
    def get_ancillaries(fname):
        endtrait = '---------------\n'
    
        mode = Modes.NAMEID
    
        nameid = ''
        name = ''
        desc = ''
        effects = ''
    
        html = ''
        with open(fname, encoding='utf_16_le') as f:
            for line in f.readlines():
                line = line.replace(r'\n', '</p><p>')
                tokens = line.split()
                if mode != Modes.SKIP:
                    if mode == Modes.NAMEID:
                        if line[0] == '{':
                            tokens = line.split('}')
                            if len(tokens) == 1:
                                # This probably shouldn't be included.
                                mode = Modes.SKIP
                            else:
                                nameid = tokens[0] + '}'
                                name = tokens[1].strip()
                                #print(f"{nameid} - {name} - {len(tokens)}")
                                if name == "GOD NOT DEFINED":
                                    mode = Modes.SKIP
                                mode = Modes.EFFECTSORDESC
                        if line == endtrait:
                            mode = Modes.DONE
                    elif mode == Modes.EFFECTSORDESC:
                        if '_effects_desc' in line:
                            mode = Modes.EFFECTS
                        elif '_desc' in line:
                            mode = Modes.DESC
                        elif line == endtrait:
                            mode = Modes.DONE
                    elif mode == Modes.DESC:
                        if line[0] == '{':
                            mode = Modes.EFFECTS
                        else:
                            desc += line
                        if line == endtrait:
                            mode = Modes.DONE
                    elif mode == Modes.EFFECTS:
                        if line[0] == '{':
                            mode = Modes.DESC
                        else:
                            effects += line
                        if line == endtrait:
                            mode = Modes.DONE
                if mode == Modes.DONE or mode == Modes.SKIP:
                    if mode != Modes.SKIP:
                        if desc or effects:
                            # Skip if neither desc or effects are defined
                            html += f'\n<h2>{name}</h2>\n<small><i>{nameid}</i></small>\n'
                            if desc:
                                html += f'<h3>Description</h3>\n{desc}\n'
                            if effects:
                                html += f'<h3>Effects</h3>\n<p>{effects}\n'
                    nameid = ''
                    name = ''
                    desc = ''
                    effects = ''
                    mode = Modes.NAMEID
        return html
    
    def get_vnvs(fname):
        mode = Modes.NAMEID
    
        nameid = ''
        name = ''
        desc = ''
        effects = ''
    
        html = ''
        with open(fname, encoding='utf_16_le') as f:
            for line in f.readlines():
                line = line.replace(r'\n', '</p><p>')
                tokens = line.split('}')
                if mode != Modes.SKIP:
                    if mode == Modes.NAMEID:
                        if len(tokens) == 1:
                            # Probably the first line containing 
                            mode = Modes.SKIP
                        elif '_desc' in line:
                            # _desc with no nameid or epithet
                            mode = Modes.SKIP
                        elif 'GOD NOT DEFINED' in line:
                            mode = Modes.SKIP
                        else:
                            nameid = tokens[0] + '}'
                            name = tokens[1]
                            mode = Modes.EFFECTSORDESC
                    elif mode == Modes.EFFECTSORDESC:
                        if '_effects_desc' in line:
                            if not tokens[1].isspace():
                                effects = tokens[1]
                                mode = Modes.DONE
                        elif 'epithet_desc' in line:
                            # skip
                            pass
                        elif '_desc' in line:
                            if not tokens[1].isspace():
                                desc = tokens[1]
                        else:
                            mode = Modes.DONE
                if mode == Modes.DONE or mode == Modes.SKIP:
                    if mode != Modes.SKIP:
                        if desc or effects:
                            # Skip if neither desc or effects are defined
                            html += f'\n<h2>{name}</h2>\n<small><i>{nameid}</i></small>\n'
                            if desc:
                                html += f'<h3>Description</h3>\n{desc}\n'
                            if effects:
                                html += f'<h3>Effects</h3>\n<p>{effects}\n'
                            #print(f"{name} - {nameid} - {desc} - {effects}")
                    nameid = ''
                    name = ''
                    desc = ''
                    effects = ''
                    mode = Modes.NAMEID
        return html
    
    if __name__ == "__main__":
        traits = get_vnvs('export_vnvs.txt')
        ancillaries = get_ancillaries('export_ancillaries.txt')
        book = book = epub.EpubBook()
        book.set_title('Europa Barbarorum II - Traits and Ancillaries')
        book.add_metadata('DC', 'description', 'Traits and Ancillaries for easy reference')
        c1 = epub.EpubHtml(title='Ancillaries',
                           file_name='ancillaries.xhtml',
                           lang='en')
        c1.set_content(f"<html><body><h1>Ancillaries</h1>{ancillaries}</body></html>")
        c2 = epub.EpubHtml(title='Traits',
                           file_name='traits.xhtml',
                           lang='en')
        c2.set_content(f"<html><body><h1>Traits</h1>{traits}</body></html>")
        book.add_item(c1)
        book.add_item(c2)
        book.toc = [c1, c2]
        book.spine = [c1, c2]
        epub.write_epub('Europa Barbarorum II - Traits and Ancillaries.epub', book)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •