Example #1
0
    def __init__(self, parent=None):
        QtWidgets.QListWidget.__init__(self, parent)
        self.setAlternatingRowColors(True)

        self.nm = NetworkManager()

        self.bus = dbus.SystemBus()
        nm_bus = self.bus.get_object(NM_BUS_NAME, NM_OBJECT_PATH)

        nm_interface = dbus.Interface(nm_bus, NM_INTERFACE)
        nm_interface.connect_to_signal(
            "DeviceAdded",
            lambda *args: self.showMessage("A new device added.", True))
        nm_interface.connect_to_signal("DeviceAdded", self.fillConnections)
        nm_interface.connect_to_signal(
            "DeviceRemoved",
            lambda *args: self.showMessage("A device removed.", True))
        nm_interface.connect_to_signal("DeviceRemoved", self.fillConnections)
        nm_interface.connect_to_signal("PropertiesChanged",
                                       lambda *args: self.stateChanged.emit())

        nm_settings_bus = self.bus.get_object(NM_BUS_NAME,
                                              NM_SETTINGS_OBJECT_PATH)
        nm_settings = dbus.Interface(nm_settings_bus, NM_SETTINGS)
        nm_settings.connect_to_signal(
            "NewConnection",
            lambda *args: self.showMessage("A new connection added.", True))
        nm_settings.connect_to_signal("NewConnection", self.fillConnections)

        self.timer = QTimer()
        self.timer.setSingleShot(True)
        self.timer.timeout.connect(self.hideMessage)

        self.msgbox = None
        self.fillConnections()
Example #2
0
File: sdl.py Project: jclopes/ZGame
def main():
 
    # create the managers
    eMngr = EventManager()
    gMngr = GraphicManager(eMngr)
    iMngr = InputManager(eMngr, gMngr)
    uMngr = UpdateManager(eMngr)
    sMngr = SoundManager(eMngr)
    nMngr = NetworkManager(eMngr)

    print "starting game"
    # create file with input
    playFile = open(sys.argv[1], 'r')
    # start the managers
    eMngr.start()
    gMngr.start()
    iMngr.start()
    uMngr.start()
    sMngr.start()
    nMngr.start()

    # create game and start it
    # FIXME playfile should be passed to the updateManager
    game = Game(iMngr, uMngr, gMngr, sMngr, nMngr, playFile)
    game.run()

    # close the managers
    eMngr.stop()
    uMngr.stop()
    iMngr.stop()
    gMngr.stop()
    sMngr.stop()
    nMngr.stop()
 def __init__(self, maxy, maxx, install_config, title, intro, dest, setup_network=False):
     self.install_config = install_config
     self.maxy = maxy
     self.maxx = maxx
     self.title = title
     self.intro = intro
     self.netmgr = None
     self.dest = dest
     self.setup_network = setup_network
     if self.setup_network:
         self.netmgr = NetworkManager(install_config)
    def __init__(self, parent = None):
        QtWidgets.QListWidget.__init__(self, parent)
        self.setAlternatingRowColors(True)

        self.nm = NetworkManager()

        self.bus = dbus.SystemBus()
        nm_bus = self.bus.get_object(NM_BUS_NAME, NM_OBJECT_PATH)

        nm_interface = dbus.Interface(nm_bus, NM_INTERFACE)
        nm_interface.connect_to_signal("DeviceAdded", lambda *args: self.showMessage("A new device added.", True))
        nm_interface.connect_to_signal("DeviceAdded", self.fillConnections)
        nm_interface.connect_to_signal("DeviceRemoved", lambda *args: self.showMessage("A device removed.", True))
        nm_interface.connect_to_signal("DeviceRemoved", self.fillConnections)
        nm_interface.connect_to_signal("PropertiesChanged", lambda *args: self.stateChanged.emit())

        nm_settings_bus = self.bus.get_object(NM_BUS_NAME, NM_SETTINGS_OBJECT_PATH)
        nm_settings = dbus.Interface(nm_settings_bus, NM_SETTINGS)
        nm_settings.connect_to_signal("NewConnection", lambda *args: self.showMessage("A new connection added.", True))
        nm_settings.connect_to_signal("NewConnection", self.fillConnections)

        self.timer = QTimer()
        self.timer.setSingleShot(True)
        self.timer.timeout.connect(self.hideMessage)

        self.msgbox = None
        self.fillConnections()
