Created
September 8, 2016 14:14
-
-
Save junmakii/9d66afd2af203bf6737087e3b89d27d1 to your computer and use it in GitHub Desktop.
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 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| """ | |
| import os | |
| import sys | |
| import sysconfig | |
| import site | |
| import argparse | |
| import logging | |
| import traceback | |
| import pprint | |
| import inspect | |
| import copy | |
| import re | |
| import time | |
| import datetime | |
| import math | |
| import random | |
| import json | |
| import csv | |
| import tempfile | |
| import types | |
| import operator | |
| import itertools | |
| import functools | |
| import collections | |
| import threading | |
| import multiprocessing as mp | |
| import subprocess | |
| import shutil | |
| import getpass | |
| import socket | |
| import urllib | |
| import importlib | |
| from wsgiref.simple_server import make_server | |
| MODULES = [ | |
| "typing", | |
| "asyncio", | |
| "concurrent.futures # futures", | |
| "PIL # pillow", | |
| "fn # fn.py", | |
| "fn.monad", | |
| "fn.op", | |
| "fn.iters", | |
| "pytz # pytz", | |
| "dateutil # python-dateutil", | |
| "markdown # markdown", | |
| "yaml # PyYAML", | |
| "flask # flask", | |
| "sphinx # sphinx", | |
| "werkzeug # werkzeug", | |
| "pygments # pygments", | |
| "jinja2 # jinja2", | |
| "lxml # lxml", | |
| "lxml.html # html5lib", | |
| "bs4 # beautifulsoup4", | |
| "requests # requests", | |
| "requests_oauthlib # requests-oauthlib", | |
| "jmespath # jmespath", | |
| "numpy # numpy", | |
| "scipy # scipy", | |
| "pandas # pandas", | |
| "IPython # ipython", | |
| "sqlalchemy # sqlalchemy", | |
| "pymongo # pymongo", | |
| ] | |
| if sys.version_info > (3, 0): | |
| pass | |
| __version__ = '0.1' | |
| __author__ = 'Jun Makii' | |
| __author_email__ = 'junmakii@gmail.com' | |
| __url__ = 'http://junmakii.umu.us' | |
| __description__ = __doc__.splitlines()[0] | |
| __long_description__ = __doc__ | |
| __classifiers__ = [ | |
| 'Programming Language :: Python :: 2.7', | |
| 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', | |
| ] | |
| __license__ = 'GPLv3' | |
| __keywords__ = '' | |
| __packages__ = [] | |
| __package_data__ = { | |
| '': [] | |
| } | |
| __install_requires__ = [] | |
| NAME = os.path.splitext(os.path.basename(__file__))[0] | |
| ENCODING = "utf-8" | |
| CONFIG_DIRS = [ | |
| os.path.join("/etc", NAME, "config"), | |
| os.path.join(os.environ.get("HOME", ""), ".config", NAME), | |
| os.path.join(os.path.dirname(__file__), "config"), | |
| os.path.abspath("config"), | |
| ] | |
| CONFIG_FILES = list(itertools.chain.from_iterable([[ | |
| os.path.join(root, file) | |
| for root, dirs, files in os.walk(i) | |
| for file in files if file.endswith(".json")] | |
| for i in CONFIG_DIRS if os.path.exists(i) | |
| ])) | |
| logging.basicConfig(level="DEBUG") | |
| pp = pprint.pprint | |
| def import_modules(modules=MODULES): | |
| """ | |
| :rtype: None | |
| """ | |
| for module in modules: | |
| try: | |
| module_path = re.findall(r'(.*?)(\s|$)', module)[0][0] | |
| module_name = (re.findall(r'#(\s*)(.*)', module)[0][1] | |
| if module.find("#") != -1 else module) | |
| if module_name != module: | |
| __install_requires__.append(module_name) | |
| globals()[module_name] = importlib.import_module(module_path) | |
| except ImportError as err: | |
| logging.warning(repr(err)) | |
| except Exception as err: | |
| logging.warning(repr(err)) | |
| return None | |
| def command(*args, **kwargs): | |
| """ | |
| :rtype: argparse.Namespace | |
| """ | |
| parser = argparse.ArgumentParser(description=__description__) | |
| parser.add_argument("-c", "--config", | |
| action="append", default=CONFIG_FILES) | |
| parser.add_argument("-e", "--execute", action="append", default=[]) | |
| parser.add_argument("-i", "--input", action="append", default=[]) | |
| parser.add_argument("arguments", nargs="*") | |
| return parser | |
| def main(*args, **kwargs): | |
| """ | |
| :rtype: int | |
| """ | |
| import_modules() | |
| parser = command() | |
| options = parser.parse_args(args or sys.argv[1:] or ["--help"]) | |
| config = dict([(os.path.splitext(os.path.basename(i))[0], | |
| json.load(open(i))) for i in options.config]) | |
| config.update(dict([(k, eval(v)) | |
| for k, v in [i.split(":", 1) for i in options.input]])) | |
| for i in options.execute: | |
| exec(i) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main(*sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment