Created
January 27, 2015 21:25
-
-
Save mbakeranalecta/4e9a9c6cfc681ea6661a to your computer and use it in GitHub Desktop.
Resolving variables in a text string in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.defines = {'HOME': self.spfe_dirs['home'], | |
'SPFEOT_HOME': self.spfe_dirs['spfe_ot_home'], | |
'CONTENT_SET_BUILD_DIR': self.content_set_build_dir, | |
'CONTENT_SET_OUTPUT_DIR': self.content_set_output_dir, | |
'CONTENT_SET_BUILD_ROOT_DIR': self.content_set_build_root_dir | |
} | |
def _resolve_defines(self, string): | |
defines_pattern = re.compile('\$\{([^}]*)\}') | |
resolved = re.sub(defines_pattern, self._replace_defines, string) | |
return resolved | |
def _replace_defines(self, match): | |
try: | |
return self.defines[match.group(1)] | |
except KeyError: | |
Exception("Invalid define ${" + match.group(1) + "}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment