<s> import gzip <EOL> from functools import partial <EOL> from pathlib import Path <EOL> from menpo . compatibility import basestring , str <EOL> from . extensions import landmark_types , image_types , pickle_types , video_types <EOL> from . . utils import _norm_path <EOL> gzip_open = partial ( gzip . open , compresslevel = <NUM_LIT:3> ) <EOL> def export_landmark_file ( landmark_group , fp , extension = None , overwrite = False ) : <EOL> r"""<STR_LIT>""" <EOL> _export ( landmark_group , fp , landmark_types , extension , overwrite ) <EOL> def export_image ( image , fp , extension = None , overwrite = False ) : <EOL> r"""<STR_LIT>""" <EOL> _export ( image , fp , image_types , extension , overwrite ) <EOL> def export_video ( images , filepath , overwrite = False , fps = <NUM_LIT:30> , ** kwargs ) : <EOL> r"""<STR_LIT>""" <EOL> exporter_kwargs = { '<STR_LIT>' : fps } <EOL> exporter_kwargs . update ( kwargs ) <EOL> path_filepath = _validate_filepath ( Path ( filepath ) , overwrite ) <EOL> extension = _parse_and_validate_extension ( path_filepath , None , video_types ) <EOL> export_function = _extension_to_export_function ( extension , video_types ) <EOL> export_function ( images , path_filepath , ** exporter_kwargs ) <EOL> def export_pickle ( obj , fp , overwrite = False , protocol = <NUM_LIT:2> ) : <EOL> r"""<STR_LIT>""" <EOL> if isinstance ( fp , basestring ) : <EOL> fp = Path ( fp ) <EOL> if isinstance ( fp , Path ) : <EOL> path_filepath = _validate_filepath ( fp , overwrite ) <EOL> extension = _parse_and_validate_extension ( path_filepath , None , <EOL> pickle_types ) <EOL> o = gzip_open if extension [ - <NUM_LIT:3> : ] == '<STR_LIT>' else open <EOL> with o ( str ( path_filepath ) , '<STR_LIT:wb>' ) as f : <EOL> _export ( obj , f , pickle_types , extension , True , protocol = protocol ) <EOL> else : <EOL> _export ( obj , fp , pickle_types , '<STR_LIT>' , overwrite , protocol = protocol ) <EOL> def _normalise_extension ( extension ) : <EOL> r"""<STR_LIT>""" <EOL> if extension [ <NUM_LIT:0> ] is not '<STR_LIT:.>' : <EOL> extension = '<STR_LIT:.>' + extension <EOL> return extension . lower ( ) <EOL> def _extension_to_export_function ( extension , extensions_map ) : <EOL> r"""<STR_LIT>""" <EOL> try : <EOL> return extensions_map [ extension ] <EOL> except KeyError : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( extension ) ) <EOL> def _validate_filepath ( fp , overwrite ) : <EOL> r"""<STR_LIT>""" <EOL> path_filepath = _norm_path ( fp ) <EOL> if path_filepath . exists ( ) and not overwrite : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return path_filepath <EOL> def _parse_and_validate_extension ( path_filepath , extension , extensions_map ) : <EOL> r"""<STR_LIT>""" <EOL> suffixes = path_filepath . suffixes <EOL> i = <NUM_LIT:1> <EOL> while i < len ( suffixes ) + <NUM_LIT:1> : <EOL> try : <EOL> suffix = '<STR_LIT>' . join ( suffixes [ - i : ] ) <EOL> _extension_to_export_function ( suffix , extensions_map ) <EOL> known_extension = suffix <EOL> break <EOL> except ValueError : <EOL> pass <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' . format ( <EOL> '<STR_LIT>' . join ( suffixes ) ) ) <EOL> if extension is not None : <EOL> extension = _normalise_extension ( extension ) <EOL> if extension != known_extension : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> extension , known_extension ) ) <EOL> known_extension = extension <EOL> return known_extension <EOL> def _export ( obj , fp , extensions_map , extension , overwrite , protocol = None ) : <EOL> r"""<STR_LIT>""" <EOL> if isinstance ( fp , basestring ) : <EOL> fp = Path ( fp ) <EOL> if isinstance ( fp , Path ) : <EOL> path_filepath = _validate_filepath ( fp , overwrite ) <EOL> extension = _parse_and_validate_extension ( path_filepath , extension , <EOL> extensions_map ) <EOL> export_function = _extension_to_export_function ( extension , <EOL> extensions_map ) <EOL> with path_filepath . open ( '<STR_LIT:wb>' ) as file_handle : <EOL> export_function ( obj , file_handle , protocol = protocol , <EOL> extension = extension ) <EOL> else : <EOL> if extension is None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> extension = _normalise_extension ( extension ) <EOL> try : <EOL> filepath = Path ( fp . name ) <EOL> _validate_filepath ( filepath , overwrite ) <EOL> extension = _parse_and_validate_extension ( filepath , extension , <EOL> extensions_map ) <EOL> except AttributeError : <EOL> pass <EOL> export_function = _extension_to_export_function ( extension , <EOL> extensions_map ) <EOL> export_function ( obj , fp , protocol = protocol , extension = extension ) </s>