Example #5
0
def main():

    # create the managers
    eMngr = EventManager()
    gMngr = GraphicManager(eMngr)
    iMngr = InputManager(eMngr, gMngr)
    uMngr = UpdateManager(eMngr)
    sMngr = SoundManager(eMngr)
    nMngr = NetworkManager(eMngr)

    print "starting game"
    # create file with input
    playFile = open(sys.argv[1], 'r')
    # start the managers
    eMngr.start()
    gMngr.start()
    iMngr.start()
    uMngr.start()
    sMngr.start()
    nMngr.start()

    # create game and start it
    # FIXME playfile should be passed to the updateManager
    game = Game(iMngr, uMngr, gMngr, sMngr, nMngr, playFile)
    game.run()

    # close the managers
    eMngr.stop()
    uMngr.stop()
    iMngr.stop()
    gMngr.stop()
    sMngr.stop()
    nMngr.stop()
Example #6
0
    def __init__(self):
        httplib.HTTPConnection.debuglevel = self.http_debuglevel
        self.network = NetworkManager(crawler=self)

        self.pool = Pool()
        self.lock = RLock()
        self.bloom_filters = {}
        self.name = self.__class__.__name__
        self._status = {
            'process_count': 0,
            'is_stop': True,
            'run_seconds': 0,
            'crawler_name': self.name,
        }
    def slotCreateVMLesson(self):
        vmBtn = self.sender()
        if vmBtn:
            LogRecord.instance().logger.info(u'开始创建虚拟机')
            if globalvariable.VM_IS_CREATE_STATUS:
                LogRecord.instance().logger.info(u'重复点击!')
                return
            else:
                globalvariable.VM_IS_CREATE_STATUS = True

            LogRecord.instance().logger.info(u'准备获取虚拟机信息!')
            paramInfo = vmBtn.getVMInfo()
            LogRecord.instance().logger.info(u'准备获取虚拟机信息完成!')
            try:
                vmName = VMOperation.instance().createVMLesson(paramInfo)
            except:
                LogRecord.instance().logger.info(u'创建虚拟机异常.........!')
            LogRecord.instance().logger.info(u'得到创建的虚拟机信息!')
            if vmName:
                LogRecord.instance().logger.info(u'创建虚拟机%s成功' % vmName)
                vmInfo = NetworkManager.instance().getVMInfo(vmName)
                if vmInfo == None:
                    globalvariable.VM_IS_CREATE_STATUS = False
                    return
                if len(vmInfo) == 0:
                    globalvariable.VM_IS_CREATE_STATUS = False
                    return
                vmInfo[0]["vmname"] = vmInfo[0]["name"]
                if vmInfo:
                    VMOperation.instance().setCurrentVMInfo(vmInfo[0])
                    if VMOperation.instance().openVM(True):
                        #保存开启虚拟机的名称
                        #globalvariable.VM_IS_CREATE_STATUS = False
                        WindowMonitor.instance().insertVmId(vmName)
                    else:
                        globalvariable.VM_IS_CREATE_STATUS = False
                        #删除没有成功运行的虚拟机
                        if VMOperation.instance().removeVMLesson(vmName):
                            LogRecord.instance().logger.info(u"删除后台相应的虚拟机成功")
                else:
                    LogRecord.instance().logger.info(u'未查询到相应的虚拟机:%s' % vmName)
                    globalvariable.VM_IS_CREATE_STATUS = False
            else:
                LogRecord.instance().logger.info(u'创建虚拟机失败')
                globalvariable.VM_IS_CREATE_STATUS = False


            #刷新虚拟机界面
            self.emit(SIGNAL("refreshVMS"))
