Python tornado.util.ObjectDict() Examples

The following are 30 code examples of tornado.util.ObjectDict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.util , or try the search function .
Example #1
Source File: web.py    From honeything with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, application, request, **kwargs):
        self.application = application
        self.request = request
        self._headers_written = False
        self._finished = False
        self._auto_finish = True
        self._transforms = None  # will be set in _execute
        self.ui = ObjectDict((n, self._ui_method(m)) for n, m in
                     application.ui_methods.iteritems())
        # UIModules are available as both `modules` and `_modules` in the
        # template namespace.  Historically only `modules` was available
        # but could be clobbered by user additions to the namespace.
        # The template {% module %} directive looks in `_modules` to avoid
        # possible conflicts.
        self.ui["_modules"] = ObjectDict((n, self._ui_module(n, m)) for n, m in
                                 application.ui_modules.iteritems())
        self.ui["modules"] = self.ui["_modules"]
        self.clear()
        # Check since connection is not available in WSGI
        if getattr(self.request, "connection", None):
            self.request.connection.stream.set_close_callback(
                self.on_connection_close)
        self.initialize(**kwargs) 
Example #2
Source File: template_test.py    From teleport with Apache License 2.0 6 votes vote down vote up
def test_error_line_number_module(self):
        loader = None

        def load_generate(path, **kwargs):
            return loader.load(path).generate(**kwargs)

        loader = DictLoader({
            "base.html": "{% module Template('sub.html') %}",
            "sub.html": "{{1/0}}",
        }, namespace={"_tt_modules": ObjectDict(Template=load_generate)})
        try:
            loader.load("base.html").generate()
            self.fail("did not get expected exception")
        except ZeroDivisionError:
            exc_stack = traceback.format_exc()
            self.assertTrue('# base.html:1' in exc_stack)
            self.assertTrue('# sub.html:1' in exc_stack) 
Example #3
Source File: lagou_companies.py    From webspider with MIT License 6 votes vote down vote up
def clean_lg_company_data(company_dict):
    """
    清洗爬取到的公司信息

    :param company_dict: tornado.util.ObjectDict
    """
    if 'size' in company_dict:
        company_dict.size = company_dict.size.strip()
    if 'finance_stage' in company_dict:
        company_dict.finance_stage = company_dict.finance_stage.strip()
    if 'features' in company_dict:
        company_dict.features = utils.text.to_plaintext(company_dict.features)
    if 'address' in company_dict:
        company_dict.address = utils.text.to_plaintext(company_dict.address)
    if 'introduce' in company_dict:
        company_dict.introduce = ''.join(company_dict.introduce) if company_dict.introduce else ''
        company_dict.introduce = company_dict.introduce[:constants.COMPANY_INTRODUCE_MAX_LEN]
    if 'advantage' in company_dict:
        company_dict.advantage = list(map(utils.text.to_plaintext, company_dict.advantage))
        company_dict.advantage = json.dumps(company_dict.advantage)[
            :constants.COMPANY_ADVANTAGE_MAX_LEN]
    if 'industries' in company_dict:
        company_dict.industries = set(re.split(r",|,|、|\s", company_dict.industries)) 
Example #4
Source File: server.py    From tornado_http2 with Apache License 2.0 6 votes vote down vote up
def __init__(self, conn, http2_params, server):
        self.conn = conn
        self.context = conn.context
        self.http2_params = http2_params
        self.server = server
        self.upgrading = False
        self.written_headers = None
        self.written_chunks = []
        self.write_finished = False
        self.close_callback = None
        self.max_body_size = None
        self.body_timeout = None

        # TODO: remove
        from tornado.util import ObjectDict
        self.stream = ObjectDict(io_loop=IOLoop.current(), close=conn.stream.close) 
Example #5
Source File: stream.py    From tornado_http2 with Apache License 2.0 6 votes vote down vote up
def __init__(self, conn, stream_id, delegate, context=None):
        self.conn = conn
        self.stream_id = stream_id
        self.set_delegate(delegate)
        self.context = context
        self.finish_future = Future()
        self.write_lock = Lock()
        from tornado.util import ObjectDict
        # TODO: remove
        self.stream = ObjectDict(io_loop=IOLoop.current(), close=conn.stream.close)
        self._incoming_content_remaining = None
        self._outgoing_content_remaining = None
        self._delegate_started = False
        self.window = Window(conn.window, stream_id,
                             conn.setting(constants.Setting.INITIAL_WINDOW_SIZE))
        self._header_frames = []
        self._phase = constants.HTTPPhase.HEADERS 
