Exemplo n.º 1
0
def component9():
    engine = TemplateEngine(file_path="templates/c9_template.txt")

    for i in range(5):
        output = engine.generate(template_name="STRACHEY")
        ending = engine.generate(template_name="ENDING")

        output_list_for_cap = output.split(" ")
        output_list_for_cap[0] = output_list_for_cap[0].capitalize()
        output_list_for_cap[1] = output_list_for_cap[1].capitalize()
        output_beginning_capped = ""
        for k in range(len(output_list_for_cap)):
            output_beginning_capped += f"{output_list_for_cap[k]} "

        output_list = output_beginning_capped.split('\\n')
        for j in range(len(output_list)):
            print(f"{output_list[j]}")

        ending_list = ending.split('\\n')
        for j in range(len(ending_list)):
            print(
                f"                                                     {ending_list[j]}"
            )

        print("\n")
Exemplo n.º 2
0
def component6():
    engine = TemplateEngine(file_path="templates/c6_template.txt")

    for i in range(5):
        output = engine.generate(template_name="DADAIST")
        output_list = output.split('\\n')
        for j in range(len(output_list)):
            print(f"{output_list[j]}")
        print("\n")
Exemplo n.º 3
0
def supplementary_challenge2():
    engine = TemplateEngine(file_path="templates/c6_template.txt")

    for i in range(5):
        print(f"<{i}>\n")
        output = engine.generate(template_name="DADAIST_ADVANCED")
        output_list = output.split('\\n')
        for j in range(len(output_list)):
            print(f"{output_list[j]}")
        print(" ")
Exemplo n.º 4
0
def component7c():
    engine = TemplateEngine(file_path="templates/c7_template.txt")

    for i in range(10):
        output = engine.generate(template_name="PALINDROME")
        output_list = output.split()
        output2 = output_list[::-1]
        output3 = ""
        for j in range(len(output2)):
            output3 += f"{output2[j]} "

        print(f"{i} {output}and {output3} \n")
Exemplo n.º 5
0
def demo():
    """James's demo functionality from our class session on 9/21."""
    # Prepare a template engine -- this is what fills in templates, 
    # given all template and slot definitions in 
    # "templates/basic_templates.txt".
    engine = TemplateEngine(file_path="templates/basic_templates.txt")
    # Write out ten lines that are generated from the template called
    # "SENTENCE"
    multi_line_output = ""
    for i in range(4):
        output = engine.generate(template_name="SENTENCE")
        multi_line_output += "\n" + " " * i + output
    print(f"Demo by James{multi_line_output}")
Exemplo n.º 6
0
def component2e():
    engine = TemplateEngine(file_path="templates/c2_template.txt")
    print("\nCapitalizing the first character of each line...\n")
    for i in range(5):
        output = engine.generate(template_name="LOWERCASE").capitalize()

        print(f"{i} {output}")
    
    print("\nNow onto capitalizing every character...\n")
    for i in range(5):
        output = engine.generate(template_name="LOWERCASE")

        final_output = ""
        for j in range(len(output)):
          final_output+= output[j].capitalize()

        print(f"{i} {final_output}")
Exemplo n.º 7
0
def component10():
    engine = TemplateEngine(file_path="templates/c10_template.txt")
    for i in range(5):
        print(f"<{i}>\n")
        for i in range(3):
            output = engine.generate(template_name="POEM")
            output_word_list = output.split()

            if output_word_list[3][0] == "a" or output_word_list[3][
                    0] == "e" or output_word_list[3][
                        0] == "o" or output_word_list[3][0] == "u":
                output_word_list[2] = "an"
            else:
                output_word_list[2] = "a"

            final_output = ""
            for j in range(len(output_word_list)):
                final_output += f"{output_word_list[j]} "

            print(f"{final_output} \n")
        print("one\ntwo\nthree\n.\n.\n.\nboom\n")
        print("Although\n\n")

        output1 = engine.generate(template_name="LUMP1")
        output2 = engine.generate(template_name="LUMP2")
        output3 = engine.generate(template_name="LUMP3")
        output4 = engine.generate(template_name="LUMP4")
        output5 = engine.generate(template_name="LUMP5")
        output6 = engine.generate(template_name="LUMP6")

        print(output1 + "\n" + output2 + "\n" + output3 + "\n" + output4 +
              "\n" + output5 + "\n" + output6)