class QNetworkManager(QtWidgets.QListWidget):
    stateChanged=pyqtSignal()
    def __init__(self, parent = None):
        QtWidgets.QListWidget.__init__(self, parent)
        self.setAlternatingRowColors(True)

        self.nm = NetworkManager()

        self.bus = dbus.SystemBus()
        nm_bus = self.bus.get_object(NM_BUS_NAME, NM_OBJECT_PATH)

        nm_interface = dbus.Interface(nm_bus, NM_INTERFACE)
        nm_interface.connect_to_signal("DeviceAdded", lambda *args: self.showMessage("A new device added.", True))
        nm_interface.connect_to_signal("DeviceAdded", self.fillConnections)
        nm_interface.connect_to_signal("DeviceRemoved", lambda *args: self.showMessage("A device removed.", True))
        nm_interface.connect_to_signal("DeviceRemoved", self.fillConnections)
        nm_interface.connect_to_signal("PropertiesChanged", lambda *args: self.stateChanged.emit())

        nm_settings_bus = self.bus.get_object(NM_BUS_NAME, NM_SETTINGS_OBJECT_PATH)
        nm_settings = dbus.Interface(nm_settings_bus, NM_SETTINGS)
        nm_settings.connect_to_signal("NewConnection", lambda *args: self.showMessage("A new connection added.", True))
        nm_settings.connect_to_signal("NewConnection", self.fillConnections)

        self.timer = QTimer()
        self.timer.setSingleShot(True)
        self.timer.timeout.connect(self.hideMessage)

        self.msgbox = None
        self.fillConnections()

    def isActive(self, connection):
        if not self.nm.active_connections:
            return False
        return len(self.filterByConnection(connection)) > 0

    def getState(self, connection):
        return self.filterByConnection(connection)[0].state.value

    def filterByConnection(self, connection):
        return filter(lambda x: x.connection.settings.uuid == \
                                  connection.settings.uuid and \
                        unicode(x.connection.settings.id) == \
                          unicode(connection.settings.id), self.nm.active_connections)

    def fillConnections(self, *args):
        self.clearList()
        actives = self.nm.active_connections
        for connection in self.nm.connections:
            item = QtWidgets.QListWidgetItem()
            item.setSizeHint(QSize(200, 38))
            self.addItem(item)
            self.setItemWidget(item, ConnectionItem(self, connection))

    def clearList(self):
        for i in range(self.count()):
            item = self.item(i)
            widget = self.itemWidget(item)
            widget.available = False
            del widget, item
        self.clear()

    def hideMessage(self):
        if self.msgbox.isVisible():
            self.msgbox.animate(start = CURRENT, stop = BOTCENTER, direction = OUT)

    def showMessage(self, message, timed=False):
        if not self.msgbox:
            self.msgbox = PMessageBox(self.viewport())
            self.msgbox.setStyleSheet(PMessageBox.Style)

        self.msgbox.setMessage(message)
        self.msgbox.animate(start = BOTCENTER, stop = BOTCENTER)

        if timed:
            self.timer.start(2000)

    def setState(self, sender):
        if self.isActive(sender.connection):
            self.disconnect(sender.connection)
        else:
            self.connect(sender.connection)

    def disconnect(self, connection):
        self.nm.disconnect_connection_devices(connection)
        self.showMessage("Disconnected from %s... " % connection.settings.id, True)

    def connect(self, connection):
        if connection in self.nm.available_connections:
            self.nm.activate_connection(connection)
            self.showMessage("Connecting to %s... " % connection.settings.id, True)
        else:
            self.showMessage("Device is not ready for %s connection." % connection.settings.id, True)
Example #9
0
class QNetworkManager(QtGui.QListWidget):
    def __init__(self, parent=None):
        QtGui.QListWidget.__init__(self, parent)
        self.setAlternatingRowColors(True)

        self.nm = NetworkManager()

        self.bus = dbus.SystemBus()
        nm_bus = self.bus.get_object(NM_BUS_NAME, NM_OBJECT_PATH)

        nm_interface = dbus.Interface(nm_bus, NM_INTERFACE)
        nm_interface.connect_to_signal(
            "DeviceAdded",
            lambda *args: self.showMessage("A new device added.", True))
        nm_interface.connect_to_signal("DeviceAdded", self.fillConnections)
        nm_interface.connect_to_signal(
            "DeviceRemoved",
            lambda *args: self.showMessage("A device removed.", True))
        nm_interface.connect_to_signal("DeviceRemoved", self.fillConnections)
        nm_interface.connect_to_signal(
            "PropertiesChanged",
            lambda *args: self.emit(SIGNAL("stateChanged()")))

        nm_settings_bus = self.bus.get_object(NM_BUS_NAME,
                                              NM_SETTINGS_OBJECT_PATH)
        nm_settings = dbus.Interface(nm_settings_bus, NM_SETTINGS)
        nm_settings.connect_to_signal(
            "NewConnection",
            lambda *args: self.showMessage("A new connection added.", True))
        nm_settings.connect_to_signal("NewConnection", self.fillConnections)

        self.timer = QTimer()
        self.timer.setSingleShot(True)
        self.timer.timeout.connect(self.hideMessage)

        self.msgbox = None
        self.fillConnections()

    def isActive(self, connection):
        if not self.nm.active_connections:
            return False
        return len(self.filterByConnection(connection)) > 0

    def getState(self, connection):
        return self.filterByConnection(connection)[0].state.value

    def filterByConnection(self, connection):
        return filter(lambda x: x.connection.settings.uuid == \
                                  connection.settings.uuid and \
                        unicode(x.connection.settings.id) == \
                          unicode(connection.settings.id), self.nm.active_connections)

    def fillConnections(self, *args):
        self.clearList()
        actives = self.nm.active_connections
        for connection in self.nm.connections:
            item = QtGui.QListWidgetItem()
            item.setSizeHint(QSize(200, 38))
            self.addItem(item)
            self.setItemWidget(item, ConnectionItem(self, connection))

    def clearList(self):
        for i in range(self.count()):
            item = self.item(i)
            widget = self.itemWidget(item)
            widget.available = False
            del widget, item
        self.clear()

    def hideMessage(self):
        if self.msgbox.isVisible():
            self.msgbox.animate(start=CURRENT, stop=BOTCENTER, direction=OUT)

    def showMessage(self, message, timed=False):
        if not self.msgbox:
            self.msgbox = PMessageBox(self.viewport())
            self.msgbox.setStyleSheet(PMessageBox.Style)

        self.msgbox.setMessage(message)
        self.msgbox.animate(start=BOTCENTER, stop=BOTCENTER)

        if timed:
            self.timer.start(2000)

    def setState(self, sender):
        if self.isActive(sender.connection):
            self.disconnect(sender.connection)
        else:
            self.connect(sender.connection)

    def disconnect(self, connection):
        self.nm.disconnect_connection_devices(connection)
        self.showMessage("Disconnected from %s... " % connection.settings.id,
                         True)

    def connect(self, connection):
        if connection in self.nm.available_connections:
            self.nm.activate_connection(connection)
            self.showMessage("Connecting to %s... " % connection.settings.id,
                             True)
        else:
            self.showMessage(
                "Device is not ready for %s connection." %
                connection.settings.id, True)
