def _get_project(package_slug, project_path): if package_slug is not None: package = import_module_from_modname(package_slug) return Project(package) elif project_path is not None: return Project.from_path(project_path) else: return Project.from_cwd()
def project(self) -> Project: """Access ballet-specific project info""" if self._prj is None: return Project.from_cwd() else: if isinstance(self._prj, Project): return self._prj elif isinstance(self._prj, ModuleType): return Project(self._prj) elif isinstance(self._prj, (str, PathLike)): return Project.from_path(self._prj) else: raise ValueError('not supported')
def project(self): # 1. configuration option passed explicitly # 2. from notebooks dir # 3. from cwd if self.ballet_yml_path: return Project.from_path(self.ballet_yml_path) path = NotebookApp.instance().notebook_dir with fy.suppress(Exception): return Project.from_path(path) with fy.suppress(Exception): return Project.from_cwd() raise ConfigurationError('Could not detect Ballet project')
def validate_feature_acceptance(feature, X, y, subsample=False, path=None, package=None): if package is not None: project = Project(package) elif path is not None: project = Project.from_path(path) else: project = Project.from_cwd() if subsample: X, y = subsample_data_for_validation(X, y) # build project result = project.build(X, y) # load accepter for this project Accepter = _load_class(project, 'validation.feature_accepter') accepter = Accepter(result.X_df, result.y, result.features, feature) return accepter.judge()
def start_new_feature(contrib_dir: Pathy = None, branching: bool = True, **cc_kwargs) -> List[Tuple[pathlib.Path, str]]: """Start a new feature within a ballet project If run from default branch, by default will attempt to switch to a new branch for this feature, given by `<username>/feature-<featurename>`. By default, will prompt the user for input using cookiecutter's input interface. Renders the feature template into a temporary directory, then copies the feature files into the proper path within the contrib directory. Args: contrib_dir: directory under which to place contributed features branching: whether to attempt to manage branching **cc_kwargs: options for the cookiecutter template Raises: ballet.exc.BalletError: the new feature has the same name as an existing one """ if contrib_dir is not None: try: project = Project.from_path(contrib_dir, ascend=True) default_username = detect_github_username(project) except ConfigurationError: default_username = '******' else: project = Project.from_cwd() contrib_dir = project.config.get('contrib.module_path') default_username = detect_github_username(project) # inject default username into context cc_kwargs.setdefault('extra_context', {}) cc_kwargs['extra_context'].update({'_default_username': default_username}) with tempfile.TemporaryDirectory() as tempdir: # render feature template output_dir = tempdir cc_kwargs['output_dir'] = output_dir rendered_dir = render_feature_template(**cc_kwargs) # clean pyc files from rendered dir for path in pwalk(rendered_dir, topdown=False): if path.suffix == '.pyc': path.unlink() if path.name == '__pycache__': with fy.suppress(OSError): path.rmdir() # copy into contrib dir src = rendered_dir dst = contrib_dir result = synctree(src, dst, onexist=_fail_if_feature_exists) target_branch = None if branching and project.on_master: # try to set the target branch name paths = [path for path, kind in result if kind == 'file'] for path in paths: parts = pathlib.Path(path).parts subpackage, module = parts[-2], parts[-1] user_match = fy.re_find(SUBPACKAGE_NAME_REGEX, subpackage) feature_match = fy.re_find(FEATURE_MODULE_NAME_REGEX, module) if feature_match: username = user_match['username'] featurename = feature_match['featurename'].replace('_', '-') target_branch = f'{username}/feature-{featurename}' if target_branch is not None: switch_to_new_branch(project.repo, target_branch) _log_start_new_feature_success(result) _log_switch_to_new_branch(target_branch) return result