def test_list_creation(): module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert len(module) == 3 assert module['a'] == 1 assert module['b'] == 2 assert module.getlist('a') == [1, 3] with pytest.raises(KeyError): module['c'] assert module.get('c', 42) == 42 with pytest.raises(TypeError): pvl.PVLModule([], []) module = pvl.PVLModule(DictLike()) assert len(module) == 3 assert module['a'] == 42 assert module['b'] == 42 assert module.getlist('a') == [42, 42] with pytest.raises(KeyError): module['c']
def test_py2_items(): module = pvl.PVLModule() assert isinstance(module.items(), list) assert module.items() == [] assert isinstance(module.keys(), list) assert module.keys() == [] assert isinstance(module.values(), list) assert module.values() == [] module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert isinstance(module.items(), list) assert module.items() == [('a', 1), ('b', 2), ('a', 3)] assert isinstance(module.keys(), list) assert module.keys() == ['a', 'b', 'a'] assert isinstance(module.values(), list) assert module.values() == [1, 2, 3]
def test_delete(): module = pvl.PVLModule(a=1, b=2) assert len(module) == 2 assert module['a'] == 1 del module['a'] assert len(module) == 1 with pytest.raises(KeyError): module['a'] module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert len(module) == 3 assert module['a'] == 1 del module['a'] assert len(module) == 1 with pytest.raises(KeyError): module['a'] with pytest.raises(KeyError): del module['c']
def test_repr(): module = pvl.PVLModule() assert isinstance(repr(module), str) assert repr(module) == 'PVLModule([])' module = pvl.PVLModule(a=1) assert isinstance(repr(module), str)
def filterCNetPVL(self, path=None): """Filters the CNET file and adds Ignored point information.""" if len(self.IgnoredPoints) == 0: return if path is None: path = self.cnet_path p = pvl.load(str(path)) cn = pvl.PVLModule() badness = 0 for (k, v) in p["ControlNetwork"].items(): if k == "ControlPoint": if (v["PointId"] in self.IgnoredPoints and "Ignore" not in v.keys()): v.append("Ignore", True) badness += 1 logger.info("Ignoring point {}".format(v["PointId"])) cn.append(k, v) logger.info(f"{badness} point(s) ignored.") new_pvl = pvl.PVLModule(ControlNetwork=cn) with open(path, "w") as stream: pvl.dump(new_pvl, stream, encoder=pvl.encoder.ISISEncoder())
def test_len(): module = pvl.PVLModule() assert len(module) == 0 module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert len(module) == 3
def test_loads(self): some_pvl = ''' a = b GROUP = c c = d END_GROUP e =false END''' decoded = pvl.PVLModule(a='b', c=pvl.PVLGroup(c='d'), e=False) self.assertEqual(decoded, pvl.loads(some_pvl)) self.assertEqual(pvl.PVLModule(a='b'), pvl.loads('a=b'))
def test_loads(self): some_pvl = """ a = b GROUP = c c = d END_GROUP e =false END""" decoded = pvl.PVLModule(a="b", c=pvl.PVLGroup(c="d"), e=False) self.assertEqual(decoded, pvl.loads(some_pvl)) self.assertEqual(pvl.PVLModule(a="b"), pvl.loads("a=b"))
def test_iterators(): module = pvl.PVLModule() assert isinstance(iteritems(module), pvl._collections.MappingView) assert list(iteritems(module)) == [] assert len(iteritems(module)) == 0 assert isinstance(repr(iteritems(module)), str) assert ('a', 1) not in iteritems(module) assert isinstance(iterkeys(module), pvl._collections.MappingView) assert list(iterkeys(module)) == [] assert len(iterkeys(module)) == 0 assert isinstance(repr(iterkeys(module)), str) assert 'a' not in iterkeys(module) assert isinstance(itervalues(module), pvl._collections.MappingView) assert list(itervalues(module)) == [] assert len(itervalues(module)) == 0 assert isinstance(repr(itervalues(module)), str) assert 1 not in itervalues(module) module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert isinstance(iteritems(module), pvl._collections.MappingView) assert list(iteritems(module)) == [('a', 1), ('b', 2), ('a', 3)] assert len(iteritems(module)) == 3 assert isinstance(repr(iteritems(module)), str) assert ('a', 1) in iteritems(module) assert ('b', 2) in iteritems(module) assert ('a', 3) in iteritems(module) assert ('c', 4) not in iteritems(module) assert isinstance(iterkeys(module), pvl._collections.MappingView) assert list(iterkeys(module)) == ['a', 'b', 'a'] assert len(iterkeys(module)) == 3 assert isinstance(repr(iterkeys(module)), str) assert 'a' in iterkeys(module) assert 'b' in iterkeys(module) assert 'c' not in iterkeys(module) assert isinstance(itervalues(module), pvl._collections.MappingView) assert list(itervalues(module)) == [1, 2, 3] assert len(itervalues(module)) == 3 assert isinstance(repr(itervalues(module)), str) assert 1 in itervalues(module) assert 2 in itervalues(module) assert 3 in itervalues(module) assert 4 not in itervalues(module)
def setUp(self): self.simple = data_dir / "pds3" / "simple_image_1.lbl" rawurl = "https://raw.githubusercontent.com/planetarypy/pvl/main/" self.url = rawurl + str(self.simple) self.simplePVL = pvl.PVLModule( { "PDS_VERSION_ID": "PDS3", "RECORD_TYPE": "FIXED_LENGTH", "RECORD_BYTES": 824, "LABEL_RECORDS": 1, "FILE_RECORDS": 601, "^IMAGE": 2, "IMAGE": pvl.PVLObject( { "LINES": 600, "LINE_SAMPLES": 824, "SAMPLE_TYPE": "MSB_INTEGER", "SAMPLE_BITS": 8, "MEAN": 51.67785396440129, "MEDIAN": 50.0, "MINIMUM": 0, "MAXIMUM": 255, "STANDARD_DEVIATION": 16.97019, "CHECKSUM": 25549531, } ), } )
def setUp(self): self.simple = data_dir / 'pds3' / 'simple_image_1.lbl' self.simplePVL = pvl.PVLModule({ 'PDS_VERSION_ID': 'PDS3', 'RECORD_TYPE': 'FIXED_LENGTH', 'RECORD_BYTES': 824, 'LABEL_RECORDS': 1, 'FILE_RECORDS': 601, '^IMAGE': 2, 'IMAGE': pvl.PVLObject({ 'LINES': 600, 'LINE_SAMPLES': 824, 'SAMPLE_TYPE': 'MSB_INTEGER', 'SAMPLE_BITS': 8, 'MEAN': 51.67785396440129, 'MEDIAN': 50.0, 'MINIMUM': 0, 'MAXIMUM': 255, 'STANDARD_DEVIATION': 16.97019, 'CHECKSUM': 25549531 }) })
def test_discard(): module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert len(module) == 3 assert module['a'] == 1 module.discard('a') assert len(module) == 1 assert module.getlist('a') == [] with pytest.raises(KeyError): module['a'] assert module['b'] == 2 module.discard('b') assert len(module) == 0 with pytest.raises(KeyError): module['b'] module.discard('c') assert len(module) == 0
def test_conversion(): module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) expected_dict = { 'a': [1, 3], 'b': [2], } expected_list = [ ('a', 1), ('b', 2), ('a', 3), ] # This is the one failing test from pvl 0.3: # assert dict(module) == expected_dict # What you get is: # >>> print(dict(module)) # {'a': 1, 'b': 2} # Since the OrderedMultiDict subclasses from dict, it is a 'mapping # object' and Python makes it into a dict with unique keys, inevitably # loosing the double value of 'a'. There does not seem to be any # functionality within the pvl library that requires this test to pass. assert list(module) == expected_list
def test_pop(): module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert len(module) == 3 assert module.pop('a') == 1 assert len(module) == 1 with pytest.raises(KeyError): module['a'] with pytest.raises(KeyError): module.pop('a') assert module.pop('a', 42) == 42 assert module.pop('b') == 2 assert len(module) == 0 with pytest.raises(KeyError): module.pop('b') with pytest.raises(KeyError): module['b'] assert module.pop('b', 42) == 42 with pytest.raises(KeyError): module.pop('c') assert module.pop('c', 42) == 42
def setUp(self): self.module = pvl.PVLModule( a="b", staygroup=pvl.PVLGroup(c="d"), obj=pvl.PVLGroup(d="e", f=pvl.PVLGroup(g="h")), ) self.string = """A = b\r
def test_empty(): module = pvl.PVLModule() assert len(module) == 0 assert module.get('c', 42) == 42 with pytest.raises(KeyError): module['c']
def test_get_index_for_insert(key, instance, expected_index): module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) module._get_index_for_insert(key, instance) == expected_index
def test_special_strings(): module = pvl.PVLModule([ ("single_quote", "'"), ("double_quote", '"'), # ('mixed_quotes', '"\''), # see above about escaped quotes. ]) assert module == pvl.loads( pvl.dumps(module, encoder=pvl.encoder.PVLEncoder()))
def test_py3_items(): module = pvl.PVLModule() assert isinstance(module.items(), pvl._collections.ItemsView) with pytest.raises(IndexError): module.items()[0] assert isinstance(module.keys(), pvl._collections.KeysView) with pytest.raises(IndexError): module.keys()[0] assert isinstance(module.values(), pvl._collections.ValuesView) with pytest.raises(IndexError): module.values()[0] module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert isinstance(module.items(), pvl._collections.ItemsView) items = module.items() assert items[0] == ('a', 1) assert items[1] == ('b', 2) assert items[2] == ('a', 3) assert items.index(('a', 1)) == 0 assert items.index(('b', 2)) == 1 assert items.index(('a', 3)) == 2 assert isinstance(module.keys(), pvl._collections.KeysView) keys = module.keys() assert keys[0] == 'a' assert keys[1] == 'b' assert keys[2] == 'a' assert keys.index('a') == 0 assert keys.index('b') == 1 assert isinstance(module.values(), pvl._collections.ValuesView) values = module.values() assert values[0] == 1 assert values[1] == 2 assert values[2] == 3 assert values.index(1) == 0 assert values.index(2) == 1 assert values.index(3) == 2
def create_pvl_header(self, version, headerstartbyte, networkid, targetname, description, username, buffer_header_size, points_bytes, creation_date, modified_date): """ Create the PVL header object Parameters ---------- version : int The current ISIS version to write, defaults to 2 headerstartbyte : int The seek offset that the protocol buffer header starts at networkid : str The name of the network targetname : str The name of the target, e.g. Moon description : str A description for the network. username : str The name of the user / application that created the control network buffer_header_size : int Total size of the header in bytes points_bytes : int The total number of bytes all points require Returns ------- : object An ISIS compliant PVL header object """ encoder = pvl.encoder.IsisCubeLabelEncoder header_bytes = buffer_header_size points_start_byte = HEADERSTARTBYTE + buffer_header_size header = pvl.PVLModule([( 'ProtoBuffer', ({ 'Core': { 'HeaderStartByte': headerstartbyte, 'HeaderBytes': header_bytes, 'PointsStartByte': points_start_byte, 'PointsBytes': points_bytes }, 'ControlNetworkInfo': pvl.PVLGroup([('NetworkId', networkid), ('TargetName', targetname), ('UserName', username), ('Created', creation_date), ('LastModified', modified_date), ('Description', description), ('NumberOfPoints', self.npoints), ('NumberOfMeasures', self.nmeasures), ('Version', version)]) }), )]) return pvl.dumps(header, cls=encoder)
def test_slice_access(): module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert module[0:3] == [('a', 1), ('b', 2), ('a', 3)] assert module[1:] == [('b', 2), ('a', 3)] assert module[:-1] == [('a', 1), ('b', 2)]
def test_dict_creation(): module = pvl.PVLModule({'a': 1, 'b': 2}) assert module['a'] == 1 assert module['b'] == 2 assert len(module) == 2 with pytest.raises(KeyError): module['c'] assert module.get('c', 42) == 42
def setUp(self): self.cub = data_dir / 'pattern.cub' self.cubpvl = pvl.PVLModule(IsisCube=pvl.PVLObject(Core=pvl.PVLObject( StartByte=65537, Format='Tile', TileSamples=128, TileLines=128, Dimensions=pvl.PVLGroup(Samples=90, Lines=90, Bands=1), Pixels=pvl.PVLGroup( Type='Real', ByteOrder='Lsb', Base=0.0, Multiplier=1.0))), Label=pvl.PVLObject(Bytes=65536))
def test_keyword_creation(): module = pvl.PVLModule(a=1, b=2) assert module['a'] == 1 assert module['b'] == 2 assert len(module) == 2 with pytest.raises(KeyError): module['c'] assert module.get('c', 42) == 42
def _create_label(self, array): """ Override PDS3Image._create_label() method to reflect desired label attributes. Parameters ---------- Returns ------- PVLModule label for the given NumPy array. """ if len(array.shape) == 3: bands = array.shape[0] lines = array.shape[1] line_samples = array.shape[2] else: bands = 1 lines = array.shape[0] line_samples = array.shape[1] record_bytes = line_samples * array.itemsize print("array.itemsize:", array.itemsize) label_module = pvl.PVLModule([ ('ODL_VERSION_ID', 'ODL3'), ('RECORD_TYPE', 'FIXED_LENGTH'), ('RECORD_BYTES', record_bytes), ('LABEL_RECORDS', 1), ('DATA_SET_ID', 'UNK'), ('PRODUCT_ID', 'UNK'), ('INSTRUMENT_HOST_NAME', 'MARS 2020'), ('INSTRUMENT_NAME', 'STEREOSIM'), ('TARGET_NAME', 'MARS'), ('START_TIME', 'UNK'), ('STOP_TIME', 'UNK'), ('SPACECRAFT_CLOCK_START_COUNT', 'UNK'), ('SPACECRAFT_CLOCK_STOP_COUNT', 'UNK'), ('PRODUCT_CREATION_TIME', 'UNK'), ('^IMAGE', 1), ('IMAGE', pvl.PVLModule([('BANDS', bands), ('BAND_STORAGE_TYPE', 'BAND_SEQUENTIAL'), ('FIRST_LINE', 1), ('FIRST_LINE_SAMPLE', 1), ('LINES', lines), ('LINE_SAMPLES', line_samples), ('SAMPLE_BITS', array.itemsize * 8), ('SAMPLE_TYPE', 'MSB_INTEGER')])) ]) return self._update_label(label_module, array)
def test_broken_labels(label, expected, expected_errors): with open(os.path.join(BROKEN_DIR, label), 'rb') as stream: module = pvl.load(stream, strict=False) expected = pvl.PVLModule(expected) assert module == expected assert module.errors == expected_errors assert not module.valid with open(os.path.join(BROKEN_DIR, label), 'rb') as stream: with pytest.raises(pvl.decoder.ParseError): pvl.load(stream, strict=True)
def test_key_access(): module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert module['a'] == 1 assert module['b'] == 2 with pytest.raises(KeyError): module['c']
def test_insert_after(expected_label, key, instance, expected_list, expected_value): module1 = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ('c', 5), ]) module2 = module1.copy() expected_module = pvl.PVLModule(expected_label) module1.insert_after(key, [('a', 4)], instance) assert expected_module == module1 assert module1['a'] == expected_value assert module1.getlist('a') == expected_list module2.insert_after(key, pvl.PVLModule([('a', 4)]), instance) assert module2 == expected_module assert module1['a'] == expected_value assert module1.getlist('a') == expected_list
def test_index_access(): module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) assert module[0] == ('a', 1) assert module[1] == ('b', 2) assert module[2] == ('a', 3) with pytest.raises(IndexError): module[3]
def test_copy(): module = pvl.PVLModule() copy = module.copy() assert module == copy assert module is not copy module['c'] = 42 assert module != copy module = pvl.PVLModule([ ('a', 1), ('b', 2), ('a', 3), ]) copy = module.copy() assert module == copy assert module is not copy module['c'] = 42 assert module != copy