From 40660b5120817dc8d486bc37977d4866a7623356 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 26 Nov 2017 21:49:06 -0800 Subject: [PATCH] Refactoring: extract CommandFile class --- dvc/command/add.py | 2 +- dvc/command/common/command_file.py | 54 +++++++++++++++++++++++++++++ dvc/command/run.py | 55 +----------------------------- 3 files changed, 56 insertions(+), 55 deletions(-) create mode 100644 dvc/command/common/command_file.py diff --git a/dvc/command/add.py b/dvc/command/add.py index 4f321cc41..917e64499 100644 --- a/dvc/command/add.py +++ b/dvc/command/add.py @@ -3,7 +3,7 @@ import os from dvc.command.common.base import CmdBase from dvc.logger import Logger from dvc.state_file import StateFile -from dvc.command.run import CommandFile +from dvc.command.common.command_file import CommandFile class CmdAdd(CmdBase): diff --git a/dvc/command/common/command_file.py b/dvc/command/common/command_file.py new file mode 100644 index 000000000..62aee62e8 --- /dev/null +++ b/dvc/command/common/command_file.py @@ -0,0 +1,54 @@ +import yaml + +from dvc.state_file import StateFileBase + + +class CommandFile(StateFileBase): + MAGIC = 'DVC-Command-State' + VERSION = '0.1' + + PARAM_CMD = 'cmd' + PARAM_OUT = 'out' + PARAM_REG = 'reg' + PARAM_DEPS = 'deps' + PARAM_LOCK = 'locked' + + def __init__(self, cmd, out, reg, deps, locked, fname): + self.cmd = cmd + self.out = out + self.reg = reg + self.deps = deps + self.locked = locked + self.fname = fname + + @property + def dict(self): + data = { + self.PARAM_CMD: self.cmd, + self.PARAM_OUT: self.out, + self.PARAM_REG: self.reg, + self.PARAM_DEPS: self.deps, + self.PARAM_LOCK: self.locked + } + + return data + + def dumps(self): + return yaml.dump(self.dict) + + def dump(self, fname): + with open(fname, 'w+') as fd: + fd.write(self.dumps()) + + @staticmethod + def loadd(data, fname=None): + return CommandFile(data.get(CommandFile.PARAM_CMD, None), + data.get(CommandFile.PARAM_OUT, None), + data.get(CommandFile.PARAM_REG, None), + data.get(CommandFile.PARAM_DEPS, None), + data.get(CommandFile.PARAM_LOCK, None), + fname) + + @staticmethod + def load(fname): + return CommandFile._load(fname, CommandFile, fname) diff --git a/dvc/command/run.py b/dvc/command/run.py index c4e84db08..d027fafd0 100644 --- a/dvc/command/run.py +++ b/dvc/command/run.py @@ -1,11 +1,9 @@ -import yaml - from dvc.command.common.base import CmdBase +from dvc.command.common.command_file import CommandFile from dvc.exceptions import DvcException from dvc.logger import Logger from dvc.state_file import StateFile from dvc.executor import Executor -from dvc.state_file import StateFileBase class RunError(DvcException): @@ -13,57 +11,6 @@ class RunError(DvcException): DvcException.__init__(self, 'Run error: {}'.format(msg)) -class CommandFile(StateFileBase): - MAGIC = 'DVC-Command-State' - VERSION = '0.1' - - PARAM_CMD = 'cmd' - PARAM_OUT = 'out' - PARAM_REG = 'reg' - PARAM_DEPS = 'deps' - PARAM_LOCK = 'locked' - - def __init__(self, cmd, out, reg, deps, locked, fname): - self.cmd = cmd - self.out = out - self.reg = reg - self.deps = deps - self.locked = locked - self.fname = fname - - @property - def dict(self): - data = { - self.PARAM_CMD: self.cmd, - self.PARAM_OUT: self.out, - self.PARAM_REG: self.reg, - self.PARAM_DEPS: self.deps, - self.PARAM_LOCK: self.locked - } - - return data - - def dumps(self): - return yaml.dump(self.dict) - - def dump(self, fname): - with open(fname, 'w+') as fd: - fd.write(self.dumps()) - - @staticmethod - def loadd(data, fname=None): - return CommandFile(data.get(CommandFile.PARAM_CMD, None), - data.get(CommandFile.PARAM_OUT, None), - data.get(CommandFile.PARAM_REG, None), - data.get(CommandFile.PARAM_DEPS, None), - data.get(CommandFile.PARAM_LOCK, None), - fname) - - @staticmethod - def load(fname): - return CommandFile._load(fname, CommandFile, fname) - - class CmdRun(CmdBase): def __init__(self, settings): super(CmdRun, self).__init__(settings)