class FileDownloader(object):

    def __init__(self, maxy, maxx, install_config, title, intro, dest, setup_network=False):
        self.install_config = install_config
        self.maxy = maxy
        self.maxx = maxx
        self.title = title
        self.intro = intro
        self.netmgr = None
        self.dest = dest
        self.setup_network = setup_network
        if self.setup_network:
            self.netmgr = NetworkManager(install_config)

    def ask_proceed_unsafe_download(self, fingerprint):
        msg = ('This server could not prove its authenticity. Its '
                'fingerprint is:\n\n' + fingerprint +
                '\n\nDo you wish to proceed?\n')
        conf_message_height = 12
        conf_message_width = 80
        conf_message_button_y = (self.maxy - conf_message_height) // 2 + 8
        r = ConfirmWindow(conf_message_height, conf_message_width, self.maxy, self.maxx,
                          conf_message_button_y, msg).do_action()
        if not r.success or not r.result.get('yes', False):
            return False
        return True

    def do_setup_network(self):
        if not self.netmgr.setup_network():
            msg = 'Failed to setup network configuration!'
            conf_message_height = 12
            conf_message_width = 80
            conf_message_button_y = (self.maxy - conf_message_height) // 2 + 8
            ConfirmWindow(conf_message_height, conf_message_width, self.maxy, self.maxx,
                          conf_message_button_y, msg, True).do_action()
            return False
        self.netmgr.restart_networkd()
        return True

    def display(self):
        if self.setup_network and not self.do_setup_network():
            return ActionResult(False, {'goBack': True})

        file_source = {}
        accepted_chars = list(range(ord('a'), ord('z')+1))
        accepted_chars.extend(range(ord('A'), ord('Z')+1))
        accepted_chars.extend(range(ord('0'), ord('9')+1))
        accepted_chars.extend([ ord('-'), ord('_'), ord('.'), ord('~'), ord(':'), ord('/') ])
        result = WindowStringReader(self.maxy, self.maxx, 18, 78, 'url',
                                    None, None, accepted_chars, None, None,
                                    self.title, self.intro, 10, file_source, 'https://',
                                    True).get_user_string(None)
        if not result.success:
            return result

        status_window = Window(10,70, self.maxy, self.maxx, 'Installing Photon', False)
        status_window.addstr(1, 0, 'Downloading file...')
        status_window.show_window()

        fd, temp_file = tempfile.mkstemp()
        result, msg = CommandUtils.wget(file_source['url'], temp_file,
                                        ask_fn=self.ask_proceed_unsafe_download)
        os.close(fd)
        if not result:
            status_window.adderror('Error: ' + msg + ' Press any key to go back...')
            status_window.content_window().getch()
            status_window.clearerror()
            status_window.hide_window()
            return ActionResult(False, {'goBack': True})

        if 'additional_files' not in self.install_config:
            self.install_config['additional_files'] = []
        copy = { temp_file: self.dest }
        self.install_config['additional_files'].append(copy)

        return ActionResult(True, None)
Example #11
0
class Crawler(object):
    """定向爬虫类"""

    http_debuglevel = 0

    #: 预定义网页编码。
    encoding = None

    #: 设置User Agent,有时候模拟Google Bot会有事倍功半的效果。
    user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'

    # 页面语言,有些网站会以这个为标记实现国际化
    accept_language = 'zh_CN'

    # 可接受的数据类型
    accept_mine = 'text/html,application/xhtml+xml,' \
                  'application/xml;q=0.9,*/*;q=0.8'

    #: 最大重定向次数,防止重定向陷阱。
    max_redirects = 20

    #: 每个爬虫的最大并发连接数。
    max_connections = 10

    #: 超时。
    timeout = 360

    #: 最大失败尝试次数。
    max_retries = 1000

    #: 每次尝试后递增休眠间隔。
    #: 例如 ``sleep_seconds = 2`` ,那么第一次连接失败会休眠2秒,第二次会休眠4秒,第三次会休眠6秒。
    sleep_seconds = 1

    #: Bloom容量
    bloom_capacity = 10000000

    #: Bloom预计错误率
    bloom_error_rate = 0.0001

    #: HTTP代理
    proxies = None

    #: 错误日志存放处
    dump_dir = 'dump/'
    is_stop = True
    stopped = False
    name = None

    retry_with_broken_content = False
    retry_with_no_content = False

    #: 如果服务器遇到这些error code,当做正常页面处理
    ignore_server_error_code = ()

    #: 如果服务器遇到这些error code,不进行重试,直接忽略掉
    do_not_retry_with_server_error_code = ()

    lock = None
    logger = logging.getLogger('Crawler')


    def __init__(self):
        httplib.HTTPConnection.debuglevel = self.http_debuglevel
        self.network = NetworkManager(crawler=self)

        self.pool = Pool()
        self.lock = RLock()
        self.bloom_filters = {}
        self.name = self.__class__.__name__
        self._status = {
            'process_count': 0,
            'is_stop': True,
            'run_seconds': 0,
            'crawler_name': self.name,
        }



        # def sync_bloom(self):

        #     """强行同步Bloom到文件"""

    #
    #     while not self.is_stop:
    #         for key in self.bloom_filters.keys():
    #             self.bloom_filters[key].sync()
    #         gevent.sleep(1)



    def work(self):
        """启动爬虫。"""

        if self.lock.acquire(blocking=False):
            self.logger.info('Starting crawler %s' % self.name)
            self.stopped = False
            self._status['is_stop'] = False
            self.pool.spawn(self.run)
            self.pool.join()
            self.network.join()
            self._status['is_stop'] = True
            self.logger.info('Finished crawler %s' % self.name)
            self.lock.release()

    def on_server_error(self, response):
        """服务器错误回调。

        :param response:
        :raise ServerError:
        """
        self.logger.warning('Something wrong with server.')
        raise ServerError('Error Code:%s' % response.status_code)


    def on_proxies_error(self, proxy):
        pass

    def on_parse_error(self, error):
        """页面分析错误回调

        :param error:
        """

    def fetch_proxies(self):
        pass

    def stop(self):
        """停止爬虫。


        """
        self.logger.info('Stopping crawler %s' % self.name)
        self.stopped = True
        while not self.network._request_queue.empty():
            self.network._request_queue.get()


    def status(self):
        """返回爬虫状态。


        :return: :rtype:
        """
        return self._status


    def run(self):
        """这里编写启动爬虫的工作。
        必须重载此函数,推倒第一块多米诺骨牌。

        """
        raise NotImplementedError
