patternpythonMinor
pjen - Simple static website generator
Viewed 0 times
websitesimplegeneratorpjenstatic
Problem
I've created a simple static website generator called pjen. The GitHub repository can be found here.
```
import os
class pjen:
def __init__(self, path=None):
"""
By default, the path will be set to the current working directory,
or the path can be specified using the 'path' keyword parameter.
"""
if path is None:
self.path = os.getcwd()
else:
self.path = path
self.path += "/website"
def create_project(self):
"""
Creates an initial file structure for a project at the specified path.
An exception is raised if a project already exists in the desired location.
Creates the following file structure:
website
|-> templates
|-> group1
|-> images
|-> scss
|-> css
|-> scripts
"""
if not os.path.exists(self.path):
#make the root directory
os.makedirs(self.path)
os.makedirs(self.path+"/templates")
os.makedirs(self.path+"/templates/group1")
os.makedirs(self.path+"/images")
os.makedirs(self.path+"/scss")
os.makedirs(self.path+"/css")
os.makedirs(self.path+"/scripts")
print("Created project in {}".format(self.path))
else:
raise IOError("A directory already exists at: {}".format(self.path))
def _count_indent(self, str, tab_equiv=4):
"""
Returns the number of leading spaces. A tab is counted as 'tab_equiv' spaces.
"""
i = 0
count = 0
while(str[i] == " " or str[i] == "\t"):
if str[i] == " ":
count += 1
if str[i] == "\t":
count += 4
i += 1
return count
def _sanatise_file_list(self, l, fname=None):
"""
Removes blacklisted files from the input list 'l'. In addition, a file with
```
import os
class pjen:
def __init__(self, path=None):
"""
By default, the path will be set to the current working directory,
or the path can be specified using the 'path' keyword parameter.
"""
if path is None:
self.path = os.getcwd()
else:
self.path = path
self.path += "/website"
def create_project(self):
"""
Creates an initial file structure for a project at the specified path.
An exception is raised if a project already exists in the desired location.
Creates the following file structure:
website
|-> templates
|-> group1
|-> images
|-> scss
|-> css
|-> scripts
"""
if not os.path.exists(self.path):
#make the root directory
os.makedirs(self.path)
os.makedirs(self.path+"/templates")
os.makedirs(self.path+"/templates/group1")
os.makedirs(self.path+"/images")
os.makedirs(self.path+"/scss")
os.makedirs(self.path+"/css")
os.makedirs(self.path+"/scripts")
print("Created project in {}".format(self.path))
else:
raise IOError("A directory already exists at: {}".format(self.path))
def _count_indent(self, str, tab_equiv=4):
"""
Returns the number of leading spaces. A tab is counted as 'tab_equiv' spaces.
"""
i = 0
count = 0
while(str[i] == " " or str[i] == "\t"):
if str[i] == " ":
count += 1
if str[i] == "\t":
count += 4
i += 1
return count
def _sanatise_file_list(self, l, fname=None):
"""
Removes blacklisted files from the input list 'l'. In addition, a file with
Solution
is is broken:>>> 1000 is 10**3
False
>>> 1000 == 10**3
TrueYou can read more about this here, but in short it's better to use
==.Ternary statements:
I really like ternaries, so I may be a little biased, but you can use ternary statements to simplify logic:
if path is None:
self.path = os.getcwd()
else:
self.path = pathinto:
self.path = path if path != None else os.getcwd()while loop:Instead of using a
while loop, you can simply iterate using a for loop:(also, Python does not require explicit brackets around the loop)
(also,
str[i] cannot be both " " and "\t", so you can use a elif for the second one.)while(str[i] == " " or str[i] == "\t"):
if str[i] == " ":
count += 1
if str[i] == "\t":
count += 4
i += 1
return countinto:
for item in str:
if item == " ":
count += 1
elif item == "\t":
count += 4
return countCode Snippets
>>> 1000 is 10**3
False
>>> 1000 == 10**3
Trueif path is None:
self.path = os.getcwd()
else:
self.path = pathself.path = path if path != None else os.getcwd()while(str[i] == " " or str[i] == "\t"):
if str[i] == " ":
count += 1
if str[i] == "\t":
count += 4
i += 1
return countfor item in str:
if item == " ":
count += 1
elif item == "\t":
count += 4
return countContext
StackExchange Code Review Q#114102, answer score: 5
Revisions (0)
No revisions yet.