Ejemplo n.º 1
0
 def notifyListeners(self, msg):
     """notify listeners for what occurs"""
     logging.debug('[%s]: notifyListeners', self.getName())
     if not msg.header.rUid:
         self.__msgBus.broadcast(msg)
     else:
         self.__msgBus.send(msg)
Ejemplo n.º 2
0
    def refreshToken(self):
        """Refresh access token"""
        super(BaiduCloudAPI, self).refreshToken()
        api_url = self.cf.get("BaiduCloud", "openapi_url") + "/token"
        logging.debug('[BaiduPlugin]: refreshToken: URL=>%s', api_url)
        param = {
            "grant_type": "refresh_token",
            "refresh_token": self.cf.get("BaiduCloud", "refresh_token"),
            "client_id": self.cf.get("BaiduCloud", "api_key"),
            "client_secret": self.cf.get("BaiduCloud", "secret_key")
        }
        try:
            ret_fp = cloud_post(api_url, param)
            data = json.load(ret_fp)

            logging.debug(
                '[BaiduPlugin]: getToken: access_token=>%s, refresh_token=>%s',
                data["access_token"], data["refresh_token"])

            self.cf.set("BaiduCloud", "access_token", data["access_token"])
            self.cf.set("BaiduCloud", "refresh_token", data["refresh_token"])
            self.cf.set("BaiduCloud", "session_key", data["session_key"])
            self.cf.set("BaiduCloud", "session_secret", data["session_secret"])
            self.cf.set("BaiduCloud", "scope", data["scope"])

            ret_fp.close()
        except ValueError, e:
            logging.error('[%s]: refreshToken JSON load error %s', e)
Ejemplo n.º 3
0
    def run(self):
        """UServer entry"""
        super(UServer, self).run()
        #TODO: below should be set by ConfManager

        if self.isEnableSocket:
            self.sock.bind(('localhost', 8089))
            self.sock.listen(5)

            while self.isRunning:
                try:
                    conn, addr = self.sock.accept()
                    try:
                        conn.settimeout(5)
                        buf = conn.recv(1024) #TODO: should be also in ConfManager
                        req = json.loads(buf)
                        logging.debug('[UniFileSync]: action %s, param %s', req['action'], req['param'])
                        #TODO: make it common for function usage
                        res, data = self.getHandler(req['action'])(req['param'])
                        ret = {'action': req['action'], 'param': {'data': data}, 'res': res, 'type': 'ack'}
                        conn.send(json.dumps(ret))
                    except socket.timeout:
                        logging.info('[UniFileSync]: socket time out from %s', addr)
                    except KeyError, e:
                        logging.error('[%s]: Key Error with param %s', self.getName(), req['param'])
                    finally:
                        conn.close()
                except KeyboardInterrupt:
                    print 'Press Ctrl+C'
                    self.stop()
Ejemplo n.º 4
0
    def loadAllPlugins(self):
        """load all plugins from destant path"""
        p_paths = ConfManager.getManager().getPluginPaths()
        pluginList = ConfManager.getManager().getValue('common', 'plugins')

        for p in p_paths:
            logging.debug('[%s]: loadAllPlugins from %s', 'PluginManager', p)
            #TODO: __import__ the module into our script
            try:
                dirs = os.listdir(p)
                for d in dirs:
                    tmp = '%s%s%s' % (p, os.sep, d)
                    if os.path.isdir(tmp):
                        module_name = 'UniFileSync.plugins.%s' % d
                        module_path = '%s%s%sPlugin.py' % (tmp, os.sep, d)
                        imp.load_source('', module_path)
                        pl = {'name': d, 'path': module_path}
                        if pl not in pluginList:
                            pluginList.append(pl)

            except OSError as exc:
                logging.error('loadAllPlugins listdir error %d', OSError.errno)

        ConfManager.getManager().setValue('common', 'plugins', pluginList)
        logging.debug('[%s]: loadAllPlugins and save %s into configuration', 'PluginManager', pluginList)
        ConfManager.getManager().save()
Ejemplo n.º 5
0
 def stop(self):
     """actor stop"""
     logging.debug('[%s]: is stopping', self.getName())
     for l in self.__lList:
         self.__msgBus.rmListener(l, self.msgUid)
     self.__msgBus.unregQ(self.__msgUid)
     self.__isRunning = False
Ejemplo n.º 6
0
 def notifyListeners(self, msg):
     """notify listeners for what occurs"""
     logging.debug('[%s]: notifyListeners', self.getName())
     if not msg.header.rUid:
         self.__msgBus.broadcast(msg)
     else:
         self.__msgBus.send(msg)
Ejemplo n.º 7
0
    def handleFileCreate(self, msg):
        """docstring for handleFileDelete"""
        logging.debug('[%s]: handleFileCreate: %s, watch dir %s', self.getName(), msg.body['path'], msg.body['watch_dir'])

        res = E_API_ERR
        result = {}
        dirpath = ""

        syncPath = msg.body['path']

        if msg.body['watch_dir'] == "":
            filePath = syncPath
        else:
            dirpath = self.getSyncFolder(msg.body['watch_dir'])
            self.__mkdir(dirpath)
            syncPath = dirpath + '/' + msg.body['path']

        filePath = msg.body['watch_dir'] + os.sep +msg.body['path']

        for p in self.pluginManager.getAllPlugins():
            res = p.getAPI().uploadSingleFile(filePath, syncPath)
            result[p.name] = [res]

        res, d = self.parseResult(result)

        if msg.header.ack:
            self.replyResult(msg, res, data=d)

        self.notify(msg, res, data=d)
Ejemplo n.º 8
0
 def stop(self):
     """actor stop"""
     logging.debug('[%s]: is stopping', self.getName())
     for l in self.__lList:
         self.__msgBus.rmListener(l, self.msgUid)
     self.__msgBus.unregQ(self.__msgUid)
     self.__isRunning = False
