Refactoring: extract CommandFile class

This commit is contained in:
Dmitry
2017-11-26 21:49:06 -08:00
parent 50f736d9d9
commit 40660b5120
3 changed files with 56 additions and 55 deletions
+1 -1
View File
@@ -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):
+54
View File
@@ -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)
+1 -54
View File
@@ -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)