def load(self, verbose=True, **kwargs): """Load existing project Args: verbose (bool): Log to stdout. Defaults to True. Returns: stubs: Project Stubs """ data = json.loads(self.info_path.read_text()) _stubs = data.get("stubs") self.name = data.get("name", self.name) self.config = data.get("config", self.config) templates = [k for k, v in self.config.items() if v] self.provider = TemplateProvider(templates) self.packages = data.get("packages", self.packages) self.dev_packages = data.get("dev-packages", self.dev_packages) self.stubs = kwargs.get('stubs', self.stubs) self.stub_manager = kwargs.get("stub_manager", self.stub_manager) self.stub_manager.verbose_log(verbose) self.data.mkdir(exist_ok=True) stubs = list(self._load_stubs(_stubs)) if self.stubs: stubs.extend(self.stubs) self.stubs = self._resolve_subresource(stubs) self.pkg_data.mkdir(exist_ok=True) self.load_packages() self._loaded = True if verbose: self.log.success(f"\nProject Ready!") return self.stubs
def test_pylint_template(stub_context, tmp_path): def test_pylint_load(): try: lint_args = ["--rcfile", str(expected_path.absolute())] pylint.lint.Run(lint_args) except SyntaxError: pytest.fail(str(SyntaxError)) # noqa except: # noqa pass stubs, paths, ctx_paths = stub_context ctx_datadir = tmp_path / 'ctx_cata' ctx_datadir.mkdir(exist_ok=True) prov = TemplateProvider(['pylint']) prov.render_to("pylint", tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir) expected_path = tmp_path / '.pylintrc' assert expected_path.exists() # Will Pylint load it? test_pylint_load() # Test Update new_path = (tmp_path / '.micropy' / 'foobar' / 'foo') ctx_paths.append(new_path) prov.update("pylint", tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir) init_hook = expected_path.read_text().splitlines(True)[2] hook_imports = init_hook.split(";") assert 'sys.path.insert(1, ".micropy/foobar/foo")' in hook_imports test_pylint_load()
def load(self, **kwargs): """Loads project templates.""" self.provider = self.get_provider(self.config.get('config')) templates = [k for k, v in self.config.get('config').items() if v] self.log.debug(f"Loading Templates: {templates}") self.provider = TemplateProvider(templates, **kwargs) self.update()
def __init__(self, path, name=None, templates=[], stubs=None, stub_manager=None): self._loaded = False self.path = Path(path).absolute() self.data = self.path / '.micropy' self.cache = self.data / '.cache' self.info_path = self.path / 'micropy.json' self.stub_manager = stub_manager self.name = name or self.path.name self.stubs = stubs self.requirements = self.path / 'requirements.txt' self.dev_requirements = self.path / 'dev-requirements.txt' self.packages = {} self.dev_packages = {'micropy-cli': '*'} self.pkg_data = self.data / self.name self.config = {'vscode': False, 'pylint': False} self.log = Log.add_logger(self.name, show_title=False) template_log = Log.add_logger( "Templater", parent=self.log, show_title=False) self.provider = None if templates: for key in self.config: if key in templates: self.config[key] = True self.provider = TemplateProvider(templates, log=template_log)
def test_pylint_template(stub_context, tmp_path): def test_pylint_load(): try: lint_args = ["--rcfile", str(expected_path.absolute())] pylint.lint.Run(lint_args) except SyntaxError: pytest.fail(str(SyntaxError)) # noqa except: # noqa pass stubs, paths, ctx_paths = stub_context ctx_datadir = tmp_path / "ctx_cata" ctx_datadir.mkdir(exist_ok=True) prov = TemplateProvider(["pylint"]) prov.render_to("pylint", tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir) expected_path = tmp_path / ".pylintrc" assert expected_path.exists() # Will Pylint load it? test_pylint_load() # Test Update new_path = tmp_path / ".micropy" / "foobar" / "foo" ctx_paths.append(new_path) prov.update("pylint", tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir) init_hook = expected_path.read_text().splitlines(True)[2] hook_imports = init_hook.split(",") hook_path = str(Path(".micropy/foobar/foo")).replace( "\\", "/" ) # no need to use \\ on pylint Windows assert f' "{hook_path}"' in hook_imports test_pylint_load()
def load(self, **kwargs): """Loads project templates.""" _data = self.config.get('config') self.enabled = {**self.enabled, **_data} templates = [k for k, v in self.enabled.items() if v] self.log.debug(f"Loading Templates: {templates}") self.provider = TemplateProvider(templates, **kwargs) self.update()
def test_vscode_template(stub_context, shared_datadir, tmp_path, mock_checks): stubs, paths, ctx_paths = stub_context prov = TemplateProvider(['vscode']) ctx_datadir = tmp_path / 'ctx_cata' ctx_datadir.mkdir(exist_ok=True) # Add test local path ctx_local = ctx_datadir / 'src' / 'lib' / 'somelib' ctx_local.mkdir(parents=True) ctx_absolute = Path('/fakedir/notinprojectdir/somelib') ctx_local_paths = [ctx_local, ctx_absolute] prov.render_to('vscode', tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir, local_paths=ctx_local_paths) expected_path = tmp_path / '.vscode' / 'settings.json' out_content = expected_path.read_text() print(out_content) # Get rid of comments with expected_path.open() as f: lines = [l.strip() for l in f.readlines() if l] valid = [l for l in lines if "//" not in l[:2]] # Valid JSON? expect_paths = [str(p.relative_to(tmp_path)) for p in ctx_paths] expect_paths.append(str(ctx_local.relative_to( tmp_path))) # add local path (should be relative) # local path outside of project dir (must be absolute) expect_paths.append(str(ctx_absolute.absolute())) content = json.loads("\n".join(valid)) assert sorted(expect_paths) == sorted( content["python.autoComplete.extraPaths"]) assert expected_path.exists() # Test Update ctx_paths.append((tmp_path / "foobar" / "foo.py")) prov.update('vscode', tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir, local_paths=ctx_local_paths) content = json.loads(expected_path.read_text()) expect_paths.append( str((tmp_path / "foobar" / "foo.py").relative_to(tmp_path))) assert sorted(expect_paths) == sorted( content["python.autoComplete.extraPaths"]) # Test update with missing file expected_path.unlink() # delete file prov.update('vscode', tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir) assert expected_path.exists()
def test_generic_template(mock_mp_stubs, tmp_path): prov = TemplateProvider(['bootstrap', 'pymakr']) prov.render_to('boot', tmp_path) expected_path = tmp_path / 'src' / 'boot.py' assert expected_path.exists() expected_content = (prov.TEMPLATE_DIR / 'src' / 'boot.py').read_text() out_content = expected_path.read_text() print(out_content) assert expected_content.strip() == out_content.strip() templ = prov.get('boot') assert templ.update(tmp_path) is None
def test_generic_template(mock_mp_stubs, tmp_path): prov = TemplateProvider(["bootstrap", "pymakr"]) prov.render_to("boot", tmp_path) expected_path = tmp_path / "src" / "boot.py" assert expected_path.exists() expected_content = (prov.TEMPLATE_DIR / "src" / "boot.py").read_text() out_content = expected_path.read_text() print(out_content) assert expected_content.strip() == out_content.strip() templ = prov.get("boot") assert templ.update(tmp_path) is None
def __init__(self, templates=None, run_checks=True, **kwargs): self.templates = templates or [] self.run_checks = run_checks self.enabled = {'vscode': False, 'pylint': False} self.log = Log.add_logger('Templater', show_title=False) if templates: for key in self.enabled: if key in self.templates: self.enabled[key] = True self.provider = TemplateProvider(self.templates, run_checks=self.run_checks, log=self.log, **kwargs)
def __init__(self, templates=None, run_checks=True, **kwargs): self.templates = templates or [] super().__init__(**kwargs) self.run_checks = run_checks self.enabled = { 'vscode': False, 'pylint': False } if templates: for key in self.enabled: if key in self.templates: self.enabled[key] = True self.provider = TemplateProvider( self.templates, **kwargs)
def test_vscode_template(stub_context, shared_datadir, tmp_path, mock_checks): stubs, paths, ctx_paths = stub_context prov = TemplateProvider(['vscode']) ctx_datadir = tmp_path / 'ctx_cata' ctx_datadir.mkdir(exist_ok=True) prov.render_to('vscode', tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir) expected_path = tmp_path / '.vscode' / 'settings.json' out_content = expected_path.read_text() print(out_content) # Get rid of comments with expected_path.open() as f: lines = [l.strip() for l in f.readlines() if l] valid = [l for l in lines if "//" not in l[:2]] # Valid JSON? expect_paths = [str(p.relative_to(tmp_path)) for p in ctx_paths] content = json.loads("\n".join(valid)) assert sorted(expect_paths) == sorted( content["python.autoComplete.extraPaths"]) assert expected_path.exists() # Test Update ctx_paths.append((tmp_path / "foobar" / "foo.py")) prov.update('vscode', tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir) content = json.loads(expected_path.read_text()) expect_paths.append( str((tmp_path / "foobar" / "foo.py").relative_to(tmp_path))) assert sorted(expect_paths) == sorted( content["python.autoComplete.extraPaths"]) # Test update with missing file expected_path.unlink() # delete file prov.update('vscode', tmp_path, stubs=stubs, paths=ctx_paths, datadir=ctx_datadir) assert expected_path.exists()
def get_provider(self, templates): return TemplateProvider(templates, run_checks=self.run_checks, log=self.log)