Esempio n. 1
0
    def click(self, control, x, y, sleep_time=0):
        if x < 0 or y < 0:
            raise RuntimeError('坐标错误:(%d, %d)' % (x, y))

        try:
            if not control:
                result = self.send_command(EnumCommand.CmdClick,
                                           X=x,
                                           Y=y,
                                           SleepTime=int(sleep_time * 1000))
            else:
                result = self.send_command(EnumCommand.CmdClick,
                                           Control=control,
                                           X=x,
                                           Y=y,
                                           SleepTime=int(sleep_time * 1000))
            # print result['Result']
            return result['Result']
        except AndroidSpyError, e:
            if str(e).find('java.lang.SecurityException') >= 0:
                # 有时操作过快会出现该问题
                return False
            elif 'ErrControlNotShown' in str(e):
                return False
            elif str(e) == '控件已失效':
                # 认为点击成功了
                pic_name = '%s.png' % int(time.time())
                logger.warn('%s %s' % (e, pic_name))
                self._device.take_screen_shot(pic_name)
                return True
            else:
                raise e
Esempio n. 2
0
 def kill_process(self, package_name):
     '''杀死进程
     '''
     try:
         self.run_driver_cmd('killProcess', package_name, root=self.adb.is_rooted())
     except RuntimeError, e:
         logger.warn('killProcess error: %r' % e)
Esempio n. 3
0
def handle_part(data, match, client, channels):
    """
    Someone is leaving a channel. Let's tell everyone about it and
    remove them from the channel's user listing (and the channel from the client's).

    '^PART (?P<channel>[^\s]+)( :(?P<message>.*))'

    :type data: str
    :type match: dict
    :type client: Client
    :type channels: list
    """

    channame = match['channel']

    logger.debug("{nick} leaves channel {channame}", nick=client.nick, channame=channame)

    if not channame in channels:
        logger.warn("no channel named {channame}", channame=channame)
        return

    channel = channels[channame]

    client.channels.remove(channel)
    channels[channame].clients.remove(client)

    announce = Protocol.part(client, channel, match['message'] or 'leaving')
    channel.send(announce)
Esempio n. 4
0
 def reboot(self, wait_cpu_low=True, usage=20, duration=10, timeout=120):
     '''重启手机
     
     :param wait_cpu_low:   是否等待CPU使用率降低
     :type wait_cpu_low:    bool
     :param usage:          cpu使用率阈值
     :type  usage:          int
     :param duration:       持续时间(秒)
     :type  duration:       int
     :param timeout:        超时时间,超市时间到后,无论当前CPU使用率是多少,都会返回
     :type  timeout:        int
     '''
     pattern = re.compile(r'^.+:\d+$')
     try:
         ret = self.run_driver_cmd('reboot', True, retry_count=1, timeout=30)
         if ret == 'false': return
     except RuntimeError, e:
         logger.warn('reboot: %r' % e)
         if pattern.match(self.device_id):
             try:
                 self.adb.reboot(60)
             except RuntimeError:
                 logger.warn('reboot: %s' % e)
                 # 虚拟机会出现明明重启成功却一直不返回的情况
         else:
             self.adb.reboot(0)  # 不能使用reboot shell命令,有些手机需要root权限才能执行
Esempio n. 5
0
 def _run_server(self, server_name):
     '''运行系统测试桩
     '''
     if not self.adb.is_rooted():
         try:
             self.adb.start_service('%s/.service.HelperService' % server_name, {'serviceName': server_name})
         except RuntimeError:
             logger.warn('start helper server failed')
             self.adb.start_activity('%s/.activity.StartServiceActivity' % server_name, extra={'serviceName': 'HelperService'}, wait=False)
             time.sleep(1)
         return self._server_opend()
     
     timeout = 10
     time0 = time.time()
     while time.time() - time0 < timeout:
         try:
             ret = self.run_driver_cmd('runServer', server_name, root=self.adb.is_rooted(), retry_count=1, timeout=10)
             logger.debug('Run server %s' % ret)
             if 'service run success' in ret:
                 # wait for parent process exit
                 time.sleep(0.5)
             elif 'service is running' in ret:
                 pass
             elif 'java.lang.UnsatisfiedLinkError' in ret:
                 raise RuntimeError('启动系统测试桩进程失败:\n%s' % ret)
         except TimeoutError, e:
             logger.warn('Run server timeout: %s' % e)
         
         if self._server_opend(): return True
         time.sleep(1)
