Пример #1
0
    def _ensure_template_dir(self):
        if self._template_dir is None:
            self._repo_dir = make_path(self._dir, "sniptool-snippets")
            if not os.path.isdir(self._repo_dir):
                git_clone(_SNIPPETS_GIT_URL, self._repo_dir)

            self._template_dir = make_path(self._repo_dir, "_snippets")
Пример #2
0
def _do_gen(config, args):
    env = jinja2.Environment(undefined=jinja2.StrictUndefined)

    env.filters["encode_cpp_literal"] = lambda s: "\"" + s.replace(
        "\\", "\\\\"
    ) + "\""  # TODO: Implement full set of C++ literal encoding rules

    for f in _public_callable_attrs(inflection):
        env.filters[f.__name__] = f

    path = make_path(config.template_dir, args.template_name)
    source = _read_source(path)
    metadata, defaults = _read_metadata(source)

    print("Template: {}".format(args.template_name))
    _show_metadata(path, metadata, defaults, 1)
    values = _prompt(env, source, defaults)

    output = env.from_string(source, values).render()
    print("==========")
    print(output)
    print("==========")

    if args.output_path is None:
        _set_clipboard_text(output)
        print("Text sent to clipboard")
    else:
        with open(args.output_path, "wt") as f:
            f.write(output)
        print("Text written to file {}".format(args.output_path))
Пример #3
0
def get_credentials(config, args):
    credential_dir = make_path(config.dir, "credentials")
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)

    credential_path = make_path(credential_dir,
                                "calendar-python-quickstart.json")
    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        client_secret_path = make_path(config.dir, _CLIENT_SECRET_FILE)
        flow = client.flow_from_clientsecrets(client_secret_path, _SCOPES)
        flow.user_agent = _APPLICATION_NAME
        credentials = run_flow(flow, store, args)
        print("Storing credentials to {}".format(credential_path))
    return credentials
Пример #4
0
def _do_list(config, args):
    for p in os.listdir(config.template_dir):
        print("Template: {}".format(p))
        path = make_path(config.template_dir, p)
        source = _read_source(path)
        metadata, defaults = _read_metadata(source)
        _show_metadata(path, metadata, defaults, 1)
        print()
Пример #5
0
 def __init__(self, name, config_dir, user, api_key, api_secret):
     self._name = name
     self._cached_token_path = make_path(config_dir, "bitbucket.token.yaml")
     self._user = user
     self._api_key = api_key
     self._api_secret = api_secret
     self._client = None
     self._owners = {}
Пример #6
0
def _template_dirs(start_dir, args):
    if len(args.template_dirs) > 0:
        return args.template_dirs

    template_dirs = []
    for i in os.listdir(start_dir):
        path = make_path(start_dir, i)
        if os.path.isdir(path):
            template_dirs.append(path)

    return template_dirs
Пример #7
0
def _do_test(start_dir, args):
    suite = unittest.TestSuite()

    if len(args.template_dirs) == 0:
        ptoollib_dir = make_path(os.path.dirname(__file__), _PTOOLLIB_MODULE_NAME)
        for i in os.listdir(ptoollib_dir):
            path = make_path(ptoollib_dir, i)
            if os.path.isfile(path) and path.endswith(".py"):
                module_name = "{}.{}".format(_PTOOLLIB_MODULE_NAME, os.path.basename(path))
                module = imp.load_source(module_name, path)
                tests = _get_test_suite(module)
                if tests is not None:
                    suite.addTests(tests)

    for template_dir in _template_dirs(start_dir, args):
        module = load_template_module(template_dir)
        tests = _get_test_suite(module)
        if tests is not None:
            suite.addTests(tests)

    unittest.TextTestRunner(verbosity=2).run(suite)
Пример #8
0
def _exif_dump(scan_dir):
    for f in os.listdir(scan_dir):
        if os.path.splitext(f)[1].lower() in _EXTENSIONS:
            path = make_path(scan_dir, f)
            with open(path, "rb") as f:
                tags = exifread.process_file(f)
                if len(tags.keys()) > 0:
                    print("{}:".format(path))
                    for key in tags.keys():
                        print("  {}".format(key))
                else:
                    print("No tags found in {}".format(path))
Пример #9
0
def main():
    parser = argparse.ArgumentParser(description="Compute version number")
    parser.add_argument("--source_path",
                        default=make_path(SCRIPT_PATH, "..", ".."),
                        help="path git source repository",
                        type=os.path.abspath)
    args = parser.parse_args()

    result = main_inner(args)
    if result is None:
        sys.exit(0)
    else:
        sys.stderr.write("{} validation errors were detected.\n".format(
            len(result)))
        for error in result:
            sys.stderr.write("  {}\n".format(error))
        sys.exit(1)
Пример #10
0
    def __init__(self, cwd=None, args=[]):
        if cwd is None:
            cwd = os.getcwd()

        cwd = os.path.abspath(cwd)

        is_bare = execute("git", "rev-parse", "--is-bare-repository",
                          cwd=cwd).strip() == "true"

        if is_bare:
            repo_dir = cwd
            git_dir = repo_dir
        else:
            repo_dir = execute("git", "rev-parse", "--show-toplevel",
                               cwd=cwd).strip()
            git_dir = make_path(repo_dir, ".git")

        super(Git, self).__init__(is_bare, repo_dir, git_dir, args)
