-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathguides.py
More file actions
executable file
·553 lines (422 loc) · 15.7 KB
/
guides.py
File metadata and controls
executable file
·553 lines (422 loc) · 15.7 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#!/usr/bin/env python
# Generate the assembly guides for each machine, as well as index files
import config
import os
import openscad
import shutil
import sys
import c14n_stl
import re
import json
import jsontools
import views
import pystache
from types import *
from assemblies import machine_dir
import csv
sourcing = {};
def md_filename(s):
s = s.replace(" ","")
return re.sub(r"\W+|\s+", "", s, re.I) + '.md'
def htm_filename(s):
s = s.replace(" ","")
return re.sub(r"\W+|\s+", "", s, re.I) + '.htm'
def gen_intro(m):
md = ''
note = jsontools.get_child_by_key_values(m, kvs={'type':'markdown', 'section':'introduction'})
if note and ('markdown' in note):
md += note['markdown'] + '\n\n'
return md
def gen_bom(m):
md = '## Bill of Materials\n\n'
md += 'Make sure you have all of the following parts before you begin.\n\n'
# vitamins
if len(m['vitamins']) > 0:
m['vitamins'].sort(key=vitamin_call, reverse=False)
md += '### Vitamins\n\n'
md += 'Qty | Vitamin | Image\n'
md += '--- | --- | ---\n'
for v in m['vitamins']:
md += str(v['qty']) + ' | '
md += '['+v['title']+']() | '
md += ' + ') | '
md += '\n'
md += '\n'
# cut parts
if len(m['cut']) > 0:
m['cut'].sort(key=cut_call, reverse=False)
md += '### Cut Parts\n\n'
md += 'Qty | Part Name | Image\n'
md += '--- | --- | ---\n'
for v in m['cut']:
md += str(v['qty']) + ' | '
md += v['title'] + ' | '
md += ' + ') | '
md += '\n'
md += '\n'
# printed parts
if len(m['printed']) > 0:
vol = 0
weight = 0
m['printed'].sort(key=printed_call, reverse=False)
md += '### Printed Parts\n\n'
md += 'Qty | Part Name | Image\n'
md += '--- | --- | ---\n'
for v in m['printed']:
md += str(v['qty']) + ' | '
md += '['+v['title']+'](../printedparts/stl/'+ openscad.stl_filename(v['title']) +') | '
md += ' + ') | '
md += '\n'
if 'plasticWeight' in v:
weight += v['qty'] * v['plasticWeight']
vol += v['qty'] * v['plasticVolume']
md += '\n\n'
md += '**Plastic Required**\n\n'
md += str(round(vol,1))+'cm3, ';
md += str(round(weight,2))+'KG, ';
md += ' approx: '+str(round(weight * 13,2))+' GBP\n\n';
md += '\n'
return md
def vitamin_call(v):
return v['call']
def printed_call(p):
return p['call']
def cut_call(c):
return c['call']
def gen_cut(m, a):
md = '## '+a['title']
if a['qty'] > 1:
md += ' (x'+str(a['qty'])+')'
md += '\n\n'
# vitamins
if len(a['vitamins']) > 0:
a['vitamins'].sort(key=vitamin_call, reverse=False)
md += '### Vitamins\n\n'
md += 'Qty | Vitamin | Image\n'
md += '--- | --- | ---\n'
for v in a['vitamins']:
md += str(v['qty']) + ' | '
md += '['+v['title']+']() | '
md += ' + ') | '
md += '\n'
md += '\n'
# fabrication steps
if len(a['steps']) > 0:
md += '### Fabrication Steps\n\n'
for step in a['steps']:
md += str(step['num']) + '. '+step['desc'] + '\n'
for view in step['views']:
md += '+'_'+view['title'])+')\n'
md += '\n'
md += '\n'
return md
def gen_assembly(m, a):
if len(a['steps']) == 0:
return ""
md = '## '+a['title']
if a['qty'] > 1:
md += ' (x'+str(a['qty'])+')'
md += '\n\n'
# vitamins
if len(a['vitamins']) > 0:
a['vitamins'].sort(key=vitamin_call, reverse=False)
md += '### Vitamins\n\n'
md += 'Qty | Vitamin | Image\n'
md += '--- | --- | ---\n'
for v in a['vitamins']:
md += str(v['qty']) + ' | '
md += '['+v['title']+']() | '
md += ' + ') | '
md += '\n'
md += '\n'
# printed parts
if len(a['printed']) > 0:
a['printed'].sort(key=printed_call, reverse=False)
md += '### Printed Parts\n\n'
md += 'Qty | Part Name | Image\n'
md += '--- | --- | ---\n'
for v in a['printed']:
md += str(v['qty']) + ' | '
md += '['+v['title']+'](../printedparts/stl/'+ openscad.stl_filename(v['title']) +') | '
md += ' + ') | '
md += '\n'
md += '\n'
# cut parts
# TODO: !!
# sub-assemblies
if len(a['assemblies']) > 0:
md += '### Sub-Assemblies\n\n'
md += 'Qty | Name \n'
md += '--- | --- \n'
for v in a['assemblies']:
md += str(v['qty']) + ' | '
md += v['title']
md += '\n'
md += '\n'
# assembly animation
if len(a['animations']) > 0:
md += '### Assembly Animation\n\n'
for anim in a['animations']:
md += '+'/'+anim['title']+'.gif)\n'
md += '\n'
# assembly steps
if len(a['steps']) > 0:
md += '### Assembly Steps\n\n'
for step in a['steps']:
md += str(step['num']) + '. '+step['desc'] + '\n'
for view in step['views']:
md += '+'/'+views.view_filename(a['title']+'_step'+str(step['num'])+'_'+view['title'])+')\n'
md += '\n'
md += '\n'
return md
def assembly_level(a):
return a['level']
def gen_assembly_guide(m, guide_template):
print((m['title']))
md = ''
md += '# '+m['title'] + '\n'
md += '# Assembly Guide\n\n'
# machine views
for c in m['children']:
if type(c) is dict and c['type'] == 'view' and 'filepath' in c:
view = c
md += '!['+view['caption']+']('+ view['filepath'][3:] +')\n\n'
# intro
md += gen_intro(m)
# BOM
md += gen_bom(m)
# Cut Parts
if len(m['cut']) > 0:
md += '# Cutting Instructions\n\n'
# Cut Parts
m['cut'].sort(key=cut_call, reverse=False)
for c in m['cut']:
md += gen_cut(m,c)
# Assemblies
if len(m['assemblies']) > 0:
md += '# Assembly Instructions\n\n'
# Assemblies
# sort by level desc
m['assemblies'].sort(key=assembly_level, reverse=True)
for a in m['assemblies']:
md += gen_assembly(m,a)
print(" Saving markdown")
mdfilename = md_filename(m['title'] +'AssemblyGuide')
mdpath = config.paths['docs'] + '/' +mdfilename
with open(mdpath,'w') as f:
f.write(md)
print(" Generating htm")
htmfilename = htm_filename(m['title'] +'AssemblyGuide')
htmpath = config.paths['docs'] + '/' + htmfilename
with open(htmpath, 'w') as f:
for line in open(guide_template, "r").readlines():
line = line.replace("{{mdfilename}}", mdfilename)
f.write(line)
return {'title':m['title']+ ' Assembly Guide', 'mdfilename':mdfilename, 'htmfilename':htmfilename}
def gen_printing_guide(m, guide_template):
print((m['title']))
if len(m['printed']) == 0:
return {};
md = ''
md += '# '+m['title'] + '\n'
md += '# Printing Guide\n\n'
vol = 0
weight = 0
qty = 0
m['printed'].sort(key=printed_call, reverse=False)
for v in m['printed']:
md += '### '+v['title']+'\n\n'
md += 'Metric | Value \n'
md += '--- | --- \n'
md += 'Quantity | ' + str(v['qty']) + '\n'
qty += v['qty']
md += 'STL | ' + '['+v['title']+'](../printedparts/stl/'+ openscad.stl_filename(v['title']) +')\n'
if 'plasticWeight' in v:
w = v['qty'] * v['plasticWeight']
weight += w
vol += v['qty'] * v['plasticVolume']
md += 'Plastic (Kg) | ' + str(round(w,2)) + '\n'
md += 'Plastic (cm3) | ' + str(round(v['qty'] * v['plasticVolume'],1)) + '\n'
md += 'Approx Plastic Cost | '+str(round(w * 15,2))+' GBP\n';
md += '\n'
md += ' + ')\n'
md += '\n'
if 'markdown' in v and len(v['markdown']) > 0:
md += '**Notes**\n\n'
for note in v['markdown']:
if 'markdown' in note:
md += ' * ' + note['markdown'] + '\n'
md += '\n\n'
md += '\n\n'
md += '## Summary\n\n'
md += '### Statistics\n\n'
md += 'Metric | Value \n'
md += '--- | --- \n'
md += 'Total Parts | ' + str(qty) + '\n'
md += 'Total Plastic (Kg) | ' +str(round(weight,2))+'KG\n'
md += 'Total Plastic (cm3) | ' +str(round(vol,1))+'cm3\n'
md += 'Approx Plastic Cost | '+str(round(weight * 15,2))+' GBP\n'
md += '\n\n'
print(" Saving markdown")
mdfilename = md_filename(m['title'] +'PrintingGuide')
mdpath = config.paths['docs'] + '/' +mdfilename
with open(mdpath,'w') as f:
f.write(md)
print(" Generating htm")
htmfilename = htm_filename(m['title'] +'PrintingGuide')
htmpath = config.paths['docs'] + '/' + htmfilename
with open(htmpath, 'w') as f:
for line in open(guide_template, "r").readlines():
line = line.replace("{{mdfilename}}", mdfilename)
f.write(line)
return {'title':m['title'] + ' Printing Guide', 'mdfilename':mdfilename, 'htmfilename':htmfilename}
def load_sources():
print("Loading sourcing info...")
load_source(config.paths['sourcingcsv'])
for filename in os.listdir(config.paths['vitamins']):
if filename[-4:] == '.csv':
print((" Parsing: "+filename))
csvfn = os.path.join(src_dir, filename)
load_source(csvfn)
def load_source(csvfn):
if os.path.isfile(csvfn):
with open(csvfn, 'r') as csvfile:
rdr = csv.DictReader(csvfile)
for row in rdr:
vn = row.get('Vitamin');
if vn not in sourcing:
sourcing[vn] = []
sourcing[vn].append({"Cost":row.get('Cost'), "Source":row.get('Source'), 'Notes':row.get('Notes')});
def gen_sourcing_guide(m, guide_template):
print((m['title']))
if len(m['vitamins']) == 0:
return {};
md = ''
md += '# '+m['title'] + '\n'
md += '# Sourcing Guide\n\n'
missing = []
cost = 0
qty = 0
m['vitamins'].sort(key=vitamin_call, reverse=False)
for v in m['vitamins']:
vn = v['title']
md += '### '+str(v['qty']) + 'x ' + vn+'\n\n'
qty += v['qty']
# table of sources and effective cost for qty required
if vn in sourcing:
md += 'Unit Cost | Source | Notes \n'
md += '--- | --- | --- \n'
# calc cheapest option and add to running total
lc = 0
for src in sourcing[vn]:
tc = float(src['Cost'])
if tc < lc or lc == 0:
lc = tc
md += src['Cost'] + ' | '
link = src['Source']
if link[:7] == 'http://':
md += '[link]('+link+') | '
else:
md += link + ' | '
md += src['Notes'] + '\n'
cost += lc * v['qty']
else:
md += 'No sources found\n'
missing.append('"'+vn+'",0,"","No sources found"')
md += '\n'
md += ' + ') \n'
md += '\n'
if 'markdown' in v and len(v['markdown']) > 0:
md += '**Notes**\n\n'
for note in v['markdown']:
if 'markdown' in note:
md += ' * ' + note['markdown'] + '\n'
md += '\n\n'
md += '\n'
md += '\n\n'
md += '## Summary\n\n'
md += '### Total Costs\n\n'
md += 'Metric | Value \n'
md += '--- | --- \n'
md += 'Total Vitamins | ' + str(qty) + '\n'
md += 'Total Cost (cheapest) | '+str(round(cost,2))+' GBP\n'
md += '\n\n'
print(" Saving markdown")
mdfilename = md_filename(m['title'] +'SourcingGuide')
mdpath = config.paths['docs'] + '/' +mdfilename
with open(mdpath,'w') as f:
f.write(md)
print(" Generating htm")
htmfilename = htm_filename(m['title'] +'SourcingGuide')
htmpath = config.paths['docs'] + '/' + htmfilename
with open(htmpath, 'w') as f:
for line in open(guide_template, "r").readlines():
line = line.replace("{{mdfilename}}", mdfilename)
f.write(line)
print(" Updating sourcing.csv")
with open(config.paths['sourcingcsv'], 'a') as f:
for line in missing:
f.write(line + "\n")
return {'title':m['title'] + ' Sourcing Guide', 'mdfilename':mdfilename, 'htmfilename':htmfilename}
def gen_index(jso, index_file, index_template):
# Generate index file
# build object
indexObj = { 'machines': [], 'project':'' };
for m in jso:
if type(m) is dict and m['type'] == 'machine':
# tack in a view filename
m['viewFilename'] = views.view_filename(m['title'] + '_view')
if indexObj['project'] == '':
indexObj['project'] = m['title']
indexObj['machines'].append(m)
print("Saving index")
with open(index_file,'w') as o:
with open(index_template,'r') as i:
o.write(pystache.render(i.read(), indexObj))
def copytree(src, dst, symlinks=False, ignore=None):
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
shutil.copy2(s, d)
def guides():
print("Guides")
print("------")
#
# Make the target directories
#
if not os.path.isdir(config.paths['docs']):
os.makedirs(config.paths['docs'])
assembly_guide_template = os.path.join(config.paths['templatedocs'], "AssemblyGuide.htm")
printing_guide_template = os.path.join(config.paths['templatedocs'], "PrintingGuide.htm")
sourcing_guide_template = os.path.join(config.paths['templatedocs'], "SourcingGuide.htm")
index_template = os.path.join(config.paths['templatedocs'], "index.htm")
index_file = os.path.join(config.paths['docs'], 'index.htm')
# load hardware.json
jf = open(config.paths['json'],"r")
jso = json.load(jf)
jf.close()
# load sourcing info
load_sources()
dl = {'type':'docs', 'assemblyGuides':[], 'printingGuides':[] }
jso.append(dl)
# for each machine
for m in jso:
if type(m) is dict and m['type'] == 'machine':
if 'guides' not in m:
m['guides'] = []
m['guides'].append(gen_assembly_guide(m, assembly_guide_template))
m['guides'].append(gen_printing_guide(m, printing_guide_template))
m['guides'].append(gen_sourcing_guide(m, sourcing_guide_template))
gen_index(jso, index_file, index_template)
# copy over css and js folders
copytree(os.path.join(config.paths['frameworkdocs'], 'js'), os.path.join(config.paths['docs'], 'js'))
copytree(os.path.join(config.paths['frameworkdocs'], 'css'), os.path.join(config.paths['docs'], 'css'))
return 0
if __name__ == '__main__':
guides()