def add_files(struct: Structure, opts: ScaffoldOpts) -> ActionParams: """Add some Travis files to structure Args: struct: project representation as (possibly) nested :obj:`dict`. opts: given options, see :obj:`create_project` for an extensive list. Returns: struct, opts: updated project representation and options """ files: Structure = { ".travis.yml": (template("travis"), no_overwrite()), "tests": { "travis_install.sh": (template("travis_install"), no_overwrite()) }, } return structure.merge(struct, files), opts
def replace_files(struct: Structure, opts: ScaffoldOpts) -> ActionParams: """Replace all rst files to proper md and activate Sphinx md. See :obj:`pyscaffold.actions.Action` """ # Define new files NO_OVERWRITE = no_overwrite() files: Structure = { "README.md": (template("readme"), NO_OVERWRITE), "AUTHORS.md": (template("authors"), NO_OVERWRITE), "CHANGELOG.md": (template("changelog"), NO_OVERWRITE), "CONTRIBUTING.md": (template("contributing"), NO_OVERWRITE), "docs": { "index.md": (template("index"), NO_OVERWRITE), "readme.md": (default_myst_include("README.md"), NO_OVERWRITE), "license.md": (template("license"), NO_OVERWRITE), "authors.md": (default_myst_include("AUTHORS.md"), NO_OVERWRITE), "changelog.md": (default_myst_include("CHANGELOG.md"), NO_OVERWRITE), "contributing.md": (default_myst_include("CONTRIBUTING.md"), NO_OVERWRITE), }, } # TODO: Automatically convert RST to MD # # >>> content, file_op = reify_leaf(struct.get("CONTRIBUTING.rst"), opts) # >>> md_content = rst_to_myst(content or "", **RST2MYST_OPTS).text # >>> files["CONTRIBUTING.md"] = (md_content, file_op) # # Currently there is a problem in rst-to-myst, preventing automatic conversion: # https://github.com/executablebooks/rst-to-myst/issues/33#issuecomment-922264030 # Modify pre-existing files content, file_op = reify_leaf(struct["setup.cfg"], opts) files["setup.cfg"] = (add_long_desc(content), file_op) content, file_op = reify_leaf(struct["docs"]["conf.py"], opts) files["docs"]["conf.py"] = (add_myst(content), file_op) # Remove all unnecessary .rst files from struct unnecessary = [ "README.rst", "AUTHORS.rst", "CHANGELOG.rst", "CONTRIBUTING.rst", "docs/index.rst", "docs/readme.rst", "docs/license.rst", "docs/authors.rst", "docs/changelog.rst", "docs/contributing.rst", ] struct = reduce(reject, unnecessary, struct) return merge(struct, files), opts
def add_files(struct, opts): nov, sou = operations.no_overwrite(), operations.skip_on_update() struct = structure.ensure(struct, "tests/file0", "new") struct = structure.ensure(struct, "tests/file1", "new", nov) struct = structure.ensure(struct, "tests/file2", "new", sou) struct = structure.merge( struct, { "tests": { "file3": ("new", nov), "file4": ("new", sou), "file5": ("new", operations.create), "file6": "new", } }, ) return struct, opts
def test_no_overwrite(monkeypatch): NO_OVERWRITE = no_overwrite(lambda *_: path) # When file does not exist, execute path = uniqpath() with monkeypatch.context() as patch: patch.setattr("pathlib.Path.exists", lambda _: False) assert NO_OVERWRITE(path, "contents", {}) == path # When file exists, skip path = uniqpath() with monkeypatch.context() as patch: patch.setattr("pathlib.Path.exists", lambda _: True) assert NO_OVERWRITE(path, "contents", {}) is None # When force is True, execute, even if file exists opts = {"force": True} path = uniqpath() for existing in (True, False): with monkeypatch.context() as patch: patch.setattr("pathlib.Path.exists", lambda _: existing) assert NO_OVERWRITE(path, "contents", opts) == path
from pyscaffold.structure import ( Leaf, ResolvedLeaf, merge, reify_content, reify_leaf, resolve_leaf, ) from pyscaffold.templates import get_template from pyscaffold.update import ConfigUpdater, pyscaffold_version from . import templates PYSCAFFOLDEXT_NS = "pyscaffoldext" EXTENSION_FILE_NAME = "extension" NO_OVERWRITE = no_overwrite() DOC_REQUIREMENTS = ["pyscaffold"] TEST_DEPENDENCIES = ( "tox", "pre-commit", "setuptools_scm", "virtualenv", "configupdater", "pytest", "pytest-cov", "pytest-xdist", ) INVALID_PROJECT_NAME = ( "The prefix ``pyscaffoldext-`` will be added to the package name " "(as in PyPI/pip install). "
from os.path import isdir, isfile from pathlib import Path import pytest from pyscaffold import actions, api, cli, operations, structure NO_OVERWRITE = operations.no_overwrite() SKIP_ON_UPDATE = operations.skip_on_update() def test_create_structure(tmpfolder): struct = { "my_file": "Some content", "my_folder": { "my_dir_file": "Some other content", "empty_file": "", "file_not_created": None, }, "empty_folder": {}, } expected = { "my_file": "Some content", "my_folder": { "my_dir_file": "Some other content", "empty_file": "" }, "empty_folder": {}, } changed, _ = structure.create_structure(struct, {})