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): class errors(Enum):
UNEXPECTED_EOF = "unexpected EOF" UNEXPECTED_EOF = "unexpected EOF"
UNKNOWN_RECORD = "unknown record encountered"
EXPECTED_HEADER = "expected file header" EXPECTED_HEADER = "expected file header"
EXPECTED_BGNLIB = "expected beginning of library" EXPECTED_BGNLIB = "expected beginning of library"
EXPECTED_LIBNAME = "Library name is missing" EXPECTED_LIBNAME = "Library name is missing"
@ -50,11 +51,14 @@ class Parser(Reader):
def next_token(self, throw=False): def next_token(self, throw=False):
self.token = self.read_record() self.token = self.read_record()
if not self.token and throw: if not self.token.len and throw:
raise ParserError(errors.UNEXPECTED_EOF) 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: # for DEBUG
# print(self.token.ident.name) #if self.token:
# print(self.token.ident.name, self.token.len)
return self.token return self.token
@ -131,6 +135,7 @@ class Parser(Reader):
raise ParserError(errors.EXPECTED_STRNAME) raise ParserError(errors.EXPECTED_STRNAME)
self.structure.name = self.read_ascii(self.token.len) self.structure.name = self.read_ascii(self.token.len)
print("structure")
while self.next_token() and self.token.ident != Records.ENDSTR: while self.next_token() and self.token.ident != Records.ENDSTR:
if self.token.ident == Records.BOUNDARY: if self.token.ident == Records.BOUNDARY:
@ -263,7 +268,11 @@ class Parser(Reader):
self.next_token(True) self.next_token(True)
if self.token.ident == Records.WIDTH: 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.next_token(True)
self.parse_strans(element.transformation) self.parse_strans(element.transformation)

View File

@ -105,11 +105,13 @@ class Reader(ProgressGetter):
def read_record(self): def read_record(self):
result = Record() result = Record()
result.len = self.read_ushort()
result.ident = self.read_short()
try: try:
result.len = self.read_ushort() result.ident = Records(result.ident)
result.ident = Records(self.read_ushort()) except ValueError as e:
except ValueError: result.ident = e
return None
result.len -= 4 # remove record header len result.len -= 4 # remove record header len
@ -117,14 +119,14 @@ class Reader(ProgressGetter):
def read_date(self): def read_date(self):
# date # date
year = self.read_ushort() year = max(self.read_ushort(),1) # year must be > then 1 for datetime
month = self.read_ushort() month = max(1, min(12, self.read_ushort())) # month must be in range 1 - 12
day = self.read_ushort() day = max(1, min(31, self.read_ushort())) # day must be in range 1 - 31
# time # time
hour = self.read_ushort() hour = max(0, min(23, self.read_ushort()))
minute = self.read_ushort() minute = max(0, min(59, self.read_ushort()))
second = 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) 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 # allow import from parent directory
sys.path.append(os.path.dirname(sys.path[0])) sys.path.append(os.path.dirname(sys.path[0]))
import gds import pygds as gds
global bar global bar
@ -46,8 +46,9 @@ for arg in glob.glob("{}/tests/*.gds".format(sys.path[0])):
print("linking structure references") print("linking structure references")
lib.link_all_refs(callback) lib.link_all_refs(callback)
except gds.ParserError as e: #except gds.ParserError as e:
print("parser error: {}".format(e)) # print("parser error: {}".format(e))
# exit(1)
except KeyboardInterrupt: except KeyboardInterrupt:
continue continue
finally: finally: