Skip to content

Commit

Permalink
Make some imports lazy for plots (iterative#5219)
Browse files Browse the repository at this point in the history
  • Loading branch information
skshetry authored Jan 6, 2021
1 parent 1cc6466 commit 2646c0d
Showing 3 changed files with 26 additions and 18 deletions.
21 changes: 12 additions & 9 deletions dvc/repo/collect.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import logging
import os
from typing import Callable, Iterable, Set, Tuple
from typing import TYPE_CHECKING, Callable, Iterable, Set, Tuple

from dvc.output.base import BaseOutput
from dvc.path_info import PathInfo
from dvc.repo import Repo
from dvc.tree.repo import RepoTree
from dvc.types import DvcPath

if TYPE_CHECKING:
from dvc.output.base import BaseOutput
from dvc.repo import Repo

logger = logging.getLogger(__name__)


FilterFn = Callable[[BaseOutput], bool]
Outputs = Set[BaseOutput]
FilterFn = Callable[["BaseOutput"], bool]
Outputs = Set["BaseOutput"]
DvcPaths = Set[DvcPath]


def _collect_outs(
repo: Repo, output_filter: FilterFn = None, deps: bool = False,
repo: "Repo", output_filter: FilterFn = None, deps: bool = False,
) -> Outputs:
outs = {
out
@@ -28,11 +29,13 @@ def _collect_outs(


def _collect_paths(
repo: Repo,
repo: "Repo",
targets: Iterable[str],
recursive: bool = False,
rev: str = None,
):
from dvc.tree.repo import RepoTree

path_infos = {PathInfo(os.path.abspath(target)) for target in targets}
tree = RepoTree(repo)

@@ -67,7 +70,7 @@ def _filter_duplicates(


def collect(
repo: Repo,
repo: "Repo",
deps: bool = False,
targets: Iterable[str] = None,
output_filter: FilterFn = None,
20 changes: 12 additions & 8 deletions dvc/repo/plots/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Dict, List
from typing import TYPE_CHECKING, Dict, List

from funcy import cached_property, first, project

@@ -9,14 +9,14 @@
NoMetricsFoundError,
NoMetricsParsedError,
)
from dvc.output import BaseOutput
from dvc.repo import Repo
from dvc.repo.collect import collect
from dvc.repo.plots.data import PlotParsingError
from dvc.schema import PLOT_PROPS
from dvc.tree.repo import RepoTree
from dvc.utils import relpath

if TYPE_CHECKING:
from dvc.output import BaseOutput
from dvc.repo import Repo

logger = logging.getLogger(__name__)


@@ -51,6 +51,8 @@ def collect(
}}}
Data parsing is postponed, since it's affected by props.
"""
from dvc.tree.repo import RepoTree

targets = [targets] if isinstance(targets, str) else targets or []
data = {}
for rev in self.repo.brancher(revs=revs):
@@ -201,12 +203,12 @@ def templates(self):
return PlotTemplates(self.repo.dvc_dir)


def _is_plot(out: BaseOutput) -> bool:
def _is_plot(out: "BaseOutput") -> bool:
return bool(out.plot) or bool(out.live)


def _collect_plots(
repo: Repo,
repo: "Repo",
targets: List[str] = None,
rev: str = None,
recursive: bool = False,
@@ -223,7 +225,9 @@ def _collect_plots(
return result


def _plot_props(out: BaseOutput) -> Dict:
def _plot_props(out: "BaseOutput") -> Dict:
from dvc.schema import PLOT_PROPS

if not (out.plot or out.live):
raise NotAPlotError(out)
if isinstance(out.plot, list):
3 changes: 2 additions & 1 deletion dvc/repo/plots/template.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@
from funcy import cached_property

from dvc.exceptions import DvcException
from dvc.utils.fs import makedirs


class TemplateNotFoundError(DvcException):
@@ -572,6 +571,8 @@ def __init__(self, dvc_dir):
self.dvc_dir = dvc_dir

def init(self):
from dvc.utils.fs import makedirs

makedirs(self.templates_dir, exist_ok=True)
for t in self.TEMPLATES:
self._dump(t())

0 comments on commit 2646c0d

Please sign in to comment.