Example #6
Source File: handlers.py    From django-livereload-server with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def on_message(self, message):
        """Handshake with livereload.js

        1. client send 'hello'
        2. server reply 'hello'
        3. client send 'info'
        """
        message = ObjectDict(escape.json_decode(message))
        if message.command == 'hello':
            handshake = {
                'command': 'hello',
                'protocols': [
                    'http://livereload.com/protocols/official-7',
                ],
                'serverName': 'livereload-tornado',
            }
            self.send_message(handshake)

        if message.command == 'info' and 'url' in message:
            logger.info('Browser Connected: %s' % message.url)
            LiveReloadHandler.waiters.add(self) 
Example #7
Source File: template_test.py    From opendevops with GNU General Public License v3.0 6 votes vote down vote up
def test_error_line_number_module(self):
        loader = None  # type: typing.Optional[DictLoader]

        def load_generate(path, **kwargs):
            assert loader is not None
            return loader.load(path).generate(**kwargs)

        loader = DictLoader(
            {"base.html": "{% module Template('sub.html') %}", "sub.html": "{{1/0}}"},
            namespace={"_tt_modules": ObjectDict(Template=load_generate)},
        )
        try:
            loader.load("base.html").generate()
            self.fail("did not get expected exception")
        except ZeroDivisionError:
            exc_stack = traceback.format_exc()
            self.assertTrue("# base.html:1" in exc_stack)
            self.assertTrue("# sub.html:1" in exc_stack) 
Example #8
Source File: session.py    From app-turbo with Apache License 2.0 6 votes vote down vote up
def __init__(self, app, handler, store, initializer, session_config=None, session_object=None):
        self.handler = handler
        self.app = app
        self.store = store or DiskStore(app_config.store_config.diskpath)

        self._config = deepcopy(app_config.session_config)
        if session_config:
            self._config.update(session_config)

        self._session_name = self._config.name

        self._session_object = (session_object or CookieObject)(
            app, handler, self.store, self._config)

        self._data = ObjectDict()
        self._initializer = initializer

        self.session_id = None

        self._dirty = False
        self._processor() 
Example #9
Source File: template_test.py    From pySINDy with MIT License 6 votes vote down vote up
def test_error_line_number_module(self):
        loader = None

        def load_generate(path, **kwargs):
            return loader.load(path).generate(**kwargs)

        loader = DictLoader({
            "base.html": "{% module Template('sub.html') %}",
            "sub.html": "{{1/0}}",
        }, namespace={"_tt_modules": ObjectDict(Template=load_generate)})
        try:
            loader.load("base.html").generate()
            self.fail("did not get expected exception")
        except ZeroDivisionError:
            exc_stack = traceback.format_exc()
            self.assertTrue('# base.html:1' in exc_stack)
            self.assertTrue('# sub.html:1' in exc_stack) 
Example #10
Source File: template_test.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
def test_error_line_number_module(self):
        loader = None  # type: typing.Optional[DictLoader]

        def load_generate(path, **kwargs):
            assert loader is not None
            return loader.load(path).generate(**kwargs)

        loader = DictLoader(
            {"base.html": "{% module Template('sub.html') %}", "sub.html": "{{1/0}}"},
            namespace={"_tt_modules": ObjectDict(Template=load_generate)},
        )
        try:
            loader.load("base.html").generate()
            self.fail("did not get expected exception")
        except ZeroDivisionError:
            exc_stack = traceback.format_exc()
            self.assertTrue("# base.html:1" in exc_stack)
            self.assertTrue("# sub.html:1" in exc_stack) 
Example #11
Source File: handlers.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
def on_message(self, message):
        """Handshake with livereload.js

        1. client send 'hello'
        2. server reply 'hello'
        3. client send 'info'
        """
        message = ObjectDict(escape.json_decode(message))
        if message.command == 'hello':
            handshake = {
                'command': 'hello',
                'protocols': [
                    'http://livereload.com/protocols/official-7',
                ],
                'serverName': 'livereload-tornado',
            }
            self.send_message(handshake)

        if message.command == 'info' and 'url' in message:
            logger.info('Browser Connected: %s' % message.url)
            LiveReloadHandler.waiters.add(self) 
