def test_sanitize(self): self.assertEqual(s.sanitize({'a': 'b'}), {'a': 'b'}) self.assertEqual(s.sanitize({'a': [1, 2]}), {'a': [1, 2]}) self.assertEqual(s.sanitize({'a.b': 'c'}), {'a_,_b': 'c'}) self.assertEqual(s.sanitize({'a': {'b': 'c'}}), {'a': {'b': 'c'}}) self.assertEqual(s.sanitize({'a': {'b.c': 'd'}}), {'a': {'b_,_c': 'd'}}) self.assertEqual(s.sanitize({'a.$.b': 'c'}), {'a_,__$__,_b': 'c'})
def sanitize(value): """"Sanitize" a value (e.g. a document) for safe storage in MongoDB. Converts periods (``.``) and dollar signs (``$``) in key names to escaped versions. See :func:`~professor.skeleton.desanitize` for the inverse. """ t = type(value) if t == list: return map(sanitize, value) elif t == dict: return dict((k.replace('$', '_$_').replace('.', '_,_'), sanitize(v)) for k, v in value.iteritems()) elif t not in BSON_TYPES: raise InvalidDocument('unknown BSON type %r' % t) else: return value
def test_sanitize(self): self.assertEqual(s.sanitize({'a': 'b'}), {'a': 'b'}) self.assertEqual(s.sanitize({'a': [1, 2]}), {'a': [1, 2]}) self.assertEqual(s.sanitize({'a.b': 'c'}), {'a_,_b': 'c'}) self.assertEqual(s.sanitize({'a': {'b': 'c'}}), {'a': {'b': 'c'}}) self.assertEqual(s.sanitize({'a': { 'b.c': 'd' }}), {'a': { 'b_,_c': 'd' }}) self.assertEqual(s.sanitize({'a.$.b': 'c'}), {'a_,__$__,_b': 'c'})
def parse(database, entry): collection = entry['ns'] collection = collection[len(database['dbname']) + 1:] entry['collection'] = collection # skip certain namespaces if collection.startswith('system.') or \ collection.startswith('tmp.mr.'): return False optype = entry.get('op') subparser = PARSERS.get(optype) if subparser: if not subparser(entry): return False entry = sanitize(entry) entry['database'] = database['_id'] db.profiles.save(entry) return True