-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
391 lines (331 loc) · 12 KB
/
test.py
File metadata and controls
391 lines (331 loc) · 12 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# ! TODO: Add Highligthing shit
import json
import os
import subprocess
import time
from collections import deque
from datetime import datetime
from glob import glob
import cson
from config import git_commands
home = os.path.expanduser("~")
BOOSTNOTE_PATH = os.path.join(home, 'Boostnote')
BOOSTNOTE_NOTES_PATH = os.path.join(home, 'Boostnote/notes')
BOOSTNOTE_SYNCNOTES_PATH = os.path.join(
home, 'Boostnote/notes/syncboostnote')
def boostnote_exists(location=os.path.join(home, 'Boostnote')):
if os.path.isdir(BOOSTNOTE_PATH):
return 1
else:
# ! YAML Config
return 0
# raise NotADirectoryError("BoostNode Base Directory doesn't exist. Either make sure BoostNote is installed or add PATH to it in syncboostnote.yaml")\
def boostnote_notes_exist(location=os.path.join(home, 'notes')):
if os.path.isdir(BOOSTNOTE_NOTES_PATH):
return 1
else:
# ! YAML Config
raise NotADirectoryError(
f"No NOTES were found in the {BOOSTNOTE_NOTES_PATH} directory")
def get_notes():
if boostnote_notes_exist():
notes = glob(os.path.join(BOOSTNOTE_NOTES_PATH, '*.cson'))
if notes:
return notes
else:
raise EnvironmentError("Emptry Notes Folder, nothing to work on.")
else:
raise NotADirectoryError(
"BoostNode Base Directory doesn't exist. Either make sure BoostNote is installed or add PATH to it in syncboostnote.yaml")
def cson_reader(location):
if os.path.isfile(location):
data = cson.load(open(location, 'r'))
return data
else:
raise FileNotFoundError(f'The cson file at {location} was not found')
def customshield(
label='label',
message='message',
style='plastic',
color='orange',
mode='markdown',
name='Custom Shield'):
'''
'''
if mode not in ['markdown', 'md', 'restructuredtext', 'rst']:
raise NotImplementedError(f'{mode} is not implemented yet.')
else:
if mode in ['markdown', 'md']:
return f""
else:
return f".. image:: https://img.shields.io/badge/{label}-{message}-{color}.svg?style={style} :alt: {name}"
def markdown_writer(things, location, shields=True,
options={
'style': 'for-the-badge',
'option': 2
}):
'''
Used to write the markdown files
--------------------------------
PARAMETERS:
things: dict, cson_reader(fp):
The dict{} which contains shit that was read via the cson
'''
embels = ['isStarred', 'isTrashed',
'updatedAt', 'type', 'folder', 'tags']
shelds = []
if shields:
for key in embels:
x = None
if things[key]:
if key == 'isStarred':
shelds.append(customshield(
key, '⭐', color='black', style=options['style']))
if key == 'isTrashed':
shelds.append(customshield(
key, '🗑', color='black', style=options['style']))
elif key == 'updatedAt':
shelds.append(customshield(
key, things[key].split(':')[0][:-3].replace('-', '/'), color='green', style=options['style']))
elif key in ['type', 'folder']:
shelds.append(customshield(
key, things[key], color='blue', style=options['style']))
elif key == 'tags':
if options['option']:
# OPTION 1: {tag| gay} {tag| notgay}
for tag in things[key]:
shelds.append(
customshield(label='tag', message=tag,
color='purple', style=options['style'])
)
else:
# OPTION 2: {tag| gay, notgay}
tags = []
for tag in things[key]:
tags.append(tag)
shelds.append(customshield(
label='tags', message='_'.join(tags), color='blueviolet', style=options['style']))
file = open(os.path.join(location,
f"{things['title']}.md"), 'w+')
for count, shield in enumerate(shelds):
if count != len(shelds) - 1:
file.write(shield)
file.write(' ')
else:
file.write(shield)
file.write('\n')
try:
file.write(things['content'])
except:
pass
try:
file.write(things['snippets'])
except:
pass
# for note in get_notes():
# markdown_writer(
# cson_reader(note),
# options={
# 'style': 'for-the-badge',
# 'option': 2
# }
# )
# boostnote_exists()
# boostnote_notes_exist()
# cson_reader()
# cson_reader()
def get_changes():
'''
uses git status to find the files which have changes.
------------------------------------------------------
Returns:
list:
A List containing all the files names which have changed.
>>> get_changes()
>>> ['test.py', 'cli.py', 'config.py', 'utils.py']
'''
files = []
os.chdir(BOOSTNOTE_NOTES_PATH)
p = subprocess.Popen(
git_commands.status, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
# Putting checks to see if any rendered file is deleted.
if '.md' in line.decode("utf-8"):
files.append(
line.decode(
"utf-8").replace('modified:', '').strip().split('/')[-1]
)
# Checking if the diff file is .cson
if '.cson' in line.decode("utf-8"):
files.append(line.decode(
"utf-8").replace('modified:', '').strip().split('/')[-1]
)
retval = p.wait()
return files
def update_changes():
changed_files = get_changes()
history_json = json.load(open(os.path.join(
BOOSTNOTE_PATH, 'history.json'), 'r'))
for file in changed_files:
if '.md' in file:
# These files have been deleted or changed without telling us 😢😢😢
# Thus we will re render them
# 1. get the filename
history_json = json.load(open(os.path.join(
BOOSTNOTE_PATH, 'history.json'), 'r'))
for filename in history_json.keys():
if history_json[filename]['title'] == file.replace('.md', ''):
# 2. Rendering the missing files.
markdown_writer(
cson_reader(
os.path.join(
BOOSTNOTE_PATH, 'notes', filename)
),
location=BOOSTNOTE_SYNCNOTES_PATH,
options={
'style': 'for-the-badge',
'option': 2
}
)
pass
else:
history_json[file]['updated'] = False
json.dump(
history_json,
open(os.path.join(BOOSTNOTE_PATH, 'history.json'), 'w+')
)
def create_history():
files = {}
for note in get_notes():
files[note.split('/')[-1]] = {
'title': cson.load(open(note, 'r'))['title'],
'updated': False
}
json.dump(
files,
open(os.path.join(BOOSTNOTE_PATH, 'history.json'), 'w+')
)
def create_readme(config):
notes = get_notes()
# data = {}
file = open(os.path.join(config['BOOSTNOTE_PATH'], 'README.md'), 'w+')
file.write(
'''# SnycBoostNotes
# This repo consists of two directories:
```bash
$ tree
.
├── boostnote.json
├── history.json
└── notes
├── ....cson
├── ....cson
└── syncboostnote
├── ....md
├── ....md
```
- Directory `base`:
- boostnote.json ``Created by boostnote``
- history.json ``Created by SyncBoostnote``
- Directory `notes`:
- Raw `.cson` files used by BoostNote.
- Directory `syncboostnote`:
- `.md` files used display content on Github.
# Index:
# This following are the documents:
'''
)
for note in get_notes():
data = cson_reader(note)
# data[note.split("/")[-1]] = {
# 'title': cson_reader(note)['title'],
# 'createdAt': cson_reader(note)['createdAt'],
# 'tags': cson_reader(note)['tags'],
# }
# ! Generate Github link here
file.write(
f"- [{data['title']}](https://github.com/DumbMachine/temp/blob/master/notes/syncboostnote/{data['title'].replace(' ','%20')}.md)")
file.write("\n")
# return data
awesome = 'https://img.shields.io/badge/made--with--%E2%99%A5--by-ProjectPy-blueviolet.svg'
file.write(
f"\n---\n<sub>This README was generated with ❤ by [SyncBoostnote](https://github.com/DumbMachine/SyncBoostNote) </sub>")
def ultimate(config):
if not os.path.isfile(os.path.join(config['BOOSTNOTE_PATH'], 'history.json')):
# Create the History json again.
create_history()
if boostnote_exists(config['BOOSTNOTE_PATH']):
print()
print('[PASSED] BOOSTNOTE_EXISTS ')
if boostnote_notes_exist(os.path.join(config['BOOSTNOTE_PATH'], 'notes')):
print('[PASSED] BOOSTNOTE_NOTES_EXISTS ')
history_json = json.load(open(os.path.join(
BOOSTNOTE_PATH, 'history.json'), 'r'))
for file in history_json.keys():
print(file, history_json[file]['updated'])
if not history_json[file]['updated']:
# If not updated, re render the file
markdown_writer(
cson_reader(
os.path.join(
config['BOOSTNOTE_PATH'], 'notes', file)
),
location=BOOSTNOTE_SYNCNOTES_PATH,
options={
'style': config['SHIELDS_TYPE'],
'option': 2
}
)
# Update the file render
history_json[file]['updated'] = True
create_readme(config)
# Writing the changes of render.
json.dump(
history_json,
open(os.path.join(BOOSTNOTE_PATH, 'history.json'), 'w+')
)
print('[PASSED] README_GEN ')
else:
print("FUCKKK")
ultimate({
"BOOSTNOTE_PATH": os.path.join(home, 'Boostnote'),
"SHIELDS": True,
"SHIELDS_TYPE": "for-the-badge",
"FREQUENCY": "hourly",
"TIME": 1200
})
# def timely_check(config):
# time_check = config['TIME']
# frequency = config['FREQUENCY']
# if frequency == 'onchange':
# raise NotImplementedError('This THING is not implemented currently')
# else:
# if datetime.now().hour == time_check:
# print("ITS HIGH NOON")
# else:
# time.sleep(2)
# print("Has the time come yet?")
# # timely_check(
# # {
# # "BOOSTNOTE_PATH": os.path.join(home, 'Boostnote'),
# # "SHIELDS": True,
# # "SHIELDS_TYPE": "for-the-badge",
# # "FREQUENCY": "hourly",
# # "TIME": 11
# # }
# # )
# def watch_file(filename, time_limit=3600, check_interval=60):
# '''
# use get changes to see changes
# '''
# now = time.time()
# last_time = now + time_limit
# while time.time() <= last_time:
# if os.path.exists(filename):
# print('yes')
# else:
# # Wait for check interval seconds, then check again.
# time.sleep(check_interval)
# print('no')
# def create_readme():
# raise NotImplementedError