def test_encode(self): m = PVLModule(a=PVLGroup(g1=2, g2=3.4), b='c') no_convert = PDSLabelEncoder(convert_group_to_object=False) self.assertRaises(ValueError, no_convert.encode, m) s = '''OBJECT = a\r G1 = 2\r G2 = 3.4\r END_OBJECT = a\r B = c\r END\r\n''' self.assertEqual(s, self.e.encode(m)) m = PVLModule(a='b', staygroup=PVLGroup(c='d'), obj=PVLGroup(d='e', f=PVLGroup(g='h'))) s = '''A = b\r GROUP = staygroup\r C = d\r END_GROUP = staygroup\r OBJECT = obj\r D = e\r GROUP = f\r G = h\r END_GROUP = f\r END_OBJECT = obj\r END\r\n''' self.assertEqual(s, self.e.encode(m))
def test_parse_module(self): groups = (('a = b c = d END', PVLModule(a='b', c='d')), ('a =b GROUP = g f=g END_GROUP END', PVLModule(a='b', g=PVLGroup(f='g'))), ('GROUP = g f=g END_GROUP END', PVLModule(g=PVLGroup(f='g'))), ('GROUP = g f=g END_GROUP a = b OBJECT = o END_OBJECT END', PVLModule(g=PVLGroup(f='g'), a='b', o=PVLObject()))) for g in groups: with self.subTest(groups=g): tokens = Lexer(g[0]) self.assertEqual(g[1], self.p.parse_module(tokens)) tokens = Lexer('blob') self.assertRaises(ParseError, self.p.parse_module, tokens) tokens = Lexer('blob =') self.assertRaises(ParseError, self.p.parse_module, tokens) tokens = Lexer('GROUP GROUP') self.assertRaises(LexerError, self.p.parse_module, tokens) tokens = Lexer('BEGIN_OBJECT = foo END_OBJECT = bar') self.assertRaises(ValueError, self.p.parse_module, tokens) tokens = Lexer(""" mixed = 'mixed"\\'quotes' number = '123' """) self.assertRaises(LexerError, self.p.parse_module, tokens)
def test_parse(self): groups = (('a = b c = d END', PVLModule(a='b', c='d')), ('a =b GROUP = g f=g END_GROUP END', PVLModule(a='b', g=PVLGroup(f='g'))), ('GROUP = g f=g END_GROUP END', PVLModule(g=PVLGroup(f='g'))), ('GROUP = g f=g END_GROUP a = b OBJECT = o END_OBJECT END', PVLModule(g=PVLGroup(f='g'), a='b', o=PVLObject()))) for g in groups: with self.subTest(groups=g): self.assertEqual(g[1], self.p.parse(g[0])) self.assertRaises(ParseError, self.p.parse, 'blob')
def test_parse_module_post_hook(self): m = PVLModule(a='b') tokens = Lexer('c = d', g=self.p.grammar, d=self.p.decoder) self.assertRaises(Exception, self.p.parse_module_post_hook, m, tokens) self.p.doc = ('a = b = d') m = PVLModule(a='b') tokens = Lexer('= d', g=self.p.grammar, d=self.p.decoder) mod = PVLModule(a=EmptyValueAtLine(0), b='d') self.assertEqual((mod, False), self.p.parse_module_post_hook(m, tokens)) self.p.doc = ('a = b =') m = PVLModule(a='b') tokens = Lexer('=', g=self.p.grammar, d=self.p.decoder) mod = PVLModule(a=EmptyValueAtLine(0), b=EmptyValueAtLine(0)) self.assertEqual((mod, False), self.p.parse_module_post_hook(m, tokens))
def test_encode(self): g = PVLGroup(a='b', c='d') s = 'a = b;\nc = d;\nEND;' self.assertEqual(s, self.e.encode(g)) m = PVLModule(foo=PVLGroup(a='b', c='d')) s = 'BEGIN_GROUP = foo;\n a = b;\n c = d;\nEND_GROUP = foo;\nEND;' self.assertEqual(s, self.e.encode(m)) m = PVLModule(foo=PVLGroup( a='b', c='d', newline='Should be quoted\nand two lines.')) s = '''BEGIN_GROUP = foo; a = b; c = d; newline = "Should be quoted and two lines."; END_GROUP = foo; END;''' self.assertEqual(s, self.e.encode(m))
def test_comments(self): some_pvl = (""" /* comment on line */ # here is a line comment /* here is a multi- line comment */ foo = bar /* comment at end of line */ weird/* in the */=/*middle*/comments baz = bang # end line comment End """) self.assertEqual(PVLModule(foo='bar', weird='comments', baz='bang'), self.p.parse(some_pvl))
def get_isis_translation(label): """ Compute the ISIS serial number for a given image using the input cube or the label extracted from the cube. Parameters ---------- label : dict or str A PVL dict object or file name to extract the PVL object from Returns ------- translation : dict A PVLModule object containing the extracted translation file """ # Instantiate a DB session if not already instantiated if not hasattr(plio, 'data_session'): print(get_data('data.db')) plio.data_session = setup_db_session(get_data('data.db')) # Grab the label is not already read if not isinstance(label, PVLModule): label = pvl.load(label) # Grab the spacecraft name and run it through the ISIS lookup spacecraft_name = find_in_dict(label, 'SpacecraftName') for row in plio.data_session.query(StringToMission).filter( StringToMission.key == spacecraft_name): spacecraft_name = row.value.lower() # Try and pull an instrument identifier try: instrumentid = find_in_dict(label, 'InstrumentId').capitalize() except: instrumentid = None translation = None # Grab the translation PVL object using the lookup for row in plio.data_session.query(Translations).filter( Translations.mission == spacecraft_name, Translations.instrument == instrumentid): # Convert the JSON back to a PVL object translation = PVLModule(row.translation) return translation
def test_encode_module(self): m = PVLModule(foo=PVLGroup(a='b', c='d')) s = 'BEGIN_GROUP = foo;\n a = b;\n c = d;\nEND_GROUP = foo;' self.assertEqual(s, self.e.encode_module(m))
def test_count_aggs(self): m = PVLModule(a=PVLGroup(), b=PVLObject(), c=PVLObject()) self.assertEqual((2, 1), self.e.count_aggs(m))