<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> import sys <EOL> import os <EOL> import errno <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from . sixing import * <EOL> from . odicting import odict <EOL> from . consoling import getConsole <EOL> console = getConsole ( ) <EOL> def ocfn ( filename , openMode = '<STR_LIT>' , binary = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> newfd = os . open ( filename , os . O_EXCL | os . O_CREAT | os . O_RDWR , <NUM_LIT> ) <EOL> if not binary : <EOL> newfile = os . fdopen ( newfd , "<STR_LIT>" ) <EOL> else : <EOL> newfile = os . fdopen ( newfd , "<STR_LIT>" ) <EOL> except OSError as ex : <EOL> if ex . errno == errno . EEXIST : <EOL> newfile = open ( filename , openMode ) <EOL> else : <EOL> raise <EOL> return newfile <EOL> def dump ( data , filepath ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT:U+0020>' in filepath : <EOL> raise IOError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( filepath ) ) <EOL> root , ext = os . path . splitext ( filepath ) <EOL> if ext == '<STR_LIT>' : <EOL> with ocfn ( filepath , "<STR_LIT>" ) as f : <EOL> json . dump ( data , f , indent = <NUM_LIT:2> ) <EOL> f . flush ( ) <EOL> os . fsync ( f . fileno ( ) ) <EOL> elif ext == '<STR_LIT>' : <EOL> if not msgpack : <EOL> raise IOError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( filepath ) ) <EOL> with ocfn ( filepath , "<STR_LIT>" , binary = True ) as f : <EOL> msgpack . dump ( data , f ) <EOL> f . flush ( ) <EOL> os . fsync ( f . fileno ( ) ) <EOL> else : <EOL> raise IOError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( filepath ) ) <EOL> def load ( filepath ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> root , ext = os . path . splitext ( filepath ) <EOL> if ext == '<STR_LIT>' : <EOL> with ocfn ( filepath , "<STR_LIT:r>" ) as f : <EOL> it = json . load ( f , object_pairs_hook = odict ) <EOL> elif ext == '<STR_LIT>' : <EOL> if not msgpack : <EOL> raise IOError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( filepath ) ) <EOL> with ocfn ( filepath , "<STR_LIT:rb>" , binary = True ) as f : <EOL> it = msgpack . load ( f , object_pairs_hook = odict ) <EOL> else : <EOL> it = None <EOL> except EOFError : <EOL> return None <EOL> except ValueError : <EOL> return None <EOL> return it <EOL> def loadPickle ( file = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if not file : <EOL> raise ParameterError ( "<STR_LIT>" . format ( file ) ) <EOL> with open ( file , "<STR_LIT>" ) as f : <EOL> p = pickle . Unpickler ( f ) <EOL> it = p . load ( ) <EOL> return it <EOL> def dumpPickle ( it = None , file = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if not it : <EOL> raise ParameterError ( "<STR_LIT>" . format ( str ( it ) ) ) <EOL> if not file : <EOL> raise ParameterError ( "<STR_LIT>" . format ( file ) ) <EOL> with open ( file , "<STR_LIT>" ) as f : <EOL> p = pickle . Pickler ( f ) <EOL> p . dump ( it ) <EOL> def loadJson ( filename = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if not filename : <EOL> raise ParameterError ( "<STR_LIT>" ) <EOL> with ocfn ( filename ) as f : <EOL> try : <EOL> it = json . load ( f , object_pairs_hook = odict ( ) ) <EOL> except EOFError : <EOL> return None <EOL> except ValueError : <EOL> return None <EOL> return it <EOL> def dumpJson ( it = None , filename = "<STR_LIT>" , indent = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> if not it : <EOL> raise ValueError ( "<STR_LIT>" . format ( it ) ) <EOL> if not filename : <EOL> raise ValueError ( "<STR_LIT>" . format ( filename ) ) <EOL> with ocfn ( filename , "<STR_LIT>" ) as f : <EOL> json . dump ( it , f , indent = <NUM_LIT:2> ) <EOL> f . flush ( ) <EOL> os . fsync ( f . fileno ( ) ) </s>
