python类cwd()的实例源码

test_config.py 文件源码 项目:markdownreveal 作者: markdownreveal 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_load_config_local_and_style():
    """
    Test `load_config()` with local and style configuration files. Local file
    should always override style, which should override template.
    """
    with TemporaryDirectory() as tmpdir:
        tmpdir = Path(tmpdir)
        # Change home directory for testing
        os.environ['MARKDOWNREVEAL_HOME'] = str(tmpdir)
        # Create local configuration file
        config_file = tmpdir / 'config.yaml'
        config_file.write_text('footer: "local footer"')
        # Create style configuration file
        style_path = tmpdir / '.markdownreveal' / 'out' / 'markdownrevealstyle'
        style_path.mkdir(parents=True)
        config_file = style_path / 'config.yaml'
        config_file.write_text('footer: "style footer"\n'
                               'header: "style header"')
        # Load configuration
        old = Path.cwd()
        os.chdir(str(tmpdir))
        config = load_config()
        os.chdir(str(old))

    assert config['local_path'] == tmpdir / '.markdownreveal'
    assert config['output_path'] == config['local_path'] / 'out'
    assert config['footer'] == 'local footer'
    assert config['header'] == 'style header'
    assert 'markdownreveal/style-default' in config['style']
views.py 文件源码 项目:nbhosting 作者: parmentelat 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def share_notebook(request, course, student, notebook):
    """
    the URL to create static snapshots; it is intended to be fetched through ajax

    * computes a hash for storing the output
    * runs nbconvert in the student's container
    * stores the result in /nbhosting/snapshots/<course>/<hash>.html
    * returns a JSON-encoded dict that is either
      * { url: "/snapshots/flotpython/5465789765789.html" }
      * or { error: "the error message" }
    """

    # the ipynb extension is removed from the notebook name in urls.py
    notebook_withext = notebook + ".ipynb"
    # compute hash from the input, so that a second run on the same notebook
    # will override any previsouly published static snapshot
    hasher = hashlib.sha1(bytes('{}-{}-{}'.format(course, student, notebook),
                                encoding='utf-8'))
    hash = hasher.hexdigest()

    subcommand = 'docker-share-student-course-notebook-in-hash'

    command = ['nbh', '-d', sitesettings.root]
    if DEBUG:
        command.append('-x')
    command.append(subcommand)

    command += [ student, course, notebook_withext, hash]

    logger.info("In {}\n-> Running command {}".format(Path.cwd(), " ".join(command)))
    completed_process = subprocess.run(
        command, universal_newlines=True,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    log_completed_process(completed_process, subcommand)

    if completed_process.returncode != 0:
        message = "command {} returned {}\nstderr:{}"\
                  .format(" ".join(command),
                          completed_process.returncode,
                          completed_process.stderr)
        return JsonResponse(dict(error=message))

    # expect docker-share-student-course-notebook to write a url_path on its stdout
    url_path = completed_process.stdout.strip()
    logger.info("reading url_path={}".format(url_path))
    # rebuild a full URL with proto and hostname,
    url = "{scheme}://{hostname}{path}"\
          .format(scheme=request.scheme, hostname=request.get_host(), path=url_path)
    return JsonResponse(dict(url_path=url_path, url=url))
cake.py 文件源码 项目:layercake 作者: bcsaller 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def setup(args=None):
    parser = argparse.ArgumentParser()
    parser.add_argument("-l", "--log-level", default=logging.INFO)
    parser.set_defaults(func=lambda options: parser.print_help())

    parsers = parser.add_subparsers()
    layer = parsers.add_parser("layer", help=layer_main.__doc__.split("\n", 1)[0])
    layer.add_argument("--layer-endpoint",
            help="API endpoint for metadata",
            default="http://layer-cake.io")
    layer.add_argument("-d", "--directory", default=Path.cwd())
    layer.add_argument("-f", "--force", action="store_true",
                        help=("Force overwrite of existing layers "
                              "in directory (-d)"))
    layer.add_argument("-n", "--no-install", action="store_true",
                        help=("when set exit after pulling layers, "
                              "and before the install phase"))

    layer.add_argument(
            "layer",
            nargs="+",
            help=("The name of the layer to include, if more "
                  "than one is provided they will be included in order"))
    layer.set_defaults(func=layer_main)

    baker = parsers.add_parser("bake", help=bake_main.__doc__.split("\n", 1)[0])
    baker.add_argument("-d", "--dockerfile",
                       help="Dockerfile to process",
                       )
    baker.add_argument("--layer-endpoint",
            help="API endpoint for metadata",
            default="http://layer-cake.io")
    baker.add_argument("-n", "--no-build", action="store_true",
                       help="Don't build Dockerfile")
    baker.add_argument("--use-devel", action="store_true")
    baker.add_argument("config",
                       nargs="?",
                       default="cake.conf")
    baker.set_defaults(func=bake_main)

    search = parsers.add_parser("search")
    search.add_argument("--layer-endpoint",
            help="API endpoint for metadata",
            default="http://layer-cake.io")
    search.add_argument("-f", "--format", default="text", help="Options text|json|yaml")
    search.add_argument("term", nargs="+")
    search.set_defaults(func=search_main)

    options = parser.parse_args(args)
    return options


问题


面经


文章

微信
公众号

扫码关注公众号