summaryrefslogtreecommitdiff
path: root/gerber/rs274x.py
diff options
context:
space:
mode:
authorHamilton Kibbe <hamilton.kibbe@gmail.com>2015-10-10 16:51:21 -0400
committerHamilton Kibbe <hamilton.kibbe@gmail.com>2015-10-10 16:51:21 -0400
commitdd63b169f177389602e17bc6ced53bd0f1ba0de3 (patch)
tree12be6d968c97be78c3910b2c84b048211e88c7e2 /gerber/rs274x.py
parentb81c9d4bf96845ced3495eb158ec3a3c9e4dce3d (diff)
downloadgerbonara-dd63b169f177389602e17bc6ced53bd0f1ba0de3.tar.gz
gerbonara-dd63b169f177389602e17bc6ced53bd0f1ba0de3.tar.bz2
gerbonara-dd63b169f177389602e17bc6ced53bd0f1ba0de3.zip
Allow files to be read from strings per #37
Adds a loads() method to the top level module which generates a GerberFile or ExcellonFile from a string
Diffstat (limited to 'gerber/rs274x.py')
-rw-r--r--gerber/rs274x.py24
1 files changed, 14 insertions, 10 deletions
diff --git a/gerber/rs274x.py b/gerber/rs274x.py
index 1df3646..000f7a1 100644
--- a/gerber/rs274x.py
+++ b/gerber/rs274x.py
@@ -21,6 +21,7 @@
import copy
import json
import re
+from cStringIO import StringIO
from .gerber_statements import *
from .primitives import *
@@ -43,6 +44,9 @@ def read(filename):
return GerberParser().parse(filename)
+def loads(data):
+ return GerberParser().parse_raw(data)
+
class GerberFile(CamFile):
""" A class representing a single gerber file
@@ -75,7 +79,6 @@ class GerberFile(CamFile):
def __init__(self, statements, settings, primitives, filename=None):
super(GerberFile, self).__init__(statements, settings, primitives, filename)
-
@property
def comments(self):
return [comment.comment for comment in self.statements
@@ -205,12 +208,14 @@ class GerberParser(object):
self.quadrant_mode = 'multi-quadrant'
self.step_and_repeat = (1, 1, 0, 0)
-
def parse(self, filename):
- fp = open(filename, "r")
- data = fp.readlines()
+ with open(filename, "r") as fp:
+ data = fp.read()
+ return self.parse_raw(data, filename=None)
- for stmt in self._parse(data):
+ def parse_raw(self, data, filename=None):
+ lines = [line for line in StringIO(data)]
+ for stmt in self._parse(lines):
self.evaluate(stmt)
self.statements.append(stmt)
@@ -225,10 +230,10 @@ class GerberParser(object):
return json.dumps(stmts)
def dump_str(self):
- s = ""
+ string = ""
for stmt in self.statements:
- s += str(stmt) + "\n"
- return s
+ string += str(stmt) + "\n"
+ return string
def _parse(self, data):
oldline = ''
@@ -404,7 +409,6 @@ class GerberParser(object):
else:
raise Exception("Invalid statement to evaluate")
-
def _define_aperture(self, d, shape, modifiers):
aperture = None
if shape == 'C':
@@ -490,7 +494,7 @@ class GerberParser(object):
self.current_region = [Arc(start, end, center, self.direction, self.apertures[self.aperture], level_polarity=self.level_polarity, units=self.settings.units),]
else:
self.current_region.append(Arc(start, end, center, self.direction, self.apertures[self.aperture], level_polarity=self.level_polarity, units=self.settings.units))
-
+
elif self.op == "D02":
pass