<s> import collections <EOL> import logging <EOL> import json <EOL> import os <EOL> import threading <EOL> import yaml <EOL> from collections import OrderedDict <EOL> from ztpserver . constants import CONTENT_TYPE_OTHER <EOL> from ztpserver . constants import CONTENT_TYPE_JSON <EOL> from ztpserver . constants import CONTENT_TYPE_YAML <EOL> READ_WRITE_LOCK = { } <EOL> log = logging . getLogger ( __name__ ) <EOL> class SerializerError ( Exception ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> class BaseSerializer ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , node_id ) : <EOL> self . node_id = node_id <EOL> def serialize ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError <EOL> def deserialize ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError <EOL> class TextSerializer ( BaseSerializer ) : <EOL> def deserialize ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> return str ( data ) <EOL> def serialize ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> return str ( data ) <EOL> def represent_odict ( dump_odict , tag , mapping , flow_style = None ) : <EOL> '''<STR_LIT>''' <EOL> value = [ ] <EOL> node = yaml . MappingNode ( tag , value , flow_style = flow_style ) <EOL> if dump_odict . alias_key is not None : <EOL> dump_odict . represented_objects [ dump_odict . alias_key ] = node <EOL> best_style = True <EOL> if hasattr ( mapping , '<STR_LIT>' ) : <EOL> mapping = mapping . items ( ) <EOL> for item_key , item_value in mapping : <EOL> node_key = dump_odict . represent_data ( item_key ) <EOL> node_value = dump_odict . represent_data ( item_value ) <EOL> if not ( isinstance ( node_key , yaml . ScalarNode ) and <EOL> not node_key . style ) : <EOL> best_style = False <EOL> if not ( isinstance ( node_value , yaml . ScalarNode ) and <EOL> not node_value . style ) : <EOL> best_style = False <EOL> value . append ( ( node_key , node_value ) ) <EOL> if flow_style is None : <EOL> if dump_odict . default_flow_style is not None : <EOL> node . flow_style = dump_odict . default_flow_style <EOL> else : <EOL> node . flow_style = best_style <EOL> return node <EOL> yaml . SafeDumper . add_representer ( <EOL> OrderedDict , <EOL> lambda dumper , <EOL> value : represent_odict ( dumper , <EOL> u'<STR_LIT>' , <EOL> value ) ) <EOL> class YAMLSerializer ( BaseSerializer ) : <EOL> def deserialize ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return yaml . safe_load ( data ) <EOL> except yaml . YAMLError as err : <EOL> msg = '''<STR_LIT>''' % ( self . node_id , data , err ) <EOL> raise SerializerError ( msg ) <EOL> def serialize ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return yaml . safe_dump ( data , default_flow_style = False ) <EOL> except yaml . YAMLError as err : <EOL> msg = '''<STR_LIT>''' % ( self . node_id , data , err ) <EOL> raise SerializerError ( msg ) <EOL> class JSONSerializer ( BaseSerializer ) : <EOL> def deserialize ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return json . loads ( data ) <EOL> except Exception as err : <EOL> msg = '''<STR_LIT>''' % ( self . node_id , data , err ) <EOL> raise SerializerError ( msg ) <EOL> def serialize ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return json . dumps ( data ) <EOL> except Exception as err : <EOL> msg = '''<STR_LIT>''' % ( self . node_id , data , err ) <EOL> raise SerializerError ( msg ) <EOL> class Serializer ( object ) : <EOL> def __init__ ( self , node_id ) : <EOL> self . node_id = node_id <EOL> self . _handlers = { <EOL> CONTENT_TYPE_OTHER : TextSerializer ( self . node_id ) , <EOL> CONTENT_TYPE_JSON : JSONSerializer ( self . node_id ) , <EOL> CONTENT_TYPE_YAML : YAMLSerializer ( self . node_id ) <EOL> } <EOL> @ property <EOL> def handlers ( self ) : <EOL> return self . _handlers <EOL> def add_handler ( self , content_type , instance ) : <EOL> if content_type in self . _handlers : <EOL> log . warning ( '<STR_LIT>' , <EOL> ( self . node_id , content_type ) ) <EOL> self . _handlers [ content_type ] = instance <EOL> def serialize ( self , data , content_type ) : <EOL> '''<STR_LIT>''' <EOL> handler = self . handlers . get ( content_type , <EOL> TextSerializer ( self . node_id ) ) <EOL> return handler . serialize ( data ) <EOL> def deserialize ( self , data , content_type = None ) : <EOL> '''<STR_LIT>''' <EOL> handler = self . handlers . get ( content_type , <EOL> TextSerializer ( self . node_id ) ) <EOL> data = self . _convert_from_unicode ( handler . deserialize ( data ) ) <EOL> return data <EOL> @ staticmethod <EOL> def _convert_from_unicode ( data ) : <EOL> if isinstance ( data , basestring ) : <EOL> return str ( data ) <EOL> elif isinstance ( data , collections . Mapping ) : <EOL> return dict ( [ Serializer . _convert_from_unicode ( x ) <EOL> for x in data . items ( ) ] ) <EOL> elif isinstance ( data , collections . Iterable ) : <EOL> return type ( data ) ( [ Serializer . _convert_from_unicode ( x ) <EOL> for x in data ] ) <EOL> else : <EOL> return data <EOL> def loads ( data , content_type , node_id ) : <EOL> serializer = Serializer ( node_id ) <EOL> return serializer . deserialize ( data , content_type ) <EOL> def load ( file_path , content_type , node_id = '<STR_LIT>' , lock = False ) : <EOL> log . debug ( '<STR_LIT>' % ( node_id , file_path ) ) <EOL> if lock and file_path not in READ_WRITE_LOCK : <EOL> READ_WRITE_LOCK [ file_path ] = threading . Lock ( ) <EOL> try : <EOL> if lock : <EOL> with READ_WRITE_LOCK [ file_path ] : <EOL> with open ( file_path ) as fhandler : <EOL> data = fhandler . read ( ) <EOL> else : <EOL> with open ( file_path ) as fhandler : <EOL> data = fhandler . read ( ) <EOL> result = loads ( data , content_type , node_id ) <EOL> except ( OSError , IOError ) as err : <EOL> log . error ( '<STR_LIT>' % <EOL> ( node_id , file_path , err ) ) <EOL> raise SerializerError ( '<STR_LIT>' % <EOL> ( node_id , file_path , err ) ) <EOL> return result <EOL> def dumps ( data , content_type , node_id ) : <EOL> serializer = Serializer ( node_id ) <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> data = data . serialize ( ) <EOL> return serializer . serialize ( data , content_type ) <EOL> def dump ( data , file_path , content_type , node_id = '<STR_LIT>' , lock = False ) : <EOL> log . debug ( '<STR_LIT>' % ( node_id , file_path ) ) <EOL> if lock and file_path not in READ_WRITE_LOCK : <EOL> READ_WRITE_LOCK [ file_path ] = threading . Lock ( ) <EOL> try : <EOL> if lock : <EOL> with READ_WRITE_LOCK [ file_path ] : <EOL> with os . fdopen ( os . open ( file_path , <EOL> os . O_WRONLY | os . O_CREAT | os . O_TRUNC , <EOL> <NUM_LIT:0> <NUM_LIT> ) , <EOL> '<STR_LIT:w>' ) as fhandler : <EOL> fhandler . write ( dumps ( data , content_type , node_id ) ) <EOL> else : <EOL> with os . fdopen ( os . open ( file_path , <EOL> os . O_WRONLY | os . O_CREAT | os . O_TRUNC , <EOL> <NUM_LIT:0> <NUM_LIT> ) , <EOL> '<STR_LIT:w>' ) as fhandler : <EOL> fhandler . write ( dumps ( data , content_type , node_id ) ) <EOL> except ( OSError , IOError ) as err : <EOL> log . error ( '<STR_LIT>' % <EOL> ( node_id , file_path , err ) ) <EOL> raise SerializerError ( '<STR_LIT>' % <EOL> ( node_id , file_path , err ) ) </s>
