def check_migrations_needed(self): not_needed_cols = dict() channels_file = PathManager.get_input_path('input.csv') channel_cols = dict(RadioChannelDefault.create_empty().__dict__) extra_cols = self._migration_check(channels_file, channel_cols) if len(extra_cols) > 0: not_needed_cols['input.csv'] = extra_cols contacts_file = PathManager.get_input_path('digital_contacts.csv') contact_cols = dict(DmrContactDefault.create_empty().__dict__) extra_cols = self._migration_check(contacts_file, contact_cols) if len(extra_cols) > 0: not_needed_cols['digital_contacts.csv'] = extra_cols zones_file = PathManager.get_input_path('zones.csv') zone_cols = dict(RadioZoneDefault.create_empty().__dict__) extra_cols = self._migration_check(zones_file, zone_cols) if len(extra_cols) > 0: not_needed_cols['zones.csv'] = extra_cols return not_needed_cols
def remove_backups(self): if not PathManager.input_path_exists(''): return files_list = os.listdir(PathManager.get_input_path()) for file_name in files_list: if re.search('\\.bak$', file_name): file_path = PathManager.get_input_path(file_name) logging.info(f'Removing backup `{file_path}`') os.remove(file_path) return
def _add_col(self, file_name, col_name, default_val): logging.info(f'Adding column `{col_name}` to `{file_name}`') reader = PathManager.open_input_file(f'{file_name}', 'r') cols = reader.readline().replace('\n', '').replace('\r', '').split(',') if cols == ['']: cols = [] if col_name not in cols: cols.append(col_name) reader.seek(0) writer = PathManager.open_input_file(f'{file_name}.tmp', 'w+') dict_writer = csv.DictWriter(writer, fieldnames=cols, dialect='unix', quoting=0) dict_reader = csv.DictReader(reader, fieldnames=cols) dict_writer.writeheader() for row in dict_reader: if dict_reader.line_num == 1: continue for k in row.keys(): if row[k] is None: row[k] = default_val dict_writer.writerow(row) reader.close() writer.close() original_name = PathManager.get_input_path(file_name) temp_name = f'{original_name}.tmp' os.remove(original_name) os.rename(temp_name, original_name) return
def _add_cols_to_file(self, file_name, cols): original_name = PathManager.get_input_path(file_name) backup_name = f'{original_name}.bak' temp_name = f'{original_name}.tmp' os.rename(original_name, backup_name) shutil.copyfile(backup_name, temp_name) for col in cols: self._add_col(f'{file_name}.tmp', col, '') os.rename(temp_name, original_name)
def _create_input(self): FileUtil.safe_create_dir(PathManager.get_input_path()) create_files = { 'channels': self._create_channel_file, 'digital_contacts': self._create_dmr_data, 'zones': self._create_zone_data, 'user': self._create_dmr_user_data, } for key in create_files: create_files[key]()
def bootstrap(self): self._create_input() self._create_output() abspath = PathManager.get_input_path() logging.info( f'''Wizard is complete! You may now open `input.csv` and add your radio channels. Input CSVs are located in `{abspath}` What each file does: input.csv: your radio channels. For best results, ONLY FILL OUT THE COLUMNS YOU NEED zones.csv: preset group that you would like your channel in (if radio supports multiple zones) DMR-ONLY FILES safe to ignore for analog radios: digital_contacts.csv: DMR contact IDs (e.g. Talkgroups) dmr_id.csv: Set your DMR id (from radioid.net) Be sure to check the 'help' menu for more guidance! Sample data has been added to each file as an example.''')
def migrate(self): existing_backups = False files_list = [] if PathManager.input_path_exists(''): files_list = os.listdir(PathManager.get_input_path()) for file_name in files_list: if re.search('\\.bak$', file_name): logging.warning(f'Existing backup file: `{PathManager.get_input_path(file_name)}`') existing_backups = True if existing_backups: logging.info('Backup files still exist. Please delete before continuing.') logging.info('MIGRATIONS HAVE NOT BEEN RUN') return self._migrate_one() self._migrate_two() self._migrate_three() self._migrate_four() self._migrate_five() logging.info('Migrations are complete. Your original files have been renamed to have a `.bak` extension.')
def _migrate_two(self): logging.info('Running migration step 2: Creating in.csv') if not PathManager.input_path_exists('input.csv'): f = PathManager.open_input_file('input.csv', 'w+') f.close() if not PathManager.input_path_exists('digital_contacts.csv'): f = PathManager.open_input_file('digital_contacts.csv', 'w+') f.close() if not PathManager.input_path_exists('zones.csv'): f = PathManager.open_input_file('zones.csv', 'w+') f.close() if not PathManager.input_path_exists('user.csv'): f = PathManager.open_input_file('user.csv', 'w+') f.close() user_columns = ['RADIO_ID', 'CALLSIGN', 'FIRST_NAME', 'LAST_NAME', 'CITY', 'STATE', 'COUNTRY', 'REMARKS'] self._add_cols_to_file(PathManager.get_input_path('user.csv'), user_columns) if not PathManager.input_path_exists('dmr_id.csv'): f = PathManager.open_input_file('dmr_id.csv', 'w+') f.close()
def select_input_folder_dialog(self, event): self._select_folder_dialog(event, 'Set input directory', PathManager.get_input_path(), self._select_input_folder)
def _migrate_five(self): logging.info("Running migration step 5: adding 'latitude' and 'longitude' columns") self._add_col(PathManager.get_input_path('input.csv'), 'latitude', None) self._add_col(PathManager.get_input_path('input.csv'), 'longitude', None)
def _migrate_four(self): logging.info("Running migration step 4: removing 'number' columns") self._delete_col(PathManager.get_input_path('input.csv'), 'number') self._delete_col(PathManager.get_input_path('dmr_id.csv'), 'number') self._delete_col(PathManager.get_input_path('digital_contacts.csv'), 'number')
def cleanup(self): FileUtil.safe_delete_dir(PathManager.get_input_path()) FileUtil.safe_delete_dir(PathManager.get_output_path())
def input_writer(cls, file_path, newline_char): input_path = PathManager.get_input_path(file_path) return RadioWriter(input_path, newline_char)