sceptre/template.py
def _get_cfn(self):
        """
        Reads in a CloudFormation template directly from a file or as a string
        from an external Python script (such as Troposphere).

        External Python scripts have arbitrary dictionary of data
        (sceptre_user_data) passed to them.

        :returns: A CloudFormation template
        :rtype: str
        :raises: sceptre.stack.UnsupportedTemplateFileTypeException
        :raises: IOError
        """
        # Get relative path as list between current working directory and where
        # the template is
        # NB: this is a horrible hack...
        relpath = os.path.relpath(self.path, os.getcwd()).split(os.path.sep)
        relpaths_to_add = [
            os.path.sep.join(relpath[:i+1])
            for i in range(len(relpath[:-1]))
        ]

        # Add any directory between the current working directory and where
        # the template is to the python path
        for directory in relpaths_to_add:
            sys.path.append(os.path.join(os.getcwd(), directory))

        self.file_extension = os.path.splitext(self.path)[1]

        if self.file_extension in (".json", ".yaml"):
            self.logger.debug("%s - Opening file %s", self.name, self.path)
            with open(self.path) as template_file:
                cfn = template_file.read()
        elif self.file_extension == ".py":
            self.logger.debug(
                "%s - Getting CloudFormation from %s", self.name, self.path
            )

            # If imp.load_source cannot find the file at self.path, it throws
            # an IOError, but doesn't specify which file couldn't be found.
            # As multiple templates are loaded when an environment is built,
            # more detail is needed. The following commmand
            # "looks before we leap", and specifies which file can't be found.
            if not os.path.isfile(self.path):
                raise IOError("No such file or directory: '%s'", self.path)

            module = imp.load_source(self.name, self.path)

            if hasattr(module, "sceptre_handler"):
                    cfn = module.sceptre_handler(self.sceptre_user_data)
            else:
                raise TemplateSceptreHandlerError(
                    "The template does not have the required "
                    "'sceptre_handler(sceptre_user_data)' function."
                )
        else:
            raise UnsupportedTemplateFileTypeError(
                "Template has file extension %s. Only .py, .yaml, and .json "
                "are supported.",
                os.path.splitext(self.path)[1]
            )

        for directory in relpaths_to_add:
            sys.path.remove(os.path.join(os.getcwd(), directory))

        return cfn

        Reads in a CloudFormation template directly from a file or as a string
        from an external Python script (such as Troposphere).

        External Python scripts have arbitrary dictionary of data
        (sceptre_user_data) passed to them.

        :returns: A CloudFormation template
        :rtype: str
        :raises: sceptre.stack.UnsupportedTemplateFileTypeException
        :raises: IOError
        
Reads in a CloudFormation template directly from a file or as a string
from an external Python script (such as Troposphere).

External Python scripts have arbitrary dictionary of data
(sceptre_user_data) passed to them.
Reads in a CloudFormation template directly from a file or as a string from an external Python script ( such as Troposphere ) . External Python scripts have arbitrary dictionary of data ( sceptre_user_data ) passed to them .
def _get_cfn(self):
        relpath = os.path.relpath(self.path, os.getcwd()).split(os.path.sep)
        relpaths_to_add = [
            os.path.sep.join(relpath[:i+1])
            for i in range(len(relpath[:-1]))
        ]
         to the python path
        for directory in relpaths_to_add:
            sys.path.append(os.path.join(os.getcwd(), directory))
        self.file_extension = os.path.splitext(self.path)[1]
        if self.file_extension in (".json", ".yaml"):
            self.logger.debug("%s - Opening file %s", self.name, self.path)
            with open(self.path) as template_file:
                cfn = template_file.read()
        elif self.file_extension == ".py":
            self.logger.debug(
                "%s - Getting CloudFormation from %s", self.name, self.path
            )
            if not os.path.isfile(self.path):
                raise IOError("No such file or directory: '%s'", self.path)
            module = imp.load_source(self.name, self.path)
            if hasattr(module, "sceptre_handler"):
                    cfn = module.sceptre_handler(self.sceptre_user_data)
            else:
                raise TemplateSceptreHandlerError(
                    "The template does not have the required "
                    "'sceptre_handler(sceptre_user_data)' function."
                )
        else:
            raise UnsupportedTemplateFileTypeError(
                "Template has file extension %s. Only .py, .yaml, and .json "
                "are supported.",
                os.path.splitext(self.path)[1]
            )
        for directory in relpaths_to_add:
            sys.path.remove(os.path.join(os.getcwd(), directory))
        return cfn
def _get_cfn ( self ) : relpath = os . path . relpath ( self . path , os . getcwd ( ) ) . split ( os . path . sep ) relpaths_to_add = [ os . path . sep . join ( relpath [ : i + 1 ] ) for i in range ( len ( relpath [ : - 1 ] ) ) ] for directory in relpaths_to_add : sys . path . append ( os . path . join ( os . getcwd ( ) , directory ) ) self . file_extension = os . path . splitext ( self . path ) [ 1 ] if self . file_extension in ( ".json" , ".yaml" ) : self . logger . debug ( "%s - Opening file %s" , self . name , self . path ) with open ( self . path ) as template_file : cfn = template_file . read ( ) elif self . file_extension == ".py" : self . logger . debug ( "%s - Getting CloudFormation from %s" , self . name , self . path ) if not os . path . isfile ( self . path ) : raise IOError ( "No such file or directory: '%s'" , self . path ) module = imp . load_source ( self . name , self . path ) if hasattr ( module , "sceptre_handler" ) : cfn = module . sceptre_handler ( self . sceptre_user_data ) else : raise TemplateSceptreHandlerError ( "The template does not have the required " "'sceptre_handler(sceptre_user_data)' function." ) else : raise UnsupportedTemplateFileTypeError ( "Template has file extension %s. Only .py, .yaml, and .json " "are supported." , os . path . splitext ( self . path ) [ 1 ] ) for directory in relpaths_to_add : sys . path . remove ( os . path . join ( os . getcwd ( ) , directory ) ) return cfn
Reads in a CloudFormation template directly from a file or as a string
from an external Python script (such as Troposphere).
Reads in a CloudFormation template directly from a file or as a string from an external Python script ( such as Troposphere ) .
"""
        Reads in a CloudFormation template directly from a file or as a string
        from an external Python script (such as Troposphere).

        External Python scripts have arbitrary dictionary of data
        (sceptre_user_data) passed to them.

        :returns: A CloudFormation template
        :rtype: str
        :raises: sceptre.stack.UnsupportedTemplateFileTypeException
        :raises: IOError
        """ # Get relative path as list between current working directory and where # the template is # NB: this is a horrible hack... # Add any directory between the current working directory and where # the template is to the python path # If imp.load_source cannot find the file at self.path, it throws # an IOError, but doesn't specify which file couldn't be found. # As multiple templates are loaded when an environment is built, # more detail is needed. The following commmand # "looks before we leap", and specifies which file can't be found.
A CloudFormation template
A CloudFormation template
str
None
None
self