Parse the command portion of a gcode line, and return a dictionary of found codes and their respective values, and a list of found flags. Codes with integer values will have an integer type, while codes with float values will have a float type @param string command Command portion of a gcod
(command)
| 23 | |
| 24 | |
| 25 | def parse_command(command): |
| 26 | """ |
| 27 | Parse the command portion of a gcode line, and return a dictionary of found codes and their respective values, and a list of found flags. Codes with integer values will have an integer type, while codes with float values will have a float type |
| 28 | @param string command Command portion of a gcode line |
| 29 | @return tuple containing a dict of codes, list of flags. |
| 30 | """ |
| 31 | codes = {} |
| 32 | flags = [] |
| 33 | |
| 34 | pairs = command.split() |
| 35 | for pair in pairs: |
| 36 | code = pair[0] |
| 37 | |
| 38 | # If the code is not a letter, this is an error. |
| 39 | if not code.isalpha(): |
| 40 | gcode_error = makerbot_driver.Gcode.InvalidCodeError() |
| 41 | gcode_error.values['InvalidCode'] = code |
| 42 | raise gcode_error |
| 43 | |
| 44 | # Force the code to be uppercase. |
| 45 | code = code.upper() |
| 46 | |
| 47 | # If the code already exists, this is an error. |
| 48 | if code in codes.keys(): |
| 49 | gcode_error = makerbot_driver.Gcode.RepeatCodeError() |
| 50 | gcode_error.values['RepeatedCode'] = code |
| 51 | raise gcode_error |
| 52 | |
| 53 | # Don't allow both G and M codes in the same line |
| 54 | if (code == 'G' and 'M' in codes.keys()) or \ |
| 55 | (code == 'M' and 'G' in codes.keys()): |
| 56 | raise makerbot_driver.Gcode.MultipleCommandCodeError() |
| 57 | |
| 58 | # If the code doesn't have a value, we consider it a flag, and set it to true. |
| 59 | if len(pair) == 1: |
| 60 | flags.append(code) |
| 61 | |
| 62 | else: |
| 63 | try: |
| 64 | codes[code] = int(pair[1:]) |
| 65 | except exceptions.ValueError: |
| 66 | codes[code] = float(pair[1:]) |
| 67 | |
| 68 | return codes, flags |
| 69 | |
| 70 | |
| 71 | def parse_line(line): |