-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnano.py
executable file
·145 lines (126 loc) · 4.26 KB
/
nano.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
import argparse
import os
import sys
from cryptography.fernet import Fernet
ENC_DIRS = ['novel','plot','setting','stuff','characters']
NOVEL_DIR = ['novel']
NOVEL_EXTENSION = '.md'
ENC_EXTENSION = '.enc'
OUT_FILE = '.'+os.sep+'out'+os.sep+'novel.md'
TEMPLATE_TEXT = "# Chapter %d\nAdd some content"
TEMPLATE_NAME = "chapter_%s.md"
#Parser Arguments
parser = argparse.ArgumentParser(description='NaNoWriMo Management Script')
parser.add_argument('-c','--count',action='store_true',help='Output wordcount')
parser.add_argument('-k','--generatekey',action='store_true',help='Generate new Key and store to secret.key')
parser.add_argument('-e','--encrypt',action='store_true',help='Encrypt Novel')
parser.add_argument('-d','--decrypt',action='store_true',help='Decrypt Novel')
parser.add_argument('-m','--merge',action='store_true',help='Merge Novel')
parser.add_argument('-g','--generate',metavar='N',help='Generate Template with N chapters')
parser.add_argument('-p','--prepare',action='store_true',help='Prepare Directory')
args = parser.parse_args();
def generateKey():
key = Fernet.generate_key()
#TODO Safety: Don't overwrite old key
with open("secret.key",'wb') as tmp:
tmp.write(key)
def loadKey():
with open("secret.key",'rb') as tmp:
key = tmp.read()
return key
def getNovelFiles(paths,extension):
files = []
for path in paths:
if(path[-1:]==os.sep):
path=path[:-1]
path = '.'+os.sep+path
for filename in os.listdir(path):
if(filename[-len(extension):]==extension):
files.append(path+os.sep+filename)
return files
def generate(paths,extension,numberOfChapters):
path = '.'+os.sep+paths[0]
for i in range(numberOfChapters):
chapter = i+1
filename = path + os.sep + TEMPLATE_NAME%str(chapter).zfill(2)
with open(filename,'w') as tmp:
tmp.write(TEMPLATE_TEXT%chapter)
print(filename)
def prepare():
os.mkdir('characters')
os.mkdir('setting')
os.mkdir('plot')
os.mkdir('novel')
os.mkdir('out')
os.mkdir('stuff')
print("Done.")
def merge(paths,extension,outfile):
files = getNovelFiles(paths,extension)
files.sort()
print(files)
with open(outfile,'w') as out:
for file in files:
with open(file,'r') as tmp:
text = tmp.read()
print(text)
out.write(text)
out.write('\n---\n')
def encrypt(paths,extension,enc_extension):
key = loadKey()
f = Fernet(key)
files = getNovelFiles(paths,extension)
for file in files:
file_enc = file+enc_extension
print("%s -> %s"%(file,file_enc))
with open(file,'rb') as plain:
token = f.encrypt(plain.read())
with open(file_enc,'wb') as cipher:
cipher.write(token)
def decrypt(paths,extension,enc_extension):
key = loadKey()
f = Fernet(key)
files = getNovelFiles(paths,enc_extension)
for file in files:
file_dec = file[:-len(enc_extension)]
print("%s -> %s"%(file,file_dec))
with open(file,'rb') as cipher:
plaintext = f.decrypt(cipher.read())
with open(file_dec,'wb') as plain:
plain.write(plaintext)
def getWordCount():
files = getNovelFiles(NOVEL_DIR,NOVEL_EXTENSION)
count = 0
for file in files:
with open(file,'r') as tmp:
text = tmp.read()
splitted = text.split()
print("%s (%d)"%(file,len(splitted)))
count = count + len(splitted)
print("Total Word count: %d"%count)
if(args.count):
getWordCount()
sys.exit()
if(args.generatekey):
generateKey()
print("Generated Key. Exiting.")
sys.exit()
if(args.encrypt):
encrypt(ENC_DIRS,NOVEL_EXTENSION,ENC_EXTENSION)
print("Done.")
sys.exit(0)
if(args.decrypt):
decrypt(ENC_DIRS,NOVEL_EXTENSION,ENC_EXTENSION)
print("Done.")
sys.exit(0)
if(args.merge):
merge(NOVEL_DIR,NOVEL_EXTENSION,OUT_FILE)
print("Done.")
sys.exit(0)
if(args.generate):
numberOfChapters = int(args.generate)
print("Generating %d chapters in directory %s"%(numberOfChapters,NOVEL_DIR))
generate(NOVEL_DIR,NOVEL_EXTENSION,numberOfChapters)
if(args.prepare):
prepare()
sys.exit(0)