Example #12
0
 def __init__(self):
     self.nm = NetworkManager()
Example #13
0
class MainImpl:

    def __init__(self):
        self.nm = NetworkManager()

    def show_status(self):
        for t in self.nm.supported_types:
            conns = self.nm.get_connections_by_type(t)
            if len(conns) == 0:
                continue

            print("-- %s ----" % (t))

            for conn in conns:
                print("Id:       %s" % (conn.settings.id))
                print("UUID:     %s" % (conn.settings.uuid))
                print("State:    %s" % (conn.state))

                if conn.settings.mac_address is not None:
                    device = self.nm.get_device_by_mac(conn.settings.mac_address)
                    print("Device:   %s" % ("Unknown" if device is None else device.interface))

                if conn.settings.auto:
                    print("Address:  auto (DHCP)")
                else:
                    print("Address:  %s" % (conn.settings.address))
                    print("Netmask:  %s" % (conn.settings.netmask))
                    print("Gateway:  %s" % (conn.settings.gateway))

                if conn.settings.dns is not None:
                    print("DNS:      %s" % (conn.settings.dns))

    def activate_connection(self, conn_id):
        tconn = None
        for conn in self.nm.connections:
            if conn.settings.id == conn_id:
                tconn = conn
                break
        if tconn is None:
            raise Exception("the specified connection-id does not match an existing connection")
        if tconn.settings.type not in self.nm.supported_types:
            raise UnsupportedConnectionType("connection type \"%s\" is not supported" % tconn.settings.type)

        device = None
        for d in self.nm.devices:
            if d.type not in self.types:
                continue
            if self.nm.get_type_string(d.type) != conn.settings.type:
                continue
            device = d
            break
        if device is None:
            raise NoDeviceFound("there are no network devices available for this connection")

        try:
            self.nm.activate_connection(conn, device)
        except dbus.exceptions.DBusException as e:
            if str(e).startswith('org.freedesktop.NetworkManager.UnmanagedDevice'):
                raise Exception("device (%s) not currently managed" % device.interface)
            else:
                raise

    def deactivate_connection(self, conn_id):
        for active in self.nm.active_connections:
            if active.connection.settings.id == conn_id:
                self.nm.deactivate_connection(active.proxy)
                return
        raise Exception("the specified connection-id does not match an existing connection")