Ejemplo n.º 9
0
    def notify(self, action, watch_dir, path, src_path=''):
        """notify to others who cares about files change"""

        #TODO: need to improve more than delete operation
        '''
        #If it is delete action, and last change is the same
        if action == FILE_DELETE:
            #We ignore the same action with the same path within some time.
            if action == self.lastChange['action'] and path == self.lastChange['path']:
                if self.lastChange['time'] + 3 < time.time():
                    return False
        else:
            if not os.path.exists(path):
                #For not delete operation, path should exist
                logging.error('[%s]: notify path:%s not exist any more', self.getName(), path)
                return False

        #TODO: buffer some modification changes
        self.lastChange['action'] = action
        self.lastChange['path'] = path
        self.lastChange['time'] = time.time()
        '''

        msg = self.initMsg(MSG_TYPE_T_FILE, MSG_ID_MAP[action])

        #For delete dir, since it does not exist, so it is always False
        isdir = os.path.isdir(watch_dir + os.sep + path)

        logging.debug('[%s]: notify file change: action=>%s, watch_dir=>%s, path=>%s, src_path=>%s, isdir=>%s',
                        self.getName(), ACTIONS_NAMES[action], watch_dir, path, src_path, isdir)
        msg.body = {'path': path, 'action': action, 'src_path': src_path, 'watch_dir': watch_dir, 'isdir': isdir}

        self.notifyListeners(msg)
Ejemplo n.º 10
0
    def closeEvent(self, event):
        """override close event"""
        if self.trayIcon.isVisible():
            self.hide()
            event.ignore()

        ConfManager.getManager().save()
        logging.debug('[%s] is closed, window is hide, configuration is saved', self.getName())
Ejemplo n.º 11
0
    def closeEvent(self, event):
        """override close event"""
        if self.trayIcon.isVisible():
            self.hide()
            event.ignore()

        ConfManager.getManager().save()
        logging.debug('[%s] is closed, window is hide, configuration is saved',
                      self.getName())
Ejemplo n.º 12
0
    def configure(self):
        """server self configure when it is started"""
        register_openers()
        PluginManager.getManager().loadAllPlugins()
        confManager = ConfManager.getManager()
        proxy = confManager.getValue('common', 'network')

        if proxy['proxy'] != "" and proxy['proxy'] != None:
            param = {'http': 'http://%s' % proxy['proxy'], 'https': 'https://%s' % proxy['proxy'] }
            set_proxy(param)
            logging.debug('[%s]: set proxy server %s', self.getName(), proxy['proxy'])
Ejemplo n.º 13
0
    def replyResult(self, msg, result, **kargs):
        """common reply result method"""
        rUid = msg.header.sUid
        rmsg = self.initMsg(msg.header.mtype, msg.header.mid, rUid)
        rmsg.body = {'result': result}

        for k, v in kargs.iteritems():
            rmsg.body[k] = v

        logging.debug('[%s]: replyResult to Uid %d with result %s, body\n%s', self.getName(), rUid, result, rmsg.body)
        self.__msgBus.send(rmsg)
Ejemplo n.º 14
0
    def parseResult(self, data):
        """parse result to make it convient to read"""
        super(BaiduCloudAPI, self).parseResult(data)

        res = E_API_ERR

        try:
            if 'list' in data:
                """
                Directory of $DIRPATH

                $TIME   <$ISDIR>   $SIZE   $PATH

                """
                strIO = io.StringIO()
                files = data['list']

                strIO.write(u'\nDirectory of %s\n' %
                            (self.cf.get('BaiduCloud', 'app_path')))
                for f in files:
                    mtime = time.ctime(f['mtime'])
                    path = f['path']
                    size = f['size']
                    isdir = f['isdir']
                    if isdir:
                        strIO.write(u'%s\t%s\t%d\t%s\n' %
                                    (mtime, '<DIR>', size, path))
                    else:
                        strIO.write(u'%s\t\t%d\t%s\n' % (mtime, size, path))

                res = strIO.getvalue()
                strIO.close()
                logging.debug('[%s]: File list result\n%s', self.getName(),
                              res)

            elif 'quota' in data:
                """
                    Quota: %dGB
                    Used: %dKB
                """
                quota = data['quota'] / (1024 * 1024 * 1024)
                used = data['used'] / (1024 * 1024)

                res = "Quota: %sGB, Used: %sKB" % (quota, used)

            elif 'fs_id' in data:
                #file operation is OK
                res = E_OK

            elif 'request_id' in data:
                res = E_OK

        except TypeError, e:
            pass
Ejemplo n.º 15
0
    def syncHandler(self, param):
        """sync handler for actors"""
        logging.debug('[%s]: syncHandler with parm %s', self.getName(), param);
        msg = self.initMsg(MSG_TYPE_T_FILE, MSG_ID_T_FILE_SYNC, MSG_UNIQUE_ID_T_CLOUD_ACTOR, True)
        msg.body['path'] = param['path']
        self.msgBus.send(msg)
        rmsg = self.getMsg(2)
        res = E_API_ERR
        if rmsg:
            res = rmsg.body['result']

        return res, None
Ejemplo n.º 16
0
    def replyResult(self, msg, result, **kargs):
        """common reply result method"""
        rUid = msg.header.sUid
        rmsg = self.initMsg(msg.header.mtype, msg.header.mid, rUid)
        rmsg.body = {'result': result}

        for k, v in kargs.iteritems():
            rmsg.body[k] = v

        logging.debug('[%s]: replyResult to Uid %d with result %s, body\n%s',
                      self.getName(), rUid, result, rmsg.body)
        self.__msgBus.send(rmsg)
Ejemplo n.º 17
0
    def syncHandler(self, param):
        """sync handler for actors"""
        logging.debug('[%s]: syncHandler with parm %s', self.getName(), param)
        msg = self.initMsg(MSG_TYPE_T_FILE, MSG_ID_T_FILE_SYNC,
                           MSG_UNIQUE_ID_T_CLOUD_ACTOR, True)
        msg.body['path'] = param['path']
        self.msgBus.send(msg)
        rmsg = self.getMsg(2)
        res = E_API_ERR
        if rmsg:
            res = rmsg.body['result']

        return res, None
