Example #1
0
 def __init__(self):
     asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
     self.executor = ThreadPoolExecutor(max_workers=2)
     self.storage = Storage()
     self.api = API(self.storage, self.executor)
     self.task_processor = TaskProcessor(self.storage)
     self.api.set_execute_and_callback(self.task_processor.execute,
                                       self.task_processor.callback)
Example #2
0
    def __init__(self):

        # получаю доступ к вк
        vk = Storage(login, password, 'VK').get()

        r, msg = vk.login()
        if r is False:
            print msg
            sys.exit(1)

        # устанавливаю плеер, запускается в отдельном потоке
        self.play = PlayerApp()
        # стартую ui
        self.ca = CursesApplication(self.play, vk)
        # стартую отдельный поток, для генерации событий, т.к. не работают с ncurses сигналы. ХЗ почему. Странно очень
        clock = ClockThread(self.ca)
        clock.start()
Example #3
0
    def __init__(self, data_path, client_path='../client', debug=False):
        self._app = web.Application()
        self._app.router.add_route('POST', '/api', self._api_handler)
        self._app.router.add_route('GET', '/{path:.*}', self._web_handler)

        #self._app.router.add_static('/', '../client')
        self.client_data = client_path
        self.storage = Storage(storage_path=data_path)
        self.executor = Executor()

        self._debug = debug
        self._auth = Authenticator(self.storage)
        self._dispatcher = jsonrpc.Dispatcher(self, self._auth)
Example #4
0
class Main:
    def __init__(self):
        asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
        self.executor = ThreadPoolExecutor(max_workers=2)
        self.storage = Storage()
        self.api = API(self.storage, self.executor)
        self.task_processor = TaskProcessor(self.storage)
        self.api.set_execute_and_callback(self.task_processor.execute,
                                          self.task_processor.callback)

    async def init(self):
        await self.storage.init_connections()
        for _ in self.storage.launch_queue():
            self.executor.submit(
                self.task_processor.execute).add_done_callback(
                    self.task_processor.callback)

    def run(self):
        loop = asyncio.get_event_loop()
        loop.run_until_complete(self.init())
        self.api.run()
Example #5
0
    def run(self):
        Logger.getlogging().info('Spider start...')
        if self.istimeout():
            Logger.getlogging().warning('Spider timeout for %s, stop' %
                                        constant.SPIDER_RUN_TIMEOUT_HOUR)
            sys.exit(-1)
        SpiderConfigure.getinstance().starttime()
        # 删除临时文件
        Storage.removecachefile()
        Storage.mkcachedir()
        self.etl.updatedb()
        #query数据库中的记录更新
        if SpiderConfigure.ismaster():
            self.etl.storagequery()

        if SpiderConfigure.iswaibu():
            #外部数据
            self.waibuetl()
        else:
            #本地数据
            self.localetl()
        #导出urls生成本地文件
        self.etl.dumpurls()
        self.upload()
        self.loop()

        if SpiderConfigure.iswaibu():
            #外部数据刷新
            self.etl.wb_updatedb()
        NewsStorage.show()
        # push result files
        # self.pushfiles()
        # 删除临时文件
        Storage.removecachefile()
        # FINISH
        Logger.getlogging().info('FINISH')
