summaryrefslogtreecommitdiff
path: root/gerber/gerber.py
diff options
context:
space:
mode:
authorPaulo Henrique Silva <ph.silva@gmail.com>2013-12-18 02:57:11 -0200
committerPaulo Henrique Silva <ph.silva@gmail.com>2013-12-18 02:57:11 -0200
commit6aa1c0567d298dc14fa4ac51fd430d329f95d920 (patch)
treec194cf15c889a546ab9e7e1ead882c94b67f9fc9 /gerber/gerber.py
parentc1518b5dfe7c5265ffa2df60598a0db223263ecb (diff)
downloadgerbonara-6aa1c0567d298dc14fa4ac51fd430d329f95d920.tar.gz
gerbonara-6aa1c0567d298dc14fa4ac51fd430d329f95d920.tar.bz2
gerbonara-6aa1c0567d298dc14fa4ac51fd430d329f95d920.zip
Make gerber package
Diffstat (limited to 'gerber/gerber.py')
-rw-r--r--gerber/gerber.py129
1 files changed, 129 insertions, 0 deletions
diff --git a/gerber/gerber.py b/gerber/gerber.py
new file mode 100644
index 0000000..74da114
--- /dev/null
+++ b/gerber/gerber.py
@@ -0,0 +1,129 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import re
+
+def red(s):
+ return '\033[1;31m{0}\033[0;m'.format(s)
+
+class Statement:
+ pass
+
+class ParamStmt(Statement):
+ pass
+
+class CoordStmt(Statement):
+ pass
+
+class ApertureStmt(Statement):
+ pass
+
+class CommentStmt(Statement):
+ def __init__(self, comment):
+ self.comment = comment
+
+class EofStmt(Statement):
+ pass
+
+class UnexpectedStmt(Statement):
+ def __init__(self, line):
+ self.line = line
+
+class GerberContext:
+ x = 0
+ y = 0
+
+class Gerber:
+ NUMBER = r"[\+-]?\d+"
+ FUNCTION = r"G\d{2}"
+ STRING = r"[a-zA-Z0-9_+-/!?<>”’(){}.\|&@# :]+"
+
+ COORD_OP = r"D[0]?[123]"
+
+ PARAM_STMT = re.compile(r"%.*%")
+
+ COORD_STMT = re.compile((
+ r"(?P<f>{f})?"
+ r"(X(?P<x>{number}))?(Y(?P<y>{number}))?"
+ r"(I(?P<i>{number}))?(J(?P<j>{number}))?"
+ r"(?P<op>{op})?\*".format(number=NUMBER, f=FUNCTION, op=COORD_OP)))
+
+ APERTURE_STMT = re.compile(r"(G54)?D\d+\*")
+
+ COMMENT_STMT = re.compile(r"G04(?P<comment>{string})\*".format(string=STRING))
+
+ EOF_STMT = re.compile(r"M02\*")
+
+ def __init__(self):
+ self.apertures = {}
+ self.ctx = GerberContext()
+
+ def parse(self, filename):
+
+ fp = open(filename, "r")
+ data = fp.readlines()
+
+ self.tokens = list(self.tokenize(data))
+ for token in self.tokens:
+ if isinstance(token, UnexpectedStmt):
+ print filename
+ print red("[UNEXPECTED TOKEN]")
+ print self.COORD_STMT.pattern
+ print token.line
+
+ def tokenize(self, data):
+ multiline = None
+
+ for i, line in enumerate(data):
+ # remove EOL
+ if multiline:
+ line = multiline + line.strip()
+ else:
+ line = line.strip()
+
+ # deal with multi-line parameters
+ if line.startswith("%") and not line.endswith("%"):
+ multiline = line
+ continue
+ else:
+ multiline = None
+
+ # parameter
+ match = self.PARAM_STMT.match(line)
+ if match:
+ yield ParamStmt()
+ continue
+
+ # coord
+ matches = self.COORD_STMT.finditer(line)
+ if matches:
+ for match in matches:
+ yield CoordStmt()
+ continue
+
+ # aperture selection
+ match = self.APERTURE_STMT.match(line)
+ if match:
+ yield ApertureStmt()
+ continue
+
+ # comment
+ match = self.COMMENT_STMT.match(line)
+ if match:
+ yield CommentStmt(match.groupdict("comment"))
+ continue
+
+ # eof
+ match = self.EOF_STMT.match(line)
+ if match:
+ yield EofStmt()
+ continue
+
+ yield UnexpectedStmt(line)
+
+if __name__ == "__main__":
+ import sys
+
+ for f in sys.argv[1:]:
+ g = Gerber()
+ g.parse(f)