Esempio n. 6
0
def handle_part(data, match, client, channels):
    """
    Someone is leaving a channel. Let's tell everyone about it and
    remove them from the channel's user listing (and the channel from the client's).

    '^PART (?P<channel>[^\s]+)( :(?P<message>.*))'

    :type data: str
    :type match: dict
    :type client: Client
    :type channels: list
    """

    channame = match['channel']

    logger.debug("{nick} leaves channel {channame}",
                 nick=client.nick,
                 channame=channame)

    if not channame in channels:
        logger.warn("no channel named {channame}", channame=channame)
        return

    channel = channels[channame]

    client.channels.remove(channel)
    channels[channame].clients.remove(client)

    announce = Protocol.part(client, channel, match['message'] or 'leaving')
    channel.send(announce)
Esempio n. 7
0
    def get_current_activity(self):
        '''获取当前窗口
        '''
        # 突然发现目前的2.3设备都可以修改系统属性了,暂时改为全部使用ViewServer获取当前窗口
#        if self.sdk_version <= 10 and not self.debuggable:  # 使用GetCurrentActivity延时较大,改用logcat方式
#            import time
#            if not hasattr(self, '_activity_logcat'):
#                self._start_activity_logcat_thread()
#
#            timeout = 10
#            time0 = time.time()
#            while time.time() - time0 < timeout:
#                if self._current_activity: return self._current_activity
#                time.sleep(1)
#            return self._send_command('GetCurrentActivity')
        if not self._use_socket:
            return self.adb.run_shell_cmd('sh %s/SpyHelper.sh getCurrentWindow' % (qt4a_path), True)
        else:
            from androiddriver import SocketError
            timeout = 5
            time0 = time.time()
            while time.time() - time0 < timeout:
                try:
                    result = self._send_command('GetCurrentWindow')
                    if not result:
                        time.sleep(0.5)
                        continue
                    return result
                except SocketError, e:
                    raise e
                except RuntimeError, e:
                    logger.warn('GetCurrentWindow error: %s' % e)
Esempio n. 8
0
 def kill_process(self, package_name):
     '''杀死进程
     '''
     try:
         self.adb.run_shell_cmd('sh %s/SpyHelper.sh killProcess %s' % (qt4a_path, package_name), True)
     except RuntimeError, e:
         logger.warn('killProcess error: %r' % e)
Esempio n. 9
0
 def install_package(self, pkg_path, pkg_name='', overwrite=False):
     '''安装应用
     '''
     from util import InstallPackageFailedError
     from adb import TimeoutError
     if not os.path.exists(pkg_path):
         raise RuntimeError('安装包:%r 不存在' % pkg_path)
     for _ in range(3):
         self.adb.install_apk(pkg_path, overwrite)
         try:
             boot_time = self.get_system_boot_time()
         except TimeoutError:
             logger.exception('get_system_boot_time failed')
             return True
         if boot_time >= 60 * 5: return True
         logger.info('System Boot Time: %s S' % boot_time)
         time0 = time.time()
         if not pkg_name: pkg_name = self.adb._get_package_name(pkg_path)
         install_success = True
         while time.time() - time0 < 60:
             # 酷派大神F2和中兴红牛V5上发现重启后安装应用容易失败
             dir_root_list = ['/data/data/%s' % pkg_name, '/sdcard/Android/data/%s' % pkg_name]
             for i in range(len(dir_root_list)):
                 try:
                     self.adb.list_dir(dir_root_list[i])
                     if len(dir_root_list) > 1: dir_root_list = [dir_root_list[i]]  # 只保留正确的位置
                     break
                 except RuntimeError, e:
                     logger.warn(u'install app error: %r' % e)
                     if i >= len(dir_root_list) - 1:
                         install_success = False
                         break
             if not install_success: break
             time.sleep(1)
         if install_success: return True
