gramfuzz/gramfuzz/__init__.py
def load_grammar(self, path):
        """Load a grammar file (python file containing grammar definitions) by
        file path. When loaded, the global variable ``GRAMFUZZER`` will be set
        within the module. This is not always needed, but can be useful.

        :param str path: The path to the grammar file
        """
        if not os.path.exists(path):
            raise Exception("path does not exist: {!r}".format(path))
        
        # this will let grammars reference eachother with relative
        # imports.
        #
        # E.g.:
        #     grams/
        #         gram1.py
        #         gram2.py
        #
        # gram1.py can do "import gram2" to require rules in gram2.py to
        # be loaded
        grammar_path = os.path.dirname(path)
        if grammar_path not in sys.path:
            sys.path.append(grammar_path)

        with open(path, "r") as f:
            data = f.read()
        code = compile(data, path, "exec")

        locals_ = {"GRAMFUZZER": self, "__file__": path}
        exec(code) in locals_

        if "TOP_CAT" in locals_:
            cat_group = os.path.basename(path).replace(".py", "")
            self.set_cat_group_top_level_cat(cat_group, locals_["TOP_CAT"])
def load_grammar(self, path):
        """Load a grammar file (python file containing grammar definitions) by
        file path. When loaded, the global variable ``GRAMFUZZER`` will be set
        within the module. This is not always needed, but can be useful.

        :param str path: The path to the grammar file
        """
        if not os.path.exists(path):
            raise Exception("path does not exist: {!r}".format(path))
        
        # this will let grammars reference eachother with relative
        # imports.
        #
        # E.g.:
        #     grams/
        #         gram1.py
        #         gram2.py
        #
        # gram1.py can do "import gram2" to require rules in gram2.py to
        # be loaded
        grammar_path = os.path.dirname(path)
        if grammar_path not in sys.path:
            sys.path.append(grammar_path)

        with open(path, "r") as f:
            data = f.read()
        code = compile(data, path, "exec")

        locals_ = {"GRAMFUZZER": self, "__file__": path}
        exec(code) in locals_

        if "TOP_CAT" in locals_:
            cat_group = os.path.basename(path).replace(".py", "")
            self.set_cat_group_top_level_cat(cat_group, locals_["TOP_CAT"])
Load a grammar file (python file containing grammar definitions) by
        file path. When loaded, the global variable ``GRAMFUZZER`` will be set
        within the module. This is not always needed, but can be useful.

        :param str path: The path to the grammar file