1from __future__ import unicode_literals 2 3import dvc.logger as logger 4from dvc.exceptions import DvcException 5from dvc.command.base import CmdBase, fix_subparsers 6 7 8class CmdMetricsShow(CmdBase): 9 def _show(self, metrics): 10 for branch, val in metrics.items(): 11 if self.args.all_branches or self.args.all_tags: 12 logger.info("{}:".format(branch)) 13 14 for fname, metric in val.items(): 15 logger.info("\t{}: {}".format(fname, metric)) 16 17 def run(self): 18 typ = self.args.type 19 xpath = self.args.xpath 20 try: 21 metrics = self.repo.metrics.show( 22 self.args.path, 23 typ=typ, 24 xpath=xpath, 25 all_branches=self.args.all_branches, 26 all_tags=self.args.all_tags, 27 recursive=self.args.recursive, 28 ) 29 30 self._show(metrics) 31 except DvcException: 32 logger.error("failed to show metrics") 33 return 1 34 35 return 0 36 37 38class CmdMetricsModify(CmdBase): 39 def run(self): 40 try: 41 self.repo.metrics.modify( 42 self.args.path, typ=self.args.type, xpath=self.args.xpath 43 ) 44 except DvcException: 45 logger.error("failed to modify metric file settings") 46 return 1 47 48 return 0 49 50 51class CmdMetricsAdd(CmdBase): 52 def run(self): 53 try: 54 self.repo.metrics.add( 55 self.args.path, self.args.type, self.args.xpath 56 ) 57 except DvcException: 58 msg = "failed to add metric file '{}'".format(self.args.path) 59 logger.error(msg) 60 return 1 61 62 return 0 63 64 65class CmdMetricsRemove(CmdBase): 66 def run(self): 67 try: 68 self.repo.metrics.remove(self.args.path) 69 except DvcException: 70 msg = "failed to remove metric file '{}'".format(self.args.path) 71 logger.error(msg) 72 return 1 73 74 return 0 75 76 77def add_parser(subparsers, parent_parser): 78 METRICS_HELP = ( 79 "A set of commands to add, manage, collect and display project " 80 "metrics." 81 ) 82 metrics_parser = subparsers.add_parser( 83 "metrics", 84 parents=[parent_parser], 85 description=METRICS_HELP, 86 help=METRICS_HELP, 87 ) 88 89 metrics_subparsers = metrics_parser.add_subparsers( 90 dest="cmd", 91 help="Use dvc metrics CMD --help to display command-specific help.", 92 ) 93 94 fix_subparsers(metrics_subparsers) 95 96 METRICS_SHOW_HELP = "Output metric values." 97 metrics_show_parser = metrics_subparsers.add_parser( 98 "show", 99 parents=[parent_parser], 100 description=METRICS_SHOW_HELP, 101 help=METRICS_SHOW_HELP, 102 ) 103 metrics_show_parser.add_argument( 104 "path", nargs="?", help="Path to a metric file or a directory." 105 ) 106 metrics_show_parser.add_argument( 107 "-t", "--type", help="Type of metrics (raw/json/tsv/htsv/csv/hcsv)." 108 ) 109 metrics_show_parser.add_argument( 110 "-x", "--xpath", help="json/tsv/htsv/csv/hcsv path." 111 ) 112 metrics_show_parser.add_argument( 113 "-a", 114 "--all-branches", 115 action="store_true", 116 default=False, 117 help="Show metrics for all branches.", 118 ) 119 metrics_show_parser.add_argument( 120 "-T", 121 "--all-tags", 122 action="store_true", 123 default=False, 124 help="Show metrics for all tags.", 125 ) 126 metrics_show_parser.add_argument( 127 "-R", 128 "--recursive", 129 action="store_true", 130 default=False, 131 help=( 132 "If path is a directory, recursively search and process metric " 133 "files in path." 134 ), 135 ) 136 metrics_show_parser.set_defaults(func=CmdMetricsShow) 137 138 METRICS_ADD_HELP = "Tag file as a metric file." 139 metrics_add_parser = metrics_subparsers.add_parser( 140 "add", 141 parents=[parent_parser], 142 description=METRICS_ADD_HELP, 143 help=METRICS_ADD_HELP, 144 ) 145 metrics_add_parser.add_argument( 146 "-t", "--type", help="Type of metrics (raw/json/tsv/htsv/csv/hcsv)." 147 ) 148 metrics_add_parser.add_argument( 149 "-x", "--xpath", help="json/tsv/htsv/csv/hcsv path." 150 ) 151 metrics_add_parser.add_argument("path", help="Path to a metric file.") 152 metrics_add_parser.set_defaults(func=CmdMetricsAdd) 153 154 METRICS_MODIFY_HELP = "Modify metric file options." 155 metrics_modify_parser = metrics_subparsers.add_parser( 156 "modify", 157 parents=[parent_parser], 158 description=METRICS_MODIFY_HELP, 159 help=METRICS_MODIFY_HELP, 160 ) 161 metrics_modify_parser.add_argument( 162 "-t", "--type", help="Type of metrics (raw/json/tsv/htsv/csv/hcsv)." 163 ) 164 metrics_modify_parser.add_argument( 165 "-x", "--xpath", help="json/tsv/htsv/csv/hcsv path." 166 ) 167 metrics_modify_parser.add_argument("path", help="Path to a metric file.") 168 metrics_modify_parser.set_defaults(func=CmdMetricsModify) 169 170 METRICS_REMOVE_HELP = "Remove files's metric tag." 171 metrics_remove_parser = metrics_subparsers.add_parser( 172 "remove", 173 parents=[parent_parser], 174 description=METRICS_REMOVE_HELP, 175 help=METRICS_REMOVE_HELP, 176 ) 177 metrics_remove_parser.add_argument("path", help="Path to a metric file.") 178 metrics_remove_parser.set_defaults(func=CmdMetricsRemove) 179