def ExpandWindowsUserEnvironmentVariables(data_string, knowledge_base, sid=None, username=None):
 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_user = knowledge_base.GetUser(sid=sid, username=username)
  kb_value = None
  if kb_user:
   kb_value = getattr(kb_user, 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 ExpandWindowsUserEnvironmentVariables ( data_string , knowledge_base , sid = None , username = None ) : 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_user = knowledge_base . GetUser ( sid = sid , username = username ) kb_value = None if kb_user : kb_value = getattr ( kb_user , 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 windows user environment variables based .