-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateDayFolder.py
68 lines (58 loc) · 1.83 KB
/
createDayFolder.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
import getopt
import os
from datetime import datetime
from pathlib import Path
from string import Template
def main(argv):
today = datetime.today()
templatePath = Path("template")
year = today.year
day = today.day
errorMsg = """
createDayFolder.py [-h] [-y <year>] [-d <day>] [-t <template>]
-h: Display this help message
-y: The Year as number
-d: The day as number
-t: The template folder as path
"""
opts = None
try:
opts = getopt.getopt(
argv, "hy:d:t:", ["year=", "day=", "template="])[0]
except getopt.GetoptError as err:
print("An error has occured")
print(errorMsg)
sys.exit(2)
try:
for opt, arg in opts:
if opt in ("-h", "--help"):
print(errorMsg)
sys.exit()
elif opt in ("-y", "--year"):
year = int(arg)
elif opt in ("-d", "--day"):
day = int(arg)
elif opt in ("-t", "--template"):
templatePath = Path(arg)
except Exception as err:
print(err)
print(errorMsg)
sys.exit(2)
destPath = Path("{:d}/{:02d}".format(year, day))
if(destPath.exists()):
print("Path does alread exists. Maybe define a day and/or year with -d and/or -y")
sys.exit(0)
else:
destPath.mkdir(parents=True)
for f in templatePath.iterdir():
if(f.is_file()):
with f.open("r") as file:
contents = Template(file.read()).safe_substitute(
{"day": str(day), "year": str(year)})
dest = destPath.joinpath(f.name)
with dest.open(mode="w") as file:
file.write(contents)
print(f.name + " copied!")
if(__name__ == "__main__"):
main(sys.argv[1:])