Esempio n. 10
0
def main():
    """
	Main entry to arrows.
	"""
    logger.warn("arrows started.")

    time.sleep(1)
    rt_arrow()
    minute_arrow()
    day_arrow()

    while True:
        try:
            # schedule crash guard.
            importlib.reload(schedule)
            schedule.every(10).seconds.do(rt_arrow)
            schedule.every(20).seconds.do(minute_arrow)
            schedule.every(120).seconds.do(day_arrow)
            while True:
                try:
                    time.sleep(1)
                    schedule.run_pending()
                except Exception as ex:
                    logger.exception(
                        ex, "Arrows schedule.run_pending exception handler:")
                    time.sleep(2)
        except Exception as ex:
            logger.exception(ex, "Root arrows exception handler:")
            time.sleep(2)
Esempio n. 11
0
def main():
    """
	Main ts entry point.
	"""
    logger.warn("ts started.")

    time.sleep(1)
    ts_rt()
    ts_minutely()
    ts_hourly()
    ts_daily()

    while True:
        try:
            # schedule crash guard:
            importlib.reload(schedule)
            schedule.every(11).seconds.do(ts_rt)
            schedule.every(21).seconds.do(ts_minutely)
            schedule.every(701).seconds.do(ts_hourly)
            schedule.every(1701).seconds.do(ts_daily)
            while True:
                try:
                    time.sleep(2)
                    schedule.run_pending()
                except Exception as ex:
                    logger.exception(ex)
                    time.sleep(2)
        except Exception as ex:
            logger.exception(ex)
            time.sleep(2)
Esempio n. 12
0
def main():
	"""
	Main rsync entry point.
	"""
	logger.warn("leprechaun rsync started.")

	time.sleep(1)
	ts_rsync()
	sqlite_rsync()

	while True:
		try:
			# schedule crash guard:
			importlib.reload(schedule)
			schedule.every(1301).seconds.do(ts_rsync)
			schedule.every(300).seconds.do(sqlite_rsync)
			while True:
				try:
					time.sleep(2)
					schedule.run_pending()
				except Exception as ex:
					logger.exception(ex)
					time.sleep(2)
		except Exception as ex:
			logger.exception(ex)
			time.sleep(2)
Esempio n. 13
0
    def pull(self, device_id, src_file, dst_file):
        '''adb pull
        '''
        time0 = time.time()
        self._transport(device_id)
        self._send_command('sync:')
        mode, fsize, ftime = self._sync_read_mode(src_file)
        if mode == 0:
            self._sock.close()
            self._sock = None
            raise AdbError('remote object %r does not exist' % src_file)
        data = 'RECV' + struct.pack('I', len(src_file)) + src_file
        self._sock.send(data)
        f = open(dst_file, 'wb')
        data_size = 0
        last_data = ''
        while True:
            result = self._sock.recv(8)
            if len(result) != 8:
                logger.warn('返回数据错误:%r' % result)

            last_data += result
            if len(last_data) < 8:
                continue
            else:
                result = last_data[:8]
                last_data = last_data[8:]

            psize = struct.unpack('I', result[4:])[0]  # 每个分包大小
            # print psize
            if result[:4] == 'DONE': break
            elif result[:4] == 'FAIL':
                raise AdbError(self._sock.recv(psize))
            elif result[:4] != 'DATA':
                raise AdbError('pull_file error')

            # print fsize
            result = self._recv(psize - len(last_data))
            result = last_data + result
            if len(result) >= psize:
                last_data = result[psize:]
                result = result[:psize]
            else:
                raise ValueError('数据长度不一致,期望值:%d 实际值:%d' %
                                 (psize, len(result)))
            f.write(result)
            data_size += len(result)

        f.close()
        self._sock.send('QUIT' + struct.pack('I', 0))
        time_cost = time.time() - time0
        self._sock.close()
        self._sock = None
        if data_size > 0:
            return '%d KB/s (%d bytes in %fs)' % (int(
                data_size / 1000 / time_cost) if time_cost > 0 else 65535,
                                                  data_size, time_cost)
        else:
            return ''
Esempio n. 14
0
 def close_activity(self, activity):
     '''关闭Activity
     '''
     try:
         return self._close_activity(activity)
     except ProcessExitError:
         logger.warn('close_activity error: process %s not exist' % self._process_name)
         return -3  # 进程退出
Esempio n. 15
0
 def reset(self):
     if self.waiting_step:
         logger.warn(
             'Called reset() while waiting for the step to complete')
         self.step_wait()
     for pipe in self.parent_pipes:
         pipe.send(('reset', None))
     return self._decode_obses([pipe.recv() for pipe in self.parent_pipes])
