##// END OF EJS Templates
fix SyntaxError on !(command)...
fix SyntaxError on !(command) prefilter_line expects that trailing newlines will be trimmed, but run_cell passed it lines with a trailing \n. Single-line run_cell now uses prefilter_lines again, and appends newline explicitly. Formerly failing test included.

File last commit:

r3875:ffe043d4
r3908:7462a578
Show More
mongodb.py
101 lines | 3.5 KiB | text/x-python | PythonLexer
MinRK
Started DB backend with mongoDB support.
r3579 """A TaskRecord backend using mongodb"""
#-----------------------------------------------------------------------------
# Copyright (C) 2010 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
from pymongo import Connection
MinRK
Add SQLite backend, DB backends are Configurable...
r3646 from pymongo.binary import Binary
MinRK
various db backend fixes...
r3875 from IPython.utils.traitlets import Dict, List, CUnicode, CStr, Instance
MinRK
Started DB backend with mongoDB support.
r3579
MinRK
eliminate relative imports
r3642 from .dictdb import BaseDB
MinRK
resort imports in a cleaner order
r3631
#-----------------------------------------------------------------------------
MinRK
Started DB backend with mongoDB support.
r3579 # MongoDB class
MinRK
resort imports in a cleaner order
r3631 #-----------------------------------------------------------------------------
MinRK
Refactor newparallel to use Config system...
r3604 class MongoDB(BaseDB):
MinRK
Started DB backend with mongoDB support.
r3579 """MongoDB TaskRecord backend."""
MinRK
Add SQLite backend, DB backends are Configurable...
r3646
MinRK
General improvements to database backend...
r3780 connection_args = List(config=True) # args passed to pymongo.Connection
connection_kwargs = Dict(config=True) # kwargs passed to pymongo.Connection
database = CUnicode(config=True) # name of the mongodb database
MinRK
various db backend fixes...
r3875
_connection = Instance(Connection) # pymongo connection
MinRK
Add SQLite backend, DB backends are Configurable...
r3646
def __init__(self, **kwargs):
super(MongoDB, self).__init__(**kwargs)
MinRK
various db backend fixes...
r3875 if self._connection is None:
self._connection = Connection(*self.connection_args, **self.connection_kwargs)
MinRK
Add SQLite backend, DB backends are Configurable...
r3646 if not self.database:
self.database = self.session
self._db = self._connection[self.database]
MinRK
Started DB backend with mongoDB support.
r3579 self._records = self._db['task_records']
MinRK
various db backend fixes...
r3875 self._records.ensure_index('msg_id', unique=True)
self._records.ensure_index('submitted') # for sorting history
# for rec in self._records.find
MinRK
Add SQLite backend, DB backends are Configurable...
r3646
def _binary_buffers(self, rec):
for key in ('buffers', 'result_buffers'):
MinRK
General improvements to database backend...
r3780 if rec.get(key, None):
MinRK
Add SQLite backend, DB backends are Configurable...
r3646 rec[key] = map(Binary, rec[key])
MinRK
General improvements to database backend...
r3780 return rec
MinRK
Started DB backend with mongoDB support.
r3579
def add_record(self, msg_id, rec):
"""Add a new Task Record, by msg_id."""
# print rec
MinRK
General improvements to database backend...
r3780 rec = self._binary_buffers(rec)
MinRK
various db backend fixes...
r3875 self._records.insert(rec)
MinRK
Started DB backend with mongoDB support.
r3579
def get_record(self, msg_id):
"""Get a specific Task Record, by msg_id."""
MinRK
various db backend fixes...
r3875 r = self._records.find_one({'msg_id': msg_id})
if not r:
# r will be '' if nothing is found
raise KeyError(msg_id)
return r
MinRK
Started DB backend with mongoDB support.
r3579
def update_record(self, msg_id, rec):
"""Update the data in an existing record."""
MinRK
General improvements to database backend...
r3780 rec = self._binary_buffers(rec)
MinRK
various db backend fixes...
r3875
self._records.update({'msg_id':msg_id}, {'$set': rec})
MinRK
Started DB backend with mongoDB support.
r3579
def drop_matching_records(self, check):
"""Remove a record from the DB."""
self._records.remove(check)
def drop_record(self, msg_id):
"""Remove a record from the DB."""
MinRK
various db backend fixes...
r3875 self._records.remove({'msg_id':msg_id})
MinRK
Started DB backend with mongoDB support.
r3579
MinRK
General improvements to database backend...
r3780 def find_records(self, check, keys=None):
"""Find records matching a query dict, optionally extracting subset of keys.
Returns list of matching records.
Parameters
----------
check: dict
mongodb-style query argument
keys: list of strs [optional]
if specified, the subset of keys to extract. msg_id will *always* be
included.
"""
if keys and 'msg_id' not in keys:
keys.append('msg_id')
matches = list(self._records.find(check,keys))
for rec in matches:
rec.pop('_id')
return matches
def get_history(self):
"""get all msg_ids, ordered by time submitted."""
cursor = self._records.find({},{'msg_id':1}).sort('submitted')
return [ rec['msg_id'] for rec in cursor ]
MinRK
Started DB backend with mongoDB support.
r3579