Ejemplo n.º 1
0
    def create_test_file(self, compression=None):
        """Helper to generate a temporary file to save

        Returns the filename, potenially pointing to a file containing
        the content of self.text and compressed as requested.

        NB File will need to be opened to use.

        """
        # In case tests loop over file used - clean as we go
        if hasattr(self, "tempfile"):  # pragma: no cover
            os.remove(self.tempfile.name)
            del self.tempfile

        # Generate a safe filename - deletion is tearDown's job
        if compression is None:
            self.tempfile = NamedTemporaryFile(prefix="unittest", delete=False)
            self.tempfile.write(self.test_text)
            self.tempfile.close()
        elif compression == "gzip":
            self.tempfile = NamedTemporaryFile(prefix="unittest", suffix=".gz", delete=False)
            self.tempfile.close()
            fh = gzip.open(self.tempfile.name, "wb")
            fh.write(self.test_text)
            fh.close()
        elif compression == "zip":
            self.tempfile = NamedTemporaryFile(prefix="unittest", suffix=".zip", delete=False)
            self.tempfile.close()
            os.remove(self.tempfile.name)
            content = StringIO(self.test_text)
            self.tempfile.name = zip_file(self.tempfile.name, content, compression)
        else:  # pragma: no cover
            raise ValueError("Can't handle compression '%s'", compression)
        return self.tempfile.name
Ejemplo n.º 2
0
 def extract_content(self, compress_with):
     content = self.content
     if compress_with is not None and\
             self.document.get('compression') is not None:
         # Note, we're compressing on the fly, not persisting
         content = zip_file(filename=self.document['filename'],
                            fileobj=content,
                            zip_protocol=compress_with)
     return content
Ejemplo n.º 3
0
    def create_test_file(self, compression=None):
        """Helper to generate a temporary file to save

        Returns the filename, potenially pointing to a file containing
        the content of self.text and compressed as requested.

        NB File will need to be opened to use.

        """
        # In case tests loop over file used - clean as we go
        if hasattr(self, 'tempfile'):  # pragma: no cover
            os.remove(self.tempfile.name)
            del self.tempfile

        # Generate a safe filename - deletion is tearDown's job
        if compression is None:
            self.tempfile = NamedTemporaryFile(prefix='unittest', delete=False)
            self.tempfile.write(self.test_text)
            self.tempfile.close()
        elif compression == 'gzip':
            self.tempfile = NamedTemporaryFile(prefix='unittest',
                                               suffix='.gz',
                                               delete=False)
            self.tempfile.close()
            fh = gzip.open(self.tempfile.name, 'wb')
            fh.write(self.test_text)
            fh.close()
        elif compression == 'zip':
            self.tempfile = NamedTemporaryFile(prefix='unittest',
                                               suffix='.zip',
                                               delete=False)
            self.tempfile.close()
            os.remove(self.tempfile.name)
            content = StringIO(self.test_text)
            self.tempfile.name = zip_file(self.tempfile.name, content,
                                          compression)
        else:  # pragma: no cover
            raise ValueError("Can't handle compression '%s'", compression)
        return self.tempfile.name
Ejemplo n.º 4
0
def upload_report(context, request):
    """View callable method for uploading reports

    Persist the provided content in the database.  If query args
    include the following, take action:

    :query param compress_with: Can be 'gzip' or 'zip' (or None)
      to invoke compression before persisting.

    :query param allow_duplicate_filename: Set true to override
      default of not allowing duplicate filename inserts.

    :query param metadata: Optional dictionary defining additional
      metadata to store with the document.  It is suggested to include
      criteria used in report creation (i.e. 'reportable_region',
      'patient_class', 'include_updates')

    """
    if not hasattr(context, 'filename'):
        raise HTTPBadRequest("Missing upload filename")

    if not hasattr(context, 'file'):
        context.file = request.params[context.filename].file

    def content_type_lookup(compression):
        content_type_map = {
            None: 'text/plain',
            'gzip': 'application/x-gzip',
            'zip': 'application/zip'
        }
        return content_type_map[compression]

    compression = request.params.get('compress_with', None)
    content_type = content_type_lookup(compression)

    if compression:
        zipfile = zip_file(request.params[context.filename].filename,
                           request.params[context.filename].file, compression)
        context.filename = os.path.basename(zipfile)
        context.file = open(zipfile, 'rb')

    allow_duplicate = request.params.get('allow_duplicate_filename', None)
    if not allow_duplicate:
        # Confirm a matching report wasn't already created.
        criteria = {
            'filename': context.filename,
            'report_type': context.report_type
        }
        metadata = json.loads(request.params.get('metadata', "{}"))
        for k in 'reportable_region', 'patient_class',\
                'include_updates':
            if k in metadata:
                criteria[k] = metadata[k]
        match = request.document_store.find_one(criteria)
        if match:
            err = "duplicate filename '%s' exists for '%s'" %\
                (context.filename, context.report_type)
            logging.error(err)
            raise HTTPBadRequest(err)

    # gridfs automatically includes uploadDate of utcnow()
    # content_type is the Mime-type
    kwargs = {
        'filename': context.filename,
        'report_type': context.report_type,
        'compression': compression,
        'content_type': content_type
    }

    for k, v in context.additional_save_attributes():
        kwargs[k] = v

    for k, v in json.loads(request.params.get('metadata', '{}')).items():
        kwargs[k] = v

    oid = request.fs.put(context.file, **kwargs)
    context.file.close()
    logging.info("New report uploaded: http://localhost:6543/%s/%s",
                 context.report_type, oid)
    return {'document_id': str(oid)}
Ejemplo n.º 5
0
 def test_gzip(self):
     filename = self.create_test_file(compression=None)
     result = zip_file(filename, open(filename, 'rb'), 'gzip')
     f = gzip.GzipFile(mode='rb', fileobj=open(result, 'rb'))
     self.assertEqual(f.read(), self.test_text)
Ejemplo n.º 6
0
 def test_zip(self):
     filename = self.create_test_file(compression=None)
     result = zip_file(filename, open(filename, 'rb'), 'zip')
     # hand verified the contents were zipped and matched...
     self.assertTrue(os.path.exists(result))
Ejemplo n.º 7
0
 def test_gzip(self):
     filename = self.create_test_file(compression=None)
     result = zip_file(filename, open(filename, "rb"), "gzip")
     f = gzip.GzipFile(mode="rb", fileobj=open(result, "rb"))
     self.assertEqual(f.read(), self.test_text)
     self.assertEqual(filename + ".gz", result)
Ejemplo n.º 8
0
 def test_zip(self):
     filename = self.create_test_file(compression=None)
     result = zip_file(filename, open(filename, "rb"), "zip")
     # hand verified the contents were zipped and matched...
     self.assertTrue(os.path.exists(result))
     self.assertEqual(filename + ".zip", result)
Ejemplo n.º 9
0
 def test_gzip(self):
     filename = self.create_test_file(compression=None)
     result = zip_file(filename, open(filename, 'rb'), 'gzip')
     f = gzip.GzipFile(mode='rb', fileobj=open(result, 'rb'))
     self.assertEqual(f.read(), self.test_text)
     self.assertEqual(filename + '.gz', result)
Ejemplo n.º 10
0
 def test_zip(self):
     filename = self.create_test_file(compression=None)
     result = zip_file(filename, open(filename, 'rb'), 'zip')
     # hand verified the contents were zipped and matched...
     self.assertTrue(os.path.exists(result))
     self.assertEqual(filename + '.zip', result)