def create_plugin(name, template): """Generate plugin in directory with sanitized *name* based upon *template*.""" name = sanitize_name(name) template_path = join_path(plugin_path, template) # Create, but do not overwrite, plugin directory if os.path.exists(name): tty.error("""Plugin directory "{}" already exists.""".format(name)) # Do a first pass to determine the template temp_files template_files = os.listdir(template_path) source_files = [] for temp_file in template_files: target_file = temp_file if temp_file.endswith('.template'): target_file = temp_file[0:-9] if temp_file.endswith('.cc.template'): source_files.append(target_file) tty.hline("""Creating "{}" with "{}" template.""".format(name, template)) os.mkdir(name) created_files = [] for source_file in template_files: target_file = source_file if source_file.endswith('.template'): target_file = source_file[0:-9] try: with open(join_path(template_path, source_file), 'r') as temp_file: contents = temp_file.read() except IOError as err: tty.error("""Unable to open {} template.""".format(source_file)) tty.error(err) sys.exit(1) contents = contents.replace('@plugin@', name) contents = contents.replace('@Plugin@', name.capitalize()) contents = contents.replace('@PLUGIN@', name.upper()) contents = contents.replace('@sources@', ' '.join(source_files)) try: with open(join_path(name, target_file), 'w') as temp_file: temp_file.write(contents) created_files.append(target_file) except IOError as err: tty.error("""Unable to create {}""".format(target_file)) tty.error(err) sys.exit(1) tty.info("Created plugin files (in {} as {}): ".format(name, template), ", ".join(created_files)) sys.exit(0)
def create_plugin(name: str, template: str) -> None: f"""Generate plugin in directory with sanitized *name* based upon *template*. Parameters ---------- name Name of plugin. Should not have any fancy characters or reserved keywords. template : {{{available_plugins}}} Which existing template to model off of. """ name = sanitize_name(name) template_path = join_path(plugin_path, template) # Create, but do not overwrite, plugin directory if os.path.exists(name): tty.error("""Plugin directory "{}" already exists.""".format(name)) # Do a first pass to determine the template temp_files template_files = os.listdir(template_path) source_files = [] for temp_file in template_files: target_file = temp_file if temp_file.endswith('.template'): target_file = temp_file[0:-9] if temp_file.endswith('.cc.template'): source_files.append(target_file) tty.hline("""Creating "{}" with "{}" template.""".format(name, template)) os.mkdir(name) created_files = [] for source_file in template_files: # Skip swp files if source_file.endswith(".swp"): continue target_file = source_file if source_file.endswith('.template'): target_file = source_file[0:-9] try: print(join_path(template_path, source_file)) with open(join_path(template_path, source_file), 'r') as temp_file: contents = temp_file.read() except IOError as err: tty.error("""Unable to open {} template.""".format(source_file)) tty.error(err) sys.exit(1) contents = contents.replace('@plugin@', name) contents = contents.replace('@Plugin@', name.capitalize()) contents = contents.replace('@PLUGIN@', name.upper()) contents = contents.replace('@sources@', ' '.join(source_files)) try: with open(join_path(name, target_file), 'w') as temp_file: temp_file.write(contents) created_files.append(target_file) except IOError as err: tty.error("""Unable to create {}""".format(target_file)) tty.error(err) sys.exit(1) tty.info("Created plugin files (in {} as {}): ".format(name, template), ", ".join(created_files)) sys.exit(0)
def create_plugin(args): """Generate plugin in sanitized directory of same name based upon *type*""" name = sanitize_name(args['new_plugin']) type = args['new_plugin_template'] template_path = join_path(plugin_path, type) # Create, but do not overwrite, plugin directory if os.path.exists(name): tty.error("""Plugin directory "{}" already exists.""".format(name)) # Do a first pass to determine the template files template_files = os.listdir(template_path) source_files = [] for file in template_files: target_file = file if file.endswith('.template'): target_file = file[0:-9] if file.endswith('.cc.template'): source_files.append(target_file) tty.hline("""Creating "{}" with "{}" template.""".format(name, type)) os.mkdir(name) created_files = [] for source_file in template_files: target_file = file if source_file.endswith('.template'): target_file = source_file[0:-9] try: with open(join_path(template_path, source_file), 'r') as file: contents = file.read() except IOError as err: tty.error("""Unable to open {} template.""".format(source_file)) tty.error(err) sys.exit(1) contents = contents.replace('@plugin@', name) contents = contents.replace('@Plugin@', name.capitalize()) contents = contents.replace('@PLUGIN@', name.upper()) contents = contents.replace('@sources@', ' '.join(source_files)) contents = contents.replace('@C@', config.c_compiler) contents = contents.replace('@CXX@', config.cxx_compiler) contents = contents.replace('@Fortran@', config.fortran_compiler) try: with open(join_path(name, target_file), 'w') as file: file.write(contents) created_files.append(target_file) except IOError as err: tty.error("""Unable to create {}""".format(target_file)) tty.error(err) sys.exit(1) tty.info("Created plugin files: ", ", ".join(created_files)) sys.exit(0)