Esempio n. 16
0
 def get_device_imei(self):
     '''获取设备imei号
     '''
     try:
         return self.adb.get_device_imei()
     except RuntimeError, e:
         logger.warn('获取设备imei失败: %s' % e)
         return self.adb.run_shell_cmd('sh %s/SpyHelper.sh getDeviceImei' % (qt4a_path), True)
Esempio n. 17
0
 def get_system_boot_time(self):
     '''获取系统启动时间,单位为秒
     '''
     for _ in range(3):
         ret = self.run_driver_cmd('getSystemBootTime', root=self.adb.is_rooted())
         if ret and ret.isdigit(): return int(ret) / 1000
         logger.warn('getSystemBootTime return %r' % ret)
         time.sleep(2)
     return 0
Esempio n. 18
0
 def _take_screen_shot(self, path, _format='png', quality=90):
     '''使用命令截图
     '''
     result = self.adb.run_shell_cmd('sh %s/SpyHelper.sh takeScreenshot %s %s %s' % (qt4a_path, path, _format, quality), True)
     if not 'true' in result:
         logger.warn('take_screen_shot failed: %s' % result)
         self.adb.run_shell_cmd('screencap %s' % path, True)
         self.adb.run_shell_cmd('chmod 444 %s' % path, True)
     return True
Esempio n. 19
0
 def get_system_boot_time(self):
     '''获取系统启动时间,单位为秒
     '''
     for _ in range(3):
         ret = self.adb.run_shell_cmd('sh %s/SpyHelper.sh getSystemBootTime' % qt4a_path, True)
         if ret != '' and ret.isdigit(): return int(ret) / 1000
         logger.warn('getSystemBootTime return %r' % ret)
         time.sleep(2)
     return 0
Esempio n. 20
0
    def _init_driver(self):
        '''初始化测试桩
        '''
        if AndroidSpyClient.server_opened(self._port): 
            # 字段赋值
            logger.info('port %d opened' % self._port)
            self._process['name'] = self._process_name
            self._process['id'] = self._device.adb.get_pid(self._process_name)
            if self.hello() != None:
                return

        timeout = 20
        time0 = time.time()
        proc_exist = False
        while time.time() - time0 < timeout:
            if not proc_exist:
                pid = self._device.adb.get_pid(self._process_name)
                if pid > 0:
                    proc_exist = True
                    self._process['name'] = self._process_name
                    self._process['id'] = pid
                    break

            time.sleep(1)

        if not proc_exist:
            raise RuntimeError('进程:%s 在%d秒内没有出现' % (self._process_name, timeout))
        
        timeout = 30
        
        try:
            if self._device.is_art():
                # Android 5.0上发现注入容易导致进程退出
                self._wait_for_cpu_low(20, 10)
            
            time0 = time.time()
            while time.time() - time0 < timeout:
                ret = self._device.adb.run_shell_cmd('.%s/inject %s' % (AndroidDriver.qt4a_path, self._process_name), True, timeout=120, retry_count=1)
                logger.debug('inject result: %s' % ret)
                if 'Inject Success' in ret: break
                elif 'Operation not permitted' in ret:
                    # 可能是进程处于Trace状态
                    pid = self._device.adb.get_pid(self._process_name)
                    status = self._device.adb.get_process_status(pid)
                    tracer_pid = int(status['TracerPid'])
                    if tracer_pid > 0:
                        if int(status['PPid']) == tracer_pid:
                            # 使用TRACEME方式防注入
                            raise Exception('应用使用了防注入逻辑,注入失败')
                        logger.warn('TracerPid is %d' % tracer_pid)
                        self._device.adb.kill_process(tracer_pid)
                time.sleep(1)
                
        except RuntimeError, e:
            logger.error('%s\n%s' % (e, self._device.adb.run_shell_cmd('ps')))
            logger.info(self._device.adb.dump_stack(self._process_name))
            raise e
Esempio n. 21
0
 def take_screen_shot(self, path, quality=90):
     '''截屏
     '''
     result = self.adb.run_shell_cmd('%s/screenshot capture -q %s' % (qt4a_path, quality))  # 为避免pull文件耗时,直接写到stdout
     if len(result) < 256:
         logger.warn('Take screenshot failed: %s' % result)
         return False
     with open(path, 'wb') as fp:
         fp.write(result)
     return True