Ejemplo n.º 18
0
    def parseResult(self, data):
        """parse result to make it convient to read"""
        super(BaiduCloudAPI, self).parseResult(data)

        res = E_API_ERR

        try:
            if 'list' in data:
                """
                Directory of $DIRPATH

                $TIME   <$ISDIR>   $SIZE   $PATH

                """
                strIO = io.StringIO()
                files = data['list']

                strIO.write(u'\nDirectory of %s\n' % (self.cf.get('BaiduCloud', 'app_path')))
                for f in files:
                    mtime = time.ctime(f['mtime'])
                    path = f['path']
                    size = f['size']
                    isdir = f['isdir']
                    if isdir:
                        strIO.write(u'%s\t%s\t%d\t%s\n' % (mtime, '<DIR>', size, path))
                    else:
                        strIO.write(u'%s\t\t%d\t%s\n' % (mtime, size, path))

                res = strIO.getvalue()
                strIO.close()
                logging.debug('[%s]: File list result\n%s', self.getName(), res)

            elif 'quota' in data:
                """
                    Quota: %dGB
                    Used: %dKB
                """
                quota = data['quota'] / (1024*1024*1024)
                used = data['used'] / (1024*1024)

                res = "Quota: %sGB, Used: %sKB" % (quota, used)

            elif 'fs_id' in data:
                #file operation is OK
                res = E_OK

            elif 'request_id' in data:
                res = E_OK

        except TypeError, e:
            pass
Ejemplo n.º 19
0
    def applyAccess(self):
        """apply baidu access to netdisk"""
        super(BaiduCloudAPI, self).applyAccess()
        api_url = self.cf.get("BaiduCloud", "openapi_url") + "/device/code"
        param = {"client_id": self.cf.get("BaiduCloud", "api_key"), "response_type": "device_code", "scope": "basic netdisk"}
        logging.debug('[BaiduPlugin]: applyAccess: URL=> %s, API key=> %s', api_url, self.cf.get("BaiduCloud", "api_key"))
        ret_fp = cloud_get(api_url, param)
        data = json.load(ret_fp)
        webbrowser.open(data["verification_url"])
        print "Please enter your user code %s into open web page" % data["user_code"]
        print "Please note it will expires in %d s" % data["expires_in"]
        ret_fp.close();

        self.cf.set("BaiduCloud", "dev_code", data["device_code"])
Ejemplo n.º 20
0
    def startHandler(self, param):
        """start handler for actors"""
        logging.debug('[%s]: startHandler with parm %s', self.getName(), param)

        if param['name']:
            name = param['name'].lower()
        else:
            name = ''
        res = E_OK

        watchpaths = ConfManager.getManager().getValue('common', 'folders')

        if name == 'all' or name == '':
            if self.cActor.isRunning or self.fsMonitor.isRunning:
                return E_ACTOR_ALIVE, None
            self.cActor.start()
            self.__startActors.append(self.cActor)
            self.fsMonitor.start()
            self.__startActors.append(self.fsMonitor)

            time.sleep(2)
            for w in watchpaths:
                self.watchHandler({'path': w, 'action': 'add'}, False)
                #logging.debug('[%s]: add watch path %s from configuration file', self.getName(), w)

        elif name == 'monitor':
            if self.fsMonitor.isRunning:
                return E_ACTOR_ALIVE, None
            self.fsMonitor.start()
            self.__startActors.append(self.fsMonitor)

            time.sleep(2)
            for w in watchpaths:
                self.watchHandler({'path': w, 'action': 'add'})
                logging.debug(
                    '[%s]: add watch path %s from configuration file',
                    self.getName(), w)

        elif name == 'cloud':
            if self.cActor.isRunning:
                return E_ACTOR_ALIVE, None
            self.cActor.start()
            self.__startActors.append(self.cActor)
        else:
            logging.error('[%s]: startHandler with name %s error',
                          self.getName(), param['name'])
            res = E_INVILD_PARAM

        return res, None
Ejemplo n.º 21
0
    def configure(self):
        """server self configure when it is started"""
        register_openers()
        PluginManager.getManager().loadAllPlugins()
        confManager = ConfManager.getManager()
        proxy = confManager.getValue('common', 'network')

        if proxy['proxy'] != "" and proxy['proxy'] != None:
            param = {
                'http': 'http://%s' % proxy['proxy'],
                'https': 'https://%s' % proxy['proxy']
            }
            set_proxy(param)
            logging.debug('[%s]: set proxy server %s', self.getName(),
                          proxy['proxy'])
Ejemplo n.º 22
0
    def listHandler(self, param):
        """list handler for actors"""
        logging.debug('[%s]: listHandler with parm %s', self.getName(), param);
        msg = self.initMsg(MSG_TYPE_T_FILE, MSG_ID_T_FILE_LIST, MSG_UNIQUE_ID_T_CLOUD_ACTOR, True)
        msg.body = param
        self.msgBus.send(msg)
        res = E_API_ERR
        data = None
        rmsg = self.getMsg(None)

        if rmsg:
            res = rmsg.body['result']
            data = rmsg.body['data']

        return res, data
Ejemplo n.º 23
0
 def infoHandler(self, param):
     """get information of cloud"""
     logging.debug('[%s]: infoHandler with parm %s', self.getName(), param);
     msg = self.initMsg(MSG_TYPE_T_FILE, MSG_ID_T_FILE_INFO, MSG_UNIQUE_ID_T_CLOUD_ACTOR, True)
     msg.body = param
     self.msgBus.send(msg)
     rmsg = self.getMsg(None)
     res = E_API_ERR
     data = "Cloud API Error"
     if rmsg:
         if 'result' in rmsg.body:
             res = rmsg.body['result']
         if 'data' in rmsg.body:
             data = rmsg.body['data']
     return res, data
Ejemplo n.º 24
0
 def infoHandler(self, param):
     """get information of cloud"""
     logging.debug('[%s]: infoHandler with parm %s', self.getName(), param)
     msg = self.initMsg(MSG_TYPE_T_FILE, MSG_ID_T_FILE_INFO,
                        MSG_UNIQUE_ID_T_CLOUD_ACTOR, True)
     msg.body = param
     self.msgBus.send(msg)
     rmsg = self.getMsg(None)
     res = E_API_ERR
     data = "Cloud API Error"
     if rmsg:
         if 'result' in rmsg.body:
             res = rmsg.body['result']
         if 'data' in rmsg.body:
             data = rmsg.body['data']
     return res, data
