def rotate(P, W, angle, xOffset, yOffset, line): REGCODE = COMPILE('([a-z]-?[0-9]+\.?([0-9]+)?)|\(.*\)') inLine = line comment = '' i = inLine.find('(') if i >= 0: comment = inLine[i:] inLine = inLine[:i - 1].strip() if len(inLine) > 0: parts = list([list(cmd)[0] for cmd in REGCODE.findall(inLine)]) if len(parts) <= 1 or parts[0] not in ['g0', 'g1', 'g2', 'g3', 'x', 'y'] or \ inLine.replace(' ','').startswith('g0z[#<_ini[axis_z]max_limit>-'): return '{}\n'.format(line) angle = math.radians(angle) params = { 'x': 0.0, 'y': 0.0, 'r': 0.0, 'i': 0.0, 'j': 0.0, } used = '' for p in parts: for n in 'xyrij': if n in p: if n == 'x': params['x'] = float(p.strip(n)) used += 'x' elif n == 'y': params['y'] = float(p.strip(n)) used += 'y' elif n == 'r': params['r'] = float(p.strip(n)) used += 'r' elif n == 'i': params['i'] = float(p.strip(n)) used += 'i' elif n == 'j': params['j'] = float(p.strip(n)) used += 'j' newLine = ('{}'.format(parts[0])) if 'x' in used: newLine += (' x{:.6f}'.format(params['x'] * math.cos(angle) - params['y'] * math.sin(angle) + xOffset)) if 'y' in used: newLine += (' y{:.6f}'.format(params['y'] * math.cos(angle) + params['x'] * math.sin(angle) + yOffset)) if 'r' in used: newLine += (' r{:.6f}'.format(params['r'])) if 'i' in used: newLine += (' i{:.6f}'.format(params['i'] * math.cos(angle) - params['j'] * math.sin(angle))) if 'j' in used: newLine += (' j{:.6f}'.format(params['j'] * math.cos(angle) + params['i'] * math.sin(angle))) return ('{}\n'.format(newLine))
def remove_compiled_regex(txt: str, compiled_regex: re.compile, substitute: str = ""): """ Search for the compiled regex in the txt and either replace it with the substitute or remove it """ entities = compiled_regex.findall(txt) txt = compiled_regex.sub(substitute, txt) return txt, entities
def rotate(P, W, angle, xOffset, yOffset, line): REGCODE = COMPILE('([a-z]-?[0-9]+\.?([0-9]+)?)|\(.*\)') inLine = line.strip() comment = '' i = inLine.find('(') if i >= 0: comment = inLine[i:] inLine = inLine[:i - 1].strip() if len(inLine) > 0: parts = list([list(cmd)[0] for cmd in REGCODE.findall(inLine)]) if len(parts) == 0 or parts[0] not in ['g0', 'g1', 'g2', 'g3']: return line angle = math.radians(angle) params = { 'x': 0.0, 'y': 0.0, 'i': 0.0, 'j': 0.0, } used = '' for p in parts: for n in 'xyij': if n in p: if n == 'x': params['x'] = float(p.strip(n)) used += 'x' elif n == 'y': params['y'] = float(p.strip(n)) used += 'y' elif n == 'i': params['i'] = float(p.strip(n)) used += 'i' elif n == 'j': params['j'] = float(p.strip(n)) used += 'j' newLine = ('{}'.format(parts[0])) if not 'x' in used and not 'y' in used: P.dialogError = True P.dialog_error('ROTATE', 'Cannot decipher G-Code correctly') return None if 'x' in used: newLine += (' x{:.6f}'.format(params['x'] * math.cos(angle) - params['y'] * math.sin(angle) + xOffset)) if 'y' in used: newLine += (' y{:.6f}'.format(params['y'] * math.cos(angle) + params['x'] * math.sin(angle) + yOffset)) if parts[0] in {'g2', 'g3'}: newLine += (' i{:.6f}'.format(params['i'] * math.cos(angle) - params['j'] * math.sin(angle))) newLine += (' j{:.6f}'.format(params['j'] * math.cos(angle) + params['i'] * math.sin(angle))) return ('{}\n'.format(newLine))
new_name = txt_file + '.html' import sys, html from re import compile as Re import generate_utils try: from generate_utils import OutFileGreen as OutFile except ImportError: OutFile = open with open(txt_file) as file: string = file.read() R = Re('\d?\d:\d+\d+') A = R.split(string) B = R.findall(string) if not (A and A[0].strip() == ''): raise ValueError("Must begin with a time") assert len(A) == 1 + len( B), "the programmer did not understand re.split and re.findall" bits = [] for i in range(len(B)): b, a = A[i + 1], B[i] x, y = a.split(':') x, y = int(x), int(y) time = x * 60 + y title = html.escape(b) if with_times:
def processMails(self, text, att_file): """ Parse mail for display in XBMC """ myemail = email.message_from_string(text) p = EmailParser() msgobj = p.parsestr(text) if msgobj['Subject'] is not None: decodefrag = decode_header(msgobj['Subject']) subj_fragments = [] for s , enc in decodefrag: if enc: s = unicode(s , enc).encode('utf8','replace') subj_fragments.append(s) subject = ''.join(subj_fragments) else: subject = None if msgobj['Date'] is not None: date = msgobj['Date'] else: date = '--' Sujet = subject realname = parseaddr(msgobj.get('From'))[1] body = None html = None for part in msgobj.walk(): content_disposition = part.get("Content-Disposition", None) prog = re.compile('attachment') #Retrouve le nom des fichiers attaches if prog.search(str(content_disposition)): file_att = str(content_disposition) pattern = Pattern(r"\"(.+)\"") att_file += str(pattern.findall(file_att)) if part.get_content_type() == "text/plain": if body is None: body = "" try : #Si pas de charset défini if (part.get_content_charset() is None): body += part.get_payload(decode=True) else: body += unicode( part.get_payload(decode=True), part.get_content_charset(), 'replace' ).encode('utf8','replace') except Exception, e: body += "Erreur unicode" print "BODY = %s " % body elif part.get_content_type() == "text/html": if html is None: html = "" try : unicode_coded_entities_html = unicode(BeautifulStoneSoup(html, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)) html += unicode_coded_entities_html html = html2text(html) except Exception, e: html += "Erreur unicode html"