Exemplo n.º 8
0
def component2f():
    engine = TemplateEngine(file_path="templates/c2_template.txt")

    for i in range(10):
        output = engine.generate(template_name="CONJUGATION")
        output_word_list = output.split()
        
        if output_word_list[3][0] == "a" or output_word_list[3][0] == "e" or output_word_list[3][0] == "i" or output_word_list[3][0] == "o" or output_word_list[3][0] == "u":
          output_word_list[2] = "an"
        else: 
          output_word_list[2] = "a"
        
        if output_word_list[7][0] == "a" or output_word_list[7][0] == "e" or output_word_list[7][0] == "i" or output_word_list[7][0] == "o" or output_word_list[7][0] == "u":
          output_word_list[6] = "an"
        else: 
          output_word_list[6] = "a"
        
        final_output = ""
        for j in range(len(output_word_list)):
          final_output += f" {output_word_list[j]}"
        
        print(f"{i} {final_output}")
Exemplo n.º 9
0
def component1d():
    engine = TemplateEngine(file_path="templates/c1_template.txt")

    multi_line_output = ""

    output1 = engine.generate(template_name="NO_CORPUS")
    output2 = engine.generate(template_name="YES_CORPUS")
    output3 = engine.generate(template_name="MY_CORPUS")
    output4 = engine.generate(template_name="HOPEFULLY_POETIC")

    multi_line_output += output1 + " " + output2 + " " + output3 + " " + output4
    print(f"{multi_line_output}")
Exemplo n.º 10
0
def component5b():
    engine = TemplateEngine(file_path="templates/c5_template.txt")
    for _ in range(5):
        print(engine.generate(template_name="PLOT_PROSE1"), end='\n\n')
Exemplo n.º 11
0
def component1a():
    engine = TemplateEngine(file_path="templates/c1_template.txt")

    for i in range(5):
        output = engine.generate(template_name="NO_CORPUS")
        print(f"{i} {output}")
Exemplo n.º 12
0
def experiment():
    engine = TemplateEngine(file_path="templates/experimental_dadaist.txt")

    for i in range(3):
        output = engine.generate(template_name="SENTENCE3")
        print(f"{i} {output} \n")
Exemplo n.º 13
0
def component2b():
    engine = TemplateEngine(file_path="templates/c2_template.txt")

    for i in range(10):
        output = engine.generate(template_name="PROBABILISTIC")
        print(f"{i} {output}")
Exemplo n.º 14
0
 def setUp(self):
     self.engine = TemplateEngine(os.getcwd() + "/test", os.getcwd() + "/test/project_dir")
