<s> import ConfigParser <EOL> import contextlib <EOL> from ceph_deploy import exc <EOL> class _TrimIndentFile ( object ) : <EOL> def __init__ ( self , fp ) : <EOL> self . fp = fp <EOL> def readline ( self ) : <EOL> line = self . fp . readline ( ) <EOL> return line . lstrip ( '<STR_LIT>' ) <EOL> class CephConf ( ConfigParser . RawConfigParser ) : <EOL> def optionxform ( self , s ) : <EOL> s = s . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> s = '<STR_LIT:_>' . join ( s . split ( ) ) <EOL> return s <EOL> def safe_get ( self , section , key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return ConfigParser . RawConfigParser . get ( self , section , key ) <EOL> except ( ConfigParser . NoSectionError , <EOL> ConfigParser . NoOptionError ) : <EOL> return None <EOL> def parse ( fp ) : <EOL> cfg = CephConf ( ) <EOL> ifp = _TrimIndentFile ( fp ) <EOL> cfg . readfp ( ifp ) <EOL> return cfg <EOL> def load ( args ) : <EOL> """<STR_LIT>""" <EOL> path = args . ceph_conf or '<STR_LIT>' . format ( cluster = args . cluster ) <EOL> try : <EOL> f = file ( path ) <EOL> except IOError as e : <EOL> raise exc . ConfigError ( <EOL> "<STR_LIT>" % e <EOL> ) <EOL> else : <EOL> with contextlib . closing ( f ) : <EOL> return parse ( f ) <EOL> def load_raw ( args ) : <EOL> """<STR_LIT>""" <EOL> path = args . ceph_conf or '<STR_LIT>' . format ( cluster = args . cluster ) <EOL> try : <EOL> with open ( path ) as ceph_conf : <EOL> return ceph_conf . read ( ) <EOL> except ( IOError , OSError ) as e : <EOL> raise exc . ConfigError ( <EOL> "<STR_LIT>" % e <EOL> ) <EOL> def write_conf ( cluster , conf , overwrite ) : <EOL> """<STR_LIT>""" <EOL> import os <EOL> path = '<STR_LIT>' . format ( cluster = cluster ) <EOL> tmp = '<STR_LIT>' . format ( path = path , pid = os . getpid ( ) ) <EOL> if os . path . exists ( path ) : <EOL> with file ( path , '<STR_LIT:rb>' ) as f : <EOL> old = f . read ( ) <EOL> if old != conf and not overwrite : <EOL> raise RuntimeError ( '<STR_LIT>' % path ) <EOL> with file ( tmp , '<STR_LIT:w>' ) as f : <EOL> f . write ( conf ) <EOL> f . flush ( ) <EOL> os . fsync ( f ) <EOL> os . rename ( tmp , path ) </s>
