<s> import google3 <EOL> from grizzled . config import ( Configuration , NoVariableError ) <EOL> from cStringIO import StringIO <EOL> import os <EOL> import tempfile <EOL> import atexit <EOL> import sys <EOL> CONFIG1 = """<STR_LIT>""" <EOL> CONFIG2 = """<STR_LIT>""" <EOL> CONFIG_ORDER_TEST = """<STR_LIT>""" <EOL> class TestParser ( object ) : <EOL> def testSubstitute1 ( self ) : <EOL> config = Configuration ( ) <EOL> config . readfp ( StringIO ( CONFIG1 ) ) <EOL> assert config . has_section ( '<STR_LIT>' ) <EOL> assert not config . has_section ( '<STR_LIT>' ) <EOL> assert not config . has_section ( '<STR_LIT:foo>' ) <EOL> assert not config . has_section ( '<STR_LIT:bar>' ) <EOL> assert not config . has_section ( '<STR_LIT>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:foo>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:name>' ) <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:name>' ) == os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT>' ) == os . getcwd ( ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:bar>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:foo>' ) == '<STR_LIT:bar>' <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:bar>' ) == '<STR_LIT:bar>' <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT:bar>' <EOL> def testSubstitute2 ( self ) : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> config = Configuration ( ) <EOL> config . readfp ( StringIO ( CONFIG2 ) ) <EOL> assert config . has_section ( '<STR_LIT>' ) <EOL> assert config . has_section ( '<STR_LIT>' ) <EOL> assert not config . has_section ( '<STR_LIT:foo>' ) <EOL> assert not config . has_section ( '<STR_LIT:bar>' ) <EOL> assert not config . has_section ( '<STR_LIT>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:foo>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:bar>' ) <EOL> assert not config . has_option ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:foo>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:bar>' ) <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:foo>' ) == '<STR_LIT:bar>' <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:bar>' ) == '<STR_LIT:bar>' <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:foo>' ) == '<STR_LIT:bar>' <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:bar>' ) == os . environ [ '<STR_LIT>' ] <EOL> def testInclude ( self ) : <EOL> fd , tempPath = tempfile . mkstemp ( suffix = '<STR_LIT>' ) <EOL> def unlinkTemp ( path ) : <EOL> try : <EOL> os . unlink ( path ) <EOL> except : <EOL> pass <EOL> atexit . register ( unlinkTemp , tempPath ) <EOL> fp = os . fdopen ( fd , "<STR_LIT:w>" ) <EOL> print >> fp , '<STR_LIT>' <EOL> fp . close ( ) <EOL> s = '<STR_LIT>' % ( CONFIG2 , tempPath ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> config = Configuration ( ) <EOL> config . readfp ( StringIO ( s ) ) <EOL> unlinkTemp ( tempPath ) <EOL> assert config . has_section ( '<STR_LIT>' ) <EOL> assert config . has_section ( '<STR_LIT>' ) <EOL> assert config . has_section ( '<STR_LIT>' ) <EOL> assert not config . has_section ( '<STR_LIT:foo>' ) <EOL> assert not config . has_section ( '<STR_LIT:bar>' ) <EOL> assert not config . has_section ( '<STR_LIT>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:foo>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:bar>' ) <EOL> assert not config . has_option ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:foo>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT:bar>' ) <EOL> assert config . has_option ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:foo>' ) == '<STR_LIT:bar>' <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:bar>' ) == '<STR_LIT:bar>' <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:foo>' ) == '<STR_LIT:bar>' <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT:bar>' ) == os . environ [ '<STR_LIT>' ] <EOL> assert config . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' <EOL> def testOrdering ( self ) : <EOL> config = Configuration ( use_ordered_sections = True ) <EOL> config . readfp ( StringIO ( CONFIG_ORDER_TEST ) ) <EOL> assert config . has_section ( '<STR_LIT:a>' ) <EOL> assert config . has_section ( '<STR_LIT:y>' ) <EOL> assert config . has_section ( '<STR_LIT:z>' ) <EOL> sections = config . sections <EOL> assert len ( sections ) == <NUM_LIT:4> <EOL> assert sections [ <NUM_LIT:0> ] == '<STR_LIT:z>' <EOL> assert sections [ <NUM_LIT:1> ] == '<STR_LIT:y>' <EOL> assert sections [ <NUM_LIT:2> ] == '<STR_LIT:a>' <EOL> assert sections [ <NUM_LIT:3> ] == '<STR_LIT>' <EOL> def testBadSubstitution ( self ) : <EOL> cfgString = """<STR_LIT>""" <EOL> import sys <EOL> from grizzled . io import AutoFlush <EOL> sys . stdout = AutoFlush ( sys . stdout ) <EOL> config = Configuration ( strict_substitution = False ) <EOL> config . readfp ( StringIO ( cfgString ) ) <EOL> config . write ( sys . stdout ) <EOL> try : <EOL> var1 = config . get ( '<STR_LIT:foo>' , '<STR_LIT>' , optional = True ) <EOL> assert var1 == None , '<STR_LIT>' <EOL> except : <EOL> raise <EOL> config = Configuration ( strict_substitution = True ) <EOL> try : <EOL> config . readfp ( StringIO ( cfgString ) ) <EOL> assert False , '<STR_LIT>' <EOL> except NoVariableError : <EOL> pass <EOL> except : <EOL> assert False , '<STR_LIT>' </s>
