-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmynotes.py
More file actions
executable file
·166 lines (108 loc) · 4.53 KB
/
mynotes.py
File metadata and controls
executable file
·166 lines (108 loc) · 4.53 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import re
import sys
import datetime
import errno
import subprocess
from optparse import OptionParser
import parsedatetime.parsedatetime as pdt
import parsedatetime.parsedatetime_consts as pdc
defaultFileExtension = ".txt"
defaultPathToDataFiles = "/Users/ajsingh/SkyDrive/myNotes/"
defaultEditor = "subl"
pathToScript = "/Users/ajsingh/dev/mynotes/"
defaultPathToView = "/Users/ajsingh/Dropbox/notes_view.html"
pathToTemplateEntry = pathToScript + "template_entry.html"
pathToTemplateYear = pathToScript + "template_year.html"
pathToTemplateBase = pathToScript + "template_base.html"
def initParser(args):
parser = OptionParser()
parser.add_option("-d", "--date", action="store", type="string",
dest="date", help="Override date. Enter it in a string\
form. Ex: \"Tomorrow\" or \"March 4th 2012\"")
parser.add_option("-e", "--editor", action="store", type="string",
dest="editor", help="Override the editor.")
parser.add_option("-o", "--output", action="store", type="string",
dest="output", help="Output file. Default is "+defaultPathToView)
parser.add_option("-r", "--root", action="store", type="string",
dest="root", help="Change the root location of this\
file. The directory structure will be preserved")
parser.add_option("-v", "--view", action="store_true",
dest="view", help="Generate a view in the form of a flat\
HTML file")
(options, args) = parser.parse_args(args)
return options
def parseDate(dateString):
c = pdc.Constants()
p = pdt.Calendar(c)
result = p.parse(dateString)
year = result[0][0]
monthDay = '%0*d' %(2, result[0][1]) + '%0*d' %(2, result[0][2])
return (year, monthDay)
def generateView(rootDir, viewFile):
regExFilename = re.compile('[0-9][0-9][0-9][0-9]\.txt')
regExDir = re.compile('[0-9][0-9][0-9][0-9]')
templateEntryData = open(pathToTemplateEntry).read()
templateBaseData = open(pathToTemplateBase).read()
templateYearData = open(pathToTemplateYear).read()
yearData = {}
allData = ""
for dirname, dirnames, filenames in os.walk(rootDir):
for filename in filenames:
if(regExFilename.match(filename) != None):
year = dirname.split("/")[-1]
filePath = os.path.join(dirname, filename)
fileData = open(filePath).read()
fileName = filePath.split("/")[-1].split(".")[0]
dataAppliedToTemplate = templateEntryData.replace("{{ date }}", fileName)
dataAppliedToTemplate = dataAppliedToTemplate.replace("{{ data }}", fileData)
try:
yearData[year] += dataAppliedToTemplate
except:
yearData[year] = dataAppliedToTemplate
# maybe sort here if needed
sortedYears = sorted(yearData)
for year in sortedYears:
perYearData = templateYearData.replace("{{ year }}", year)
perYearData = perYearData.replace("{{ year_data }}", yearData[year])
allData += perYearData
#print out to view
viewFileHandle = open(viewFile, "w")
viewFileHandle.write(templateBaseData.replace("{{ full_data }}", allData))
def main():
options = initParser(sys.argv)
if(options.root):
pathToDataFiles = options.root #make sure we have a / in the end
else:
pathToDataFiles = defaultPathToDataFiles
if(options.editor):
editor = options.editor
else:
editor = defaultEditor
if(options.date):
(year, monthDay) = parseDate(options.date)
else:
now = datetime.datetime.now()
year = now.year
monthDay = '%0*d' %(2, now.month) + '%0*d' %(2, now.day)
if(options.output):
pathToViewFile = options.output
else:
pathToViewFile = defaultPathToView
if(options.view):
generateView(pathToDataFiles, pathToViewFile)
return
currDir = pathToDataFiles + '%0*d' %(4, year)
currFile = currDir + "/" + monthDay + defaultFileExtension
try:
os.makedirs(currDir)
except OSError, e:
if e.errno != errno.EEXIST:
raise
if(editor == "subl"):
subprocess.call([editor, "-w", currFile])
else:
subprocess.call([editor, currFile])
generateView(pathToDataFiles, pathToViewFile)
if __name__ == "__main__":
main()