diff options
author | Paulo Henrique Silva <ph.silva@gmail.com> | 2015-10-14 15:37:02 -0300 |
---|---|---|
committer | Paulo Henrique Silva <ph.silva@gmail.com> | 2015-10-14 15:37:02 -0300 |
commit | 944c8329222b8c1166a4952df0ca553cbec71505 (patch) | |
tree | c9b4263852cf6e8a2f4003ac746011424d1edc69 /gerber/common.py | |
parent | b81c9d4bf96845ced3495eb158ec3a3c9e4dce3d (diff) | |
parent | 10d9028e1fdf7431baee73c7f1474d2134bac5fa (diff) | |
download | gerbonara-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/common.py')
-rw-r--r-- | gerber/common.py | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/gerber/common.py b/gerber/common.py index 78da2cd..50ba728 100644 --- a/gerber/common.py +++ b/gerber/common.py @@ -15,6 +15,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +from . import rs274x +from . import excellon +from .utils import detect_file_format + def read(filename): """ Read a gerber or excellon file and return a representative object. @@ -30,10 +34,9 @@ def read(filename): CncFile object representing the file, either GerberFile or ExcellonFile. Returns None if file is not an Excellon or Gerber file. """ - from . import rs274x - from . import excellon - from .utils import detect_file_format - fmt = detect_file_format(filename) + with open(filename, 'r') as f: + data = f.read() + fmt = detect_file_format(data) if fmt == 'rs274x': return rs274x.read(filename) elif fmt == 'excellon': @@ -41,3 +44,28 @@ def read(filename): else: raise TypeError('Unable to detect file format') +def loads(data): + """ Read gerber or excellon file contents from a string and return a + representative object. + + Parameters + ---------- + data : string + gerber or excellon file contents as a string. + + Returns + ------- + file : CncFile subclass + CncFile object representing the file, either GerberFile or + ExcellonFile. Returns None if file is not an Excellon or Gerber file. + """ + + fmt = detect_file_format(data) + if fmt == 'rs274x': + return rs274x.loads(data) + elif fmt == 'excellon': + return excellon.loads(data) + else: + raise TypeError('Unable to detect file format') + + |