Disclaimer(s): the author is nobody's pythonista. This could probably
be done more elegantly.
The driver for the effort is to get PyMacs to work with new-style
classes.
This rendering stage stands alone, and might be used for other
purposes.
A subsequent post will show using the resulting file to produce (I
think valid) .el trampoline
signatures for PyMacs.
If nothing else, it shows some python internals in an interesting way.
Tested against version 2.5.1
Maybe "lumberjack .py" would be a better name, since "It cuts down
trees, goes real slow, and uses disk galore. Wishes it'd been
webfoot[1], just like its dear author".
Cheers,
Chris
[1] Author was born in Oregon.
#A sample file:
class sample( object ):
"""fairly trivial sample class for demonstration purposes.
"""
def __init__( self
, some_string ):
self.hold_it = some_string
def show( self ):
print self.hold_it
#Invocation:
# ./pysqlrender.py -f sample.py -o output
#Script:
#!/usr/bin/python
"""Script to dump the parse tree of an input file to a SQLite
database.
"""
from optparse import OptionParser
import os
import parser
import pprint
import re
import sqlite3
import symbol
import token
import types
from types import ListType \
, TupleType
target_table = """CREATE TABLE tbl_parse_tree (
parse_tree_id INTEGER PRIMARY KEY
AUTOINCREMENT
, parse_tree_symb ol_id
, parse_tree_inde nt
, parse_tree_valu e );"""
target_insert = """INSERT INTO tbl_parse_tree (
parse_tree_symb ol_id
, parse_tree_inde nt
, parse_tree_valu e )
VALUES (%s, %s, '%s' );"""
symbol_table = """CREATE TABLE tlp_parse_tree_ symbol (
parse_tree_symb ol_id INTEGER PRIMARY KEY
, parse_tree_symb ol_val );"""
symbol_insert = """INSERT INTO tlp_parse_tree_ symbol (
parse_tree_symb ol_id
, parse_tree_symb ol_val )
VALUES ( %s, '%s' );"""
class symbol_manager( object ):
""" Class to merge symbols and tokens for ease of use.
"""
def __init__( self
, c ):
for k in symbol.sym_name :
sql = symbol_insert % ( k, symbol.sym_name[k] )
try:
c.execute( sql )
except sqlite3.Integri tyError:
pass
for k in token.tok_name:
sql = symbol_insert % ( k, token.tok_name[k] )
try:
c.execute( sql )
except sqlite3.Integri tyError:
pass
def get_symbol( self
, key ):
ret = -1
if symbol.sym_name .has_key(key): ret = symbol.sym_name[key]
elif token.tok_name. has_key(key) : ret = token.tok_name[ key]
return ret
def recurse_it( self, tester ):
"""Check to see if dump_tup should recurse
"""
if self.get_symbol (tester) 0:
return True
return False
class stocker( object ):
"""Remember s the depth of the tree and effects the INSERTs
into the output file.
"""
def __init__( self ):
self.cur_indent = 0
def do_symbol( self
, c
, symbol_value
, val = "" ):
"""Stuff something from the parse tree into the database
table.
"""
if symbol_value==5 : self.cur_indent += 1
elif symbol_value==6 : self.cur_indent -= 1
try:
sql = target_insert \
% ( symbol_value
, self.cur_indent
, re.sub( "'", "`", str(val) ))
c.execute( sql )
except AttributeError:
print "connection bad in lexer"
except sqlite3.Operati onalError:
print "suckage at indent of %s for %s" \
% (self.cur_inden t, sql)
def dump_tup( tup
, sym
, c
, stok ):
"""Recursiv e function to descend TUP and analyze its elements.
tup parse tree of a file, rendered as a tuple
sym dictionary rendered from symbol module
c live database cursor
stok output object effect token storage
"""
for node in tup:
typ = type( node )
r = getattr( typ
, "__repr__"
, None )
if (issubclass(typ , tuple) and r is tuple.__repr__) :
if token.tok_name. has_key( node[0] ):
stok.do_symbol( c
, node[0]
, node[1] )
elif sym.recurse_it( node[0] ):
stok.do_symbol( c
, node[0]
, '__py__' ) #If you say node[1] here,
# the sqlite file is fat
# and instructive
for node2 in node[1:]:
dump_tup( node2
, sym
, c
, stok )
else:
stok.do_symbol( c
, node[0]
, node[1] )
dump_tup( node[1]
, sym
, c
, stok )
else:
stok.do_symbol( c
, 0
, node )
def convert_python_ source_tree_to_ table( file_name
, target_name ):
"""Retrieve information from the parse tree of a source file.
Create an output database file in sqlite.
Make a table in there, and then procede to stuff the flattened
input parse tree into it.
file_name Name of the file to read Python source code from.
target_name Name for the sqlite database
"""
x = open( file_name ).readlines()
y = []
[y.append( line.replace("\ r\n","") ) for line in x]
ast = parser.suite( "\n".join(y ) )
conn = sqlite3.connect ( target_name )
conn.isolation_ level = None
c = conn.cursor()
c.execute( target_table )
c.execute( symbol_table )
sym = symbol_manager( c )
stok = stocker()
#pprint.pprint( ast.totuple() )
dump_tup( ast.totuple()
, sym
, c
, stok )
def main():
usage = "usage: %prog [options] arg"
parser = OptionParser(us age)
parser.add_opti on("-f", "--file", dest="filename"
, action="store", type="string"
, help ="read python source from FILENAME")
#TODO: test for existence of output file, eject if exists
parser.add_opti on("-o", "--output",dest="o utput"
, action="store", type="string"
, help ="name of sqlite output file")
(options, args) = parser.parse_ar gs()
convert_python_ source_tree_to_ table( options.filenam e
, options.output )
if __name__ == "__main__":
main() 0 1433 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David Fowler |
last post by:
I'm new to getting in touch with other PHP users/PHP team, so please excuse
me if this post is at all inappropriate or in the wrong place. In the
current release of PHP (5.1.4) and in the CVS for PHP5.1.* and 5.2.* the
SQLite source version only stands at 3.2.8, while the actual realease is now
at 3.3.6. What I was wondering is whether or not this could be updated, as
there have been numerous bug fixes in the SQLite source in the past 6...
|
by: John Salerno |
last post by:
I've been looking around and reading, and I have a few more questions
about SQLite in particular, as it relates to Python.
1. What is the current module to use for sqlite? sqlite3? or is that not
out until Python 2.5?
2. What's the difference between sqlite and pysqlite? Do you need both,
just one, or is one an older version of the same thing?
3. What's the difference between the command line program called sqlite3
|
by: Jim Carlock |
last post by:
I added the following lines to PHP.INI.
extension=php_pdo.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite.dll
specifically in that order. I noticed the extensions getting loaded
are alphabetally listed, so I'm thinking that the order of the load
doesn't matter.
|
by: Luigi |
last post by:
Hello all!
I'm a newbie in PHP. I have written a short script that tries to
update a SQLite database with the user data. It is pretty simple,
something like this:
<?php
$sqlite = sqlite_open("mytest.db", 0666, $sqlite_error);
|
by: Gilles Ganault |
last post by:
Hello
I was looking for a lighter web server than Apache, and
installed Lighttpd on CentOS through yum. It works fine, but I now
need to use SQLite from a PHP script. I seem to understand that it can
either be access directly, or through the PDO interface.
Can you confirm that the RPM that I used will only allow me to work
through PDO, and not the SQLite library directly, and which is more
recommended?
| |
by: Gilles Ganault |
last post by:
Hello
I need to compile PHP5 with SQLite statically and without PDO.
I don't need DB-independence, so it's fine using sqlite_*() functions
instead of PDO.
1. I've downloaded, compiled, and installed
sqlite.org/sqlite-3.5.4.tar.gz, and...
2. used "--with-sqlite --disable-pdo", but it fails.
|
by: Daniel Fetchinson |
last post by:
Does Python 2.5.2's embedded SQLite support full text searching?
Sqlite itself is not distributed with python. Only a python db api
compliant wrapper is part of the python stdlib and as such it is
completely independent of the sqlite build. In other words, if your
sqlite build supports full text searching you can use it through the
python sqlite wrapper (that is part of the stdlib) and if it doesn't
then not. This is true for any sqlite...
|
by: Joe Goldthwaite |
last post by:
Thanks Guilherme. That helped. I guess I was thinking that pysqlite would
automatically come with some version of sqlite. The fact that it doesn't is
what was causing me to get the strange results.
I downloaded the Windows version of the SQLite3.dll. I didn't know where to
put it so I first put it in its own directory and tried to register it.
That didn't work so I just moved it to the pysqlite directory in
site-packages. That did...
|
by: timotoole |
last post by:
Hi all,
On a (sun) webserver that I use, there is python 2.5.1 installed. I'd
like to use sqlite3 with this, however sqlite3 is not installed on the
webserver. If I were able to compile sqlite using a sun machine (I
normally use linux machines) and place this in my lunix home account
would I be able to use python and sqlite?
Any thoughts? I know its a bit of a stretch ...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |