def show(app, args): """Show file metadata.""" mf = MadFile(app, args.file) if not args.human: for k in mf.keys(): print('{}\t{}'.format(k, mf[k])) else: keylen = max([len(k) for k in mf.keys()]) mfs = '{:' + str(keylen) + '}' for k in sorted(mf.keys()): if k != '_id': kname, kinfo = key_info(app.conf, k) tag = '' for cc in 'transient core'.split(): if cc in kinfo['cat']: tag += colors.color(cc[0], fg=app.conf['color'][cc]['fg'], bg=app.conf['color'][cc]['bg']) else: tag += ' ' val = mf[k] if kinfo['shape'] == 'one' else '|'.join(mf[k]) print(tag, colors.color(mfs.format(k)), colors.color(': {}'.format(val)), sep=' ')
def dset(app, args): key, info = key_info(app.conf, args.key) value = info['transformer'](args.value) d = {} if os.path.exists('./mad.config'): with open('mad.config') as F: d = yaml.load(F) info['setter'](d, key, value) with open('mad.config', 'w') as F: yaml.dump(d, F, default_flow_style=False)
def find(app, args): query = {} db = get_db(app) for term in args.term: rawkey, rawval = term.split('=', 1) keyname, keyinfo = key_info(app, rawkey) val = keyinfo['transformer'](rawval) lg.info('search: {} {}'.format(keyname, val)) if keyinfo['shape'] == 'one': if keyname not in query: query[keyname] = {"$in": [val]} else: raise NotImplemented() elif keyinfo['shape'] == 'set': if keyname not in query: query[keyname] = {'$all': [val]} for rec in db.transient.find(query): print(rec['filename'])
def forget(app, args): """Forget a key, or key value combination.""" key, kinfo = key_info(app.conf, args.key) db = get_db(app) if args.value: lg.warning("forget key=value {}={}".format(key, args.value)) if kinfo.get('shape', 'one') == 'set': db.transient.update({}, {"$pull": { key: args.value }}, upsert=False, multi=True) db.core.update({}, {"$pull": { key: args.value }}, upsert=False, multi=True) else: raise NotImplemented() else: lg.warning("forget key {}".format(key)) raise NotImplemented()
def __setitem__(self, rawkey, val): """Set value for this MF.""" key, kinfo = key_info(self.app.conf, rawkey) val = kinfo['transformer'](val) setter = kinfo['setter'] keycat = kinfo['cat'] if not 'transient' in keycat: return # determine new key value using the `setter` changed, setvalue = setter(self.transient_rec, key, val) lg.debug('Set "{}" = "{}" for {}"'.format(key, val, setvalue)) if not self.quick and 'core' in keycat: if (key in self.core_rec) and (self.core_rec[key] == setvalue): #already in core pass else: self.core_rec[key] = setvalue changed = True # store in transient db if (key in self.transient_rec) and (self.transient_rec[key] == setvalue): #already in transient pass else: self.transient_rec[key] = setvalue changed = True if changed: self.dirty = True lg.debug('saving transient+rec for {}'.format(self.filename)) self.save()
def sum(app, args): """ Show the associated mongodb record """ if not args.key: db = get_db(app) notrans = db.transient.count() print("No Transient records: ", notrans) if notrans > 0: print( "Total data Transient: ", nicesize( list( db.transient.aggregate([{ "$group": { "_id": None, "total": { "$sum": "$size" } } }]))[0]['total'])) print(" No Core records: ", db.transient.count()) return kname, kinfo = key_info(app.conf, args.key) res = _single_sum(app, group_by=kname, force=args.force) total_size = int(0) total_count = 0 mgn = len("Total") for reshost in res: gid = reshost['_id'] if gid is None: mgn = max(4, mgn) else: mgn = max(len(str(reshost['_id'])), mgn) fms = "{:" + str(mgn) + "}\t{:>10}\t{:>9}" if args.human: print("# {}:".format(kname)) for reshost in res: total = reshost['total'] count = reshost['count'] total_size += int(total) total_count += count if args.human: total_human = nicesize(total) count_human = nicenumber(count) categ = reshost['_id'] if categ is None: categ = "<undefined>" print(fms.format(categ, total_human, count_human)) else: print("{}\t{}\t{}".format(reshost['_id'], total, count)) if args.human: total_size_human = nicesize(total_size) total_count_human = nicenumber(total_count) print(fms.format('', '-' * 10, '-' * 9)) print(fms.format("Total", total_size_human, total_count_human)) else: print("Total\t{}\t{}".format(total_size, total_count))