Package gavo :: Package formats :: Module common
[frames] | no frames]

Module common

source code

Common code for generation of various data formats.

The main function here is formatData. It receives a string format id, a data instance and a destination file. It dispatches this to formatters previously registred using registerDataWriter.

The data writers must take a data instance and a file instance; their effect must be that a serialized representation of data, or, if the format does not support this, the data's primary table is written to the file instance.

Classes
  CannotSerializeIn
  FORMATS_REGISTRY
a registry for data formats that can be produced by DaCHS.
Functions
 
getMIMEKey(contentType)
makes a DaCHS mime key from a content-type string.
source code
 
formatData(formatName, table, outputFile, acquireSamples=True, **moreFormatterArgs)
writes a table to outputFile in the format given by key.
source code
 
getFormatted(formatName, table, acquireSamples=False)
returns a string containing a representation of table in the format given by formatName.
source code
 
guessMediaType(fName)
returns a media type plausible for a file named fName.
source code
 
getExtensionFor(mediaType)
returns a suggested extension for files of mediaType.
source code
Variables
  PRESERVED_MIMES = set(['application/x-votable+xml', 'text/plai...
  EXTENSION_FALLBACKS = {'.fits': 'application/fits', '.fz': 'im...
  __package__ = 'gavo.formats'
Function Details

getMIMEKey(contentType)

source code 

makes a DaCHS mime key from a content-type string.

This is used for retrieving matching mime types and is a triple of major and minor mime type and a set of parameter pairs.

contentType is a string-serialized mime type.

formatData(formatName, table, outputFile, acquireSamples=True, **moreFormatterArgs)

source code 

writes a table to outputFile in the format given by key.

Table may be a table or a ``Data`` instance. ``formatName`` is a format shortcut (``formats.iterFormats()`` gives keys available) or a media type. If you pass None, the default VOTable format will be selected.

This raises a ``CannotSerializeIn`` exception if ``formatName`` is not recognized. Note that you have to import the serialising modules from the format package to make the formats available (fitstable, csvtable, geojson, jsontable, texttable, votable; api itself already imports the more popular of these).

If a client knows a certain formatter understands additional arguments, it can hand them in as keywords arguments. This will raise an error if another formatter that doesn't understand the argument is being used.

getFormatted(formatName, table, acquireSamples=False)

source code 

returns a string containing a representation of table in the format given by formatName.

This is just wrapping the `function formatData`_; se there for formatName. This function will use large amounts of memory for large data.

guessMediaType(fName)

source code 

returns a media type plausible for a file named fName.

This first uses the extension map inferred by our formats registry, has some built-in safety catches in case the formatters haven't been imported, and then falls back to built-in python mimetypes.guess_type If nothing matches, it returns application/octet-stream.

Extensions are used case-insensitively. We don't do any encoding inference (yet). We may, though, so by all means shout if you're using this in DaCHS-external code.

getExtensionFor(mediaType)

source code 

returns a suggested extension for files of mediaType.

mediaType can be an RFC 2045 media type, or one of DaCHS' internal format codes.

As a fallback, .dat will be returned.


Variables Details

PRESERVED_MIMES

Value:
set(['application/x-votable+xml', 'text/plain', 'text/xml'])

EXTENSION_FALLBACKS

Value:
{'.fits': 'application/fits',
 '.fz': 'image/fits',
 '.vot': 'application/x-votable+xml'}