Esempio n. 1
0
    def store_objects(self,
                      upload: Tuple[str, Iterable[dict]],
                      resource_id: Optional[str] = None) -> Optional[str]:

        resource_id = self._to_resource_id(resource_id)
        name, objs = upload

        num_stored = 0
        with removing(create_tempfilename()) as path:
            with self._open_archive(path, 'w') as archive:
                with NamedTemporaryFile() as fobj:
                    num_bytes = 0
                    for obj in objs:
                        serialized = to_json(obj)
                        encoded = serialized.encode(self._encoding)
                        fobj.write(encoded)
                        fobj.write(b'\n')
                        num_bytes += len(encoded) + 1
                        num_stored += 1
                        self.log_debug('stored obj %s', obj.get('_uid', ''))

                    if num_bytes > 0:
                        fobj.seek(0)
                        archive.add(fobj.name, name)

            if num_stored > 0:
                self._file_storage.store_file(resource_id, path)

        self.log_debug('stored %d objects at %s', num_stored, resource_id)
        return resource_id if num_stored > 0 else None
Esempio n. 2
0
    def store_objects(self, objs: Iterable[dict]) -> Optional[str]:
        resource_id = str(uuid4())

        num_stored = 0
        with removing(create_tempfilename()) as path:
            with gzip_open(path, 'wb') as fobj:
                for obj in objs:
                    serialized = to_json(obj)
                    encoded = serialized.encode(self._encoding)
                    fobj.write(encoded)
                    fobj.write(b'\n')
                    num_stored += 1
                    self.log_debug('stored email %s', obj.get('_uid'))

            if num_stored > 0:
                self._file_storage.store_file(resource_id, path)

        self.log_debug('stored %d objects at %s', num_stored, resource_id)
        return resource_id if num_stored > 0 else None
Esempio n. 3
0
 def _pack(cls, content: dict) -> Message:
     body = to_json(content).encode('utf-8')
     return Message(body)
def store_email(email_id: str, email: dict):
    email['_uid'] = email_id

    STORAGE.store_text(email_id, to_json(email))

    INDEX.insert(email_id, email)
    def test_creates_slim_json(self):
        serialized = serialization.to_json({'a': 1, 'b': 2})

        self.assertNotIn('\n', serialized)
        self.assertNotIn(' ', serialized)
    def test_roundtrip(self):
        obj = {'a': 1, 'b': [2]}

        self.assertEqual(obj,
                         serialization.from_json(serialization.to_json(obj)))
Esempio n. 7
0
 def store_object(self, resource_id: str, obj: dict) -> None:
     serialized = to_json(obj)
     self._text_storage.store_text(resource_id, serialized)