Example #6
0
class Main(MainFrame):
    ABOUT_LICENSE = "(under the BSD License)"
    ABOUT_URL    = "https://github.com/DarkCthulhu/PyNote"
    
    # constructor
    def __init__(self):
        MainFrame.__init__(self, None)
        self.store = Storage()
        self.populate_list()
    
    # shows an info-message            
    def show_info(self, message):
        wx.MessageBox(message, 'Info', wx.OK | wx.ICON_INFORMATION)
    
    # Find the entries from the sqlite database and populate
    def populate_list(self):
        notes = self.store.list_notes()
        notes_list = [note[0] for note in notes]
        self.m_noteList.InsertItems(notes_list, 0)
    
    # called when the list needs to be refreshed
    def list_modified(self):
        # if not deselected, it remembers selection even after element is deleted
        self.m_noteList.Deselect(self.m_noteList.GetSelection())
        self.m_noteList.Clear()
        self.populate_list()
        
    # overrides for UI events
    def list_itemclick(self, event):
        selected = self.m_noteList.GetStringSelection()
        self.m_noteName.SetValue(selected)
        self.m_noteContent.SetValue(self.store.retrieve_file(selected))
    
    #handler for the about button click in the Help menu
    def about_click( self, event ):
        about_str = Main.ABOUT_URL +"\n"+ Main.ABOUT_LICENSE
        self.show_info(about_str)
	
    #on clicking save button on the UI
    def save_click( self, event ):
        #TODO: Validation
        operation_state = self.store.save_file(self.m_noteName.GetValue(), self.m_noteContent.GetValue())
        if(operation_state):
            self.list_modified()
            self.m_statusBar.SetStatusText("Saved Successfully", 0)
        else:
            self.show_info("An Error Occurred")
        
    # click handler for the delete button on the UI
    def delete_click(self, event):
        selected = self.m_noteList.GetStringSelection()
        operation_state = self.store.delete_file(selected)
        if(operation_state):
            self.list_modified()
            self.m_statusBar.SetStatusText("Deleted Successfully", 0)
        else:
            self.show_info("An Error Occurred")
    
    # click handler for the new button on the UI        
    def new_click(self, event):
        self.m_noteName.Clear()
        self.m_noteContent.Clear()
        self.m_noteContent.SetFocus()
        
        
    # filtering through titles
    def search_event( self, event ):
        # deselect current selection and clear the list
        self.m_noteList.Deselect(self.m_noteList.GetSelection())
        self.m_noteList.Clear()
        
        notes = self.store.list_notes(self.m_search.GetValue())
        notes_list = [note[0] for note in notes]
        self.m_noteList.InsertItems(notes_list, 0)
Example #7
0
 def __init__(self):
     MainFrame.__init__(self, None)
     self.store = Storage()
     self.populate_list()
