Make gen_lfuns.py python3 compatible

Also read files with universal line endings and write files using native line
endings, so the script can also be used on windows without changing the line
endings.
This commit is contained in:
Georg Baum 2016-06-04 19:40:53 +02:00
parent 7ead59da40
commit d9703617bd

View File

@ -13,6 +13,7 @@
# gen_lfuns.py <path/to/LyXAction.cpp> <where/to/save/LFUNs.lyx> # gen_lfuns.py <path/to/LyXAction.cpp> <where/to/save/LFUNs.lyx>
import sys,re,os.path import sys,re,os.path
import io
def error(message): def error(message):
sys.stderr.write(message + '\n') sys.stderr.write(message + '\n')
@ -24,24 +25,24 @@ def usage(prog_name):
DOXYGEN_START = "/*!" DOXYGEN_START = "/*!"
DOXYGEN_END = "}," DOXYGEN_END = "},"
LYX_NEWLINE = "\n\\begin_inset Newline newline\n\\end_inset\n\n" LYX_NEWLINE = u"\n\\begin_inset Newline newline\n\\end_inset\n\n"
LYX_BACKSLASH = "\n\\backslash\n" LYX_BACKSLASH = u"\n\\backslash\n"
HTMLONLY_START = "\\htmlonly" HTMLONLY_START = u"\\htmlonly"
HTMLONLY_END = "\\endhtmlonly" HTMLONLY_END = u"\\endhtmlonly"
LFUN_NAME_ID = "\\var lyx::FuncCode lyx::" LFUN_NAME_ID = u"\\var lyx::FuncCode lyx::"
LFUN_ACTION_ID = "\\li Action: " LFUN_ACTION_ID = u"\\li Action: "
LFUN_NOTION_ID = "\\li Notion: " LFUN_NOTION_ID = u"\\li Notion: "
LFUN_SYNTAX_ID = "\\li Syntax: " LFUN_SYNTAX_ID = u"\\li Syntax: "
LFUN_PARAMS_ID = "\\li Params: " LFUN_PARAMS_ID = u"\\li Params: "
LFUN_SAMPLE_ID = "\\li Sample: " LFUN_SAMPLE_ID = u"\\li Sample: "
LFUN_ORIGIN_ID = "\\li Origin: " LFUN_ORIGIN_ID = u"\\li Origin: "
LFUN_ENDVAR = "\\endvar" LFUN_ENDVAR = u"\\endvar"
ID_DICT = dict(name=LFUN_NAME_ID, action=LFUN_ACTION_ID, notion=LFUN_NOTION_ID, ID_DICT = dict(name=LFUN_NAME_ID, action=LFUN_ACTION_ID, notion=LFUN_NOTION_ID,
syntax=LFUN_SYNTAX_ID, params=LFUN_PARAMS_ID, sample=LFUN_SAMPLE_ID, origin=LFUN_ORIGIN_ID) syntax=LFUN_SYNTAX_ID, params=LFUN_PARAMS_ID, sample=LFUN_SAMPLE_ID, origin=LFUN_ORIGIN_ID)
LFUNS_HEADER = """# gen_lfuns.py generated this file. For more info see http://www.lyx.org/ LFUNS_HEADER = u"""# gen_lfuns.py generated this file. For more info see http://www.lyx.org/
\\lyxformat 506 \\lyxformat 506
\\begin_document \\begin_document
\\begin_header \\begin_header
@ -146,7 +147,7 @@ The \\SpecialChar LyX
""" """
LFUNS_INTRO ="""\\begin_layout Section* LFUNS_INTRO = u"""\\begin_layout Section*
About this manual About this manual
\\end_layout \\end_layout
@ -200,7 +201,7 @@ In the following, all LFUNs are listed, categorized by function.
""" """
LFUNS_FOOTER = """\\end_body LFUNS_FOOTER = u"""\\end_body
\\end_document \\end_document
""" """
@ -322,58 +323,58 @@ def parse_lfun(str):
def write_fields(file, lfun): def write_fields(file, lfun):
"""Writes the LFUN contained in the dict lfun to the file. Does not write a the file header or footer""" """Writes the LFUN contained in the dict lfun to the file. Does not write a the file header or footer"""
# add lfun to LFUNs.lyx # add lfun to LFUNs.lyx
file.write("\\begin_layout Subsection*\n") file.write(u"\\begin_layout Subsection*\n")
file.write(lfun["name"] + "\n") file.write(lfun["name"] + "\n")
file.write("\\end_layout\n") file.write(u"\\end_layout\n")
file.write("\n") file.write(u"\n")
if lfun["action"] != "": if lfun["action"] != "":
file.write("\\begin_layout Description\n") file.write(u"\\begin_layout Description\n")
file.write("Action " + lfun["action"] + "\n") file.write("Action " + lfun["action"] + "\n")
file.write("\\end_layout\n") file.write(u"\\end_layout\n")
file.write("\n") file.write(u"\n")
if lfun["notion"] != "": if lfun["notion"] != "":
file.write("\\begin_layout Description\n") file.write(u"\\begin_layout Description\n")
file.write("Notion " + lfun["notion"] + "\n") file.write("Notion " + lfun["notion"] + "\n")
file.write("\\end_layout\n") file.write(u"\\end_layout\n")
file.write("\n") file.write(u"\n")
if lfun["syntax"] != "": if lfun["syntax"] != "":
file.write("\\begin_layout Description\n") file.write(u"\\begin_layout Description\n")
file.write("Syntax " + lfun["syntax"] + "\n") file.write("Syntax " + lfun["syntax"] + "\n")
file.write("\\end_layout\n") file.write(u"\\end_layout\n")
file.write("\n") file.write(u"\n")
if lfun["params"] != "": if lfun["params"] != "":
file.write("\\begin_layout Description\n") file.write(u"\\begin_layout Description\n")
file.write("Params " + lfun["params"] + "\n") file.write("Params " + lfun["params"] + "\n")
file.write("\\end_layout\n") file.write(u"\\end_layout\n")
file.write("\n") file.write(u"\n")
if lfun["sample"] != "": if lfun["sample"] != "":
file.write("\\begin_layout Description\n") file.write(u"\\begin_layout Description\n")
file.write("Sample " + lfun["sample"] + "\n") file.write("Sample " + lfun["sample"] + "\n")
file.write("\\end_layout\n") file.write(u"\\end_layout\n")
file.write("\n") file.write(u"\n")
if lfun["origin"] != "": if lfun["origin"] != "":
file.write("\\begin_layout Description\n") file.write(u"\\begin_layout Description\n")
file.write("Origin " + lfun["origin"] + "\n") file.write("Origin " + lfun["origin"] + "\n")
file.write("\\end_layout\n") file.write(u"\\end_layout\n")
file.write("\n") file.write(u"\n")
def write_sections(file,lfuns): def write_sections(file,lfuns):
"""Write sections of LFUNs""" """Write sections of LFUNs"""
sections = ["Layout", "Edit", "Math", "Buffer", "System", "Hidden"] sections = ["Layout", "Edit", "Math", "Buffer", "System", "Hidden"]
section_headings = { section_headings = {
"Layout": "Layout Functions (Font, Layout and Textclass related)", "Layout": u"Layout Functions (Font, Layout and Textclass related)",
"Edit": "Editing Functions (Cursor and Mouse Movement, Copy/Paste etc.)", "Edit": u"Editing Functions (Cursor and Mouse Movement, Copy/Paste etc.)",
"Math": "Math Editor Functions", "Math": u"Math Editor Functions",
"Buffer": "Buffer Fuctions (File and Window related)", "Buffer": u"Buffer Fuctions (File and Window related)",
"System": "System Functions (Preferences, LyX Server etc.)", "System": u"System Functions (Preferences, LyX Server etc.)",
"Hidden": "Hidden Functions (not listed for configuration)" "Hidden": u"Hidden Functions (not listed for configuration)"
} }
# write the lfuns to the file # write the lfuns to the file
for val in sections: for val in sections:
file.write("\\begin_layout Section\n") file.write(u"\\begin_layout Section\n")
file.write(section_headings[val] + "\n") file.write(section_headings[val] + "\n")
file.write("\\end_layout\n") file.write(u"\\end_layout\n")
file.write("\n") file.write(u"\n")
for lf in lfuns: for lf in lfuns:
if lf["type"] == val: if lf["type"] == val:
write_fields(file, lf) write_fields(file, lf)
@ -395,13 +396,13 @@ def main(argv):
lfuns_path = lfuns_path + "LFUNs.lyx" lfuns_path = lfuns_path + "LFUNs.lyx"
elif os.path.exists(lfuns_path): elif os.path.exists(lfuns_path):
error(script_name + ": %s already exists, delete it and rerun the script" % lfuns_path) error(script_name + ": %s already exists, delete it and rerun the script" % lfuns_path)
lfuns_file = open(lfuns_path, 'wb') lfuns_file = io.open(lfuns_path, 'w', encoding='utf_8')
else: else:
lfuns_file = sys.stdout lfuns_file = sys.stdout
sys.stderr.write(script_name + ": Start processing " + argv[1] + '\n') sys.stderr.write(script_name + ": Start processing " + argv[1] + '\n')
# Read the input file and write the output file # Read the input file and write the output file
lyxaction_file = open(lyxaction_path, 'rb') lyxaction_file = io.open(lyxaction_path, 'r', encoding='utf_8')
lyxaction_text = lyxaction_file.read() lyxaction_text = lyxaction_file.read()