Esempio n. 22
0
 def wake_screen(self, wake=True):
     '''唤醒屏幕
     '''
     try:
         ret = self._send_command('WakeScreen', Wake=wake)
     except RuntimeError, e:
         logger.warn('wake_screen error: %s' % e)
         if 'NullPointerException' in str(e):
             # 重启下系统测试桩
             self._restart_server()
             return self.wake_screen(wake)
Esempio n. 23
0
 def pull_file(self, src_path, dst_path):
     '''将手机中的文件拷贝到PC中
     '''
     fuse_path = '/storage/emulated/0'  # 4.2中对sdcard的引用
     if src_path.startswith(fuse_path):
         src_path = src_path.replace(fuse_path, '/sdcard')
     try:
         ret = self.adb.pull_file(src_path, dst_path)
         if ret.find('does not exist') < 0: return
     except RuntimeError, e:
         logger.warn('pull file failed: %s' % e)
         if src_path.startswith('/data/local/tmp/'): raise e
Esempio n. 24
0
 def connect(self):
     self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     # socket.setdefaulttimeout(120)
     for i in range(3):
         try:
             self._sock.connect((self._addr, self._port))
             self._connect = True
             return True
         except Exception, e:
             logger.warn('connect (%s, %s) error: %s' %
                         (self._addr, self._port, e))
             if i == 2: return False  # 连接失败
             time.sleep(1)
Esempio n. 25
0
 def _wait_for_event(self, thread_id, timeout):
     '''等待事件
     '''
     if self._get_lock(thread_id).is_set(): return
     time0 = time.time()
     ret = self._get_lock(thread_id).wait(timeout)
     if ret:
         cost_time = time.time() - time0
         logger.debug('thread %s wait %s S' % (thread_id, cost_time))
     else:
         # 超时
         logger.warn('thread %s wait for sync event timeout' % thread_id)
         self.resume_thread(thread_id)  # 防止出错后一直等待
     return ret
Esempio n. 26
0
def day_arrow():
	"""
	External day level wrapper.
	"""
	logger.info("<day_arrow>")
	for key in keys:
		if "timestamp" in key:
			continue
		fn = f"/data/tsdb/daily/{key}.parq"
		if os.path.exists(fn):
			df = pd.read_parquet(fn)
			racoon.to_csv(df, f"/data/adbcsv/{key}_daily.csv")
		else:
			logger.warn(f"/data/tsdb/daily/{key}.parq files are missing.")
	logger.info("</day_arrow>")
Esempio n. 27
0
 def get_current_activity(self):
     '''获取当前窗口
     '''
     timeout = 10
     time0 = time.time()
     result = None
     while time.time() - time0 < timeout:
         if not self.adb.is_rooted():
             result = self._get_current_window()
         else:
             try:
                 result = self._send_command('GetCurrentWindow')
             except SocketError, e:
                 raise e
             except RuntimeError, e:
                 logger.warn('GetCurrentWindow error: %s' % e)
Esempio n. 28
0
 def lock_keyguard(self):
     '''锁屏
     '''
     if not self.is_screen_lock_enabled():
         # 先设置为有屏幕锁
         self.set_screen_lock_enable(True)
     self._last_activity_before_lock = self._get_current_activity(True)  # 保存锁屏前的Activity
     logger.debug('锁屏前Activity为:%r' % self._last_activity_before_lock)
     ret = self._lock_keyguard()
     if not ret: 
         logger.warn('lock keyguard failed')
         self.wake_screen(False)
         time.sleep(1)
         self.wake_screen(True)
         return self.is_keyguard_locked()
     return True
    def read_temp(self, addr):
        try:
            tx_frame = [addr, 0x03, 0x00, 0x00, 0x00, 0x02]
            c16 = _crc16(tx_frame)
            h = c16 >> 8
            l = c16 & 0xff
            tx_frame += [l, h]

            self.ser.write(tx_frame)
            rsp = b""
            n = 0
            while 1:
                if n > self.timeout:
                    logger.warn("Temp meter No resp,port %s ID%s" %
                                (self.ser.port, addr))
                    break

                rsp += self.ser.read(11)
                if len(rsp) >= 9:
                    break
                else:
                    time.sleep(1 / 1000)
                    n += 1
            if len(rsp) < 9:
                return None, None
            rsp_c16 = _crc16(rsp[:7])
            h = rsp_c16 >> 8
            l = rsp_c16 & 0xff

            if not (rsp[7] == l and rsp[8] == h):
                logger.warn("CRC16 check fail, not %s should be %s" %
                            (rsp[7:], [l, h]))

            temp = rsp[5] * 256 + rsp[6]
            if temp >= 32768:
                temp -= 65536
            humi = rsp[3] * 256 + rsp[4]

            temp = round(temp / 10, 2)
            humi = round(humi / 10, 2)
            return temp, humi
        except:
            traceback.print_exc()
            return None, None
