port to poetry for library managing

fix bug in text parsing
add better error reporting
This commit is contained in:
Julian Daube 2020-04-21 14:09:43 +02:00
parent dbda901499
commit dda0df7a2b
9 changed files with 48 additions and 18 deletions

View File

@ -3,6 +3,7 @@ from enum import Enum
class errors(Enum):
UNEXPECTED_EOF = "unexpected EOF"
UNKNOWN_RECORD = "unknown record encountered"
EXPECTED_HEADER = "expected file header"
EXPECTED_BGNLIB = "expected beginning of library"
EXPECTED_LIBNAME = "Library name is missing"
@ -50,11 +51,14 @@ class Parser(Reader):
def next_token(self, throw=False):
self.token = self.read_record()
if not self.token and throw:
if not self.token.len and throw:
raise ParserError(errors.UNEXPECTED_EOF)
if not isinstance(self.token.ident, Records):
raise ParserError("{}: {}".format(errors.UNKNOWN_RECORD.value, self.token.ident))
# if self.token:
# print(self.token.ident.name)
# for DEBUG
#if self.token:
# print(self.token.ident.name, self.token.len)
return self.token
@ -131,6 +135,7 @@ class Parser(Reader):
raise ParserError(errors.EXPECTED_STRNAME)
self.structure.name = self.read_ascii(self.token.len)
print("structure")
while self.next_token() and self.token.ident != Records.ENDSTR:
if self.token.ident == Records.BOUNDARY:
@ -148,7 +153,7 @@ class Parser(Reader):
if self.progress_callback:
self.progress_callback(self)
self.Library.structures[self.structure.name] = self.structure
self.structure = None
@ -263,7 +268,11 @@ class Parser(Reader):
self.next_token(True)
if self.token.ident == Records.WIDTH:
element.pathWidth = self.read_short()
if self.token.len == 2:
element.pathWidth = self.read_short()
elif self.token.len == 4:
element.pathWidth = self.read_int()
self.next_token(True)
self.parse_strans(element.transformation)

View File

@ -105,11 +105,13 @@ class Reader(ProgressGetter):
def read_record(self):
result = Record()
result.len = self.read_ushort()
result.ident = self.read_short()
try:
result.len = self.read_ushort()
result.ident = Records(self.read_ushort())
except ValueError:
return None
result.ident = Records(result.ident)
except ValueError as e:
result.ident = e
result.len -= 4 # remove record header len
@ -117,14 +119,14 @@ class Reader(ProgressGetter):
def read_date(self):
# date
year = self.read_ushort()
month = self.read_ushort()
day = self.read_ushort()
year = max(self.read_ushort(),1) # year must be > then 1 for datetime
month = max(1, min(12, self.read_ushort())) # month must be in range 1 - 12
day = max(1, min(31, self.read_ushort())) # day must be in range 1 - 31
# time
hour = self.read_ushort()
minute = self.read_ushort()
second = self.read_ushort()
hour = max(0, min(23, self.read_ushort()))
minute = max(0, min(59, self.read_ushort()))
second = max(0, min(59, self.read_ushort()))
return datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)

18
pyproject.toml Normal file
View File

@ -0,0 +1,18 @@
[tool.poetry]
name = "pygds"
version = "1.0.0"
description = "A simple gdsii stream importing library written in native python"
authors = ["Julian Daube <joposter@gmail.com>"]
repository = "https://git.dedaa.de/julixau/pygds"
homepage = "https://git.dedaa.de/julixau/pygds"
keywords = ["gds", "gdsii", "import"]
[tool.poetry.dependencies]
python = "^3.4"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

View File

@ -8,7 +8,7 @@ import glob
# allow import from parent directory
sys.path.append(os.path.dirname(sys.path[0]))
import gds
import pygds as gds
global bar
@ -46,8 +46,9 @@ for arg in glob.glob("{}/tests/*.gds".format(sys.path[0])):
print("linking structure references")
lib.link_all_refs(callback)
except gds.ParserError as e:
print("parser error: {}".format(e))
#except gds.ParserError as e:
# print("parser error: {}".format(e))
# exit(1)
except KeyboardInterrupt:
continue
finally: