예제 #1
0
    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'})
예제 #2
0
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
예제 #3
0
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
예제 #4
0
    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'})
예제 #5
0
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
예제 #6
0
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