-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmk-recipe.py
More file actions
executable file
·49 lines (35 loc) · 1.23 KB
/
mk-recipe.py
File metadata and controls
executable file
·49 lines (35 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/python3
import argparse
import os
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--name', help='The name of the recipe')
parser.add_argument('--type', help='The type if the recipe, is it main, breakfast, or dessert?')
parser.add_argument('--serves', help='How many does it server or make?')
args = parser.parse_args()
allowed_types = ["main", "dessert", "breakfast"]
if not args.name:
print("ERROR: name is a required argument!")
sys.exit(1)
if not args.type:
print("ERROR: type is a required argument")
sys.exit(1)
if args.type not in allowed_types:
print("ERROR: invalid type \"{}\"".format(args.type))
sys.exit(1)
templatePath = 'template.md'
newFileName = args.name.lower().replace(' ', '-') + '.md'
outputPath = args.type + "/" + newFileName
if os.path.isfile(outputPath):
print("ERROR: file {} already exists".format(outputPath))
sys.exit(1)
with open(templatePath) as file:
fileData = file.read()
fileData = fileData.replace('$name', args.name)
if args.serves:
fileData = fileData.replace('$serves', args.serves)
else:
fileData = fileData.replace('$serves', 'n/a')
with open(outputPath, 'w+') as file:
file.write(fileData)
print("Created file " + outputPath)