Debian Codegraph @

texmf-dist/scripts/lilyglyphs/lily-glyph-commands.py

raw
1 #!/usr/bin/env python
2
3 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 # %
5 # This file is part of the 'lilyglyphs' LaTeX package. %
6 # ========== %
7 # %
8 # https://github.com/openlilylib/lilyglyphs %
9 # http://www.openlilylib.org/lilyglyphs %
10 # %
11 # Copyright 2012-2020 Urs Liska and others, [email protected] %
12 # %
13 # 'lilyglyphs' is free software: you can redistribute it and/or modify %
14 # it under the terms of the LaTeX Project Public License, either %
15 # version 1.3 of this license or (at your option) any later version. %
16 # You may find the latest version of this license at %
17 # http://www.latex-project.org/lppl.txt %
18 # more information on %
19 # http://latex-project.org/lppl/ %
20 # and version 1.3 or later is part of all distributions of LaTeX %
21 # version 2005/12/01 or later. %
22 # %
23 # This work has the LPPL maintenance status 'maintained'. %
24 # The Current Maintainer of this work is Urs Liska (see above). %
25 # %
26 # This work consists of the files listed in the file 'manifest.txt' %
27 # which can be found in the 'license' directory. %
28 # %
29 # This program is distributed in the hope that it will be useful, %
30 # but WITHOUT ANY WARRANTY; without even the implied warranty of %
31 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. %
32 # %
33 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
35 # ########################################################################
36 # #
37 # genGlyphCommands.py #
38 # #
39 # generate commands based on Emmentaler glyphs #
40 # #
41 # ########################################################################
42
43 import os, sys, argparse, lilyglyphs_common as lg
44
45 def main(input_file):
46 """Do the main work of the script"""
47
48 in_dir, in_file = os.path.split(input_file)
49 os.chdir(in_dir)
50
51 # load and parse input file
52 lg.read_input_file(in_file)
53 read_entries()
54
55 # generate LaTeX commands and save them to output file
56 # (will be a .tex sibling of the input file)
57 lg.generate_latex_commands()
58 out_file, out_ext = os.path.splitext(input_file)
59 lg.write_latex_file(os.path.join(os.getcwd(), out_file + '.tex'))
60
61 def read_entries():
62 """Parse the input file and fill a dictionary"""
63 entry = {}
64 def reset_entry():
65 """Set all members of the dict empty,
66 because not all are present in each entry."""
67 entry['cmd'] = ''
68 entry['element'] = ''
69 entry['type'] = 'glyphname'
70 entry['comment'] = []
71 entry['raise'] = ''
72 entry['scale'] = ''
73
74 reset_entry()
75 scale = ''
76 rais = ''
77 for line in lg.definitions_file:
78 line = line.strip()
79 # empty line = end of entry
80 if not len(line):
81 # skip if cmd and glyph haven't been filled both
82 if not (entry['cmd'] and entry['element']):
83 print('Skip malformed entry \'' + entry['cmd'] + '\'. Please check input file')
84 reset_entry()
85 else:
86 print('Read entry \'' + entry['cmd'] + '\'')
87 lg.in_cmds[entry['cmd']] = {}
88 lg.in_cmds[entry['cmd']]['element'] = entry['element']
89 lg.in_cmds[entry['cmd']]['type'] = entry['type']
90 lg.in_cmds[entry['cmd']]['comment'] = entry['comment']
91 if scale:
92 lg.in_cmds[entry['cmd']]['scale'] = scale
93 if rais:
94 lg.in_cmds[entry['cmd']]['raise'] = rais
95 reset_entry()
96 # ignore Python or LaTeX style comments
97 elif line[0] in '#%':
98 continue
99 else:
100 # add element to the entry
101 keyval = line.split('=')
102 if keyval[0] == 'scale':
103 scale = keyval[1]
104 elif keyval[0] == 'raise':
105 rais = keyval[1]
106 if keyval[0] == 'comment':
107 entry['comment'] = [keyval[1]]
108 else:
109 entry[keyval[0].strip()] = keyval[1].strip()
110
111
112 def usage():
113 print('genGlyphCommands.py')
114 print('is part of the lilyglyphs package')
115 print('')
116 print('Usage:')
117 print('Pass the name (without path) of an input definitions file')
118 print('(this has to be located in the /stash_new_commands directory.')
119 print('Please refer to the manual (documentation/lilyglyphs.pdf).')
120
121 # ####################################
122 # Finally launch the program
123 if __name__ == "__main__":
124 # parse command line arguments
125 parser = argparse.ArgumentParser(
126 description='Generate LaTeX commands using Emmentaler glyphs',
127 parents=[lg.common_arguments])
128 parser.add_argument('i',
129 type=lg.is_file,
130 metavar='INPUT',
131 help='File with command templates.')
132 args = parser.parse_args()
133
134 # if we have exactly one existing file
135 # join its components and run the program
136 main(os.path.join(os.getcwd(), vars(args)['i']))