Esempio n. 30
0
    def parse(self, nodeTree, physicalInterfaces=[]):
        host_name = get_hostname(ElementParser(nodeTree))
        interfaceNodes = [
            interface for i in ElementParser(nodeTree).all("interfaces")
            if i.parent().tag() in ['configuration']
            for interface in i.all("interface")
        ]

        interface_map = {}
        for node in interfaceNodes:
            iname = node.first("name").text()
            if iname is None:
                continue
            interface = interface_map.get(iname, self.new_interface(iname))

            if physicalInterfaces and iname not in physicalInterfaces:
                logger.warn(
                    "Interface {0} is configured but not found in {1}".format(
                        iname, host_name))
                continue

            # Update interface
            self._interface(interface, node)
            interface_map[interface.name] = interface

        # Add remaining physical interfaces if any
        for iface in physicalInterfaces:
            if iface not in interface_map:
                interface_map[iface] = self.new_interface(iface)

        # Handle logical systems
        logicalNodes = [
            iface for i in ElementParser(nodeTree).all("interfaces")
            if i.parent().tag() in ['logical-systems']
            for iface in i.all("interface")
        ]
        for node in logicalNodes:
            iname = node.first("name").text()
            interface = interface_map.get(iname, self.new_interface(iname))
            # Only update unitdict for logical systems
            interface.unitdict += [self._unit(u) for u in node.all("unit")]
            interface_map[interface.name] = interface

        return sorted(interface_map.values(), key=lambda i: i.name)
Esempio n. 31
0
 def load_from_folder(self, resample, use_default_labels):
     os.chdir(self.folder_path)
     
     training_files = []
     if use_default_labels:
         for filename in [f for f in os.listdir('.') if os.path.isfile(f)]:
             label = filename.replace('_', ' ').replace('.mp3', '').title()
             training_files.append((label, filename))
             
             logger.log("Using %s with label %s" % (filename, label))
     else:    
         resample_prefix = 'resample_'
         for filename in [f for f in os.listdir('.') if os.path.isfile(f)]:
             print " enter label for '" + filename + "' >"
             label = raw_input()
             
             if len(label) == 0: label = filename
             
             target = filename
             if resample:
                 target = resample_prefix + filename
                 try:
                     subprocess.call(['/usr/local/bin/lame', '-V5', '--vbr-new', '--resample', '16', filename, target], 
                                     stdout=subprocess.PIPE)
                     
                     generated_name = label.strip().replace(" ", "_").lower() + '.mp3'
                     subprocess.call(['rm', filename])
                     subprocess.call(['mv', target, generated_name])
                     
                     training_files.append((label, generated_name))
                 except:
                     logger.warn(" could not resample file '%s'!" % filename)
             else:
                 training_files.append((label, target))
         print 'finished resampling!'
     
     self.train_set.data = audio.train(training_files, self.folder_path, self.train_set.config, self.train_set.name, conn_str)
     self.train_set.s3_links = []
     self.train_set.status = "trained"
     
     self.train_set.save()
     
     
     return self.train_set._id
Esempio n. 32
0
def main():
    """
	Main rt entry point.
	"""
    logger.warn("rt started")
    started = int(time.time())
    while True:
        try:
            elapsed = int(time.time())
            time.sleep(1)
            if int(time.time()) - started > 600:
                logger.info("heartbeat")
                started = int(time.time())
            logger.info("<write_rt>")
            write_rt()
            logger.info("</write_rt>")
            elapsed = int(time.time()) - elapsed
            time.sleep(max(0, PERIOD - elapsed))
        except Exception as ex:
            logger.exception(ex)
