summaryrefslogtreecommitdiff
path: root/gerber/excellon.py
diff options
context:
space:
mode:
authorPaulo Henrique Silva <ph.silva@gmail.com>2015-10-14 15:37:02 -0300
committerPaulo Henrique Silva <ph.silva@gmail.com>2015-10-14 15:37:02 -0300
commit944c8329222b8c1166a4952df0ca553cbec71505 (patch)
treec9b4263852cf6e8a2f4003ac746011424d1edc69 /gerber/excellon.py
parentb81c9d4bf96845ced3495eb158ec3a3c9e4dce3d (diff)
parent10d9028e1fdf7431baee73c7f1474d2134bac5fa (diff)
downloadgerbonara-944c8329222b8c1166a4952df0ca553cbec71505.tar.gz
gerbonara-944c8329222b8c1166a4952df0ca553cbec71505.tar.bz2
gerbonara-944c8329222b8c1166a4952df0ca553cbec71505.zip
Merge pull request #41 from curtacircuitos/read_from_memory
Read from memory
Diffstat (limited to 'gerber/excellon.py')
-rwxr-xr-xgerber/excellon.py54
1 files changed, 43 insertions, 11 deletions
diff --git a/gerber/excellon.py b/gerber/excellon.py
index d89b349..7333a98 100755
--- a/gerber/excellon.py
+++ b/gerber/excellon.py
@@ -26,6 +26,11 @@ This module provides Excellon file classes and parsing utilities
import math
import operator
+try:
+ from cStringIO import StringIO
+except(ImportError):
+ from io import StringIO
+
from .excellon_statements import *
from .cam import CamFile, FileSettings
from .primitives import Drill
@@ -46,9 +51,28 @@ def read(filename):
"""
# File object should use settings from source file by default.
- settings = FileSettings(**detect_excellon_format(filename))
+ with open(filename, 'r') as f:
+ data = f.read()
+ settings = FileSettings(**detect_excellon_format(data))
return ExcellonParser(settings).parse(filename)
+def loads(data):
+ """ Read data from string and return an ExcellonFile
+ Parameters
+ ----------
+ data : string
+ string containing Excellon file contents
+
+ Returns
+ -------
+ file : :class:`gerber.excellon.ExcellonFile`
+ An ExcellonFile created from the specified file.
+
+ """
+ # File object should use settings from source file by default.
+ settings = FileSettings(**detect_excellon_format(data))
+ return ExcellonParser(settings).parse_raw(data)
+
class DrillHit(object):
def __init__(self, tool, position):
@@ -302,9 +326,12 @@ class ExcellonParser(object):
def parse(self, filename):
with open(filename, 'r') as f:
- for line in f:
- self._parse(line.strip())
-
+ data = f.read()
+ return self.parse_raw(data, filename)
+
+ def parse_raw(self, data, filename=None):
+ for line in StringIO(data):
+ self._parse(line.strip())
for stmt in self.statements:
stmt.units = self.units
return ExcellonFile(self.statements, self.tools, self.hits,
@@ -428,14 +455,13 @@ class ExcellonParser(object):
zeros=self.zeros, notation=self.notation)
-def detect_excellon_format(filename):
+def detect_excellon_format(data=None, filename=None):
""" Detect excellon file decimal format and zero-suppression settings.
Parameters
----------
- filename : string
- Name of the file to parse. This does not check if the file is actually
- an Excellon file, so do that before calling this.
+ data : string
+ String containing contents of Excellon file.
Returns
-------
@@ -449,10 +475,16 @@ def detect_excellon_format(filename):
detected_format = None
zeros_options = ('leading', 'trailing', )
format_options = ((2, 4), (2, 5), (3, 3),)
+
+ if data is None and filename is None:
+ raise ValueError('Either data or filename arguments must be provided')
+ if data is None:
+ with open(filename, 'r') as f:
+ data = f.read()
# Check for obvious clues:
p = ExcellonParser()
- p.parse(filename)
+ p.parse_raw(data)
# Get zero_suppression from a unit statement
zero_statements = [stmt.zeros for stmt in p.statements
@@ -485,8 +517,8 @@ def detect_excellon_format(filename):
settings = FileSettings(zeros=zeros, format=fmt)
try:
p = ExcellonParser(settings)
- p.parse(filename)
- size = tuple([t[1] - t[0] for t in p.bounds])
+ p.parse_raw(data)
+ size = tuple([t[0] - t[1] for t in p.bounds])
hole_area = 0.0
for hit in p.hits:
tool = hit.tool