Ejemplo n.º 25
0
    def listHandler(self, param):
        """list handler for actors"""
        logging.debug('[%s]: listHandler with parm %s', self.getName(), param)
        msg = self.initMsg(MSG_TYPE_T_FILE, MSG_ID_T_FILE_LIST,
                           MSG_UNIQUE_ID_T_CLOUD_ACTOR, True)
        msg.body = param
        self.msgBus.send(msg)
        res = E_API_ERR
        data = None
        rmsg = self.getMsg(None)

        if rmsg:
            res = rmsg.body['result']
            data = rmsg.body['data']

        return res, data
Ejemplo n.º 26
0
    def watchHandler(self, param, ack=True):
        """watch handler for actors"""
        logging.debug('[%s]: watchHandler with parm %s', self.getName(), param);
        msg = self.initMsg(MSG_TYPE_T_OPER, MSG_ID_T_OPER_ADD_WATCH, MSG_UNIQUE_ID_T_FS_MONITOR, ack)
        msg.body = param
        self.msgBus.send(msg)

        if ack:
            rmsg = self.getMsg(2)
            res = E_WATCH_ERR
            if rmsg:
                res = rmsg.body['result']
        else:
            res = E_OK

        return res, None
Ejemplo n.º 27
0
    def stopHandler(self, param):
        """stop handler for actors"""
        logging.debug('[%s]: stopHandler with parm %s', self.getName(), param)

        if param['name']:
            name = param['name'].lower()
        else:
            name = ''

        res = E_ACTOR_DEAD

        if name == 'all' or name == '':
            #The msg is broadcase
            msg = self.initMsg(MSG_TYPE_T_OPER, MSG_ID_T_OPER_STOP, None, True)
            self.notifyListeners(msg)

            for a in self.__startActors:
                rmsg = self.getMsg(2)
                if rmsg:
                    res = rmsg.body['result']

            logging.info('[UniFileSync]: stop server...')
            self.stop()
        elif name == 'monitor':
            if not self.fsMonitor.isRunning:
                res = E_ACTOR_DEAD
            msg = self.initMsg(MSG_TYPE_T_OPER, MSG_ID_T_OPER_STOP,
                               MSG_UNIQUE_ID_T_FS_MONITOR, True)
            self.msgBus.send(msg)
            rmsg = self.getMsg(2)
            if rmsg:
                res = rmsg.body['result']
        elif name == 'cloud':
            if not self.cActor.isRunning:
                res = E_ACTOR_DEAD
            msg = self.initMsg(MSG_TYPE_T_OPER, MSG_ID_T_OPER_STOP,
                               MSG_UNIQUE_ID_T_CLOUD_ACTOR, True)
            self.msgBus.send(msg)
            rmsg = self.getMsg(2)
            if rmsg:
                res = rmsg.body['result']
        else:
            logging.error('[%s]: stopHandler with name %s error',
                          self.getName(), param['name'])
            res = E_INVILD_PARAM

        return res, None
Ejemplo n.º 28
0
    def handleFileList(self, msg):
        """docstring for handleFileList"""
        logging.debug('[%s]: handleFileList: %s', self.getName(), msg.body['path'])
        res = E_API_ERR
        d = ''
        result = {}

        for p in self.pluginManager.getAllPlugins():
            res, d = p.getAPI().lsInCloud(msg.body['path'])
            result[p.name] = [res, d]

        res, d = self.parseResult(result)

        if msg.header.ack:
            self.replyResult(msg, res, data=d)

        self.notify(msg, res, data=d)
Ejemplo n.º 29
0
    def startHandler(self, param):
        """start handler for actors"""
        logging.debug('[%s]: startHandler with parm %s', self.getName(), param);

        if param['name']:
            name = param['name'].lower()
        else:
            name = ''
        res = E_OK

        watchpaths = ConfManager.getManager().getValue('common', 'folders')

        if name == 'all' or name == '':
            if self.cActor.isRunning or self.fsMonitor.isRunning:
                return E_ACTOR_ALIVE, None
            self.cActor.start()
            self.__startActors.append(self.cActor)
            self.fsMonitor.start()
            self.__startActors.append(self.fsMonitor)

            time.sleep(2)
            for w in watchpaths:
                self.watchHandler({'path': w, 'action': 'add'}, False)
                #logging.debug('[%s]: add watch path %s from configuration file', self.getName(), w)

        elif name == 'monitor':
            if self.fsMonitor.isRunning:
                return E_ACTOR_ALIVE, None
            self.fsMonitor.start()
            self.__startActors.append(self.fsMonitor)

            time.sleep(2)
            for w in watchpaths:
                self.watchHandler({'path': w, 'action': 'add'})
                logging.debug('[%s]: add watch path %s from configuration file', self.getName(), w)

        elif name == 'cloud':
            if self.cActor.isRunning:
                return E_ACTOR_ALIVE, None
            self.cActor.start()
            self.__startActors.append(self.cActor)
        else:
            logging.error('[%s]: startHandler with name %s error', self.getName(), param['name'])
            res = E_INVILD_PARAM

        return res, None
Ejemplo n.º 30
0
    def watchHandler(self, param, ack=True):
        """watch handler for actors"""
        logging.debug('[%s]: watchHandler with parm %s', self.getName(), param)
        msg = self.initMsg(MSG_TYPE_T_OPER, MSG_ID_T_OPER_ADD_WATCH,
                           MSG_UNIQUE_ID_T_FS_MONITOR, ack)
        msg.body = param
        self.msgBus.send(msg)

        if ack:
            rmsg = self.getMsg(2)
            res = E_WATCH_ERR
            if rmsg:
                res = rmsg.body['result']
        else:
            res = E_OK

        return res, None
Ejemplo n.º 31
0
    def handleFileRename(self, msg):
        """docstring for handleFileRename"""
        logging.debug('[%s]: handleFileRename: %s=>%s', self.getName(), msg.body['src_path'], msg.body['path'])
        filePath = msg.body['path']
        isdir = msg.body['isdir']

        res = E_API_ERR
        for p in self.pluginManager.getAllPlugins():
            if isdir:
                #res = p.getAPI().mkdirInCloud(path)
                pass
            else:
                #res = p.getAPI().uploadSingleFile(filePath, syncPath)
                pass

        if msg.header.ack:
            self.replyResult(msg, res)
        return res
