def graph_ip_port(self, output): print >> output, 'digraph foo {' print >> output, 'graph [ rankdir = "LR" overlap = "scale" splines = "true" ];' print >> output, 'node [ style = "filled" fillcolor = "white" id = "\N" URL = "javascript:top.click(\'\N\')" ]' c = self.cursor() c.execute("select ip_src, ip_dst, port_dst, trans_proto, count(*) as count from packet_eth_ipv4_unicast \ group by ip_src, ip_dst, port_dst, trans_proto") rows = c.fetchall() ports = set(map ((lambda x: (x[2], x[3])), rows)) for (port_dst, proto) in ports: print >> output, '"%s%s" [ shape = "rectangle" label="%s" URL = ""];' % (proto, port_dst, port_dst) host_edges = dict() port_edges = dict() for ip_src, ip_dst, port_dst, proto, count in rows: key = (min(ip_src, ip_dst), max(ip_src, ip_dst), proto) if key not in host_edges: backward = (ip_src != min(ip_src, ip_dst)) if backward: dir = "back" else: dir = "forward" host_edges[key] = (count, dir); else: backward = (ip_src != min(ip_src, ip_dst)) if backward: dir = "back" else: dir = "forward" cdir = host_edges[key][1] if cdir != dir: dir = "both" c = host_edges[key][0] host_edges[key] = (c+count, dir); key = (ip_dst, port_dst, proto) if key not in port_edges: port_edges[key] = count; else: port_edges[key] += count; counts = map ((lambda x: x[0]), host_edges.values()) + port_edges.values() pen_width = utilities.pen_selector(10, sorted(counts)) for ((ip_src, ip_dst, proto), (count, dir)) in host_edges.iteritems(): print >> output, '"%s" -> "%s" [ color = "%s" penwidth = "%f" dir = "%s" caption="%i"];' \ % (ip_src, ip_dst, utilities.color(proto), (pen_width(count)+1), dir, count) for ((ip_dst, port_dst, proto), (count)) in port_edges.iteritems(): print >> output, '"%s" -> "%s%s" [ color = "%s" penwidth = "%f" caption="%i"];' \ % (ip_dst, proto, port_dst, utilities.color(proto), (pen_width(count)+1), count) print >> output, '}'
def stage6(): print color('OKGREEN', 'STAGE 6') events.unlisten('switch_top', stage6) events.listen('stop_tremandhits', th.stop) midi.setSustain(True) amer.stop() th.start()
def stage5(): print color('OKGREEN', 'STAGE 5') events.unlisten('stomp', stage5) events.listen('switch_top', stage6) stomp.stop() midi.setSustain(True) rhythm.start()
def stage3(): print color('OKGREEN', 'STAGE 3') events.unlisten('long_tremolo', stage3) armswipe.detect('arm_swipe') events.listen('arm_swipe', stage4) midi.setSustain(True) hr.start()
def stage2(): print color('OKGREEN', 'STAGE 2') events.unlisten('switch_top', stage2) events.listen('long_tremolo', stage3) midi.setSustain(True) ap.stop() ap2.start()
def stage5(): print color('OKGREEN','STAGE 5') events.unlisten('stomp', stage5) events.listen('switch_top', stage6) stomp.stop() midi.setSustain(True) rhythm.start()
def stage6(): print color('OKGREEN','STAGE 6') events.unlisten('switch_top', stage6) events.listen('stop_tremandhits', th.stop) midi.setSustain(True) amer.stop() th.start()
def stage3(): print color('OKGREEN','STAGE 3') events.unlisten('long_tremolo', stage3) armswipe.detect('arm_swipe') events.listen('arm_swipe', stage4) midi.setSustain(True) hr.start()
def stage2(): print color('OKGREEN','STAGE 2') events.unlisten('switch_top', stage2) events.listen('long_tremolo', stage3) midi.setSustain(True) ap.stop() ap2.start()
def stage4(): print color('OKGREEN', 'STAGE 4') events.unlisten('arm_swipe', stage4) armswipe.stop() stomp.detect() events.listen('stomp', stage5) midi.setSustain(False) amer.start() ap2.stop() hr.stop()
def stage4(): print color('OKGREEN','STAGE 4') events.unlisten('arm_swipe', stage4) armswipe.stop() stomp.detect() events.listen('stomp', stage5) midi.setSustain(False) amer.start() ap2.stop() hr.stop()
def stage1(): print color('OKGREEN', 'STAGE 1') events.listen('switch_top', stage2) midi.setSustain(True) ap.start()
if rule: rule['active'] = True rule['mode'] = 'default' print( f"{color('Upserting rule', Colors.GREEN)} for {color(track.get('name'), Colors.CYAN)} [{color(track.get('artist'), Colors.YELLOW)}]..." ) update_rule(uri, rule, track=track) else: print("Invalid rule!") else: rules, tracks = get_rules() or [{}, {}] if rules: for i, [uri, rule] in enumerate(rules.items(), start=1): data = tracks.get(uri, {}) q = tracks.get(rule.get('queue')) or {} output = ' '.join([ color(i, Colors.WHITE), '->', color(data.get('name', uri), Colors.CYAN), f"[{color(data.get('artist'), Colors.YELLOW)}] - " if data.get('artist') else ' - ', color(ts(rule.get('start', 0)), Colors.GREEN), '<->', color( ts(rule.get('end') or data.get('duration')) or 'END', Colors.RED), '/', color(ts(data.get('duration')) or 'END', Colors.RED), f"- [Q: {color(q.get('name', rule.get('queue', None)), Colors.MAGENTA)}]" ]).replace(' ', ' ').replace(' ,', ',').strip() print(output)
def graph_mac_ip(self, output): print >> output, 'digraph foo {' print >> output, '\tgraph [ rankdir = "LR" overlap = "scale" splines = "true" ];' print >> output, '\tnode [ shape = "Mrecord" ];' c = self.cursor() c.execute("select mac, group_concat(distinct label) from (\ select mac_src as mac, ip_src as label from tmp_packets_mac_ip where mac not in tmp_routers \ union \ select mac_dst as mac, ip_dst as label from tmp_packets_mac_ip where mac not in tmp_routers \ union \ select mac, 'router' as label from tmp_routers) group by mac;") for mac, label in c: print >> output, '\t"%s" [ label = "<mac> %s' % (mac, mac), for ip in label.split(','): print >> output, '| <%s> %s' % (ip.replace('.',''), ip), print >> output, '"];' c.execute("select sum(count) as count from tmp_packets_mac_ip group by mac_src, \ (case (mac_src in tmp_routers) when 1 then 'router' else ip_src end), mac_dst, \ (case (mac_dst in tmp_routers) when 1 then 'router' else ip_dst end);") counts = map ((lambda x: x[0]), c.fetchall()) pen_width = utilities.pen_selector(10, sorted(counts)) c.execute("select mac_src, \ (case (mac_src in tmp_routers) when 1 then 'router' else ip_src end) as ip_src, mac_dst, \ (case (mac_dst in tmp_routers) when 1 then 'router' else ip_dst end) as ip_dst, sum(count) as count, \ trans_proto from tmp_packets_mac_ip group by mac_src, ip_src, mac_dst, ip_dst;") for mac_src, ip_src, mac_dst, ip_dst, count, proto in c: print >> output, '\t"%s":%s -> "%s":%s [ penwidth = "%f" color = "%s" ];' \ % (mac_src, ip_src.replace('.',''), mac_dst, ip_dst.replace('.',''), (pen_width(count)+1), utilities.color(proto)) print >> output, '}'
def stage1(): print color('OKGREEN','STAGE 1') events.listen('switch_top', stage2) midi.setSustain(True) ap.start()
def queue_track(): parser = argparse.ArgumentParser(description="Spotify track queuer") parser.add_argument('title', nargs='?', default=None) parser.add_argument('artist', nargs='?', default=None) parser.add_argument('-a', '--album', action="store_true") parser.add_argument('-i', '--ignore', action='store_true') parser.add_argument('-o', '--open', action="store_true") parser.add_argument('-c', '--song', action="store_true") parser.add_argument('-s', '--save', action='store_true') parser.add_argument('-t', '--times', nargs='?', default=1, const=1, type=int) parser.add_argument('-p', '--previous', nargs='?', const=1, type=int) parser.add_argument('-l', '--list_rules', action='store_true') parser.add_argument('-m', '--make_group', action='store_true') parser.add_argument('-d', '--delete_group') parser.add_argument('-g', '--group', type=str) parser.add_argument('-r', '--remember', nargs='*', default=None) parser.add_argument('-f', '--forget', nargs='*', default=None) parser.add_argument('-n', '--amnesia', action='store_true') args = parser.parse_args() mode = "tracks" if not args.album else "albums" if args.forget: print(f"Deleting shortcut:", f"'{args.forget[0]}'", f"'{args.forget[1]}'" if len(args.forget) > 1 else '') remember_track(args.forget[0], args.forget[1] if len(args.forget) > 1 else None, None, mode, delete=True) elif args.make_group: make_group() elif args.delete_group: with open(group_file, 'r+') as gf: try: groups = json.load(gf) if args.delete_group in groups: del groups[args.delete_group] print(f"Deleting group {args.delete_group}...") sleep(2) else: print(f"Group {args.delete_group} not found!") except json.JSONDecodeError: print("No groups found!") groups = {} with open(group_file, 'w+') as gf: json.dump(groups, gf) elif args.list_rules: with open(group_file, 'r') as gf: try: groups = json.load(gf) if groups: print(f"[{color('Saved Groups', Colors.CYAN)}]\n") for name, data in groups.items(): tracks = "\n".join([ f"\t{i}. {color(d.get('name'), Colors.GREEN)} by {color(d.get('artist'), Colors.GREEN)} [{color(d.get('uri'), Colors.YELLOW)}]" for i, d in enumerate(data, start=1) ]) print(f"{color(name, Colors.MAGENTA)}: {tracks}\n") print() except json.JSONDecodeError: pass if os.path.isfile(short_file): with open(short_file, 'r') as cf: try: shortcuts = json.load(cf) tracks, albums = shortcuts.get('tracks'), shortcuts.get( 'albums') for title, ss in [("Track Shortcuts", tracks), ("Album Shortcuts", albums)]: if ss: print(f"[{color(title, Colors.CYAN)}]\n") for r in sorted([[ color(", ".join(key.split(PART_SEPARATOR)), Colors.MAGENTA), "->", f"{color(track.get('name' if mode == 'tracks' else 'album'), Colors.GREEN)} by {color(track.get('artist'), Colors.GREEN)}", f"[{color(track.get('relevant_uri', track.get('uri')), Colors.YELLOW)}]" ] for key, track in ss.items()], key=lambda l: l[2].lower()): print(*r) print() except json.JSONDecodeError: pass else: with open(short_file, 'r') as cf: try: memory = json.load(cf) except: memory = {"tracks": {}, "albums": {}} memory_key = "" if not args.title else f"{args.title.lower()}{PART_SEPARATOR}{(args.artist or '').lower()}" mobject = memory.get(mode, {}).get(memory_key, {}) artist = mobject.get('artist', args.artist) if not args.amnesia else args.artist title = mobject.get('name', args.title) if not args.amnesia else args.title uri = mobject.get('uri') or mobject.get('relevant_uri') tracks = enqueue(title=title, artist=artist, times=args.times, last=args.previous, group=args.group, uri=uri, ignore=args.ignore or args.open, mode=mode) if args.remember and tracks: if len(args.remember) == 0: print("Cannot create a shortcut without any arguments!") else: print( f"Creating shortcut for {mode[:-1]} {tracks[0].get('name' if mode == 'tracks' else 'album')} by {tracks[0].get('artist')}: ", f"'{args.remember[0]}'", f"'{args.remember[1]}'" if len(args.remember) > 1 else '') remember_track( args.remember[0], args.remember[1] if len(args.remember) > 1 else None, tracks[0], mode) if args.save: if prefs.get("DEFAULT_PLAYLIST"): track_uris = [t.get("uri") for t in tracks if t.get("uri")] if len(track_uris) > 0: resp = spotify.post( f"https://api.spotify.com/v1/playlists/{prefs.get('DEFAULT_PLAYLIST')}/tracks?uris={','.join(track_uris)}" ) if 200 <= resp.status_code < 300: print( f"Added {', '.join(t.get('name') for t in tracks)} to playlist!" ) else: print( f"Something went wrong while adding to playlist (status code {resp.status_code})" ) else: print("No tracks found!") else: print( "Could not locate a default playlist; try adding one to preferences.json?" ) if args.open: if prefs.get("LASTFM_USER"): artist_fmt = lambda t: t.get("artist").replace(" ", "+").split( ",")[0] track_artists = set(artist_fmt(t) for t in tracks) track_albums = set( (artist_fmt(t), t.get("album").replace(" ", "+")) for t in tracks) track_songs = set( (artist_fmt(t), t.get("name").replace(" ", "+")) for t in tracks) if args.song: for art, s in track_songs: webbrowser.open( f"https://www.last.fm/user/Nathansbud/library/music/{art}/_/{s}" ) elif args.album: for art, alb in track_albums: webbrowser.open( f"https://www.last.fm/user/Nathansbud/library/music/{art}/{alb}" ) else: for t in track_artists: webbrowser.open( f"https://www.last.fm/user/Nathansbud/library/music/{t}" ) else: print( "Could not find a Last.fm username; try adding one to preferences.json?" )