Package kenozooid :: Package cli :: Module dc

Source Code for Module kenozooid.cli.dc

  1  # 
  2  # Kenozooid - dive planning and analysis toolbox. 
  3  # 
  4  # Copyright (C) 2009-2019 by Artur Wroblewski <wrobell@riseup.net> 
  5  # 
  6  # This program is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 18  # 
 19   
 20  """ 
 21  Dive computer related Kenozooid command line commands. 
 22  """ 
 23   
 24  import logging 
 25   
 26  from kenozooid.component import inject 
 27  from kenozooid.cli import CLICommand, ArgumentError, add_master_command, \ 
 28          add_uddf_input 
 29  from kenozooid.component import query, params 
 30  from kenozooid.driver import DeviceDriver, Simulator, DataParser 
 31   
 32  log = logging.getLogger('kenozooid.cli.dc') 
 33   
 34  # for commands 'sim plan', 'sim replay' 
 35  add_master_command('sim', 
 36          'Kenozooid dive simulation commands', 
 37          'simulate dives with a dive computer') 
38 39 @inject(CLICommand, name='drivers') 40 -class ListDrivers(object):
41 """ 42 Command to list dive computers drivers. 43 """ 44 description = 'list available dive computer drivers and their capabilities' 45 46 @classmethod
47 - def add_arguments(self, parser):
48 """ 49 No arguments for drivers listing. 50 """
51 52
53 - def __call__(self, args):
54 """ 55 Execute drivers listing command. 56 """ 57 drivers = query(DeviceDriver) 58 print('Available drivers:\n') 59 for cls in drivers: 60 p = params(cls) 61 id = p['id'] 62 name = p['name'] 63 drivers = query(id=id) 64 65 # find capabilities 66 caps = [] 67 if len(tuple(query(Simulator, id=id))) > 0: 68 caps.append('simulation') 69 if len(tuple(query(DataParser, id=id))) > 0: 70 caps.append('backup') 71 #if len(tuple(query(DiveLog, id=id))) > 0: 72 # caps.append('divelog') 73 # ... etc ... 74 75 print('%s (%s): %s' % (id, name, ', '.join(caps)))
76
77 78 ### def cmd_scan(parser, options, args): 79 ### from kenozooid.component import query, params 80 ### from kenozooid.driver import DeviceDriver, DeviceError 81 ### 82 ### print 'Scanning...\n' 83 ### for cls in query(DeviceDriver): 84 ### for drv in cls.scan(): 85 ### p = params(cls) 86 ### id = p['id'] 87 ### name = p['name'] 88 ### try: 89 ### print 'Found %s (%s): %s' % (id, name, drv.version()) 90 ### except DeviceError, ex: 91 ### print >> sys.stderr, 'Device %s (%s) error: %s' % (id, name, ex) 92 93 94 @inject(CLICommand, name='sim plan') 95 -class Simulate(object):
96 """ 97 Simulate dive on a dive computer. 98 """ 99 description = 'simulate dive with a dive computer' 100 101 @classmethod
102 - def add_arguments(self, parser):
103 """ 104 Add dive computer dive simulation arguments. 105 """ 106 parser.add_argument('--no-start', 107 action='store_false', 108 dest='sim_start', 109 default=True, 110 help='assume simulation is started, don\'t start simulation') 111 parser.add_argument('--no-stop', 112 action='store_false', 113 dest='sim_stop', 114 default=True, 115 help='don\'t stop simulation, leave dive computer in simulation mode') 116 parser.add_argument('driver', 117 nargs=1, 118 help='device driver id') 119 parser.add_argument('port', 120 nargs=1, 121 help='device port, i.e. /dev/ttyUSB0, COM1') 122 parser.add_argument('plan', 123 nargs=1, 124 help='dive plan')
125 126
127 - def __call__(self, args):
128 """ 129 Execute dive computer dive simulation. 130 """ 131 import kenozooid.simulation as ks 132 from kenozooid.driver import Simulator, find_driver 133 134 drv = args.driver[0] 135 port = args.port[0] 136 spec = args.plan[0] 137 138 sim = find_driver(Simulator, drv, port) 139 140 if sim is None: 141 raise ArgumentError('Device driver %s does not support simulation' 142 .format(drv)) 143 # '0:30,15 3:00,25 9:00,25 10:30,5 13:30,5 14:00,0') 144 p = ks.interpolate(ks.parse(spec)) 145 ks.simulate(sim, p, args.sim_start, args.sim_stop)
146
147 148 149 @inject(CLICommand, name='sim replay') 150 -class Simulate(object):
151 """ 152 Replay dive profile on a dive computer. 153 """ 154 description = 'replay dive on a dive computer' 155 156 @classmethod
157 - def add_arguments(self, parser):
158 """ 159 Add dive computer dive replay arguments. 160 """ 161 parser.add_argument('driver', 162 help='device driver id') 163 parser.add_argument('port', 164 help='device port, i.e. /dev/ttyUSB0, COM1') 165 add_uddf_input(parser)
166 167
168 - def __call__(self, args):
169 """ 170 Execute dive computer dive replay. 171 """ 172 import kenozooid.simulation as ks 173 from kenozooid.driver import Simulator, find_driver 174 import kenozooid.logbook as kl 175 176 drv = args.driver 177 port = args.port 178 179 r, f = args.input 180 dives = kl.find_dives(f, r, args.dives) 181 182 sim = find_driver(Simulator, drv, port) 183 184 if sim is None: 185 raise ArgumentError('Device driver %s does not support simulation' 186 .format(drv)) 187 188 for d, p in dives: 189 ks.simulate(sim, p)
190
191 192 193 @inject(CLICommand, name='backup') 194 -class Backup(object):
195 """ 196 Dive computer data backup Kenozooid command. 197 """ 198 description = 'backup dive computer data (logbook, settings, etc.)' 199 200 @classmethod
201 - def add_arguments(self, parser):
202 """ 203 Add arguments for dive computer data backup command. 204 """ 205 parser.add_argument('driver', 206 help='device driver id') 207 parser.add_argument('port', 208 help='device port, i.e. /dev/ttyUSB0, COM1') 209 parser.add_argument('output', 210 help='UDDF file to contain dive computer backup')
211 212
213 - def __call__(self, args):
214 """ 215 Execute dive computer data backup command. 216 """ 217 import kenozooid.dc as kd 218 219 drv_name = args.driver 220 port = args.port 221 fout = args.output 222 223 kd.backup(drv_name, port, fout)
224
225 226 227 @inject(CLICommand, name='dive extract') 228 -class DumpExtract(object):
229 """ 230 Extract dive profiles from dive computer dump (binary) data. 231 """ 232 description = 'extract dives from dive computer backup' 233 234 @classmethod
235 - def add_arguments(self, parser):
236 """ 237 Add options for dive extract command. 238 """ 239 parser.add_argument('input', 240 help='UDDF file with dive computer dump data') 241 parser.add_argument('output', 242 help='output UDDF file')
243 244
245 - def __call__(self, args):
246 """ 247 Execute dive extract command. 248 """ 249 import kenozooid.dc as kd 250 251 fin = args.input 252 fout = args.output 253 log.debug('extracting dive profiles from {} (saving to {})' \ 254 .format(fin, fout)) 255 kd.extract_dives(fin, fout)
256
257 258 259 @inject(CLICommand, name='convert') 260 -class Convert(object):
261 """ 262 Command to store binary dive computer data in UDDF file. 263 """ 264 description = 'store binary dive computer data in UDDF file' 265 266 @classmethod
267 - def add_arguments(self, parser):
268 """ 269 Add arguments for dive computer data conversion command. 270 """ 271 parser.add_argument('driver', 272 help='device driver id') 273 parser.add_argument('input', 274 help='dive computer binary data') 275 parser.add_argument('output', 276 help='UDDF file to contain dive computer backup')
277 278
279 - def __call__(self, args):
280 """ 281 Execute dive computer data conversion command. 282 """ 283 import kenozooid.dc as kd 284 285 drv_name = args.driver 286 fin = args.input 287 fout = args.output 288 289 kd.convert(drv_name, fin, fout)
290 291 292 # vim: sw=4:et:ai 293