sqlite to .el translator [2 of 2]
smitty1e
smitty1e at gmail.com
Fri Oct 26 00:25:08 EDT 2007
Since I haven't yet been shot for the earlier post of a .py to sqlite
rendering script, here is another script that takes the previous
output and does something useful.
Note that this is not the full integration with PyMacs--I rather hope
to spark some interest and save some time headbanging on a) getting
this right, and b) getting it integrated.
What sort of a poetic reference can we get here? "Inflammation of the
source code reminds me of your script..."
Cheers,
Chris
#invocation:
smitty at localhost ~/proj/mddl4/test $ ./elisp_extractor.py -f output -o
sample.el -p sample
#output:
smitty at localhost ~/proj/mddl4/test $ cat sample.el
(sample-sample---init-- (self some-string))
(sample-sample-show (self))
#script:
#!/usr/bin/python
from optparse import OptionParser
import re
import sqlite3
CLASS_OFFSET = 4
ALL_CLASSES = """SELECT parse_tree_id
, parse_tree_value
FROM tbl_parse_tree A
INNER JOIN
( SELECT MAX(parse_tree_id) AS
parse_tree_id_
FROM tbl_parse_tree
UNION ALL
SELECT parse_tree_id + %s --
offset to get name
FROM tbl_parse_tree
WHERE parse_tree_symbol_id=328 --
classdefs
AND parse_tree_indent = 0 --
but not nested
ORDER BY 1 DESC) B
ON A.parse_tree_id = B.parse_tree_id_;""" %
CLASS_OFFSET
FUNC_OFFSET = 4
CLASS_FUNCS = """SELECT parse_tree_id
, parse_tree_value
FROM
tbl_parse_tree A
INNER JOIN
( SELECT parse_tree_id + %s AS
parse_tree_id_
FROM tbl_parse_tree
WHERE parse_tree_id BETWEEN %s AND
%s
AND parse_tree_symbol_id = 261
--funcdefs
AND parse_tree_indent = 1 ) B
ON A.parse_tree_id=B.parse_tree_id_;
--no nested funcs"""
FUNC_ARGS = """SELECT parse_tree_value
FROM tbl_parse_tree
WHERE parse_tree_id BETWEEN %s AND
(SELECT MIN(parse_tree_id)
FROM tbl_parse_tree
WHERE parse_tree_id > %s
AND parse_tree_symbol_id = 8)
AND parse_tree_symbol_id = 1;"""
def extract_emacs_representation_of_python_classes( module_name
, file_name
, target_name ):
"""module_name prefixes all of the functions
file_name is an sqlite database file specification
target_name is an output .el file specification
"""
conn = sqlite3.connect( file_name )
conn.isolation_level = None
c = conn.cursor()
c.execute( ALL_CLASSES )
buf = 0
class_ranges = []
targ_file = open( target_name, 'w' )
#build up a list of class data
for row in c:
if (buf != 0):
class_ranges.append( (row[0],buf-1,row[1]) )
buf = row[0]
for cur_class in class_ranges:
c.execute( CLASS_FUNCS % ( FUNC_OFFSET,
cur_class[0],cur_class[1]) )
func_args = []
for row in c:
func_args.append( (row[0]+1,row[1]) )
#print report
cur_prefix = "%s-%s" % (module_name,
re.sub("_","-",cur_class[2]))
for row in func_args:
c.execute( FUNC_ARGS % (row[0], row[0]) )
args = []
for arg in c:
args.append( arg[0] )
targ_file.write( "(%s-%s (%s))\n" \
% ( cur_prefix
, re.sub( "_", "-", row[1])
, re.sub( "_", "-", " ".join(args))))
targ_file.close()
def main():
usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.add_option("-f", "--file", dest="filename"
, action="store", type="string"
, help ="connect to sqlite file FILENAME")
parser.add_option("-o", "--output",dest="output"
, action="store", type="string"
, help ="name of .el output file")
parser.add_option("-p", "--prefix",dest="prefix"
, action="store", type="string"
, help ="prefix for use in elisp")
(options, args) = parser.parse_args()
extract_emacs_representation_of_python_classes( options.prefix
, options.filename
, options.output )
if __name__ == "__main__":
main()
More information about the Python-list
mailing list