Exemplo n.º 15
0
class TemplateEngineTest(unittest.TestCase):
    def setUp(self):
        self.engine = TemplateEngine(os.getcwd() + "/test", os.getcwd() + "/test/project_dir")


    def test_get_block_content(self):
        with open("test/templates/common.py", "r") as f:
            content = f.read()
            block_content = self.engine.get_block_content(content, "MIDDLEWARE_CLASSES")
            self.assertNotIn("#!chuck", block_content)

            block_content = self.engine.get_block_content(content, "SETTINGS")
            self.assertEqual(block_content, " ")

    #
    # TEST BASIC KEYWORDS
    #

    def test_renders(self):
        shutil.copy("test/templates/base.html", "test/project_dir/base.html")
        self.engine.handle("test/templates/replace.html", "test/project_dir/replace.html", {})

        with open("test/project_dir/base.html") as f:
            content = f.read()
            self.assertIn("<html>", content)
            self.assertIn("#!chuck_renders content", content)


    def test_render_multiple_blocks(self):
        shutil.copy("test/templates/base.html", "test/project_dir/base.html")
        self.engine.handle("test/templates/replace_three_blocks.html", "test/project_dir/replace_three_blocks.html", {})

        with open("test/project_dir/base.html") as f:
            content = f.read()
            self.assertIn("<html>", content)
            self.assertIn("#!chuck_renders content", content)
            self.assertIn("lets start", content)
            self.assertIn("MOOOOOH", content)
            self.assertIn("go home", content)


    def test_appends(self):
        shutil.copy("test/templates/base.html", "test/project_dir/base.html")
        self.engine.handle("test/templates/append.html", "test/project_dir/append.html", {})

        with open("test/project_dir/base.html") as f:
            content = f.read()
            self.assertTrue(re.search(r"One must have chaos in oneself to give birth to a dancing star.+The big MOOOOOH has pwned you", content, re.MULTILINE|re.DOTALL), content)


    def test_prepends(self):
        shutil.copy("test/templates/base.html", "test/project_dir/base.html")
        self.engine.handle("test/templates/prepend.html", "test/project_dir/prepend.html", {})

        with open("test/project_dir/base.html") as f:
            content = f.read()
            self.assertTrue(re.search(r"The big MOOOOOH has pwned you.+One must have chaos in oneself to give birth to a dancing star", content, re.MULTILINE|re.DOTALL), content)


    def test_extends_if_exists(self):
        shutil.copy("test/templates/base.html", "test/project_dir/base.html")
        self.engine.handle("test/templates/extends_if_exists.html", "test/project_dir/extends_if_exists.html", {})

        with open("test/project_dir/base.html") as f:
            content = f.read()
            self.assertTrue(re.search(r"One must have chaos in oneself to give birth to a dancing star.+The big MOOOOOH has pwned you", content, re.MULTILINE|re.DOTALL), content)


    def test_placeholder(self):
        shutil.copy("test/templates/base.html", "test/project_dir/base.html")
        self.engine.handle("test/templates/placeholder.html", "test/project_dir/placeholder.html", {"SOMETHING": "cool things"})

        with open("test/project_dir/base.html") as f:
            content = f.read()
            self.assertIn("cool things", content)


    def test_placeholder_without_extend(self):
        shutil.copy("test/templates/placeholder_without_extend.html", "test/project_dir/placeholder_without_extend.html")
        self.engine.handle("test/templates/placeholder_without_extend.html", "test/project_dir/placeholder_without_extend.html", {"SOMETHING": "cool things"})

        with open("test/project_dir/placeholder_without_extend.html") as f:
            content = f.read()
            self.assertIn("cool things", content)


    def test_remove_keywords(self):
        shutil.copy("test/templates/remove_keywords.html", "test/project_dir/remove_keywords.html")
        self.engine.handle("test/project_dir/remove_keywords.html", "test/project_dir/remove_keywords.html", {})
        self.engine.remove_keywords("test/project_dir/remove_keywords.html")

        with open("test/project_dir/remove_keywords.html") as f:
            content = f.read()
            self.assertFalse("#!chuck" in content, content)
            self.assertTrue("chaos" in content, content)



    def test_complex_example(self):
        shutil.copy("test/templates/common.py", "test/project_dir/common.py")

        self.engine.handle("test/templates/extends_common.py", "test/project_dir/extends_common.py", {"PROJECT_NAME": "test"})
        self.engine.remove_keywords("test/project_dir/common.py")

        with open("test/project_dir/common.py") as f:
            content = f.read()
            self.assertIn("# -*- coding", content.splitlines()[0])  # right order
            self.assertIn("CMS_TEMPLATES", content)                 # appends
            self.assertIn("ROOT_URLCONF = 'test.urls'", content)    # placeholder
            self.assertFalse("#!chuck" in content, content)         # removed keywords


    #
    # ADVANCED FEATURES
    #

    def test_extends_two_files(self):
        shutil.copy("test/templates/base.html", "test/project_dir/base.html")
        shutil.copy("test/templates/base2.html", "test/project_dir/base2.html")

        self.engine.handle("test/templates/extend_two_files.html", "test/project_dir/extend_two_files.html", {})

        with open("test/project_dir/base.html") as f:
            content = f.read()
            self.assertIn("big MOOOOOH", content)
            self.assertIn("#!chuck_renders content", content)

        with open("test/project_dir/base2.html") as f:
            content = f.read()
            self.assertIn("Balle was here", content)
            self.assertIn("#!chuck_renders content", content)



    #
    # ERRORS
    #
    def test_keyword_not_found(self):
        with self.assertRaises(TemplateError):
            self.engine.handle("test/templates/broken_keyword.html", "test/project_dir/broken_keyword.html", {})


    def test_syntax_error(self):
        with self.assertRaises(TemplateError):
            self.engine.handle("test/templates/broken_syntax.html", "test/project_dir/broken_syntax.html", {})


    def test_non_existent_block_name(self):
        with self.assertRaises(TemplateError):
            self.engine.handle("test/templates/broken_block_name.html", "test/project_dir/brocken_block_name.html", {})


    def test_non_existent_base_file(self):
        with self.assertRaises(TemplateError):
            self.engine.handle("test/templates/broken_base_file.html", "test/project_dir/broken_base_file.html", {})

    def test_non_existent_base_file_with_if_exists(self):
        self.engine.handle("test/templates/broken_base_file_with_if_exists.html", "test/project_dir/broken_base_file_with_if_exists.html", {})
Exemplo n.º 16
0
def component2d():
    engine = TemplateEngine(file_path="templates/c2_template.txt")

    for i in range(10):
        output = engine.generate(template_name="OPTIONAL2")
        print(f"{i} {output}")
Exemplo n.º 17
0
def component7a():
    engine = TemplateEngine(file_path="templates/c7_template.txt")

    for i in range(10):
        output = engine.generate(template_name="ALPHABETICAL")
        print(f"{i} {output} \n")
Exemplo n.º 18
0
def component4():
    engine = TemplateEngine(file_path="templates/c4_template.txt")

    for i in range(5):
        output = engine.generate(template_name="SHANNON_ZERO")
        print(f"{i} {output} \n")