Created
May 12, 2015 02:34
-
-
Save qiuwch/1299c87317558b111e49 to your computer and use it in GitHub Desktop.
Utility python script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import argcomplete, argparse, requests, pprint | |
| import types | |
| import os, sys, pprint | |
| # http://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory | |
| cwd = os.getcwd() | |
| print cwd | |
| sys.path.append(cwd) | |
| import todo | |
| def check_port(port): | |
| import socket | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| try: | |
| result = sock.connect(('localhost', port)) | |
| # Successfully connected | |
| print('Somebody is using the port, very likely the server is already running.') | |
| return False | |
| except Exception as e: | |
| print e | |
| return True | |
| def complete_subparser_action(prefix, **kwargs): | |
| internal_actions = func_names | |
| return internal_actions | |
| #if prefix and any(action.startswith(prefix) for action in internal_actions): | |
| #return internal_actions | |
| def init_func(): | |
| # add global functions in this file | |
| global_func = [a for a in globals() if isinstance(globals()[a], types.FunctionType) and a[:1] == '_'] | |
| # global_func = [a for a in locals()] | |
| print global_func | |
| # sub-command: https://github.com/kislyuk/argcomplete/issues/76 | |
| # ref: https://github.com/kislyuk/argcomplete/issues/10 | |
| # prepare data | |
| # ref: http://stackoverflow.com/questions/139180/listing-all-functions-in-a-python-module | |
| local_func = [a for a in dir(todo) if isinstance(todo.__dict__.get(a), types.FunctionType) and a[0] != '_'] | |
| #functions = dir(todo) | |
| #local_func = [a for a in functions if a[0] != '_'] | |
| for func in local_func: | |
| pprint.pprint('-' + func) | |
| #parser = argparse.ArgumentParser() | |
| #for func in local_func: | |
| #parser.add_argument(func, help="") | |
| #argcomplete.autocomplete(parser) | |
| #args = parser.parse_args() | |
| return local_func + global_func | |
| def init_parser(func_names): | |
| parser = argparse.ArgumentParser() | |
| # ref: http://stackoverflow.com/questions/4575747/get-selected-subcommand-with-argparse | |
| subparsers = parser.add_subparsers(dest="subparser_name") | |
| subparsers.completer = complete_subparser_action | |
| for cmd in func_names: | |
| subparsers.add_parser(cmd) | |
| argcomplete.autocomplete(parser) | |
| return parser | |
| # Global function | |
| def _ccle(): | |
| os.system('open http://ccle.ucla.edu') | |
| def _publish_html(): | |
| os.system('ipython nbconvert --to html *.ipynb') | |
| def simple_server(): | |
| if __name__ == '__main__': | |
| func_names = init_func() | |
| parser = init_parser(func_names) | |
| args = parser.parse_args() | |
| func = args.subparser_name | |
| if func[:1] == '_': # global functions | |
| method = locals()[func] | |
| else: | |
| method = getattr(todo, func) | |
| method() | |
| projectName = os.getcwd().split('/')[-1] | |
| # Set project name | |
| # os.system('echo "\e]1;%s\a"' % projectName) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment