Skip to content

Commit b77e4ee

Browse files
author
Matthew Shirtliffe
committed
website generator with some tests
1 parent e9cc2d8 commit b77e4ee

File tree

7 files changed

+190
-0
lines changed

7 files changed

+190
-0
lines changed

website_generator/app.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import os
2+
from string import Template
3+
4+
def print_header():
5+
""" Print header
6+
"""
7+
header_text = ''
8+
TEXT = f' Website generator \n'
9+
line = '-' * len(TEXT)
10+
line += '\n'
11+
12+
header_text += line
13+
header_text += TEXT
14+
header_text += line
15+
print(header_text)
16+
17+
18+
def get_site_name():
19+
while True:
20+
site_name = input('Site name: ')
21+
if len(site_name) > 0:
22+
return site_name
23+
else:
24+
print('A valid input required')
25+
26+
27+
def get_author_name():
28+
while True:
29+
name = input('Author: ')
30+
if len(name) > 0:
31+
return name
32+
else:
33+
print('A valid input required')
34+
35+
36+
def ask_if_javacript():
37+
38+
while True:
39+
answer = input('Do you want a folder for JavaScript? ')
40+
answer = answer.lower()
41+
if answer == 'y' or answer == 'yes':
42+
return True
43+
elif answer == 'n' or answer == 'no':
44+
return False
45+
else:
46+
print('A valid input is required')
47+
48+
def ask_if_css():
49+
50+
while True:
51+
answer = input('Do you want a folder for CSS? ')
52+
answer = answer.lower()
53+
if answer == 'y' or answer == 'yes':
54+
return True
55+
elif answer == 'n' or answer == 'no':
56+
return False
57+
else:
58+
print('A valid input is required')
59+
60+
def create_folder(name):
61+
try:
62+
os.mkdir(name)
63+
except OSError:
64+
print (f'Failed to create ./{name}')
65+
else:
66+
print (f'Created ./{name}')
67+
68+
69+
def create_file(path, content):
70+
path = os.path.join(path,'index.html')
71+
with open(path, 'w') as f:
72+
f.writelines(content)
73+
74+
if __name__ == "__main__":
75+
print_header()
76+
site_name = get_site_name()
77+
author_name = get_author_name()
78+
create_folder(site_name)
79+
javacript = ask_if_javacript()
80+
css = ask_if_css()
81+
if css:
82+
create_folder(os.path.join(site_name, 'css'))
83+
84+
if javacript:
85+
create_folder(os.path.join(site_name, 'js'))
86+
87+
template_file_text = ''
88+
with open('template.html', 'r') as f:
89+
template_file_text = ''.join(f.readlines())
90+
s = Template(template_file_text)
91+
92+
create_file(site_name, s.substitute(name=site_name, author=author_name))
93+
94+

website_generator/template.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang='en'>
3+
4+
<head>
5+
<meta charset='UTF-8'>
6+
<meta http-equiv='X-UA-Compatible' content='ie=edge'>
7+
<meta name="author" content="$author">
8+
<title>$name</title>
9+
<link rel='shortcut icon' href='favicon.png'>
10+
<link href='https://fonts.googleapis.com/css?family=Anton' rel='stylesheet'>
11+
<link href='https://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet'>
12+
<link rel='stylesheet' href='main.css'>
13+
</head>
14+
15+
<body>
16+
<main>
17+
<section id='hello'>
18+
<h1>Hello, World</h1>
19+
</section>
20+
</main>
21+
</body>
22+
23+
</html>

website_generator/tests/__init__.py

Whitespace-only changes.

website_generator/tests/system/__init__.py

Whitespace-only changes.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch, call
3+
import app
4+
5+
6+
class AppTest(TestCase):
7+
def test_print_header(self):
8+
9+
expected = "--------------------\n Website generator \n--------------------\n"
10+
with patch("builtins.print") as mocked_print:
11+
app.print_header()
12+
mocked_print.assert_called_with(expected)
13+
14+
def test_get_site_name(self):
15+
with patch("builtins.input") as mocked_input, patch(
16+
"builtins.print"
17+
) as mocked_print:
18+
mocked_input.side_effect = ("", "awesomeco")
19+
site_name = app.get_site_name()
20+
mocked_input.assert_called_with('Site name: ')
21+
mocked_print.assert_called_with("A valid input required")
22+
self.assertEqual(site_name, "awesomeco")
23+
24+
def test_get_author_name(self):
25+
with patch("builtins.input") as mocked_input, patch(
26+
"builtins.print"
27+
) as mocked_print:
28+
mocked_input.side_effect = ("", "Max Power")
29+
author_name = app.get_author_name()
30+
mocked_input.assert_called_with('Author: ')
31+
mocked_print.assert_called_with("A valid input required")
32+
self.assertEqual(author_name, "Max Power")
33+
34+
def test_ask_if_javacript(self):
35+
expected = False
36+
expected_input = "Do you want a folder for JavaScript? "
37+
with patch("builtins.input") as mocked_input:
38+
with patch("builtins.print") as mocked_print:
39+
mocked_input.side_effect = ("", "y", "n")
40+
answer = app.ask_if_javacript()
41+
mocked_print.assert_called_with("A valid input is required")
42+
mocked_input.assert_called_with(expected_input)
43+
self.assertTrue(answer)
44+
answer = app.ask_if_javacript()
45+
self.assertFalse(answer)
46+
47+
def test_ask_if_css(self):
48+
expected = False
49+
expected_input = "Do you want a folder for CSS? "
50+
with patch("builtins.input") as mocked_input:
51+
with patch("builtins.print") as mocked_print:
52+
mocked_input.side_effect = ("", "y", "n")
53+
answer = app.ask_if_css()
54+
mocked_print.assert_called_with("A valid input is required")
55+
mocked_input.assert_called_with(expected_input)
56+
self.assertTrue(answer)
57+
answer = app.ask_if_css()
58+
self.assertFalse(answer)
59+
60+
def test_create_folder(self):
61+
with patch("os.mkdir") as mocked_os_mkdir:
62+
app.create_folder("awesomeco")
63+
mocked_os_mkdir.assert_called_with("awesomeco")
64+
65+
def test_create_file(self):
66+
with patch("builtins.open") as mocked_open:
67+
app.create_file("awesomeco", "test")
68+
mocked_open.assert_called_with('awesomeco/index.html', 'w')

website_generator/tests/unit/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from unittest import TestCase
2+
import app
3+
4+
class FilteringRecordsTest(TestCase):
5+
pass

0 commit comments

Comments
 (0)