1 """RQL client for cubicweb, connecting to application using pyro |
1 """RQL client for cubicweb, connecting to instance using pyro |
2 |
2 |
3 :organization: Logilab |
3 :organization: Logilab |
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
77 'rollback' : "CubicWeb", |
77 'rollback' : "CubicWeb", |
78 'autocommit' : "Others", |
78 'autocommit' : "Others", |
79 'debug' : "Others", |
79 'debug' : "Others", |
80 }) |
80 }) |
81 |
81 |
82 def __init__(self, application=None, user=None, password=None, |
82 def __init__(self, instance=None, user=None, password=None, |
83 host=None, debug=0): |
83 host=None, debug=0): |
84 CLIHelper.__init__(self, os.path.join(os.environ["HOME"], ".erqlhist")) |
84 CLIHelper.__init__(self, os.path.join(os.environ["HOME"], ".erqlhist")) |
85 self.cnx = None |
85 self.cnx = None |
86 self.cursor = None |
86 self.cursor = None |
87 # XXX give a Request like object, not None |
87 # XXX give a Request like object, not None |
90 from logilab.common.ureports import TextWriter |
90 from logilab.common.ureports import TextWriter |
91 self.writer = TextWriter() |
91 self.writer = TextWriter() |
92 self.autocommit = False |
92 self.autocommit = False |
93 self._last_result = None |
93 self._last_result = None |
94 self._previous_lines = [] |
94 self._previous_lines = [] |
95 if application is not None: |
95 if instance is not None: |
96 self.do_connect(application, user, password, host) |
96 self.do_connect(instance, user, password, host) |
97 self.do_debug(debug) |
97 self.do_debug(debug) |
98 |
98 |
99 def do_connect(self, application, user=None, password=None, host=None): |
99 def do_connect(self, instance, user=None, password=None, host=None): |
100 """connect to an cubicweb application""" |
100 """connect to an cubicweb instance""" |
101 from cubicweb.dbapi import connect |
101 from cubicweb.dbapi import connect |
102 if user is None: |
102 if user is None: |
103 user = raw_input('login: ') |
103 user = raw_input('login: ') |
104 if password is None: |
104 if password is None: |
105 from getpass import getpass |
105 from getpass import getpass |
106 password = getpass('password: ') |
106 password = getpass('password: ') |
107 if self.cnx is not None: |
107 if self.cnx is not None: |
108 self.cnx.close() |
108 self.cnx.close() |
109 self.cnx = connect(login=user, password=password, host=host, |
109 self.cnx = connect(login=user, password=password, host=host, |
110 database=application) |
110 database=instance) |
111 self.schema = self.cnx.get_schema() |
111 self.schema = self.cnx.get_schema() |
112 self.cursor = self.cnx.cursor() |
112 self.cursor = self.cnx.cursor() |
113 # add entities types to the completion commands |
113 # add entities types to the completion commands |
114 self._completer.list = (self.commands.keys() + |
114 self._completer.list = (self.commands.keys() + |
115 self.schema.entities() + ['Any']) |
115 self.schema.entities() + ['Any']) |
116 print _('You are now connected to %s') % application |
116 print _('You are now connected to %s') % instance |
117 |
117 |
118 |
118 |
119 help_do_connect = ('connect', "connect <application> [<user> [<password> [<host>]]]", |
119 help_do_connect = ('connect', "connect <instance> [<user> [<password> [<host>]]]", |
120 _(do_connect.__doc__)) |
120 _(do_connect.__doc__)) |
121 |
121 |
122 def do_debug(self, debug=1): |
122 def do_debug(self, debug=1): |
123 """set debug level""" |
123 """set debug level""" |
124 self._debug = debug |
124 self._debug = debug |
140 for line_desc in self.rset.description]) |
140 for line_desc in self.rset.description]) |
141 |
141 |
142 help_do_description = ('description', "description", _(do_description.__doc__)) |
142 help_do_description = ('description', "description", _(do_description.__doc__)) |
143 |
143 |
144 def do_schema(self, name=None): |
144 def do_schema(self, name=None): |
145 """display information about the application schema """ |
145 """display information about the instance schema """ |
146 if self.cnx is None: |
146 if self.cnx is None: |
147 print _('You are not connected to an application !') |
147 print _('You are not connected to an instance !') |
148 return |
148 return |
149 done = None |
149 done = None |
150 if name is None: |
150 if name is None: |
151 # display the full schema |
151 # display the full schema |
152 self.display_schema(self.schema) |
152 self.display_schema(self.schema) |
187 """handle non command line : |
187 """handle non command line : |
188 if the query is complete, executes it and displays results (if any) |
188 if the query is complete, executes it and displays results (if any) |
189 else, stores the query line and waits for the suite |
189 else, stores the query line and waits for the suite |
190 """ |
190 """ |
191 if self.cnx is None: |
191 if self.cnx is None: |
192 print _('You are not connected to an application !') |
192 print _('You are not connected to an instance !') |
193 return |
193 return |
194 # append line to buffer |
194 # append line to buffer |
195 self._previous_lines.append(stripped_line) |
195 self._previous_lines.append(stripped_line) |
196 # query are ended by a ';' |
196 # query are ended by a ';' |
197 if stripped_line[-1] != ';': |
197 if stripped_line[-1] != ';': |
230 |
230 |
231 |
231 |
232 class CubicWebClientCommand(Command): |
232 class CubicWebClientCommand(Command): |
233 """A command line querier for CubicWeb, using the Relation Query Language. |
233 """A command line querier for CubicWeb, using the Relation Query Language. |
234 |
234 |
235 <application> |
235 <instance> |
236 identifier of the application to connect to |
236 identifier of the instance to connect to |
237 """ |
237 """ |
238 name = 'client' |
238 name = 'client' |
239 arguments = '<application>' |
239 arguments = '<instance>' |
240 options = CONNECT_OPTIONS + ( |
240 options = CONNECT_OPTIONS + ( |
241 ("verbose", |
241 ("verbose", |
242 {'short': 'v', 'type' : 'int', 'metavar': '<level>', |
242 {'short': 'v', 'type' : 'int', 'metavar': '<level>', |
243 'default': 0, |
243 'default': 0, |
244 'help': 'ask confirmation to continue after an error.', |
244 'help': 'ask confirmation to continue after an error.', |