def test_zero_entries(self): fields_def = OrderedDict([('text', {'len': 0}), ('empty', {'len': 0}), ('number', {'len': 6, 'decoder': decode_int, 'encoder': encode_int })]) fields = {'text': 'testfilename', 'number': 0} smrtf = SmartFilename(fields, fields_def, ext='.tif', compact=True) self.assertEqual(str(smrtf), 'testfilename__0.tif') smrtf_fn = SmartFilename.from_filename('fromfilename__0.tif', fields_def=fields_def, compact=True, convert=True) self.assertEqual(smrtf_fn['number'], 0)
def test_variable_length_fields(self): """ Test if required delimiter for field with variable length is detected. """ self.fields_def.update({'variable': {'len': 0, 'delim': ''}}) fields = {'pflag': 'M', 'dtime_1': '20180101120000'} with self.assertRaises(ValueError): smrtf = SmartFilename(fields, self.fields_def) fn = 'M_20180101120000.tif' with self.assertRaises(ValueError): smrtf = SmartFilename.from_filename(fn, self.fields_def)
def create_output_filepath(ds_def, gridname, sub_gridname, ftilename): """ Creates folder structure for the external dataset for a given tile id. Parameters ---------- ftile: str Tile id consisting of sub grid and tile name (e.g. EU500M_E048N012T6). Returns ------- tile_path: str File path to the tile (e.g. E048N012T6) folder. It has the following structure: [product id]/[product name]/sub grid]/[tile name] (e.g. DEMSlope/SRTM-VFP/EQUI7_EU500M/E048N012T6). """ ftilename_parts = ftilename.split('_') hierarchy = ['root', 'grid', 'tile'] levels = {'root': ds_def.root_dirpath, 'grid': gridname + "_" + sub_gridname, 'tile': ftilename_parts[1]} smart_path = SmartPath(levels, hierarchy, make_dir=True) fields = ds_def.fields_fixed fields['grid'] = ftilename_parts[0] fields['tile'] = ftilename_parts[1] smart_filename = SmartFilename(fields, ds_def.fields_def, ext='.tif') out_filepath = os.path.join(smart_path.get_dir(), str(smart_filename)) return out_filepath
def test_init_wrong_field_length(self): """ Test initialization with wrong field length. """ fields = {'pflag': 'ME'} with self.assertRaises(ValueError): SmartFilename(fields, self.fields_def, ext='.tif')
def test_init_undefined_field(self): """ Test initialization with undefined field name. """ fields = {'pflag': 'M', 'test_time': '20180101120000'} with self.assertRaises(KeyError): SmartFilename(fields, self.fields_def, ext='.tif')
def test_build_filename_with_ext(self): """ Test building file naming with extension. """ fields = {'pflag': 'M', 'dtime_1': '20180101120000'} smrtf = SmartFilename(fields, self.fields_def, ext='.tif') self.assertEqual(str(smrtf), 'M_20180101120000.tif')
def test_set_wrong_fields_len(self): """ Test setting field with wrong length. """ fields = {'pflag': 'M', 'dtime_1': '20180101120000'} smrtf = SmartFilename(fields, self.fields_def, ext='.tif') with self.assertRaises(ValueError): smrtf['pflag'] = 'MM'
def test_set_nonexisting_fields(self): """ Test setting field which is non-existing in definition. """ fields = {'pflag': 'M', 'dtime_1': '20180101120000'} smrtf = SmartFilename(fields, self.fields_def, ext='.tif') with self.assertRaises(KeyError): smrtf['new_field'] = 'test'
def test_set_and_get_fields(self): """ Test set and get file name fields. """ fields = {'pflag': 'M', 'dtime_1': '20180101120000'} smrtf = SmartFilename(fields, self.fields_def, ext='.tif') self.assertEqual(smrtf['pflag'], 'M') self.assertEqual(smrtf['dtime_1'], '20180101120000') smrtf['pflag'] = 'D' smrtf['dtime_1'] = '20180101130000' self.assertEqual(smrtf['pflag'], 'D') self.assertEqual(smrtf['dtime_1'], '20180101130000')