def main(): parser = argparse.ArgumentParser(epilog=str_examples(),formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('-m', '--view-mode', type=str, default="list", help="View mode (list or tree)") parser.add_argument('-w', '--wildcard', type=str, default="*.nc", help="wildcards. Default *.nc") parser.add_argument('-f', '--filepaths', nargs="+", default=None, help="List of files.") #parser.add_argument('-r', '--recurse', type=bool, default=False, # help="Recursive mode.") parser.add_argument("dirpaths", nargs="*", help="List of directories.") # Parse the command line. try: options = parser.parse_args() except: show_examples_and_exit(error_code=1) if not options.dirpaths and not options.filepaths: options.dirpaths = [os.getcwd()] if options.view_mode in ["list", "l"]: app = wxapps.wxapp_listbrowser(dirpaths=options.dirpaths, filepaths=options.filepaths, wildcard=options.wildcard, ) elif options.view_mode in ["compare", "c"]: app = wxapps.wxapp_comparison(dirpaths=options.dirpaths, filepaths=options.filepaths, wildcard=options.wildcard, ) elif options.view_mode in ["tree", "t"]: app = wxapps.wxapp_dirbrowser(dirpath=options.dirpaths[0]) else: raise ValueError("Wrong view_mode %s" % options.view_mode) app.MainLoop() return 0
def main(): # Decorate argparse classes to add portable support for aliases in add_subparsers class MyArgumentParser(argparse.ArgumentParser): def add_subparsers(self, **kwargs): new = super(MyArgumentParser, self).add_subparsers(**kwargs) # Use my class new.__class__ = MySubParserAction return new class MySubParserAction(argparse._SubParsersAction): def add_parser(self, name, **kwargs): """Allows one to pass the aliases option even if this version of ArgumentParser does not support it.""" try: return super(MySubParserAction, self).add_parser(name, **kwargs) except Exception as exc: if "aliases" in kwargs: # Remove aliases and try again. kwargs.pop("aliases") return super(MySubParserAction, self).add_parser(name, **kwargs) else: # Wrong call. raise exc parser = MyArgumentParser(epilog=str_examples(),formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('--loglevel', default="ERROR", type=str, help="set the loglevel. Possible values: CRITICAL, ERROR (default), WARNING, INFO, DEBUG") parser.add_argument('-v', '--verbose', default=0, action='count', # -vv --> verbose=2 help='verbose, can be supplied multiple times to increase verbosity') # Create the parsers for the sub-commands. subparsers = parser.add_subparsers(dest='command', help='sub-command help', description="Valid subcommands") # Subparser for single command. p_files = subparsers.add_parser('files', aliases=["file", "f"], help="Run the specified file(s).") p_files.add_argument("filepaths", nargs="+", help="List of filepaths.") # Subparser for list command. p_list = subparsers.add_parser('list', aliases=["l"], help="List files in directory.") p_list.add_argument("dirpaths", nargs="+", help="List of filepaths.") p_list.add_argument('-w', '--wildcard', type=str, default="*.nc", help="wildcards. Default *.nc") # Subparser for tree command. p_tree = subparsers.add_parser('tree', aliases=["t"], help="Show files in directory tree.") p_tree.add_argument("dirpaths", nargs="+", help="List of filepaths.") p_tree.add_argument('-w', '--wildcard', type=str, default="*.nc", help="wildcards. Default *.nc") # Subparser for scan command. p_scan = subparsers.add_parser('scan', aliases=["s"], help="Show files in directory tree.") p_scan.add_argument("top", help="Top.") p_scan.add_argument('-w', '--wildcard', type=str, default="*.nc", help="wildcards. Default *.nc") p_scan.add_argument('--no-walk', default=False, action="store_true", help="Disable walk mode.") #p_scan.add_argument('-e', '--extension', type=str, default="GSR.nc", help="File extension to search for") #p_scan.add_argument('-t', '--type', type=string, default="", help="Recursive mode.") # Parse the command line. try: options = parser.parse_args() except Exception: show_examples_and_exit(error_code=1) if options.verbose: print("*** Command line options *** ") print(options) # loglevel is bound to the string value obtained from the command line argument. # Convert to upper case to allow the user to specify --loglevel=DEBUG or --loglevel=debug import logging numeric_level = getattr(logging, options.loglevel.upper(), None) if not isinstance(numeric_level, int): raise ValueError('Invalid log level: %s' % options.loglevel) logging.basicConfig(level=numeric_level) if options.command == "list": if not options.dirpaths: options.dirpaths = [os.getcwd()] wxapps.wxapp_listbrowser(dirpaths=options.dirpaths, #filepaths=options.filepaths, wildcard=options.wildcard).MainLoop() elif options.command == "tree": if not options.dirpaths: options.dirpaths = [os.getcwd()] wxapps.wxapp_dirbrowser(dirpath=options.dirpaths[0]).MainLoop() elif options.command == "files": # Dictionary mapping WX application classes to list of files to open. acls2files = appclasses_from_files(options.filepaths) if options.verbose: print(acls2files) for cls, files in acls2files.items(): cls(files).MainLoop() elif options.command == "scan": # Select the files to open. filepaths = select_files(options) # Dictionary mapping WX application classes to list of files to open. acls2files = appclasses_from_files(filepaths) if options.verbose: print(acls2files) for cls, files in acls2files.items(): cls(files).MainLoop() return 0
def main(): def str_examples(): examples = ( "\n" "Usage example:\n\n" "abiopen.py files foo_WFK.nc ==> Visualize the WFK file foo_WFK.nc\n" " (many other Abinit files are supported, just try!).\n" "abiopen.py list dirpath ==> Visualize all files in the directory dirpath (flat list mode) .\n" "abiopen.py tree dirpath ==> Visualize all files in the directory dirpath (tree mode).\n" "abiopen.py scan dirpath ==> Scan all the supported files in the given directory (recursive mode).\n" "abiopen.py scan dirpath -w *GSR.nc ==> Walk the directory tree starting from dirpath \n" " and open all the GSR.nc files encountered.\n" ) return examples def show_examples_and_exit(err_msg=None, error_code=1): """Display the usage of the script.""" sys.stderr.write(str_examples()) if err_msg: sys.stderr.write("Fatal Error\n" + err_msg + "\n") sys.exit(error_code) parser = argparse.ArgumentParser(epilog=str_examples(),formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('--loglevel', default="ERROR", type=str, help="set the loglevel. Possible values: CRITICAL, ERROR (default), WARNING, INFO, DEBUG") parser.add_argument('-v', '--verbose', default=0, action='count', # -vv --> verbose=2 help='verbose, can be supplied multiple times to increase verbosity') # Create the parsers for the sub-commands. subparsers = parser.add_subparsers(dest='command', help='sub-command help', description="Valid subcommands") # Subparser for single command. p_files = subparsers.add_parser('files', help="Run the specified file(s).") p_files.add_argument("filepaths", nargs="+", help="List of filepaths.") # Subparser for list command. p_list = subparsers.add_parser('list', help="List files in directory.") p_list.add_argument("dirpaths", nargs="+", help="List of filepaths.") p_list.add_argument('-w', '--wildcard', type=str, default="*.nc", help="wildcards. Default *.nc") # Subparser for tree command. p_tree = subparsers.add_parser('tree', help="Show files in directory tree.") p_tree.add_argument("dirpaths", nargs="+", help="List of filepaths.") p_tree.add_argument('-w', '--wildcard', type=str, default="*.nc", help="wildcards. Default *.nc") # Subparser for scan command. p_scan = subparsers.add_parser('scan', help="Show files in directory tree.") p_scan.add_argument("top", help="Top.") p_scan.add_argument('-w', '--wildcard', type=str, default="*.nc", help="wildcards. Default *.nc") p_scan.add_argument('--no-walk', default=False, action="store_true", help="Disable walk mode.") #p_scan.add_argument('-e', '--extension', type=str, default="GSR.nc", help="File extension to search for") #p_scan.add_argument('-t', '--type', type=string, default="", help="Recursive mode.") # Subparser for single command. p_ipython = subparsers.add_parser('ipython', help="Open file in ipython shell.") p_ipython.add_argument('--argv', nargs="?", default="", type=shlex.split, help="Command-line options passed to ipython. Must be enclosed by quotes. " "Example: --argv='--matplotlib=wx'") p_ipython.add_argument("filepath", help="File to open.") # Parse the command line. try: options = parser.parse_args() except Exception: show_examples_and_exit(error_code=1) if options.verbose: print("*** Command line options *** ") print(options) # loglevel is bound to the string value obtained from the command line argument. # Convert to upper case to allow the user to specify --loglevel=DEBUG or --loglevel=debug import logging numeric_level = getattr(logging, options.loglevel.upper(), None) if not isinstance(numeric_level, int): raise ValueError('Invalid log level: %s' % options.loglevel) logging.basicConfig(level=numeric_level) if options.command == "ipython": # Start ipython shell with namespace abifile = abilab.abiopen(options.filepath) import IPython # USe embed because I don't know how to show a header with start_ipython. IPython.embed(header="The Abinit file object is bound to the `abifile` variable.") #IPython.start_ipython(argv=options.argv, # user_ns={"abifile": abifile}, # banner="hello", # banner1="hello1", # header="hello_header", # ) # return 0 elif options.command == "list": if not options.dirpaths: options.dirpaths = [os.getcwd()] wxapps.wxapp_listbrowser(dirpaths=options.dirpaths, #filepaths=options.filepaths, wildcard=options.wildcard).MainLoop() elif options.command == "tree": if not options.dirpaths: options.dirpaths = [os.getcwd()] wxapps.wxapp_dirbrowser(dirpath=options.dirpaths[0]).MainLoop() elif options.command == "files": # Dictionary mapping WX application classes to list of files to open. acls2files = appclasses_from_files(options.filepaths) if options.verbose: print(acls2files) for cls, files in acls2files.items(): cls(files).MainLoop() elif options.command == "scan": # Select the files to open. filepaths = select_files(options) # Dictionary mapping WX application classes to list of files to open. acls2files = appclasses_from_files(filepaths) if options.verbose: print(acls2files) for cls, files in acls2files.items(): cls(files).MainLoop() return 0
#!/usr/bin/env python import abipy import abipy.data import abipy.gui.wxapps as wxapps wxapps.wxapp_listbrowser(dirpaths=abipy.data.dirpath).MainLoop()
#!/usr/bin/env python import os import abipy import abipy.gui.wxapps as wxapps wxapps.wxapp_listbrowser(dirpaths=abipy.get_datadir()).MainLoop()