Esempio n. 33
0
def handle_privmsg(data, match, client, channels):
    """
    This happens when someone sends something. If the targeted channel exists,
    send it to everyone in it.

    TODO: Users can be privmsg'd too, so we should be able to handle that.

    '^PRIVMSG (?P<channel>[^\s]+) :(?P<message>.*)'

    :type data: str
    :type match: dict
    :type client: Client
    :type channels: list
    """

    logger.debug('{nick} says "{message}" to {channel}', nick=client.nick, **match)
    channame = match['channel']

    if channame in channels:
        channels[channame].say(client, match['message'])
    else:
        logger.warn('channel {channame} does not exist', channame=channame)
Esempio n. 34
0
    def parse(self, nodeTree, physicalInterfaces=[]):
        host_name = get_hostname(ElementParser(nodeTree))
        interfaceNodes = [ 
                interface for i in ElementParser(nodeTree).all("interfaces") 
                if i.parent().tag() in ['configuration']
                for interface in i.all("interface") ]

        interfaces = []
        for node in interfaceNodes:
            interface = Interface()
            interface.name = node.first("name").text()
            if physicalInterfaces:
                if not interface.name in physicalInterfaces:
                    logger.warn("Interface {0} is configured but not found in {1}".format(interface.name, host_name))
                    continue
                else:
                    physicalInterfaces.remove(interface.name)

            interface.vlantagging = len(node.all("vlan-tagging")) > 0
            interface.bundle = node.first("bundle").text()
            interface.description = node.first("description").text()
            #TODO: tunnel dict..? Does it make sense when source/dest is empty?
            interface.tunneldict.append({
                'source': node.first("source").text(),
                'destination': node.first("destination").text(),
                })

            #Units
            interface.unitdict = [ self._unit(u) for u in node.all("unit") ]
            interfaces.append(interface)
        for iface in physicalInterfaces:
            interface = Interface()
            interface.name = iface
            interfaces.append(interface)

        return interfaces
Esempio n. 35
0
def train(tracks, training_audio_path, conf, set_name, conn_str):
    logger.log('begin training sequence on %s ' % training_audio_path)
    
    # set up all model parameters
    num_components      = conf['num_components']
    em_epsilon          = conf['em_epsilon']
    em_iter             = conf['em_iter']
    cv_type             = conf['cv_type']
    
    audio_freq          = conf['audio_freq']
    mfcc_step_size      = conf['mfcc_step_size']
    mfcc_block_size     = conf['mfcc_block_size']
    frames_per_second   = audio_freq / mfcc_step_size
    
    audio_block_size    = frames_per_second * conf['sample_length']
    audio_step_size     = frames_per_second * conf['sample_step_size']
    
    # set up Yaafe
    afp, engine = get_engine(audio_freq, mfcc_block_size, mfcc_step_size)

    connection = conn.get()
    connection.register([TrackData])
    result = []
    
    for label, filename in tracks:
        logger.log('begin processing %s.' % filename)
        afp.processFile(engine, training_audio_path + '/' + filename)
        
        output      = engine.readAllOutputs()['mfcc']
        mfcc        = output     
        num_samples = mfcc.shape[0]
        track_gmms  = []
        
        track       = connection.TrackData()
        track.label = label
        track.set   = set_name

        for index in range(0, (num_samples - audio_block_size), audio_step_size):
            with warnings.catch_warnings(record=True) as w:
                mfcc_data = mfcc[index:index + audio_block_size]
    
                classifier = GMM(n_components = num_components, cvtype = cv_type)
                classifier.fit(mfcc_data, thresh = em_epsilon, n_iter = em_iter)
    
                means = classifier._get_means().tolist()  
                
                if cv_type == 'diag':          
                    covars = [np.diag(diag).tolist() for diag in classifier._get_covars()]
                else:
                    covars = classifier._get_covars().tolist()
                weights = classifier._get_weights().tolist()
                
                for weight in weights:
                    if math.isnan(weight):
                        logger.warn("Warning: invalid GMM entry, skipping at label: %s, index %s" % (label, str(index)))
                        break
                else:
                    track_gmms.append([means, covars, weights])

        track.data = track_gmms
        track.save()
        
        result.append(track._id)

    return result