Пример #11
0
def _main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    config_dir = make_path(
        os.path.expanduser(os.environ.get("SNIPTOOL_DIR", "~/.sniptool")))
    config = Config(config_dir)

    parser = argparse.ArgumentParser(prog=__project_name__,
                                     description=__description__)
    parser.add_argument("--version",
                        action="version",
                        version="{} version {}".format(__project_name__,
                                                       __version__))

    subparsers = parser.add_subparsers(help="subcommand help")

    update_parser = subparsers.add_parser(
        "update", help="Update local snippet repository")
    update_parser.set_defaults(func=_do_update)

    gen_parser = subparsers.add_parser("gen", help="Generate code snippet")
    gen_parser.set_defaults(func=_do_gen)
    gen_parser.add_argument("template_name", help="Template name")
    gen_parser.add_argument(
        "--output-file",
        "-f",
        dest="output_path",
        default=None,
        type=make_path,
        help="Write to specified file instead of clipboard")

    list_parser = subparsers.add_parser("list",
                                        help="List available templates")
    list_parser.set_defaults(func=_do_list)

    args = parser.parse_args(argv)
    args.func(config, args)
Пример #12
0
def _main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    config_dir = make_path(
        os.path.expanduser(
            os.environ.get("GOOGLE_CALENDAR_DIR", "~/.google-calendar")))
    config = Config(config_dir)

    parser = argparse.ArgumentParser(prog=__project_name__,
                                     description=__description__,
                                     parents=[oauth2client.tools.argparser])
    parser.add_argument("--version",
                        action="version",
                        version="{} version {}".format(__project_name__,
                                                       __version__))

    subparsers = parser.add_subparsers(help="subcommand help")

    dump_parser = subparsers.add_parser("dump", help="Show calendar entries")
    dump_parser.set_defaults(func=_do_dump)

    args = parser.parse_args(argv)
    args.func(config, args)
Пример #13
0
 def __init__(self, dir):
     self._dir = dir
     self._data_dir = make_path(self._dir, "data")
Пример #14
0
def _main():
    default_config_dir = make_path(os.path.expanduser("~/.repotool"))
    default_config_path = make_path(default_config_dir, "config.yaml")
    default_user = getpass.getuser()

    config_obj = _read_config(default_config_path)

    provider_map = {}
    for provider_config_obj in config_obj.get("providers", []):
        name = provider_config_obj["name"]
        type = provider_config_obj["type"]
        cls = _PROVIDER_CLASSES[type]
        provider_map[name] = cls.parse_config(default_config_dir, default_user,
                                              provider_config_obj)

    parser = argparse.ArgumentParser(description="Repository tool")
    parser.add_argument("--version",
                        action="version",
                        version="{} version {}".format(__project_name__,
                                                       __version__))

    subparsers = parser.add_subparsers(help="subcommand help")

    list_parser = subparsers.add_parser("list", help="List projects")
    list_parser.set_defaults(func=_do_list)
    list_parser.add_argument("--filter",
                             "-f",
                             dest="project_filter_expr",
                             default=None)
    list_parser.add_argument("--provider",
                             "-p",
                             nargs="+",
                             dest="provider_names",
                             default=None,
                             choices=sorted(provider_map.keys()))
    list_parser.add_argument("--include-archived",
                             "-a",
                             dest="include_archived",
                             action="store_true",
                             default=False)

    info_parser = subparsers.add_parser("info",
                                        help="Show information about project")
    info_parser.set_defaults(func=_do_info)
    info_parser.add_argument("provider_name",
                             metavar="PROVIDERNAME",
                             help="Name of project provider")
    info_parser.add_argument("project_name",
                             metavar="PROJECTNAME",
                             help="Project name")

    create_parser = subparsers.add_parser("create", help="Create project")
    create_parser.set_defaults(func=_do_create)
    create_parser.add_argument("provider_name",
                               metavar="PROVIDERNAME",
                               help="Name of project provider")
    create_parser.add_argument("project_name",
                               metavar="PROJECTNAME",
                               help="Project name")

    delete_parser = subparsers.add_parser("delete", help="Delete project")
    delete_parser.set_defaults(func=_do_delete)
    delete_parser.add_argument("provider_name",
                               metavar="PROVIDERNAME",
                               help="Name of project provider")
    delete_parser.add_argument("project_name",
                               metavar="PROJECTNAME",
                               help="Project name")

    archive_parser = subparsers.add_parser("archive", help="Archive project")
    archive_parser.set_defaults(func=_do_archive)
    archive_parser.add_argument("provider_name",
                                metavar="PROVIDERNAME",
                                help="Name of project provider")
    archive_parser.add_argument("project_name",
                                metavar="PROJECTNAME",
                                help="Project name")

    dupes_parser = subparsers.add_parser(
        "dupes", help="Show possible duplicate projects")
    dupes_parser.set_defaults(func=_do_dupes)

    args = parser.parse_args()
    args.func(args, provider_map)