diff options
Diffstat (limited to 'gerber/rs274x.py')
-rw-r--r-- | gerber/rs274x.py | 66 |
1 files changed, 39 insertions, 27 deletions
diff --git a/gerber/rs274x.py b/gerber/rs274x.py index 9fd63da..d9fd317 100644 --- a/gerber/rs274x.py +++ b/gerber/rs274x.py @@ -26,7 +26,7 @@ try: from cStringIO import StringIO except(ImportError): from io import StringIO - + from .gerber_statements import * from .primitives import * from .cam import CamFile, FileSettings @@ -49,8 +49,21 @@ def read(filename): def loads(data): + """ Generate a GerberFile object from rs274x data in memory + + Parameters + ---------- + data : string + string containing gerber file contents + + Returns + ------- + file : :class:`gerber.rs274x.GerberFile` + A GerberFile created from the specified file. + """ return GerberParser().parse_raw(data) + class GerberFile(CamFile): """ A class representing a single gerber file @@ -215,7 +228,7 @@ class GerberParser(object): def parse(self, filename): with open(filename, "rU") as fp: data = fp.read() - return self.parse_raw(data, filename=None) + return self.parse_raw(data, filename) def parse_raw(self, data, filename=None): lines = [line for line in StringIO(data)] @@ -254,27 +267,10 @@ class GerberParser(object): oldline = line continue - did_something = True # make sure we do at least one loop while did_something and len(line) > 0: did_something = False - # Region Mode - (mode, r) = _match_one(self.REGION_MODE_STMT, line) - if mode: - yield RegionModeStmt.from_gerber(line) - line = r - did_something = True - continue - - # Quadrant Mode - (mode, r) = _match_one(self.QUAD_MODE_STMT, line) - if mode: - yield QuadrantModeStmt.from_gerber(line) - line = r - did_something = True - continue - # coord (coord, r) = _match_one(self.COORD_STMT, line) if coord: @@ -292,14 +288,6 @@ class GerberParser(object): line = r continue - # comment - (comment, r) = _match_one(self.COMMENT_STMT, line) - if comment: - yield CommentStmt(comment["comment"]) - did_something = True - line = r - continue - # parameter (param, r) = _match_one_from_many(self.PARAM_STMT, line) @@ -350,6 +338,30 @@ class GerberParser(object): line = r continue + # Region Mode + (mode, r) = _match_one(self.REGION_MODE_STMT, line) + if mode: + yield RegionModeStmt.from_gerber(line) + line = r + did_something = True + continue + + # Quadrant Mode + (mode, r) = _match_one(self.QUAD_MODE_STMT, line) + if mode: + yield QuadrantModeStmt.from_gerber(line) + line = r + did_something = True + continue + + # comment + (comment, r) = _match_one(self.COMMENT_STMT, line) + if comment: + yield CommentStmt(comment["comment"]) + did_something = True + line = r + continue + # deprecated codes (deprecated_unit, r) = _match_one(self.DEPRECATED_UNIT, line) if deprecated_unit: |