|
| 1 | +import argparse |
| 2 | +import configparser |
| 3 | + |
| 4 | +from .userio import UserIO |
| 5 | +from .helper import cpp_str_esc, cpp_img_esc, get_files_rec, shorten |
| 6 | +from .generator import * |
| 7 | + |
| 8 | + |
| 9 | +def command_version(userio, args): |
| 10 | + userio.print("Current version: " + __version__) |
| 11 | + |
| 12 | + |
| 13 | +def command_generate(userio, args): |
| 14 | + # Check port |
| 15 | + if args.port < 0 or args.port > 65535: |
| 16 | + userio.error("Invalid port!") |
| 17 | + |
| 18 | + # Check mode |
| 19 | + if args.mode.lower() != "wifinina": |
| 20 | + userio.error("Target mode not supported!\nSupported modes: wifinina") |
| 21 | + |
| 22 | + # Check templates folder |
| 23 | + if args.template == "": |
| 24 | + args.template = get_template_path() |
| 25 | + userio.print("Using build in template files at " + args.template, |
| 26 | + verbose=True) |
| 27 | + |
| 28 | + # Get SSID and password |
| 29 | + args.ssid, args.ssid_pass = get_ssid_pass(userio, args.quiet) |
| 30 | + |
| 31 | + # Pack meta data |
| 32 | + userio.section("Processing misc. data...") |
| 33 | + meta_data = { |
| 34 | + "mode": cpp_str_esc(args.mode), |
| 35 | + "ssid": cpp_str_esc(args.ssid), |
| 36 | + "pass": cpp_str_esc(args.ssid_pass), |
| 37 | + "port": str(args.port) |
| 38 | + } |
| 39 | + |
| 40 | + # Eventually create output |
| 41 | + generate(userio, args.input, args.output, args.template, meta_data) |
| 42 | + |
| 43 | + |
| 44 | +def command_init(userio, project_path, delete_block): |
| 45 | + userio.section("Generating hello world project") |
| 46 | + |
| 47 | + if not os.path.exists(project_path): |
| 48 | + userio.error("Path " + project_path + " is invalid or does not exist!") |
| 49 | + |
| 50 | + if len(get_files_rec(project_path)) > 0: |
| 51 | + userio.warn("Target folder (%s) is not empty!" |
| 52 | + % os.path.abspath(project_path)) |
| 53 | + |
| 54 | + userio.warn("Data will %sbe deleted by this action!" |
| 55 | + % ("" if delete_block else "not ")) |
| 56 | + |
| 57 | + userio.print("Press Enter to continue anyway. Ctrl+C to cancel!") |
| 58 | + try: |
| 59 | + input() |
| 60 | + except KeyboardInterrupt: |
| 61 | + return |
| 62 | + |
| 63 | + path_input = os.path.join(project_path, "input") |
| 64 | + path_template = os.path.join(project_path, "template") |
| 65 | + path_template_src = get_template_path() |
| 66 | + |
| 67 | + path_config = os.path.join(project_path, ".webduino-generator") |
| 68 | + path_config_file = os.path.join(project_path, "project.cfg") |
| 69 | + |
| 70 | + def delete_file_or_folder(target): |
| 71 | + if os.path.isfile(target): |
| 72 | + os.remove(target) |
| 73 | + else: |
| 74 | + shutil.rmtree(target) |
| 75 | + |
| 76 | + # Check before we start |
| 77 | + if os.path.exists(path_config): |
| 78 | + if delete_block: |
| 79 | + delete_file_or_folder(path_config) |
| 80 | + else: |
| 81 | + userio.error("Config folder " + path_config + " exists! Is there already a project here?") |
| 82 | + |
| 83 | + if os.path.exists(path_config_file): |
| 84 | + if delete_block: |
| 85 | + delete_file_or_folder(path_config_file) |
| 86 | + else: |
| 87 | + userio.error("Project file " + path_config_file + " exists! Is there already a project here?") |
| 88 | + |
| 89 | + userio.print("Creating project files") |
| 90 | + os.mkdir(path_config) |
| 91 | + with open(path_config_file, "w") as config_file: |
| 92 | + config_file.write("todo") |
| 93 | + |
| 94 | + def copy_tree_if_not_exists(src, target, name): |
| 95 | + if os.path.exists(target) and delete_block: |
| 96 | + delete_file_or_folder(target) |
| 97 | + if os.path.isfile(target): |
| 98 | + userio.error(name + " exists and is file!") |
| 99 | + if os.path.isdir(target): |
| 100 | + userio.warn(name + " exists! Do nothing!") |
| 101 | + return |
| 102 | + shutil.copytree(src, target) |
| 103 | + |
| 104 | + userio.print("Creating input files") |
| 105 | + copy_tree_if_not_exists(get_demo_path(), path_input, "Input folder") |
| 106 | + |
| 107 | + userio.print("Creating template files") |
| 108 | + copy_tree_if_not_exists(get_template_path(), path_template, "Template folder") |
| 109 | + |
| 110 | + userio.section("Project created successfully.") |
| 111 | + userio.print("Use 'webduino-generator build' to build your project.") |
| 112 | + |
| 113 | + |
| 114 | +def main(): |
| 115 | + install_traceback() |
| 116 | + userio = UserIO() |
| 117 | + |
| 118 | + # |
| 119 | + # Parser input |
| 120 | + # |
| 121 | + parser = argparse.ArgumentParser(prog="webduino-generator", |
| 122 | + description="Webduino source builder") |
| 123 | + |
| 124 | + subparsers = parser.add_subparsers(dest="command") |
| 125 | + |
| 126 | + parser_generate = subparsers.add_parser("generate", help="Generate Arduino code from input folder without a project") |
| 127 | + parser_generate.add_argument("input", metavar="input", type=str, |
| 128 | + help="Input folder") |
| 129 | + parser_generate.add_argument("-t", "--template", metavar="folder", type=str, |
| 130 | + default="", dest='template', |
| 131 | + help="location of the template folder (build in is used if no path is supplied)") |
| 132 | + parser_generate.add_argument("-s", "--ssid", metavar="ssid", type=str, |
| 133 | + default="", dest='ssid', |
| 134 | + help="SSID of network") |
| 135 | + parser_generate.add_argument("-p", "--port", metavar="port", type=int, |
| 136 | + default=80, dest='port', |
| 137 | + help="Port of webserver") |
| 138 | + parser_generate.add_argument("-m", "--mode", metavar="mode", type=str, |
| 139 | + default="wifinina", dest='mode', |
| 140 | + help="Connection mode/library to be used") |
| 141 | + parser_generate.add_argument("-q", "--quiet", |
| 142 | + action="store_true", dest='quiet', |
| 143 | + help="Hides password warning") |
| 144 | + parser_generate.add_argument("-o", "--output", metavar="folder", type=str, |
| 145 | + default=".", dest='output', |
| 146 | + help="location of the output folder (default: ./output/)") |
| 147 | + |
| 148 | + parser_init = subparsers.add_parser("init", help="Create new project in current working directory") |
| 149 | + parser_init.add_argument("target", metavar="target", type=str, |
| 150 | + default=".", nargs="?", |
| 151 | + help="Target folder where project will be created") |
| 152 | + parser_init.add_argument("-f", "--force", |
| 153 | + action="store_true", dest='force', |
| 154 | + help="Delete files that block project creation.") |
| 155 | + |
| 156 | + parser_build = subparsers.add_parser("build", help="Generate Arduino code from current project") |
| 157 | + parser_compile = subparsers.add_parser("compile", help="Compile Arduino code from current project") |
| 158 | + parser_upload = subparsers.add_parser("upload", help="Upload Arduino code from current project") |
| 159 | + parser_open = subparsers.add_parser("open", help="Open generated code in arduino ide") |
| 160 | + parser_version = subparsers.add_parser("version", help="Display current version") |
| 161 | + |
| 162 | + # Global arguments |
| 163 | + for subparser in subparsers.choices.values(): |
| 164 | + group = subparser.add_argument_group("global arguments") |
| 165 | + group.add_argument("-v", "--verbose", |
| 166 | + action="store_true", dest='verbose', |
| 167 | + help="Enable verbose output") |
| 168 | + |
| 169 | + # |
| 170 | + # Check arguments |
| 171 | + # |
| 172 | + args = parser.parse_args() |
| 173 | + userio.verbose = args.verbose |
| 174 | + |
| 175 | + userio.print("[bold]Stone Labs. Webduino Gernerator\n") |
| 176 | + |
| 177 | + userio.print("Dumping arguments", verbose=True) |
| 178 | + userio.quick_table("", ["Argument", "Value"], |
| 179 | + [[arg, getattr(args, arg)] for arg in vars(args)], |
| 180 | + verbose=True) |
| 181 | + |
| 182 | + # Check command |
| 183 | + if args.command is None: |
| 184 | + userio.error("No command specified!") |
| 185 | + elif args.command == "version": |
| 186 | + command_version(userio, args) |
| 187 | + elif args.command == "init": |
| 188 | + command_init(userio, args.target, args.force) |
| 189 | + elif args.command == "build": |
| 190 | + raise NotImplementedError |
| 191 | + elif args.command == "compile": |
| 192 | + raise NotImplementedError |
| 193 | + elif args.command == "upload": |
| 194 | + raise NotImplementedError |
| 195 | + elif args.command == "open": |
| 196 | + raise NotImplementedError |
| 197 | + elif args.command == "generate": |
| 198 | + command_generate(userio, args) |
| 199 | + else: |
| 200 | + userio.error("Unknown command. This should never happen!") |
| 201 | + |
| 202 | + |
| 203 | +if __name__ == "__main__": |
| 204 | + main() |
0 commit comments