Ejemplo n.º 32
0
    def handleFileInfo(self, msg):
        """handle get cloud information"""
        logging.debug('[%s]: handleFileInfo: %s', self.getName(), msg.body)

        res = E_API_ERR
        d = ""
        result = {}

        for p in self.pluginManager.getAllPlugins():
            res, d = p.getAPI().getCloudInfo()
            result[p.name] = [res, d]

        res, d = self.parseResult(result)

        if msg.header.ack:
            self.replyResult(msg, res, data=d)

        self.notify(msg, res, data=d)
Ejemplo n.º 33
0
    def getToken(self):
        """Get access Token from Baidu API"""
        super(BaiduCloudAPI, self).getToken()
        api_url = self.cf.get("BaiduCloud", "openapi_url") + "/token"
        logging.debug('[BaiduPlugin]: getToken: URL=>% s', api_url)
        param = {"grant_type": "device_token", "code": self.cf.get("BaiduCloud", "dev_code"),
                 "client_id": self.cf.get("BaiduCloud", "api_key"),
                 "client_secret": self.cf.get("BaiduCloud", "secret_key")}
        ret_fp = cloud_get(api_url, param)
        data = json.load(ret_fp)
        logging.debug('[BaiduPlugin]: getToken: access_token=>%s, refresh_token=>%s', data["access_token"], data["refresh_token"])
        ret_fp.close()

        self.cf.set("BaiduCloud", "access_token", data["access_token"])
        self.cf.set("BaiduCloud", "refresh_token", data["refresh_token"])
        self.cf.set("BaiduCloud", "session_key", data["session_key"])
        self.cf.set("BaiduCloud", "session_secret", data["session_secret"])
        self.cf.set("BaiduCloud", "scope", data["scope"])
Ejemplo n.º 34
0
    def stopHandler(self, param):
        """stop handler for actors"""
        logging.debug('[%s]: stopHandler with parm %s', self.getName(), param);

        if param['name']:
            name = param['name'].lower()
        else:
            name = ''

        res = E_ACTOR_DEAD

        if name == 'all' or name == '':
            #The msg is broadcase
            msg = self.initMsg(MSG_TYPE_T_OPER, MSG_ID_T_OPER_STOP, None, True)
            self.notifyListeners(msg)

            for a in self.__startActors:
                rmsg = self.getMsg(2)
                if rmsg:
                    res = rmsg.body['result']

            logging.info('[UniFileSync]: stop server...')
            self.stop()
        elif name == 'monitor':
            if not self.fsMonitor.isRunning:
                res = E_ACTOR_DEAD
            msg = self.initMsg(MSG_TYPE_T_OPER, MSG_ID_T_OPER_STOP, MSG_UNIQUE_ID_T_FS_MONITOR, True)
            self.msgBus.send(msg)
            rmsg = self.getMsg(2)
            if rmsg:
                res = rmsg.body['result']
        elif name == 'cloud':
            if not self.cActor.isRunning:
                res = E_ACTOR_DEAD
            msg = self.initMsg(MSG_TYPE_T_OPER, MSG_ID_T_OPER_STOP, MSG_UNIQUE_ID_T_CLOUD_ACTOR, True)
            self.msgBus.send(msg)
            rmsg = self.getMsg(2)
            if rmsg:
                res = rmsg.body['result']
        else:
            logging.error('[%s]: stopHandler with name %s error', self.getName(), param['name'])
            res = E_INVILD_PARAM

        return res, None
Ejemplo n.º 35
0
    def run(self):
        """UServer entry"""
        super(UServer, self).run()
        #TODO: below should be set by ConfManager

        if self.isEnableSocket:
            self.sock.bind(('localhost', 8089))
            self.sock.listen(5)

            while self.isRunning:
                try:
                    conn, addr = self.sock.accept()
                    try:
                        conn.settimeout(5)
                        buf = conn.recv(
                            1024)  #TODO: should be also in ConfManager
                        req = json.loads(buf)
                        logging.debug('[UniFileSync]: action %s, param %s',
                                      req['action'], req['param'])
                        #TODO: make it common for function usage
                        res, data = self.getHandler(req['action'])(
                            req['param'])
                        ret = {
                            'action': req['action'],
                            'param': {
                                'data': data
                            },
                            'res': res,
                            'type': 'ack'
                        }
                        conn.send(json.dumps(ret))
                    except socket.timeout:
                        logging.info('[UniFileSync]: socket time out from %s',
                                     addr)
                    except KeyError, e:
                        logging.error('[%s]: Key Error with param %s',
                                      self.getName(), req['param'])
                    finally:
                        conn.close()
                except KeyboardInterrupt:
                    print 'Press Ctrl+C'
                    self.stop()
Ejemplo n.º 36
0
    def applyAccess(self):
        """apply baidu access to netdisk"""
        super(BaiduCloudAPI, self).applyAccess()
        api_url = self.cf.get("BaiduCloud", "openapi_url") + "/device/code"
        param = {
            "client_id": self.cf.get("BaiduCloud", "api_key"),
            "response_type": "device_code",
            "scope": "basic netdisk"
        }
        logging.debug('[BaiduPlugin]: applyAccess: URL=> %s, API key=> %s',
                      api_url, self.cf.get("BaiduCloud", "api_key"))
        ret_fp = cloud_get(api_url, param)
        data = json.load(ret_fp)
        webbrowser.open(data["verification_url"])
        print "Please enter your user code %s into open web page" % data[
            "user_code"]
        print "Please note it will expires in %d s" % data["expires_in"]
        ret_fp.close()

        self.cf.set("BaiduCloud", "dev_code", data["device_code"])
Ejemplo n.º 37
0
    def notify(self, action, watch_dir, path, src_path=''):
        """notify to others who cares about files change"""

        #TODO: need to improve more than delete operation
        '''
        #If it is delete action, and last change is the same
        if action == FILE_DELETE:
            #We ignore the same action with the same path within some time.
            if action == self.lastChange['action'] and path == self.lastChange['path']:
                if self.lastChange['time'] + 3 < time.time():
                    return False
        else:
            if not os.path.exists(path):
                #For not delete operation, path should exist
                logging.error('[%s]: notify path:%s not exist any more', self.getName(), path)
                return False

        #TODO: buffer some modification changes
        self.lastChange['action'] = action
        self.lastChange['path'] = path
        self.lastChange['time'] = time.time()
        '''

        msg = self.initMsg(MSG_TYPE_T_FILE, MSG_ID_MAP[action])

        #For delete dir, since it does not exist, so it is always False
        isdir = os.path.isdir(watch_dir + os.sep + path)

        logging.debug(
            '[%s]: notify file change: action=>%s, watch_dir=>%s, path=>%s, src_path=>%s, isdir=>%s',
            self.getName(), ACTIONS_NAMES[action], watch_dir, path, src_path,
            isdir)
        msg.body = {
            'path': path,
            'action': action,
            'src_path': src_path,
            'watch_dir': watch_dir,
            'isdir': isdir
        }

        self.notifyListeners(msg)
