Allow dependent groups of variables in a list

This commit is contained in:
Ben Jackson 2019-07-26 18:29:05 +01:00
commit 98effd3355

View file

@ -33,6 +33,10 @@ def SetUpLogging( logger ):
logger.addHandler( _log_handler ) logger.addHandler( _log_handler )
_logger = logging.getLogger( __name__ )
SetUpLogging( _logger )
def BufferNumberForFile( file_name ): def BufferNumberForFile( file_name ):
return int( vim.eval( 'bufnr( "{0}", 1 )'.format( file_name ) ) ) return int( vim.eval( 'bufnr( "{0}", 1 )'.format( file_name ) ) )
@ -343,33 +347,45 @@ def ExpandReferencesInDict( obj, mapping, **kwargs ):
obj[ k ] = expand_refs_in_object( obj[ k ] ) obj[ k ] = expand_refs_in_object( obj[ k ] )
def ParseVariables( variables, mapping, **kwargs ): def ParseVariables( variables_list, mapping, **kwargs ):
new_variables = {} new_variables = {}
for n, v in variables.items(): new_mapping = mapping.copy()
if isinstance( v, dict ):
if 'shell' in v:
import subprocess
import shlex
new_v = v.copy() if not isinstance( variables_list, list ):
# Bit of a hack. Allows environment variables to be used. variables_list = [ variables_list ]
ExpandReferencesInDict( new_v, mapping, **kwargs )
env = os.environ.copy() for variables in variables_list:
env.update( new_v.get( 'env' ) or {} ) new_mapping.update( new_variables )
cmd = new_v[ 'shell' ] for n, v in variables.items():
if not isinstance( cmd, list ): if isinstance( v, dict ):
cmd = shlex.split( cmd ) if 'shell' in v:
import subprocess
import shlex
new_variables[ n ] = subprocess.check_output( new_v = v.copy()
cmd, # Bit of a hack. Allows environment variables to be used.
cwd = new_v.get( 'cwd' ) or os.getcwd(), ExpandReferencesInDict( new_v, new_mapping, **kwargs )
env = env ).decode( 'utf-8' ).strip()
env = os.environ.copy()
env.update( new_v.get( 'env' ) or {} )
cmd = new_v[ 'shell' ]
if not isinstance( cmd, list ):
cmd = shlex.split( cmd )
new_variables[ n ] = subprocess.check_output(
cmd,
cwd = new_v.get( 'cwd' ) or os.getcwd(),
env = env ).decode( 'utf-8' ).strip()
_logger.debug( "Set new_variables[ %s ] to '%s' from %s",
n,
new_variables[ n ],
new_v )
else:
raise ValueError(
"Unsupported variable defn {}: Missing 'shell'".format( n ) )
else: else:
raise ValueError( new_variables[ n ] = v
"Unsupported variable defn {}: Missing 'shell'".format( n ) )
else:
new_variables[ n ] = v
return new_variables return new_variables