def ExpandWindowsEnvironmentVariables(data_string, knowledge_base):
 win_environ_regex = re.compile('%([^%]+?)%')
 components = []
 offset = 0
 for match in win_environ_regex.finditer(data_string):
  components.append(data_string[offset:match.start()])
  kb_value = getattr(knowledge_base, ('environ_%s' % match.group(1).lower()), None)
  if (isinstance(kb_value, basestring) and kb_value):
   components.append(kb_value)
  else:
   components.append(('%%%s%%' % match.group(1)))
  offset = match.end()
 components.append(data_string[offset:])
 return ''.join(components)

def ExpandWindowsEnvironmentVariables ( data_string , knowledge_base ) : win_environ_regex = re . compile ( '%([^%]+?)%' ) components = [ ] offset = 0 for match in win_environ_regex . finditer ( data_string ) : components . append ( data_string [ offset : match . start ( ) ] ) kb_value = getattr ( knowledge_base , ( 'environ_%s' % match . group ( 1 ) . lower ( ) ) , None ) if ( isinstance ( kb_value , basestring ) and kb_value ) : components . append ( kb_value ) else : components . append ( ( '%%%s%%' % match . group ( 1 ) ) ) offset = match . end ( ) components . append ( data_string [ offset : ] ) return '' . join ( components )
take a string and expand any windows environment variables .