Ejemplo n.º 38
0
    def refreshToken(self):
        """Refresh access token"""
        super(BaiduCloudAPI, self).refreshToken()
        api_url = self.cf.get("BaiduCloud", "openapi_url") + "/token"
        logging.debug('[BaiduPlugin]: refreshToken: URL=>%s', api_url)
        param = {"grant_type": "refresh_token", "refresh_token": self.cf.get("BaiduCloud", "refresh_token"),
                 "client_id": self.cf.get("BaiduCloud", "api_key"),
                 "client_secret": self.cf.get("BaiduCloud", "secret_key")}
        try:
            ret_fp = cloud_post(api_url, param)
            data = json.load(ret_fp)

            logging.debug('[BaiduPlugin]: getToken: access_token=>%s, refresh_token=>%s', data["access_token"], data["refresh_token"])

            self.cf.set("BaiduCloud", "access_token", data["access_token"])
            self.cf.set("BaiduCloud", "refresh_token", data["refresh_token"])
            self.cf.set("BaiduCloud", "session_key", data["session_key"])
            self.cf.set("BaiduCloud", "session_secret", data["session_secret"])
            self.cf.set("BaiduCloud", "scope", data["scope"])

            ret_fp.close()
        except ValueError, e:
            logging.error('[%s]: refreshToken JSON load error %s', e)
Ejemplo n.º 39
0
    def getToken(self):
        """Get access Token from Baidu API"""
        super(BaiduCloudAPI, self).getToken()
        api_url = self.cf.get("BaiduCloud", "openapi_url") + "/token"
        logging.debug('[BaiduPlugin]: getToken: URL=>% s', api_url)
        param = {
            "grant_type": "device_token",
            "code": self.cf.get("BaiduCloud", "dev_code"),
            "client_id": self.cf.get("BaiduCloud", "api_key"),
            "client_secret": self.cf.get("BaiduCloud", "secret_key")
        }
        ret_fp = cloud_get(api_url, param)
        data = json.load(ret_fp)
        logging.debug(
            '[BaiduPlugin]: getToken: access_token=>%s, refresh_token=>%s',
            data["access_token"], data["refresh_token"])
        ret_fp.close()

        self.cf.set("BaiduCloud", "access_token", data["access_token"])
        self.cf.set("BaiduCloud", "refresh_token", data["refresh_token"])
        self.cf.set("BaiduCloud", "session_key", data["session_key"])
        self.cf.set("BaiduCloud", "session_secret", data["session_secret"])
        self.cf.set("BaiduCloud", "scope", data["scope"])
Ejemplo n.º 40
0
    def parseResult(self, result):
        """parse result to return"""
        res = E_OK
        d = {}
        strIO = io.StringIO()

        for k, v in result.iteritems():
            """
            <$PLUGIN_NAME>: $RESULT
             $DATA
            """
            if v[0] != E_OK:
                res = v[0]
            strIO.write(u'\n%s: %s\n' % (k, ERR_STR_TABLE[v[0]]))

            if len(v) > 1:
                strIO.write(u'%s' % v[1])

        d = strIO.getvalue()
        strIO.close()
        logging.debug("[%s]: parseResult %s\n", self.getName(), d.encode('utf-8'))

        return res, d
Ejemplo n.º 41
0
 def mkdirInCloud(self, dirPath):
     """make dir in net disk"""
     logging.debug('[%s]: mkdirInCloud %s', self.name, dirPath)
Ejemplo n.º 42
0
 def loadPluginFromPath(self, path):
     """load plugin from special path"""
     if os.path.isfile(path):
         imp.load_source('', path)
         logging.debug('[%s] Loading plugin from path %s', 'PluginManager', path)
Ejemplo n.º 43
0
 def errorHandler(self, error):
     """error handler"""
     logging.debug('[%s]: errorHandler error\n%s', self.name, error)
Ejemplo n.º 44
0
 def mvInCloud(self, toPath, fromPath):
     """move in net disk"""
     logging.debug('[%s]: mvInCloud %s=>%s', self.name, fromPath, toPath)
Ejemplo n.º 45
0
 def lsInCloud(self, filePath):
     """list files in filePath in cloud"""
     logging.debug('[%s]: lsInCloud %s', self.name, filePath)
Ejemplo n.º 46
0
 def proxyHandler(self, param):
     """proxy handler for actors"""
     logging.debug('[%s]: proxyHandler with parm %s', self.getName(), param)
     res = E_PROXY_ERR
     res = set_proxy(param)
     return res, None
Ejemplo n.º 47
0
 def parseResult(self, data):
     """parse plugin result"""
     logging.debug('[%s]: parseResult %s...', self.name, data)
Ejemplo n.º 48
0
 def getCloudInfo(self):
     """get cloud disk information"""
     logging.debug('[%s]: getCloudInfo...', self.name)
Ejemplo n.º 49
0
 def errorHandler(self, error):
     """error handler"""
     logging.debug('[%s]: errorHandler error\n%s', self.name, error)
Ejemplo n.º 50
0
 def process_IN_ATTRIB(self, event):
     logging.debug('[%s]: ATTRIB file: %s', self.fsMonitor.getName(), os.path.join(event.path,event.name))
