11##
2- # copyright 2007 , pg/python project.
2+ # copyright 2009 , pg/python project.
33# http://python.projects.postgresql.org
44##
55"""
6- Python command with a postgresql.driver.pg_api connection.
6+ Python command with a postgresql.driver.pgapi connection.
77"""
88import os
9+ import sys
10+ import re
11+ import code
912import optparse
10- import postgresql .commandoptions as pg_opt
11- import postgresql .clientparams as clientparams
12- import postgresql .python as pg_python
13- import postgresql .driver .pg_api as pg_api
13+ import contextlib
14+ from .. import clientparams
15+ from .. import clientoptparse as pg_opt
16+ from ..resolved import pythoncommand as pycmd
17+
18+ from .pgapi import Connector
1419
1520pq_trace = optparse .make_option (
1621 '--pq-trace' ,
1722 dest = 'pq_trace' ,
1823 help = 'trace PQ protocol transmissions' ,
1924 default = None ,
2025)
21- default_options = pg_python .default_options + [
26+ default_options = pg_opt .default_options + [
27+ pg_opt .in_xact ,
2228 pq_trace ,
23- ]
29+ ] + pycmd .default_optparse_options
30+
31+ param_pattern = re .compile (
32+ r'^\s*#\s+-\*-\s+postgresql\.([^:]+):\s+([^\s]*)\s+-\*-\s*$' ,
33+ re .M
34+ )
35+ def extract_parameters (src ):
36+ 'extract hard parameters out of the "-*- postgresql.*: -*-" magic lines'
37+ return [
38+ x for x in re .findall (param_pattern , src )
39+ ]
2440
25- def command (args , environ = os . environ ):
41+ def command (args = sys . argv ):
2642 # Allow connection options to be collected in #!pg_python lines
2743 p = pg_opt .DefaultParser (
2844 "%prog [connection options] [script] [-- script options] [args]" ,
@@ -31,31 +47,73 @@ def command(args, environ = os.environ):
3147 )
3248 p .enable_interspersed_args ()
3349 co , ca = p .parse_args (args [1 :])
50+ in_xact = co .in_xact
3451
35- cond = clientparams .create (co , environ = environ )
36- connector = pg_api . connector (** cond )
52+ cond = clientparams .create (co , os . environ )
53+ connector = Connector (** cond )
3754 connection = connector .create ()
3855
56+ pythonexec = pycmd .Execution (ca ,
57+ context = getattr (co , 'python_context' , None ),
58+ loader = getattr (co , 'python_main' , None ),
59+ )
60+ # Some points of configuration need to be demanded by a script.
61+ src = pythonexec .get_main_source ()
62+ if src is not None :
63+ hard_params = dict (extract_parameters (src ))
64+ if hard_params :
65+ iso = hard_params .get ('isolation' )
66+ if iso is not None :
67+ if iso == 'none' :
68+ in_xact = False
69+ else :
70+ in_xact = True
71+ connection .xact (isolation = iso )
72+
73+ builtin_overload = {
74+ # New built-ins
75+ 'pg_connector' : connector ,
76+ 'pg_con' : connection ,
77+ 'db' : connection ,
78+ 'query' : connection .query ,
79+ 'cquery' : connection .cquery ,
80+ 'statement' : connection .statement ,
81+ 'execute' : connection .execute ,
82+ 'settings' : connection .settings ,
83+ 'cursor' : connection .cursor ,
84+ 'proc' : connection .proc ,
85+ 'xact' : connection .xact ,
86+ }
87+ restore = {k : __builtins__ [k ] for k in builtin_overload }
88+
3989 trace_file = None
4090 if co .pq_trace is not None :
4191 trace_file = open (co .pq_trace , 'a' )
92+ __builtins__ .update (builtin_overload )
4293 try :
4394 if trace_file is not None :
4495 connection .tracer = trace_file .write
45- return pg_python .run (
46- connection , ca , co ,
47- in_xact = co .in_xact ,
48- environ = environ
49- )
96+
97+ with connection :
98+ if in_xact :
99+ with connection .xact :
100+ rv = pythonexec (
101+ context = pycmd .postmortem (os .environ .get ('PYTHON_POSTMORTEM' ))
102+ )
103+ else :
104+ rv = pythonexec (
105+ context = pycmd .postmortem (os .environ .get ('PYTHON_POSTMORTEM' ))
106+ )
50107 finally :
108+ # restore __builtins__
109+ __builtins__ .update (restore )
110+ for x in builtin_overload .keys ():
111+ del __builtins__ [x ]
51112 if trace_file is not None :
52113 trace_file .close ()
53-
54- def pg_python ():
55- import sys
56- sys .exit (command (sys .argv ))
114+ return rv
57115
58116if __name__ == '__main__' :
59- pg_python ( )
117+ sys . exit ( command () )
60118##
61119# vim: ts=3:sw=3:noet:
0 commit comments