def validate_files_exist(cls): errors = [] files_list = [ 'input.csv', 'digital_contacts.csv', 'dmr_id.csv', 'zones.csv', 'user.csv' ] for file_name in files_list: if not PathManager.input_path_exists(file_name): err = ValidationError(f"Cannot open file: `{file_name}`", None, file_name) errors.append(err) if len(errors) > 0: logging.error("--- FILE MISSING ERRORS, CANNOT CONTINUE ---") logging.info(f"Checked `{PathManager.get_input_path()}`") for err in errors: logging.error(f"\t\t{err.message}") logging.info( "Have you run `Wizard (new)` or `Migrations (update)` under `Dangerous Operations`?" ) else: logging.info("All necessary files found") return errors
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 _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 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 main(): logger = logging.getLogger() formatter = logging.Formatter(fmt='%(asctime)s.%(msecs)03d %(levelname)7s %(filename).6s:%(lineno)3s: %(message)s', datefmt="%Y-%m-%d %H:%M:%S") handler = logging.StreamHandler(stream=sys.stdout) handler.setFormatter(formatter) logger.setLevel(logging.INFO) logger.addHandler(handler) logging.warning("COMMAND LINE IS DEPRECATED. NEWER FEATURES MAY NOT BE SUPPORTED.") parser = argparse.ArgumentParser( prog='Ham Radio channel wizard', description='''Convert a ham channel list to various radio formats. All of these options can be chained to run multiple steps in order.''') parser.add_argument( '--clean', '-c', action='store_true', default=False, required=False, help='Destroys input and output directories along with all their contents.', ) parser.add_argument( '--migrate-check', action='store_true', default=False, required=False, help='Checks for outdated columns.', ) parser.add_argument( '--migrate', '-m', action='store_true', default=False, required=False, help='Safely updates your input files to the latest version.', ) parser.add_argument( '--migrate-cleanup', action='store_true', default=False, required=False, help='Removes .bak files created during migration process.', ) parser.add_argument( '--wizard', '-w', action='store_true', default=False, required=False, help='Runs the setup wizard and creates minimum needed files', ) parser.add_argument( '--input', '-i', default=False, required=False, help='Sets the input folder', ) parser.add_argument( '--output', '-o', default=False, required=False, help='Sets the output folder', ) parser.add_argument( '--force', '-f', action='store_true', default=False, required=False, help="Defaults to 'yes' for all prompts (DANGEROUS)", ) parser.add_argument( '--radios', '-r', choices=src.ham.util.radio_types.radio_choices(), default=[], nargs='+', help=f"""Target radios to create.""" ) parser.add_argument( '--version', action='store_true', default=False, required=False, help='Display app version.', ) parser.add_argument( '--debug', action='store_true', default=False, required=False, help='Enable debug logging.', ) arg_values = parser.parse_args() op_performed = False if arg_values.debug: logger.setLevel(logging.DEBUG) logging.debug("Logging level set to debug.") PathManager.set_input_path('in') if arg_values.input: PathManager.set_input_path(arg_values.input) logging.info(f"Input directory set to {PathManager.get_input_path()}") PathManager.set_output_path('out') if arg_values.output: PathManager.set_output_path(arg_values.output) logging.info(f"Input directory set to {PathManager.get_output_path()}") if arg_values.force: logging.warning("FORCE HAS BEEN SET. ALL PROMPTS WILL DEFAULT YES. Files may be destroyed.") if arg_values.clean: logging.info("Running cleanup.") wizard = Wizard() wizard.cleanup() op_performed = True if arg_values.wizard: logging.info("Running wizard.") wizard = Wizard() if PathManager.input_path_exists(''): logging.info(f"Your input directory is located at: `{PathManager.get_input_path()}`") logging.warning("INPUT DIRECTORY ALREADY EXISTS!! Input files will be overwritten. Continue? (y/n)[n]") prompt = input() if prompt != 'y': logging.info("Wizard cancelled") return else: logging.warning('Input directory will be overwritten') wizard.bootstrap(arg_values.force) op_performed = True if arg_values.migrate_check: logging.info("Running migration check") migrations = MigrationManager() migrations.log_check_migrations() op_performed = True if arg_values.migrate: logging.info("Running migration") migrations = MigrationManager() migrations.migrate() op_performed = True if arg_values.migrate_cleanup: logging.info("Running migration") migrations = MigrationManager() migrations.remove_backups() op_performed = True if arg_values.version: logging.info(f"App version {src.radio_sync_version.version}") src.radio_sync_version_check.check_version() op_performed = True if len(arg_values.radios) > 0: logging.info("Running radio generator.") radio_generator = RadioGenerator(arg_values.radios) radio_generator.generate_all_declared() op_performed = True if not op_performed: parser.print_usage() return