Ejemplo n.º 51
0
    def connBtnSlots(self, btn):
        """docstring for connBtnSlots"""
        if btn is self.ui.connBtn:
            if btn.text() == 'Connect':
                msg = self.server.initMsg('info', None, MSG_UNIQUE_ID_T_CONTROLLER, True, {'name': 'all'})
                UMsgBus.getBus().send(msg)
                #res, data = self.server.getHandler('info')({'name': 'all'})
                btn.setText('Connecting')
                #self.ui.infoLabel.setText(data)
                logging.debug('[%s]: Press Connect to getCloudInfo', self.getName())
            elif btn.text() == 'Disconnect':
                #self.server.getHandler('stop')({'name': 'all'})
                btn.setText('Connect')
                self.ui.infoLabel.setText('Cloud Disk is disconnected')

        elif btn is self.ui.addFolderBtn:
            fileDialog = QFileDialog(self)
            fileDialog.setWindowTitle('Select Folder')
            folderPath = fileDialog.getExistingDirectory()
            if folderPath != "":
                listItem = QListWidgetItem(QIcon('icon/folder.png'), folderPath, self.ui.folderList)
                pyStr = '%s' % folderPath
                logging.debug('[%s]: add folder path %s', self.getName(), pyStr)
                self.ui.folderList.insertItem(self.ui.folderList.count(), listItem)
                folderList = ConfManager.getManager().getValue('common', 'folders')
                folderList.append(pyStr)
                ConfManager.getManager().setValue('common', 'folders', folderList)
                #send watch dir request
                msg =  self.server.initMsg('watch', None, MSG_UNIQUE_ID_T_CONTROLLER, True, {'path': pyStr, 'action': 'add'})
                UMsgBus.getBus().send(msg)
                self.statusbar.showMessage('Adding watch path %s' % folderPath)

        elif btn is self.ui.rmFolderBtn:
            row = self.ui.folderList.currentRow()
            item = self.ui.folderList.currentItem()
            folderList = ConfManager.getManager().getValue('common', 'folders')
            self.statusbar.showMessage('Removing watch path %s' % item.text())
            pyStr = '%s' % item.text()
            folderList.remove(pyStr)
            ConfManager.getManager().setValue('common', 'folders', folderList)
            logging.debug('[%s]: remove item %d %s', self.getName(), row, item.text())
            msg =  self.server.initMsg('watch', None, MSG_UNIQUE_ID_T_CONTROLLER, True, {'path': pyStr, 'action': 'rm'})
            UMsgBus.getBus().send(msg)
            self.ui.folderList.takeItem(row)

        elif btn is self.ui.saveBtn:
            proxyConf = ConfManager.getManager().getValue('common', 'network')
            server = str(self.ui.proxyLineEdit.text())

            if server != "" and server != None:
                user = str(self.ui.proxyNameLineEdit.text())
                password = str(self.ui.proxyPwdLineEdit.text())
                logging.debug('[%s]: save server=>%s user=>%s password=>%s into configuration',
                              self.getName(), server, user, password)
                proxyConf['proxy'] = server
                proxyConf['user'] = user
                proxyConf['password'] = password
                ConfManager.getManager().setValue('common', 'network', proxyConf)
                msg =  self.server.initMsg('proxy', None, MSG_UNIQUE_ID_T_CONTROLLER, True,
                            {'http': 'http://%s' % server, 'https': 'https://%s' % server})
                UMsgBus.getBus().send(msg)
                self.statusbar.showMessage('Applying proxy %s' % server)

                ConfManager.getManager().save()

        elif btn is self.ui.resetBtn:
            proxyConf = ConfManager.getManager().getValue('common', 'network')
            server = proxyConf['proxy']
            user = proxyConf['user']
            password = proxyConf['password']
            port = proxyConf['port']

            self.ui.proxyLineEdit.setText(server)
            self.ui.proxyNameLineEdit.setText(user)
            self.ui.proxyPwdLineEdit.setText(password)
            self.ui.portLineEdit.setText(str(port))
            self.ui.serverEnableCheckBox.setCheckState(0)

        elif btn is self.ui.unloadBtn:
            row = self.ui.pluginList.currentRow()
            it = str(self.ui.pluginList.currentItem().text())
            logging.debug('[%s]: unload plugin name %s', self.getName(), it)
            self.statusbar.showMessage('Unloading plugin %s' % it)
            PluginManager.getManager().unload(it)
            self.ui.pluginList.takeItem(row)
            conf = ConfManager.getManager().getValue('common', 'plugins')
            for pc in conf:
                if pc['name'] == it:
                    conf.remove(pc)
            ConfManager.getManager().setValue('common', 'plugins', conf)

        elif btn is self.ui.reloadBtn:
            row = self.ui.pluginList.currentRow()
            it = str(self.ui.pluginList.currentItem().text())
            logging.debug('[%s]: reload plugin name %s', self.getName(), it)
            self.statusbar.showMessage('Reloading plugin %s' % it)
            PluginManager.getManager().reload(it)

        elif btn is self.ui.addPluginBtn:
            path = QFileDialog.getOpenFileName(self)
            PluginManager.getManager().loadPluginFromPath(str(path))

        elif btn is self.ui.syncFolderBtn:
            folder = str(self.ui.folderList.currentItem().text())
            msg =  self.server.initMsg('sync', None, MSG_UNIQUE_ID_T_CONTROLLER, True, {'path': str(folderPath), 'action': 'add'})
            UMsgBus.getBus().send(msg)
            self.statusbar.showMessage('Sync %s with cloud' % folder)
Ejemplo n.º 52
0
 def process_IN_MOVED_TO(self, event):
     logging.debug('[%s]: name=>%s, path=>%s, src_pathname=>%s', event.name, event.path, event.src_pathname)
     self.fsMonitor.notify(FILE_MOVED_TO, event.path, event.name, event.src_pathname)
Ejemplo n.º 53
0
 def proxyHandler(self, param):
     """proxy handler for actors"""
     logging.debug('[%s]: proxyHandler with parm %s', self.getName(), param);
     res = E_PROXY_ERR
     res = set_proxy(param)
     return res, None
Ejemplo n.º 54
0
 def register(self, plugin):
     """register plugin to manager"""
     self.__plugin_list.append(plugin)
     logging.debug('[%s] registers to PluginManager', plugin.name)
Ejemplo n.º 55
0
 def debug(self):
     """docstring for test"""
     for p in self.__plugin_list:
         logging.debug('load %s ...' % p.name)