Example #12
Source File: web_test.py    From pySINDy with MIT License 5 votes vote down vote up
def __init__(self, cookie_secret='0123456789', key_version=None):
        # don't call super.__init__
        self._cookies = {}
        if key_version is None:
            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret))
        else:
            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret,
                                                        key_version=key_version)) 
Example #13
Source File: template.py    From pySINDy with MIT License 5 votes vote down vote up
def generate(self, **kwargs):
        """Generate this template with the given arguments."""
        namespace = {
            "escape": escape.xhtml_escape,
            "xhtml_escape": escape.xhtml_escape,
            "url_escape": escape.url_escape,
            "json_encode": escape.json_encode,
            "squeeze": escape.squeeze,
            "linkify": escape.linkify,
            "datetime": datetime,
            "_tt_utf8": escape.utf8,  # for internal use
            "_tt_string_types": (unicode_type, bytes),
            # __name__ and __loader__ allow the traceback mechanism to find
            # the generated source code.
            "__name__": self.name.replace('.', '_'),
            "__loader__": ObjectDict(get_source=lambda name: self.code),
        }
        namespace.update(self.namespace)
        namespace.update(kwargs)
        exec_in(self.compiled, namespace)
        execute = namespace["_tt_execute"]
        # Clear the traceback module's cache of source data now that
        # we've generated a new template (mainly for this module's
        # unittests, where different tests reuse the same name).
        linecache.clearcache()
        return execute() 
Example #14
Source File: template.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def generate(self, **kwargs):
        """Generate this template with the given arguments."""
        namespace = {
            "escape": escape.xhtml_escape,
            "xhtml_escape": escape.xhtml_escape,
            "url_escape": escape.url_escape,
            "json_encode": escape.json_encode,
            "squeeze": escape.squeeze,
            "linkify": escape.linkify,
            "datetime": datetime,
            "_utf8": escape.utf8,  # for internal use
            "_string_types": (unicode, bytes_type),
            # __name__ and __loader__ allow the traceback mechanism to find
            # the generated source code.
            "__name__": self.name.replace('.', '_'),
            "__loader__": ObjectDict(get_source=lambda name: self.code),
        }
        namespace.update(self.namespace)
        namespace.update(kwargs)
        exec self.compiled in namespace
        execute = namespace["_execute"]
        # Clear the traceback module's cache of source data now that
        # we've generated a new template (mainly for this module's
        # unittests, where different tests reuse the same name).
        linecache.clearcache()
        try:
            return execute()
        except Exception:
            formatted_code = _format_code(self.code).rstrip()
            logging.error("%s code:\n%s", self.name, formatted_code)
            raise 
