-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhalo_postprocessor.py
More file actions
77 lines (58 loc) · 2.15 KB
/
halo_postprocessor.py
File metadata and controls
77 lines (58 loc) · 2.15 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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
# Copyright (C) 2018 Ethereal Machines Pvt. Ltd. All rights reserved.
# Distributed under terms of the MIT license.
__author__ = 'Toran Sahu <toran.sahu@yahoo.com>'
__version__ = '1.18.06.11'
"""
Halo Postprocessor.
"""
# TODO: add publisher info in exe
# TODO: beautify window
import os
import sys
from tkinter.filedialog import askopenfilename
import re
import logging
logger = logging.getLogger(__name__)
file_path = askopenfilename(initialdir="./", title="Select file", filetypes=(("gcode files", "*.gcode"),("all files", "*.*")))
# file_path = "CFFFP_bridging_test.gcode"
if file_path is None or not file_path:
logger.warning('File not selected.')
sys.exit('Exiting..')
elif os.path.splitext(file_path)[1].lower() != '.gcode':
logger.warning('Unsupported file.')
sys.exit('Unsupported file. Exiting..')
count = 1
parent_dir, filename = os.path.split(file_path)
name, ext = os.path.splitext(filename)
similar_files = []
regex = re.escape(filename) + r'\-eth\-[0-9]+\.nc\Z'
regex = r'(?s:' + re.escape(name) + r'-eth\-[0-9]+\.nc)\Z'
re_obj = re.compile(regex)
for file in os.listdir(parent_dir, ):
if re_obj.match(file):
similar_files.append(file)
output = None
if len(similar_files) > 0:
logger.info('Similar output files found.')
max_file_count = max(map(lambda f: int((f.rsplit('-eth-', 1)[1]).rsplit('.',1)[0]), similar_files))
count = max_file_count + 1
output = os.path.join(parent_dir, name) + f'-eth-{count}.nc'
else:
output = os.path.join(parent_dir, name) + f'-eth-{count}.nc'
with open(file_path, 'r') as r_stream, open(output, 'w') as w_stream:
w_stream.write("%\n")
# w_stream.write("M100 P170")
for datum in r_stream:
if ";" not in datum:
datum = datum.replace('E', 'B').replace('M104 S', 'M100 P')
if "M" in datum and all(i not in datum for i in ["M100", "M9", "M30"]):
pass
else:
w_stream.write(datum)
w_stream.write("M9\n")
w_stream.write("M30\n")
w_stream.write("%\n")
logger.info(f'File {output} has been processed successfully..')