Example #8
0
class Server:    
    def __init__(self, data_path, client_path='../client', debug=False):
        self._app = web.Application()
        self._app.router.add_route('POST', '/api', self._api_handler)
        self._app.router.add_route('GET', '/{path:.*}', self._web_handler)

        #self._app.router.add_static('/', '../client')
        self.client_data = client_path
        self.storage = Storage(storage_path=data_path)
        self.executor = Executor()

        self._debug = debug
        self._auth = Authenticator(self.storage)
        self._dispatcher = jsonrpc.Dispatcher(self, self._auth)

    @jsonrpc_method(str, str)
    @jsonrpc_noauth
    async def user_authorize(self, login, password):
        self._log('user_authorize({0}, {1})'.format(login, password))

        auth_token = self.storage.users.auth(login, password)

        if not auth_token:
            self._log('  auth failed: invalid login or password')
            raise jsonrpc.AuthError("Неверная пара логин\\пароль!")

        self._log('return: auth token: {0}'.format(auth_token))
        return auth_token

    @jsonrpc_method(str, bool)
    async def path_list(self, path, recursive):
        self._log('path_list({0})'.format(path))

        context = self._context()

        try:
            contents = self.storage.list(path, context, recursive=recursive)
            result = [item.to_dict() for item in contents]
            self._log("return: items: {0}".format(", ".join([item.name for item in contents])))

            return result
        except InvalidPathError as err:
            raise RpcInvalidPathError("Неверный путь", path) from err
        except NoSuchPathError as err:
            raise RpcNoSuchPathError("Нет такого каталога", path) from err

    @jsonrpc_method(str)
    async def algorithm_fetch(self, path):
        self._log('algorithm_fetch({0})'.format(path))

        context = self._context()

        try:
            contents = await self.storage.file_read(path, context)
            result = contents.to_dict()
            result['name'] = path.split('/')[-1]

            self._log("return: algorithm {0}".format(result.get("name")))

            return result
        except InvalidPathError as err:
            raise RpcInvalidPathError("Неверный путь", path) from err
        except NoSuchPathError as err:
            raise RpcNoSuchPathError("Нет такого файла", path) from err

    @jsonrpc_method(dict, dict)
    async def algorithm_exec(self, alg, args):
        self._log('algorithm_exec({0}, {1})'.format("alg", args))

        context = self._context()

        try:
            self._log("  executing algorithm")
            result = await self.executor.run(Algorithm.from_dict(alg), args)

            self._log("return: result: {0}".format(result))
            return result
        except AlgorithmError as err:
            raise RpcAlgorithmExecError(err.msg)

    @jsonrpc_method(str, dict)
    async def algorithm_create(self, path, alg_dict):
        self._log('algorithm_create({0}, alg)'.format(path))

        context = self._context()

        alg = Algorithm.from_dict(alg_dict)

        try:
            await self.storage.create(path, content=alg, context=context)

            self._log("return: OK")

            return 'OK'
        except InvalidPathError as err:
            raise RpcInvalidPathError("Неверный путь", path) from err

    @jsonrpc_method(str)
    async def path_create(self, path):
        self._log('path_create({0})'.format(path))

        context = self._context()

        try:
            await self.storage.create(path, context=context)

            self._log("return: OK")

            return 'OK'
        except InvalidPathError as err:
            raise RpcInvalidPathError("Неверный путь", path) from err

    @jsonrpc_method(str, str)
    async def path_move(self, source, dest):
        self._log('path_move({0},{1})'.format(source, dest))

        context = self._context()

        try:
            await self.storage.move(source, dest, context)

            self._log("return: OK")

            return 'OK'
        except InvalidPathError as err:
            raise RpcInvalidPathError("Неверный путь", err.args[1]) from err
        except NoSuchPathError as err:
            raise RpcNoSuchPathError("Нет такого файла или каталога", err.args[1]) from err

    @jsonrpc_method(str, dict)
    async def algorithm_update(self, path, alg_dict):
        self._log('algorithm_update({0}, alg)'.format(path))

        context = self._context()
        alg = Algorithm.from_dict(alg_dict)

        try:
            await self.storage.file_write(path, alg, context)

            self._log("return: OK")

            return 'OK'
        except InvalidPathError as err:
            raise RpcInvalidPathError("Неверный путь", err.args[1]) from err
        except NoSuchPathError as err:
            raise RpcNoSuchPathError("Нет такого файла", err.args[1]) from err

    @jsonrpc_method(str)
    async def path_remove(self, path):
        self._log('algorithm_update({0}, alg)'.format(path))

        context = self._context()

        try:
            await self.storage.remove(path, context)

            self._log("return: OK")

            return 'OK'
        except InvalidPathError as err:
            raise RpcInvalidPathError("Неверный путь", err.args[1]) from err
        except NoSuchPathError as err:
            raise RpcNoSuchPathError("Нет такого файла или каталога", err.args[1]) from err

    async def _web_handler(self, request):
        path = request.path
        if path == '/':
            path = '/index.html'

        content_root = self.client_data
        if self._debug:
            print('GET static file: ' + content_root + path)

        if os.path.exists(content_root + path) and not os.path.isdir(content_root + path):
            with open(content_root + path, 'rb') as f:
                resp = f.read()
        else:
            raise web.HTTPNotFound()
	
        mimetype,_ = mimetypes.guess_type(content_root + path)

        return web.Response(body=resp, content_type = mimetype if mimetype else 'application/octet-stream')

    async def _api_handler(self, request):
        text = await request.text()
        resp = await self._dispatcher.handle(text)

        return web.Response(body=resp.encode('utf-8'))

    def run(self, host, port):
        web.run_app(self._app, host=host, port=port)

    def _context(self):
        user = self.storage.users.by_token(self._auth.token)
        return Storage.StorageContext(user_login=user.login)

    def _log(self, msg):
        if self._debug:
            print(msg)