Example #15
Source File: template_test.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def test_error_line_number_module(self):
        loader = DictLoader({
            "base.html": "{% module Template('sub.html') %}",
            "sub.html": "{{1/0}}",
        }, namespace={"_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})})
        try:
            loader.load("base.html").generate()
        except ZeroDivisionError:
            exc_stack = traceback.format_exc()
            self.assertTrue('# base.html:1' in exc_stack)
            self.assertTrue('# sub.html:1' in exc_stack) 
Example #16
Source File: web_test.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        # don't call super.__init__
        self._cookies = {}
        self.application = ObjectDict(settings=dict(cookie_secret='0123456789')) 
Example #17
Source File: session.py    From app-turbo with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, key):
        path = self._get_path(key)
        if os.path.exists(path):
            pickled = open(path).read()
            return self.decode(pickled)
        else:
            return ObjectDict() 
Example #18
Source File: base.py    From webspider with MIT License 5 votes vote down vote up
def format(cls, data):
        if isinstance(data, list):
            return [cls.format(item) for item in data]
        else:
            formatter = cls.get_formatter(data)
            if not formatter:
                raise ValueError(u'Can not find the formatter by model {}'.format(type(data)))

            format_result = ObjectDict()
            for field in formatter.FIELDS:
                if not isinstance(field, Field):
                    raise ValueError('formatter field must be Field instance')
                try:
                    value = getattr(data, field.name)
                    # 可再次渲染
                    if isinstance(value, list) or cls.get_formatter(value):
                        value = cls.format(value)
                    if field.converter:
                        value = field.converter(value)
                except Exception:
                    # Field 设置了降级
                    if field.downgrade:
                        value = field.downgrade.value
                    else:
                        raise
                format_result[field.name] = value

            return format_result 
Example #19
Source File: base.py    From webspider with MIT License 5 votes vote down vote up
def dict(self):
        """sqlalchemy object -> dict"""
        columns = self.__table__.columns.keys()
        return ObjectDict((column, getattr(self, column)) for column in columns) 
Example #20
Source File: lagou_jobs.py    From webspider with MIT License 5 votes vote down vote up
def get_job_detail_from_lg(lg_job_id):
    """
    爬取职位详情页的数据

    返回的 dict 组成:
    department:
        type: str
        meaning: 招聘部门
        eg:  商业部
    keywords:
        type: List[str]
        meaning: 职位关键词
        eg: ['后端', 'Web', 'Python']
    description:
        type: List[str]
        meaning: 职位介绍
        eg: ['职位要求:', 'blablabla', '.......']

    :param lg_job_id: 接口使用的职位 id
    :return: 职位详情页数据
    :rtype: tornado.util.ObjectDict
    """
    response = utils.http_tools.requests_get(
        url=constants.JOB_DETAIL_URL.format(lg_job_id=lg_job_id))
    job_detail_html = etree.HTML(response.text)

    department = job_detail_html.xpath('//div[@class="job-name"]/div[@class="company"]/text()')
    description = job_detail_html.xpath('//dd[@class="job_bt"]/div//text()')
    keywords = job_detail_html.xpath('//dd[@class="job_request"]//li[@class="labels"]/text()')

    if not department:
        logger.error('can not get department by lg_job_id = {}, html is \n {}'.format(
            lg_job_id, response.text))

    return ObjectDict(
        department=department[0] if department else '',
        description=description,
        keywords=keywords,
    ) 
Example #21
Source File: lagou_cites.py    From webspider with MIT License 5 votes vote down vote up
def get_cites_from_lg():
    """
    爬取城市数据

    返回的 dict 组成:
    id:
        type: int
        meaning: 城市 id
        eg: 1
    name:
        type: str
        meaning: 城市名
        eg: 北京

    :return: 城市数据集合
    :rtype: List[tornado.util.ObjectDict]
    """
    logger.info(u'begin crawl cities info......')

    response_html = etree.HTML(requests.get(constants.ALL_CITY_URL).text)
    cities_html_list = response_html.xpath("//ul[@class='city_list']/li/a")

    cities_dicts = []
    for city_html in cities_html_list:
        city_name = city_html.xpath('./text()')[0]
        city_id = re.findall(pattern=r'/(\d+)-\d+-\d+', string=city_html.xpath('./@href')[0])[0]
        cities_dicts.append(ObjectDict(id=city_id, name=city_name))

    logger.info(u'crawl cities info finished! cites quantity is {cities_count}'.format(
        cities_count=len(cities_dicts)))
    return cities_dicts 
Example #22
Source File: template.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def generate(self, **kwargs):
        """Generate this template with the given arguments."""
        namespace = {
            "escape": escape.xhtml_escape,
            "xhtml_escape": escape.xhtml_escape,
            "url_escape": escape.url_escape,
            "json_encode": escape.json_encode,
            "squeeze": escape.squeeze,
            "linkify": escape.linkify,
            "datetime": datetime,
            "_tt_utf8": escape.utf8,  # for internal use
            "_tt_string_types": (unicode_type, bytes),
            # __name__ and __loader__ allow the traceback mechanism to find
            # the generated source code.
            "__name__": self.name.replace('.', '_'),
            "__loader__": ObjectDict(get_source=lambda name: self.code),
        }
        namespace.update(self.namespace)
        namespace.update(kwargs)
        exec_in(self.compiled, namespace)
        execute = namespace["_tt_execute"]
        # Clear the traceback module's cache of source data now that
        # we've generated a new template (mainly for this module's
        # unittests, where different tests reuse the same name).
        linecache.clearcache()
        return execute() 
Example #23
Source File: template_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_error_line_number_module(self):
        loader = DictLoader({
            "base.html": "{% module Template('sub.html') %}",
            "sub.html": "{{1/0}}",
        }, namespace={"_tt_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})})
        try:
            loader.load("base.html").generate()
            self.fail("did not get expected exception")
        except ZeroDivisionError:
            exc_stack = traceback.format_exc()
            self.assertTrue('# base.html:1' in exc_stack)
            self.assertTrue('# sub.html:1' in exc_stack) 
Example #24
Source File: web_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, cookie_secret='0123456789', key_version=None):
        # don't call super.__init__
        self._cookies = {}
        if key_version is None:
            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret))
        else:
            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret,
                                                        key_version=key_version)) 