Ejemplo n.º 56
0
 def process_IN_ATTRIB(self, event):
     logging.debug('[%s]: ATTRIB file: %s', self.fsMonitor.getName(),
                   os.path.join(event.path, event.name))
Ejemplo n.º 57
0
    def connBtnSlots(self, btn):
        """docstring for connBtnSlots"""
        if btn is self.ui.connBtn:
            if btn.text() == 'Connect':
                msg = self.server.initMsg('info', None,
                                          MSG_UNIQUE_ID_T_CONTROLLER, True,
                                          {'name': 'all'})
                UMsgBus.getBus().send(msg)
                #res, data = self.server.getHandler('info')({'name': 'all'})
                btn.setText('Connecting')
                #self.ui.infoLabel.setText(data)
                logging.debug('[%s]: Press Connect to getCloudInfo',
                              self.getName())
            elif btn.text() == 'Disconnect':
                #self.server.getHandler('stop')({'name': 'all'})
                btn.setText('Connect')
                self.ui.infoLabel.setText('Cloud Disk is disconnected')

        elif btn is self.ui.addFolderBtn:
            fileDialog = QFileDialog(self)
            fileDialog.setWindowTitle('Select Folder')
            folderPath = fileDialog.getExistingDirectory()
            if folderPath != "":
                listItem = QListWidgetItem(QIcon('icon/folder.png'),
                                           folderPath, self.ui.folderList)
                pyStr = '%s' % folderPath
                logging.debug('[%s]: add folder path %s', self.getName(),
                              pyStr)
                self.ui.folderList.insertItem(self.ui.folderList.count(),
                                              listItem)
                folderList = ConfManager.getManager().getValue(
                    'common', 'folders')
                folderList.append(pyStr)
                ConfManager.getManager().setValue('common', 'folders',
                                                  folderList)
                #send watch dir request
                msg = self.server.initMsg('watch', None,
                                          MSG_UNIQUE_ID_T_CONTROLLER, True, {
                                              'path': pyStr,
                                              'action': 'add'
                                          })
                UMsgBus.getBus().send(msg)
                self.statusbar.showMessage('Adding watch path %s' % folderPath)

        elif btn is self.ui.rmFolderBtn:
            row = self.ui.folderList.currentRow()
            item = self.ui.folderList.currentItem()
            folderList = ConfManager.getManager().getValue('common', 'folders')
            self.statusbar.showMessage('Removing watch path %s' % item.text())
            pyStr = '%s' % item.text()
            folderList.remove(pyStr)
            ConfManager.getManager().setValue('common', 'folders', folderList)
            logging.debug('[%s]: remove item %d %s', self.getName(), row,
                          item.text())
            msg = self.server.initMsg('watch', None,
                                      MSG_UNIQUE_ID_T_CONTROLLER, True, {
                                          'path': pyStr,
                                          'action': 'rm'
                                      })
            UMsgBus.getBus().send(msg)
            self.ui.folderList.takeItem(row)

        elif btn is self.ui.saveBtn:
            proxyConf = ConfManager.getManager().getValue('common', 'network')
            server = str(self.ui.proxyLineEdit.text())

            if server != "" and server != None:
                user = str(self.ui.proxyNameLineEdit.text())
                password = str(self.ui.proxyPwdLineEdit.text())
                logging.debug(
                    '[%s]: save server=>%s user=>%s password=>%s into configuration',
                    self.getName(), server, user, password)
                proxyConf['proxy'] = server
                proxyConf['user'] = user
                proxyConf['password'] = password
                ConfManager.getManager().setValue('common', 'network',
                                                  proxyConf)
                msg = self.server.initMsg('proxy', None,
                                          MSG_UNIQUE_ID_T_CONTROLLER, True, {
                                              'http': 'http://%s' % server,
                                              'https': 'https://%s' % server
                                          })
                UMsgBus.getBus().send(msg)
                self.statusbar.showMessage('Applying proxy %s' % server)

                ConfManager.getManager().save()

        elif btn is self.ui.resetBtn:
            proxyConf = ConfManager.getManager().getValue('common', 'network')
            server = proxyConf['proxy']
            user = proxyConf['user']
            password = proxyConf['password']
            port = proxyConf['port']

            self.ui.proxyLineEdit.setText(server)
            self.ui.proxyNameLineEdit.setText(user)
            self.ui.proxyPwdLineEdit.setText(password)
            self.ui.portLineEdit.setText(str(port))
            self.ui.serverEnableCheckBox.setCheckState(0)

        elif btn is self.ui.unloadBtn:
            row = self.ui.pluginList.currentRow()
            it = str(self.ui.pluginList.currentItem().text())
            logging.debug('[%s]: unload plugin name %s', self.getName(), it)
            self.statusbar.showMessage('Unloading plugin %s' % it)
            PluginManager.getManager().unload(it)
            self.ui.pluginList.takeItem(row)
            conf = ConfManager.getManager().getValue('common', 'plugins')
            for pc in conf:
                if pc['name'] == it:
                    conf.remove(pc)
            ConfManager.getManager().setValue('common', 'plugins', conf)

        elif btn is self.ui.reloadBtn:
            row = self.ui.pluginList.currentRow()
            it = str(self.ui.pluginList.currentItem().text())
            logging.debug('[%s]: reload plugin name %s', self.getName(), it)
            self.statusbar.showMessage('Reloading plugin %s' % it)
            PluginManager.getManager().reload(it)

        elif btn is self.ui.addPluginBtn:
            path = QFileDialog.getOpenFileName(self)
            PluginManager.getManager().loadPluginFromPath(str(path))

        elif btn is self.ui.syncFolderBtn:
            folder = str(self.ui.folderList.currentItem().text())
            msg = self.server.initMsg('sync', None, MSG_UNIQUE_ID_T_CONTROLLER,
                                      True, {
                                          'path': str(folderPath),
                                          'action': 'add'
                                      })
            UMsgBus.getBus().send(msg)
            self.statusbar.showMessage('Sync %s with cloud' % folder)
Ejemplo n.º 58
0
 def parseResult(self, data):
     """parse plugin result"""
     logging.debug('[%s]: parseResult %s...', self.name, data)
Ejemplo n.º 59
0
 def run(self):
     """actor entry"""
     logging.debug('[%s]: is running', self.getName())
     self.regSelfToBus()
     self.__isRunning = True