parameters: (path)
return_statement: return res
docstring: Expand shell variables of the forms $var, ${var} and %var%.
  
      Unknown variables are left unchanged.
docstring_summary: Expand shell variables of the forms $var, ${var} and %var%.
docstring_tokens: Expand shell variables of the forms $var $ { var } and %var% .
function: def expandvars(path):
      """Expand shell variables of the forms $var, ${var} and %var%.
  
      Unknown variables are left unchanged."""
      path = os.fspath(path)
      if isinstance(path, bytes):
          if b'$' not in path and b'%' not in path:
              return path
          import string
          varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii')
          quote = b'\''
          percent = b'%'
          brace = b'{'
          rbrace = b'}'
          dollar = b'$'
          environ = getattr(os, 'environb', None)
      else:
          if '$' not in path and '%' not in path:
              return path
          import string
          varchars = string.ascii_letters + string.digits + '_-'
          quote = '\''
          percent = '%'
          brace = '{'
          rbrace = '}'
          dollar = '$'
          environ = os.environ
      res = path[:0]
      index = 0
      pathlen = len(path)
      while index < pathlen:
          c = path[index:index+1]
          if c == quote:   # no expansion within single quotes
              path = path[index + 1:]
              pathlen = len(path)
              try:
                  index = path.index(c)
                  res += c + path[:index + 1]
              except ValueError:
                  res += c + path
                  index = pathlen - 1
          elif c == percent:  # variable or '%'
              if path[index + 1:index + 2] == percent:
                  res += c
                  index += 1
              else:
                  path = path[index+1:]
                  pathlen = len(path)
                  try:
                      index = path.index(percent)
                  except ValueError:
                      res += percent + path
                      index = pathlen - 1
                  else:
                      var = path[:index]
                      try:
                          if environ is None:
                              value = os.fsencode(os.environ[os.fsdecode(var)])
                          else:
                              value = environ[var]
                      except KeyError:
                          value = percent + var + percent
                      res += value
          elif c == dollar:  # variable or '$$'
              if path[index + 1:index + 2] == dollar:
                  res += c
                  index += 1
              elif path[index + 1:index + 2] == brace:
                  path = path[index+2:]
                  pathlen = len(path)
                  try:
                      index = path.index(rbrace)
                  except ValueError:
                      res += dollar + brace + path
                      index = pathlen - 1
                  else:
                      var = path[:index]
                      try:
                          if environ is None:
                              value = os.fsencode(os.environ[os.fsdecode(var)])
                          else:
                              value = environ[var]
                      except KeyError:
                          value = dollar + brace + var + rbrace
                      res += value
              else:
                  var = path[:0]
                  index += 1
                  c = path[index:index + 1]
                  while c and c in varchars:
                      var += c
                      index += 1
                      c = path[index:index + 1]
                  try:
                      if environ is None:
                          value = os.fsencode(os.environ[os.fsdecode(var)])
                      else:
                          value = environ[var]
                  except KeyError:
                      value = dollar + var
                  res += value
                  if c:
                      index -= 1
          else:
              res += c
          index += 1
      return res
function_tokens: def expandvars ( path ) : path = os . fspath ( path ) if isinstance ( path , bytes ) : if b'$' not in path and b'%' not in path : return path import string varchars = bytes ( string . ascii_letters + string . digits + '_-' , 'ascii' ) quote = b'\'' percent = b'%' brace = b'{' rbrace = b'}' dollar = b'$' environ = getattr ( os , 'environb' , None ) else : if '$' not in path and '%' not in path : return path import string varchars = string . ascii_letters + string . digits + '_-' quote = '\'' percent = '%' brace = '{' rbrace = '}' dollar = '$' environ = os . environ res = path [ : 0 ] index = 0 pathlen = len ( path ) while index < pathlen : c = path [ index : index + 1 ] if c == quote : # no expansion within single quotes path = path [ index + 1 : ] pathlen = len ( path ) try : index = path . index ( c ) res += c + path [ : index + 1 ] except ValueError : res += c + path index = pathlen - 1 elif c == percent : # variable or '%' if path [ index + 1 : index + 2 ] == percent : res += c index += 1 else : path = path [ index + 1 : ] pathlen = len ( path ) try : index = path . index ( percent ) except ValueError : res += percent + path index = pathlen - 1 else : var = path [ : index ] try : if environ is None : value = os . fsencode ( os . environ [ os . fsdecode ( var ) ] ) else : value = environ [ var ] except KeyError : value = percent + var + percent res += value elif c == dollar : # variable or '$$' if path [ index + 1 : index + 2 ] == dollar : res += c index += 1 elif path [ index + 1 : index + 2 ] == brace : path = path [ index + 2 : ] pathlen = len ( path ) try : index = path . index ( rbrace ) except ValueError : res += dollar + brace + path index = pathlen - 1 else : var = path [ : index ] try : if environ is None : value = os . fsencode ( os . environ [ os . fsdecode ( var ) ] ) else : value = environ [ var ] except KeyError : value = dollar + brace + var + rbrace res += value else : var = path [ : 0 ] index += 1 c = path [ index : index + 1 ] while c and c in varchars : var += c index += 1 c = path [ index : index + 1 ] try : if environ is None : value = os . fsencode ( os . environ [ os . fsdecode ( var ) ] ) else : value = environ [ var ] except KeyError : value = dollar + var res += value if c : index -= 1 else : res += c index += 1 return res