Example #25
Source File: web.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def __init__(
        self,
        application: "Application",
        request: httputil.HTTPServerRequest,
        **kwargs: Any
    ) -> None:
        super(RequestHandler, self).__init__()

        self.application = application
        self.request = request
        self._headers_written = False
        self._finished = False
        self._auto_finish = True
        self._prepared_future = None
        self.ui = ObjectDict(
            (n, self._ui_method(m)) for n, m in application.ui_methods.items()
        )
        # UIModules are available as both `modules` and `_tt_modules` in the
        # template namespace.  Historically only `modules` was available
        # but could be clobbered by user additions to the namespace.
        # The template {% module %} directive looks in `_tt_modules` to avoid
        # possible conflicts.
        self.ui["_tt_modules"] = _UIModuleNamespace(self, application.ui_modules)
        self.ui["modules"] = self.ui["_tt_modules"]
        self.clear()
        assert self.request.connection is not None
        # TODO: need to add set_close_callback to HTTPConnection interface
        self.request.connection.set_close_callback(  # type: ignore
            self.on_connection_close
        )
        self.initialize(**kwargs)  # type: ignore 
Example #26
Source File: template.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def generate(self, **kwargs: Any) -> bytes:
        """Generate this template with the given arguments."""
        namespace = {
            "escape": escape.xhtml_escape,
            "xhtml_escape": escape.xhtml_escape,
            "url_escape": escape.url_escape,
            "json_encode": escape.json_encode,
            "squeeze": escape.squeeze,
            "linkify": escape.linkify,
            "datetime": datetime,
            "_tt_utf8": escape.utf8,  # for internal use
            "_tt_string_types": (unicode_type, bytes),
            # __name__ and __loader__ allow the traceback mechanism to find
            # the generated source code.
            "__name__": self.name.replace(".", "_"),
            "__loader__": ObjectDict(get_source=lambda name: self.code),
        }
        namespace.update(self.namespace)
        namespace.update(kwargs)
        exec_in(self.compiled, namespace)
        execute = typing.cast(Callable[[], bytes], namespace["_tt_execute"])
        # Clear the traceback module's cache of source data now that
        # we've generated a new template (mainly for this module's
        # unittests, where different tests reuse the same name).
        linecache.clearcache()
        return execute() 
Example #27
Source File: web_test.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def __init__(self, cookie_secret="0123456789", key_version=None):
        # don't call super.__init__
        self._cookies = {}  # type: typing.Dict[str, bytes]
        if key_version is None:
            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret))
        else:
            self.application = ObjectDict(
                settings=dict(cookie_secret=cookie_secret, key_version=key_version)
            ) 
Example #28
Source File: web_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        # don't call super.__init__
        self._cookies = {}
        self.application = ObjectDict(settings=dict(cookie_secret='0123456789')) 
Example #29
Source File: template_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_error_line_number_module(self):
        loader = DictLoader({
            "base.html": "{% module Template('sub.html') %}",
            "sub.html": "{{1/0}}",
        }, namespace={"_tt_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})})
        try:
            loader.load("base.html").generate()
            self.fail("did not get expected exception")
        except ZeroDivisionError:
            exc_stack = traceback.format_exc()
            self.assertTrue('# base.html:1' in exc_stack)
            self.assertTrue('# sub.html:1' in exc_stack) 
Example #30
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def __init__(self, cookie_secret='0123456789', key_version=None):
        # don't call super.__init__
        self._cookies = {}
        if key_version is None:
            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret))
        else:
            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret,
                                                        key_version=key_version))