def init(self): parser = Parser(self.file_path) parser.parse() grid_factory = GridFactory(parser.get_parsed_data()) grid_factory.create_grid() self.grid = grid_factory.get_grid() self.constraint_service = ConstraintService(self.grid)
def extract(self, source): """Extract an image from *source*. If the image is supported an instance of PIL's Image is returned, otherwise None. """ p = Parser() f = open_pds(source) if self.log: self.log.debug("Parsing '%s'" % (source)) self.labels = p.parse(f) if self.log: self.log.debug("Found %d labels" % (len(self.labels))) if self._check_image_is_supported(): if self.log: self.log.debug("Image in '%s' is supported" % (source)) dim = self._get_image_dimensions() loc = self._get_image_location() if self.log: self.log.debug("Image dimensions should be %s" % (str(dim))) if self.log: self.log.debug("Seeking to image data at %d" % (loc)) f.seek(loc) if self.log: self.log.debug("Seek successful, reading data") # rawImageData = f.readline() # f.seek(-int(self.labels["RECORD_BYTES"]), os.SEEK_CUR) rawImageData = f.read(dim[0] * dim[1]) if self.log: self.log.debug("Read successful (len: %d), creating Image object" % (len(rawImageData))) # The frombuffer defaults may change in a future release; # for portability, change the call to read: # frombuffer(mode, size, data, 'raw', mode, 0, 1). img = Image.frombuffer('L', dim, rawImageData, 'raw', 'L', 0, 1) if self.log: self.log.debug("Image result: %s" % (str(img))) if self.log: self.log.debug("Image info: %s" % (str(img.info))) if self.log: self.log.debug("Image size: %s" % (str(img.size))) else: if self.log: self.log.error("Image is not supported '%s'" % (source)) img = None f.close() return img, self.labels
def extract(self, source): """Extract an image from *source*. If the image is supported an instance of PIL's Image is returned, otherwise None. """ p = Parser() f = open_pds(source) if self.log: self.log.debug("Parsing '%s'" % (source)) self.labels = p.parse(f) if self.log: self.log.debug("Found %d labels" % (len(self.labels))) if self._check_image_is_supported(): if self.log: self.log.debug("Image in '%s' is supported" % (source)) dim = self._get_image_dimensions() loc = self._get_image_location() imageSampleBits = int(self.labels['IMAGE']['SAMPLE_BITS']) imageSampleType = self.labels['IMAGE']['SAMPLE_TYPE'] md5Checksum = self._get_image_checksum() if self.log: self.log.debug("Image dimensions should be %s" % (str(dim))) if self.log: self.log.debug("Seeking to image data at %d" % (loc)) f.seek(loc) if imageSampleBits == 8: readSize = dim[0] * dim[1] elif imageSampleBits == 16: readSize = dim[0] * dim[1] * 2 print readSize if self.log: self.log.debug("Seek successful, reading data (%s)" % (readSize)) # rawImageData = f.readline() # f.seek(-int(self.labels["RECORD_BYTES"]), os.SEEK_CUR) rawImageData = f.read(readSize) if md5Checksum: rawImageChecksum = hashlib.md5(rawImageData).hexdigest() checksumVerificationPassed = rawImageChecksum == md5Checksum and True or False if not checksumVerificationPassed: if self.log: self.log.debug("Secure hash verification failed") if self.raisesChecksumError: errorMessage = "Verification failed! Expected '%s' but got '%s'." % (md5Checksum, rawImageChecksum) raise ChecksumError, errorMessage else: if self.log: self.log.debug("Secure hash verification passed") if self.log: self.log.debug("Read successful (len: %d), creating Image object" % (len(rawImageData))) # The frombuffer defaults may change in a future release; # for portability, change the call to read: # frombuffer(mode, size, data, 'raw', mode, 0, 1). if (imageSampleBits == 16) and imageSampleType == ('MSB_INTEGER'): #img = Image.frombuffer('I', dim, rawImageData, 'raw', 'I;16BS', 0, 1) img = Image.frombuffer('F', dim, rawImageData, 'raw', 'F;16B', 0, 1) img = ImageMath.eval("convert(a/16.0, 'L')", a=img) else: img = Image.frombuffer('L', dim, rawImageData, 'raw', 'L', 0, 1) if self.log: self.log.debug("Image result: %s" % (str(img))) self.log.debug("Image info: %s" % (str(img.info))) self.log.debug("Image mode: %s" % (str(img.mode))) self.log.debug("Image size: %s" % (str(img.size))) else: if self.log: self.log.error("Image is not supported '%s'" % (source)) img = None f.close() return img, self.labels
def eval(self, code_string): try: ast = Parser.parse(code_string) return self.interpret(ast) except Exception as e: name = e.__class__.__name__ message = e.args[0] return '{0}: {1}'.format(name, message)
def parse(self, entrypoint): """ Parse the entry point Args: entrypoint (str): url of any readable tutorial from HOST """ if not is_valid_hostname(entrypoint): raise HostNameError(f'{entrypoint} is not a valid host name') meta = Parser.resolve_path( Parser.parse(url=entrypoint, section=Section.META), config.HOST) table_contents = Parser.resolve_path( Parser.parse(url=entrypoint, section=Section.TABLE_CONTENTS), config.HOST) name = self.__parse_tutorial_name(entrypoint) return Tutorial(name, meta, table_contents)
def test_parse_function_with_types(self): definition = "(fun ([x: Num] [y: Num]) (+ x y))" fun_node = Parser.parse(definition) fun_args = fun_node.args self.assertEqual(len(fun_args), 2) self.assertTrue(isinstance(fun_args[0], Argument)) self.assertEqual(fun_args[0].type, NumType()) self.assertEqual(fun_args[0].identifier, "x") self.assertEqual(fun_args[1].type, NumType()) self.assertEqual(fun_args[1].identifier, "y") self.assertTrue(isinstance(fun_node.body, App))
def extract(self, tutorial, urls=[], trace=True): """ Extracting content section from each given url Args: tutorial (document.Tutorial): tutorial object urls (list): urls trace (boolean): print the current url that is being parse """ if not isinstance(tutorial, Tutorial): return for url in urls: if trace: print(f'\t. {url}....................') content = Parser.parse(url=url, section=Section.CONTENT) tutorial.contents.append(content)
def extract(self, source): """Extract an image from *source*. If the image is supported an instance of PIL's Image is returned, otherwise None. """ p = Parser() f = open_pds(source) pdsdatadir, pdsfile = os.path.split(source) if self.log: self.log.debug("Parsing '%s'" % (source)) self.labels = p.parse(f) if self.log: self.log.debug("Found %d labels" % (len(self.labels))) if self._check_table_is_supported(): if self.log: self.log.debug("Table in '%s' is supported" % (source)) dim = self._get_table_dimensions() # Get the location of the table location = self._get_table_location().strip().replace('"', "") # location = os.path.join(pdsdatadir,location) # Get the structure of the table from the pointer struct_fname = self._get_table_structure().strip().replace('"', "") structurefile = getPdsFileName(struct_fname, pdsdatadir) sp = ColumnParser() s = open_pds(structurefile) slabels = sp.parse(s) columns = [] for l in slabels: columns.append(l["COLUMN"]["NAME"].strip().replace('"', "")) if self.log: self.log.debug("Found %d columns" % (len(columns))) if self.labels["TABLE"]["INTERCHANGE_FORMAT"] == "ASCII": locationfile = getPdsFileName(location, pdsdatadir) tbl = csv.DictReader(open(locationfile), fieldnames=columns, delimiter=" ") else: if self.log: self.log.error("Table is not supported '%s'" % (source)) tbl = None f.close() return tbl, self.labels
def extract(self, source): """Extract an image from *source*. If the image is supported an instance of PIL's Image is returned, otherwise None. """ p = Parser() f = open_pds(source) pdsdatadir, pdsfile = os.path.split(source) if self.log: self.log.debug("Parsing '%s'" % (source)) self.labels = p.parse(f) if self.log: self.log.debug("Found %d labels" % (len(self.labels))) if self._check_table_is_supported(): if self.log: self.log.debug("Table in '%s' is supported" % (source)) dim = self._get_table_dimensions() # Get the location of the table location = self._get_table_location().strip().replace("\"", "") #location = os.path.join(pdsdatadir,location) # Get the structure of the table from the pointer struct_fname = self._get_table_structure().strip().replace( "\"", "") structurefile = getPdsFileName(struct_fname, pdsdatadir) sp = ColumnParser() s = open_pds(structurefile) slabels = sp.parse(s) columns = [] for l in slabels: columns.append(l['COLUMN']['NAME'].strip().replace("\"", "")) if self.log: self.log.debug("Found %d columns" % (len(columns))) if self.labels['TABLE']['INTERCHANGE_FORMAT'] == 'ASCII': locationfile = getPdsFileName(location, pdsdatadir) tbl = csv.DictReader(open(locationfile), fieldnames=columns, delimiter=' ') else: if self.log: self.log.error("Table is not supported '%s'" % (source)) tbl = None f.close() return tbl, self.labels
def test_parseTableContents_validEntryPoint_returnStr(self): res = Parser.parse(url=config.ENTRYPOINT, section=Section.TABLE_CONTENTS) print('') self.assertIsInstance(res, str)
def test_ast_generation(self): self.assertTrue(isinstance(Parser.parse("false"), Boolean)) self.assertTrue(isinstance(Parser.parse("2"), Number)) self.assertTrue(isinstance(Parser.parse("0.2"), Number))