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()
def get(self): ctx = ObjectDict() ctx.search_active = True ctx.search = ObjectDict() data = self.application.data ctx.particle_types = yield data.find_particle_types() self.render("latticemodel/lattice_search.html", **ctx)
def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: logger.info(args) logger.info(kwargs) logger.info("====[EXIT]====") # svc = args[0] # # from code import interact # # interact(local=locals()) # if isinstance(e, Exception): # svc.db.rollback() # logger.info("====[ROLLBACK]====") # else: # svc.db.commit() # # svc.db.flush() # logger.info("====[COMMIT]====") # svc.db.remove() # svc.db.close() logger.info("====[CLOSE]====") # svc = args[0] # svc.db.close() # svc.db.remove() # logger.info(svc.db) logThrown() data = ObjectDict() data.return_code = ERROR.system_err.errcode data.return_message = e.__unicode__() data.data = None return data
def read_meta(path): """ :param path: path for the theme. :return: Theme meta read in path. """ if not os.path.exists(path): return meta = os.path.join(path, 'theme.py') if not os.path.exists(meta): logging.warn("%s is not a catsup theme." % path) return theme = ObjectDict( name='', author='', homepage='', pages=[], has_index=False, path=path, vars={}, ) execfile(meta, {}, theme) templates_path = os.path.join(path, 'templates') for page in theme.pages: if page == 'page.html': theme.has_index = True # If your theme does not have index page, # catsup will rename page/1.html to page.html. if not os.path.exists(os.path.join(templates_path, page)): logging.warning("%s announces a page %s" " which not exists." % (theme.name, page)) theme.name = theme.name.lower() return theme
def on_message(self, message): if not self.current_user.is_authenticated(): self.write_message({"error": "logged_out"}) return False obj = loads(message) data = ObjectDict() if settings.DEBUG: print(obj) data.debug = True if obj.get("competition"): if obj["competition"] == "global": self.competition = "global" IOLoop.current().add_callback(load_competition_scores, self.competition) else: self.competition = obj["competition"] self._competition = Competition.objects.get( pk=self.competition) listeners.competitions[self.competition][ self.current_user.id].append(self) if load_competition(self.competition): IOLoop.current().add_callback(send_scores, self.competition) listeners.scoreboards[self.competition].append(self) if obj.get("clock") and (self._competition.get_role(self.current_user) != "compete"): competition_clock_operation(obj["clock"], self._competition) self.write_message(data)
def post(self): data = self.application.data lattice_ids = self.get_arguments("lattice") if len(lattice_ids) < 2: self.send_error(400, message="Must select two lattice for comparison") return lattice1 = yield data.find_lattice_by_id(lattice_ids[0]) if not lattice1: self.send_error(400, message="Lattice (1) not found: " + lattice_ids[0]) return lattice_elements1 = yield data.find_lattice_elements_by_lattice_id(lattice_ids[0]) lattice2 = yield data.find_lattice_by_id(lattice_ids[1]) if not lattice2: self.send_error(400, message="Lattice (2) not found: " + lattice_ids[1]) return lattice_elements2 = yield data.find_lattice_elements_by_lattice_id(lattice_ids[1]) ctx = ObjectDict() ctx.lattice = (lattice1, lattice2) n1 = len(lattice_elements1) n2 = len(lattice_elements2) ctx.lattice_elements = [] for idx in range(max(n1, n2)): if idx < n1 and idx < n2: ctx.lattice_elements.append((lattice_elements1[idx], lattice_elements2[idx])) elif idx < n1: ctx.lattice_elements.append((lattice_elements1[idx], None)) elif idx < n2: ctx.lattice_elements.append((None, lattice_elements2[idx])) ctx.particle_types = yield data.find_particle_types() self.render("latticemodel/lattice_compare.html", **ctx)
def get_resources_by_status(self): logger.info("\t ==========[ get_resources_by_status ]==========") conditions = and_() or_conditions = or_() or_conditions.append(Pro_Resource_Apply.status == STATUS_RESOURCE.APPLIED) or_conditions.append(Pro_Resource_Apply.status == STATUS_RESOURCE.CHECKED) or_conditions.append(Pro_Resource_Apply.status == STATUS_RESOURCE.PAYED) or_conditions.append(Pro_Resource_Apply.status == STATUS_RESOURCE.CONFIRMPAYED) or_conditions.append(Pro_Resource_Apply.status == STATUS_RESOURCE.STARTED) or_conditions.append(Pro_Resource_Apply.status == STATUS_RESOURCE.CLOSED) conditions.append(or_conditions) res_status = self.params.get("res_status") if res_status: conditions.append(Pro_Resource_Apply.status == res_status) env_id = self.params.get("env_id", 0) if env_id: conditions.append(Pro_Info.env_id == env_id) logger.info("\t [res_status]: %s" % res_status) resource_list = self.db.query( Pro_Resource_Apply ).outerjoin( Pro_Info, Pro_Info.id == Pro_Resource_Apply.pro_id ).filter( conditions ).order_by( Pro_Resource_Apply.create_time.desc() ).all() # 按状态查询申请数量 status_counts = self.db.query( Pro_Resource_Apply.status, func.count(Pro_Resource_Apply.id) ).filter( or_conditions ).group_by( Pro_Resource_Apply.status ).all() status_counts = dict(status_counts) logger.info("status_counts: %s" % status_counts) # 按环境查询申请数量 env_counts = self.db.query( Pro_Info.env_id, func.count(Pro_Resource_Apply.id) ).outerjoin( Pro_Resource_Apply, Pro_Resource_Apply.pro_id == Pro_Info.id ).outerjoin( Env_Info, Env_Info.id == Pro_Info.env_id ).filter( or_conditions ).group_by( Env_Info.id ).all() env_counts = dict(env_counts) logger.info("env_counts: %s" % env_counts) data = ObjectDict() data.resource_list = resource_list data.status_counts = status_counts data.env_counts = env_counts return self.success(data=data)
def fetch(s, url, **kwargs): self.assertEqual(url, expected_url) fut = asyncio.Future() response = ObjectDict() response.code = 200 response.body = content fut.set_result(response) return fut
def failures(self, failure_list, data=None): result = ObjectDict() result.return_code = ERROR.database_save_err.errcode # result.return_message = u",".join(["(%s)%s" % (f.return_code, f.return_message) for f in failure_list]) result.return_message = u"\n,".join([f for f in failure_list]) result.return_messages = failure_list result.data = data return result
def _to_object_dict(ret): if isinstance(ret, list): for i in range(len(ret)): ret[i] = _to_object_dict(ret[i]) elif isinstance(ret, TARGET): ret = ObjectDict(ret) for k, v in ret.iteritems(): if isinstance(v, datetime): ret[k] = str(v) return ret
def _create_upload_context(self): data = self.application.data ctx = ObjectDict() ctx.upload_active = True ctx.particle_types = yield data.find_particle_types() ctx.lattice_types = yield data.find_lattice_types() ctx.lattice = ObjectDict(lattice_type=self.type) ctx.lattice_autoversion = True ctx.errors = ObjectDict() raise Return(ctx)
def _create_upload_context(self): data = self.application.data ctx = ObjectDict() ctx.upload_active = True ctx.particle_types, ctx.model_types, ctx.lattices = yield [ data.find_particle_types(), data.find_model_types(), data.find_lattices() ] ctx.model = ObjectDict(model_type=self.model_type) ctx.errors = ObjectDict() raise Return(ctx)
def generate_entries(self): logging.debug('Rendering entries') userinfo = ObjectDict(self.storage.userinfo) self.storage.ensure_dir(self.html_entries_path) for entry_id, entry in self.storage.entries.items(): comments = self.storage.comments[entry_id] comments_tree = CommentsTree(userinfo.username) for comment in sorted(comments, key=lambda x: x['comment_id']): comments_tree.add_comment(comment) # ensure we have all basic entry attributes for attr in ('subject', 'props'): if attr not in entry: entry[attr] = None # tags if 'taglist' in entry['props']: taglist = entry['props']['taglist'].strip() tags = [t.strip() for t in taglist.split(',')] else: tags = None meta = ObjectDict( entry_id=entry_id, comments=entry['reply_count'], image_links=0, music_links=0, document_links=0, title=entry['subject'], tags=tags, year=int(entry['eventtime'][:4]), ) self.entries_meta[entry_id] = meta # remove <lj-cut> entry['event'] = re.sub('</?lj-cut[^>]*>', '', entry['event']) formatted = True if 'opt_preformatted' in entry['props'] and entry[ 'props']['opt_preformatted'] == 1 else False if not formatted: tmp = [] for line in entry['event'].splitlines(False): tmp.append('%s<br>' % line) entry['event'] = '\n'.join(tmp) html = self._render('entry.html', entry=ObjectDict(entry), meta=meta, comments=comments_tree.render(), userinfo=userinfo) self.storage.save_file(self.html_entries_path, '%d.html' % entry['ditemid'], html)
def post(self): ctx = ObjectDict() ctx.search_active = True ctx.search = ObjectDict() ctx.search.model_type = self.get_argument("model_type", None) ctx.search.name = self.get_argument("name", None) ctx.search.properties = self.get_argument("properties", None) #ctx.model_description = self.get_argument("description", None) data = self.application.data ctx.models = yield data.search_models(**ctx.search) self.render("latticemodel/model_search.html", ctx)
def read(self, phone): endpoint = self.selector(phone) if endpoint == self.bemuch.endpoint: if phone in self.phonebook: item = ObjectDict(self.phonebook[phone]) item.phone = phone return item else: raise tornado.web.HTTPError(404) else: item = yield self.bemuch.call(endpoint, 'read', phone) return item
def get_resources_by_status(self): logger.info("\t ==========[ get_resources_by_status ]==========") conditions = and_() or_conditions = or_() or_conditions.append( Pro_Resource_Apply.status == STATUS_RESOURCE.APPLIED) or_conditions.append( Pro_Resource_Apply.status == STATUS_RESOURCE.CHECKED) or_conditions.append( Pro_Resource_Apply.status == STATUS_RESOURCE.PAYED) or_conditions.append( Pro_Resource_Apply.status == STATUS_RESOURCE.CONFIRMPAYED) or_conditions.append( Pro_Resource_Apply.status == STATUS_RESOURCE.STARTED) or_conditions.append( Pro_Resource_Apply.status == STATUS_RESOURCE.CLOSED) conditions.append(or_conditions) res_status = self.params.get("res_status") if res_status: conditions.append(Pro_Resource_Apply.status == res_status) env_id = self.params.get("env_id", 0) if env_id: conditions.append(Pro_Info.env_id == env_id) logger.info("\t [res_status]: %s" % res_status) resource_list = self.db.query(Pro_Resource_Apply).outerjoin( Pro_Info, Pro_Info.id == Pro_Resource_Apply.pro_id).filter( conditions).order_by( Pro_Resource_Apply.create_time.desc()).all() # 按状态查询申请数量 status_counts = self.db.query( Pro_Resource_Apply.status, func.count(Pro_Resource_Apply.id)).filter(or_conditions).group_by( Pro_Resource_Apply.status).all() status_counts = dict(status_counts) logger.info("status_counts: %s" % status_counts) # 按环境查询申请数量 env_counts = self.db.query( Pro_Info.env_id, func.count(Pro_Resource_Apply.id)).outerjoin( Pro_Resource_Apply, Pro_Resource_Apply.pro_id == Pro_Info.id).outerjoin( Env_Info, Env_Info.id == Pro_Info.env_id).filter( or_conditions).group_by(Env_Info.id).all() env_counts = dict(env_counts) logger.info("env_counts: %s" % env_counts) data = ObjectDict() data.resource_list = resource_list data.status_counts = status_counts data.env_counts = env_counts return self.success(data=data)
def filter_list(self): conditions = and_() group_conditions = and_() # if "pro_info.view" in self.handler.current_user.get_current_perms(): if not self.handler.current_user.imchecker: conditions.append(Pro_Info.user_id == self.handler.current_user.id) group_conditions.append(Pro_Info.user_id == self.handler.current_user.id) env = self.params.get("env") status = self.params.get("status") if env: conditions.append(Pro_Info.env_id == env) if status: conditions.append(Pro_Resource_Apply.status == status) projects = self.db.query( Pro_Info ).outerjoin( Pro_Resource_Apply, Pro_Info.last_apply_id == Pro_Resource_Apply.id ).filter( conditions ).order_by( Pro_Info.id.desc() ).all() # project_list = [i.as_dict() for i in projects] # logger.info("project_list %s" % project_list) # self.db.commit() # self.db.remove() projects_by_env = self.db.query( Env_Info.id, Env_Info.name, func.count(Pro_Info.id) ).outerjoin( Pro_Info, Env_Info.id == Pro_Info.env_id ).filter( group_conditions ).group_by( Env_Info.id ).all() logger.info(projects_by_env) projects_by_status = self.db.query( Pro_Resource_Apply.status, func.count(Pro_Info.id) ).outerjoin( Pro_Info, Pro_Resource_Apply.id == Pro_Info.last_apply_id ).filter( group_conditions ).group_by( Pro_Resource_Apply.status ).all() logger.info(projects_by_status) data = ObjectDict() data.projects = projects data.projects_by_env = projects_by_env data.projects_by_status = projects_by_status return self.success(data=data)
def run_cmd(self, payload): host = payload['host'] request = ObjectDict(host=host) request.track = ObjectDict(host=host) uri = urlparse(payload['cmd']) cmd = uri.path if cmd == 'delete/module': result = delete_module(request, parse_qs(uri.query)['name']) log.debug('result: {0}'.format(result)) else: raise HTTPServerError(title="command '{0}' is not supported".format( cmd)) log.info('background processed command: {0}'.format(payload['cmd']))
def run_cmd(self, payload): host = payload['host'] request = ObjectDict(host=host) request.track = ObjectDict(host=host) uri = urlparse(payload['cmd']) cmd = uri.path if cmd == 'delete/module': result = delete_module(request, parse_qs(uri.query)['name']) log.debug('result: {0}'.format(result)) else: raise HTTPServerError( title="command '{0}' is not supported".format(cmd)) log.info('background processed command: {0}'.format(payload['cmd']))
def respond(data, msg=None): parser = feedparser.parse(options.feed_url) articles = [] i = 0 for entry in parser.entries: if i > 9: break article = ObjectDict() article.title = entry.title article.description = entry.description[0:100] article.url = entry.link article.picurl = '' articles.append(article) i += 1 return articles
def post(self): ctx = ObjectDict() ctx.search_active = True ctx.search = ObjectDict() ctx.search.lattice_type = self.get_argument("lattice_type", None) ctx.search.particle_type = self.get_argument("particle_type", None) ctx.search.name = self.get_argument("name", None) ctx.search.branch = self.get_argument("branch", None) ctx.search.version = self.get_argument("version", None) ctx.search.properties = self.get_argument("properties", None) #ctx.lattice_description = self.get_argument("name", None) data = self.application.data ctx.lattices = yield data.search_lattices(**ctx.search) ctx.particle_types = yield data.find_particle_types() self.render("latticemodel/lattice_search.html", **ctx)
def get_current_user(self): sid = self.get_secure_cookie(COOKIE_NAME) if not sid: return None #这个因为在 log_function 中, 所以比 init 还要早, 数据库的连接可能出问题 try: session = self.session.query(Session).filter_by(id=sid).first() except: self.session.rollback() session = self.session.query(Session).filter_by(id=sid).first() if not session: return None #if self.request.remote_ip != session.ip: # return None if session.user is None: return None user = self.session.query(User).filter_by(id=session.user).first() if not user: return None p = user.dict() p['sid'] = sid return ObjectDict(p)
def get_json_arguments(self, raise_error=True): try: return ObjectDict(json_decode(self.request.body)) except Exception, e: print traceback.format_exc() if raise_error: raise JsonDecodeError()
def run_job(self): """lagou job页面的信息任务""" for doc_dict in self.from_col.find({ '_id': { '$gte': self.last_id } }).sort('_id', 1): if 'job' in doc_dict['url']: # job url doc = ObjectDict(doc_dict) assert doc.url and doc.html if LagouCrawler.is_deleted_html(doc.html, False): self.from_col.delete_one({'url': doc.url}) continue job_parser = LagouHtmlParser(doc.url, doc.html) data_dict = job_parser.parse_job() if data_dict is None: self.from_col.delete_one({'url': doc.url}) continue self.logger.info('handle url: %s %s:%s', doc.url, data_dict['source'], data_dict['job']) if not DEBUG: self.to_col.update({ '_id': doc._id, }, {'$set': data_dict}, upsert=True) self.set_id(doc._id)
def get_cites_from_lagou(): """ 爬取拉勾城市数据 返回的 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
def __init__(self, handler): self['__dict'] = ObjectDict(handler=handler) if not hasattr(SessionManager, 'db'): raise SetupError('Please call SessionManager.setup() first.') self._setup_session_id()
def todict(self): return ObjectDict( id=self.id, merchant_id=self.merchant_id, hotel_id=self.hotel_id, roomtype_id=self.roomtype_id, base_hotel_id=self.base_hotel_id, base_roomtype_id=self.base_roomtype_id, name=self.name, pay_type=self.pay_type, stay_days=self.stay_days, ahead_days=self.ahead_days, cancel_type=self.cancel_type, cancel_days=self.cancel_days, cancel_time=self.cancel_time, punish_type=self.punish_type, punish_value=self.punish_value, guarantee_start_time=self.guarantee_start_time, guarantee_type=self.guarantee_type, guarantee_count=self.guarantee_count, start_date=self.start_date, end_date=self.end_date, is_online=self.is_online, is_delete=self.is_delete, )
def crawl_lagou_cites(): """ 获取拉勾上所有城市的信息 :return [ { 'id': 111, 'name': '广州' }, ..... ] """ 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
def __init__(self): settings = ObjectDict() settings.debug = options.debug settings.autoescape = None self.base_dir = os.path.abspath(os.path.dirname(__file__)) settings.template_path = os.path.join(self.base_dir, 'templates') handlers = [ (r'/', IndexHandler), (r'/api/torrent/link', TorrentLinkHandler), (r'/api/torrent/file', TorrentFileHandler), (r'/api/torrent/bttiantang', TorrentBTTianTangHandler), ] super(Application, self).__init__(handlers, **settings)
def on_message(self, message): """Handshake with livereload.js 1. client send 'hello' 2. server reply 'hello' 3. client send 'info' http://feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol """ 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: # print '- Client connected for url {0}'.format(message.url) self.url = message.url LiveReloadHandler.waiters.add(self) if hasattr(LiveReloadHandler, 'update_status'): LiveReloadHandler.update_status()
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()
class ThreadlocalLikeRequestContext(object): """ 通过request_context.data,可以在任意位置访问当前请求的request对象, 以达到类似多线程wsgi程序中全局threadlocal request的目的。 eg:current_request = request_context.request """ _state = threading.local() _state.data = ObjectDict({}) class __metaclass__(type): @property def data(cls): if not hasattr(cls._state, 'data'): return ObjectDict({}) return ObjectDict(cls._state.data) @property def request(cls): return cls.data.get('request') def __init__(self, **data): self._data = data def __enter__(self): self._prev_data = self.__class__.data self.__class__._state.data = self._data def __exit__(self, *exc): self.__class__._state.data = self._prev_data del self._prev_data return False
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 = {} handshake['command'] = 'hello' handshake['protocols'] = [ 'http://livereload.com/protocols/official-7', ] handshake['serverName'] = 'livereload-tornado' self.send_message(handshake) if message.command == 'info' and 'url' in message: logging.info('Browser Connected: %s' % message.url) LiveReloadHandler.waiters.add(self) if not LiveReloadHandler._last_reload_time: if not self.watcher._tasks: logging.info('Watch current working directory') self.watcher.watch(os.getcwd()) LiveReloadHandler._last_reload_time = time.time() logging.info('Start watching changes') if not self.watcher.start(self.poll_tasks): ioloop.PeriodicCallback(self.poll_tasks, 800).start()
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
def to_dict(self): """ 只有当class 有 __init__ 方法的时候, instance的self.__dict__ 才不为空 """ #_ = ObjectDict() #for name, value in self.__dict__.iteritems(): # if not name.startswith('_'): # if isinstance(value, datetime.datetime): # value = str(value) # _[name] = value #return _ _ = ObjectDict() for name in dir(self): if not name.startswith('_'): value = self.__getattribute__(name) # TODO 需要将所有的输入输出都要转化为同一个字符类型 #print(name, value, type(value)) if isinstance(value, (unicode, str, int, long, dict)): _[name] = value if isinstance(value, datetime.datetime): _[name] = str(value) if isinstance(value, decimal.Decimal): _[name] = str(value) elif isinstance(value, ModelMixin): _[name] = value.to_dict() return _
def execute_shell(self, command): ''' 异步执行外部shell命令 @gen.coroutine def digest(self, message): result = yield self.execute_shell('sleep 1;echo hello') print result.returncode print result.output ''' process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) pipe = process.stdout shell_result = ObjectDict() def handle_events(shell_result, io_loop, fd, events): ''' ioloop异步回调 ''' io_loop.remove_handler(fd) output = ''.join(pipe) process.wait() shell_result['returncode'] = process.returncode shell_result['output'] = output event.set() event = toro.Event() self.io_loop.add_handler( pipe.fileno(), partial(handle_events, shell_result, self.io_loop), self.io_loop.READ) yield event.wait() raise gen.Return(shell_result)
def on_message(self, message): """Handshake with livereload.js 1. client send 'hello' 2. server reply 'hello' 3. client send 'info' http://help.livereload.com/kb/ecosystem/livereload-protocol """ message = ObjectDict(escape.json_decode(message)) if message.command == 'hello': handshake = {} handshake['command'] = 'hello' protocols = message.protocols protocols.append( 'http://livereload.com/protocols/2.x-remote-control') handshake['protocols'] = protocols handshake['serverName'] = 'livereload-tornado' self.send_message(handshake) if message.command == 'info' and 'url' in message: logging.info('Browser Connected: %s' % message.url) LiveReloadHandler.waiters.add(self) if not LiveReloadHandler._last_reload_time: if os.path.exists('Guardfile'): logging.info('Reading Guardfile') execfile('Guardfile', {}) else: logging.info('No Guardfile') Task.add(os.getcwd()) LiveReloadHandler._last_reload_time = time.time() logging.info('Start watching changes') if not Task.start(self.watch_tasks): ioloop.PeriodicCallback(self.poll_tasks, 800).start()
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()
def get_stations(self): regions = yield self.get_regions() for region in regions: for q in self.const.BAIDU_POI_Q: for page_num in range(0, 40): baidu_poi_list = yield self.infra_ps.get_city_poi( q, region.pname, region.rname, page_num, coord_type=2) for item in baidu_poi_list.results: if not item.get("location"): continue data_list = yield self.infra_ps.get_xian_nearby( item.get("location", {}).get("lng", ""), item.get("location", {}).get("lat", "")) if data_list.status == 0 and data_list.data: for item in data_list.data: station = ObjectDict() station['code'] = str(item.get('id')) station['status'] = self.const.STATUS_INUSE # station['type'] = "" station['total'] = int(item['lock']) station['name'] = item['name'] station['address'] = item['addr'] # station['district'] = "" station['longitude'] = item["longitude"] station['latitude'] = item["latitude"] # station['service_time'] = "" # station['is_24'] = "" # station['is_duty'] = "" print(station) yield self.update_station(station) x = int(random.random() * 10) yield self.async_sleep(x) raise gen.Return(True)
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
def _find_user(self, account): rs = self.db.query("SELECT * FROM users WHERE account=%s", account) if not rs: return None r = rs[0] d = copy.deepcopy(r) d = ObjectDict(d) d._id = d.id del d['id'] del d['auth_time'] del d['ins_time'] d.user_no = str(d.user_no) d.token = self._gen_token(d._id, d.user_no) if d.ext_data: d.ext_data = json.loads(d.ext_data) d['class'] = 'user' # TODO class是python关键字,不建议用对象属性的方法赋值 return d
def get_pm25(self): res = yield http_get( "http://www.pm25.in/api/querys/all_cities.json?token=dSL6xvEWvyYxTeAX4Wyy", timeout=60) Obj_dict = ObjectDict() for item in res: city = item['area'] if not isinstance(Obj_dict.get(city, None), list): Obj_dict[city] = list() Obj_dict[city].append(item) else: Obj_dict[city].append(item) if Obj_dict: self.hztrip.set_pm25_session(Obj_dict)
def update_nested_dict(a, b): for k, v in b.items(): if isinstance(v, dict): d = a.setdefault(k, ObjectDict()) update_nested_dict(d, v) else: a[k] = v return a
def get(self, hid, iid): http = tornado.httpclient.AsyncHTTPClient() host = self.db.query("SELECT * FROM host WHERE id= %s", int(hid))[0] response = yield http.fetch("http://%s:%d/images/%s/json" % (host.hostname, host.port, iid)) if response.error: raise tornado.web.HTTPError(500) obj = json.loads(response.body) self.render('image/detail.html', image=ObjectDict(obj), hid=hid)
def _on_github_request(future, response): """ Parse the JSON from the API """ if response.error: print response.error future.set_exception( AuthError("Error response {0!s} fetching {1!s}".format(response.error, response.request.url))) return result = ObjectDict(code=response.code, headers=response.headers, body=None) try: result.body = json_decode(response.body) except Exception: gen_log.warning("Invalid JSON from Github: %r", response.body) future.set_result(result) return future.set_result(result)
def __init__(self): settings = ObjectDict() settings.debug = options.debug settings.autoescape = None self.base_dir = os.path.abspath(os.path.dirname(__file__)) settings.template_path = os.path.join(self.base_dir, 'templates') settings.static_path = os.path.join(self.base_dir, 'static') handlers = [ (r'/', IndexHandler), (r'/upload', IndexHandler), (r'/myfile', MyFileHandler), (r'/myfile/list', ListRemoteFileHandler), (r'/myfile/delete', DeleteRemoteFileHandler), ] super(Application, self).__init__(handlers, **settings)
def parse_user_msg(xml): if not xml: return None parser = ElementTree.fromstring(xml) msg_type = decode(parser.find('MsgType').text) touser = decode(parser.find('ToUserName').text) fromuser = decode(parser.find('FromUserName').text) create_at = int(parser.find('CreateTime').text) msg = ObjectDict( type=msg_type, touser=touser, fromuser=fromuser, time=create_at ) if msg_type == MSG_TYPE_TEXT: msg.content = decode(parser.find('Content').text) elif msg_type == MSG_TYPE_LOCATION: msg.location_x = decode(parser.find('Location_X').text) msg.location_y = decode(parser.find('Location_Y').text) msg.scale = int(parser.find('Scale').text) msg.label = decode(parser.find('Label').text) elif msg_type == MSG_TYPE_IMAGE: msg.picurl = decode(parser.find('PicUrl').text) return msg
def torrent2magnet(torrent, rich=False): metadata = bencode.bdecode(torrent) hashcontents = bencode.bencode(metadata['info']) digest = hashlib.sha1(hashcontents).digest() b32hash = base64.b32encode(digest) magnet = 'magnet:?xt=urn:btih:{}'.format(b32hash) if rich: params = ObjectDict() params.xt = 'urn:btih:{}'.format(b32hash) params.dn = metadata['info']['name'] params.tr = metadata['announce'] if 'length' in metadata['info']: params.xl = metadata['info']['length'] paramstr = urllib.urlencode(params) magnet = 'magnet:?{}'.format(paramstr) return magnet, metadata['info']
def respond(data, msg=None, bot=None, handler=None): myfus = handler.get_user_follows(msg.fromuser) post_query = handler.db.query(CoePost).filter( CoePost.username.in_(myfus) ).order_by(CoePost.created.desc()).limit(9) articles = [] for post in post_query: if post.is_ignore == 1: continue article = ObjectDict() article.title = post.topic article.description = rtitle(post.content,79) article.url = "%s/mps/post/%s?otoken=%s" % (handler.settings['server_base'], post.post_id,handler.encrypt_otoken(bot.id)) article.picurl = handler.get_1img_from_content(post.content) or handler.settings['mps_default_bg'] articles.append(article) return articles
def post(self): data = self.application.data model_ids = self.get_arguments("model") if len(model_ids) < 2: self.send_error(400, message="Must select two models for comparison") return model1, model2 = yield [ data.find_model_by_id(model_ids[0]), data.find_model_by_id(model_ids[1]) ] if not model1: self.send_error(400, message="Model (1) not found: " + model_ids[0]) return if not model2: self.send_error(400, message="Model (2) not found: " + model_ids[1]) return lattice1, lattice2, model_elements1, model_elements2 = yield [ data.find_lattice_by_id(model1.lattice_id), data.find_lattice_by_id(model2.lattice_id), data.find_model_elements_by_model_id(model1._id), data.find_model_elements_by_model_id(model2._id) ] ctx = ObjectDict() ctx.model = (model1, model2) ctx.lattice = (lattice1, lattice2) n1 = len(model_elements1) n2 = len(model_elements2) ctx.model_elements = [] for idx in range(max(n1, n2)): if idx < n1 and idx < n2: ctx.model_elements.append((model_elements1[idx], model_elements2[idx])) elif idx < n1: ctx.model_elements.append((model_elements1[idx], None)) elif idx < n2: ctx.model_elements.append((None, model_elements2[idx])) self.render("latticemodel/model_compare.html", **ctx)
def respond(data, msg=None, bot=None, handler=None): nodes = handler.db.query(CoeNode).filter( CoeNode.is_top == 1).order_by(CoeNode.created.asc()).limit(9) articles = [] for node in nodes: if node.is_hide == 1: continue article = ObjectDict() article.title = node.node_desc article.description = rtitle(node.node_intro,79) article.url = "%s/mps/post/new/%s?otoken=%s" % ( handler.settings['server_base'], node.node_name, handler.encrypt_otoken(bot.id)) article.picurl = '' articles.append(article) if len(articles)>1: articles[0].picurl = handler.settings['mps_default_bg'] return articles
def load_config(filename='config.py'): catsup_path = os.path.dirname(__file__) config = ObjectDict( catsup_path=catsup_path, posts_path=os.path.join(catsup_path, '_posts'), common_template_path=os.path.join(catsup_path, 'template'), deploy_path=os.path.join(catsup_path, 'deploy'), disqus_shortname='catsup', feed='feed.xml', post_per_page=3, gzip=True, static_url='static', theme_name='sealscript', google_analytics='' ) execfile(filename, {}, config) if 'theme_path' not in config: config.theme_path = os.path.join(catsup_path, 'themes', config.theme_name) if 'template_path' not in config: config.template_path = os.path.join(config.theme_path, 'template') if 'static_path' not in config: config.static_path = os.path.join(config.theme_path, 'static') if config.site_url.endswith('/'): config.site_url = config.site_url[:-1] if config.static_url.endswith('/'): config.static_url = config.static_url[:-1] return config
def get(self, model_id): ctx = ObjectDict() data = self.application.data ctx.model, ctx.model_elements = yield [ data.find_model_by_id(model_id), data.find_model_elements_by_model_id(model_id) ] #data.find_lattice_elements_by_lattice_id(lattice_id) if not ctx.model: self.render_error(404) return ctx.lattice, lattice_elements = yield [ data.find_lattice_by_id(ctx.model.lattice_id), data.find_lattice_elements_by_lattice_id(ctx.model.lattice_id) ] ctx.lattice_elements = {} for lattice_element in lattice_elements: ctx.lattice_elements[lattice_element._id] = lattice_element self.render("latticemodel/model_details.html", ctx)
class A(object): __slots__ = ['_data', '_dirty'] def __init__(self): self._dirty = False self._data = ObjectDict({'a':10}) def __getitem__(self, name): return self._data.get(name, None) def __setitem__(self, name, value): self._data[name] = value self._dirty = True def __delitem__(self, name): del self._data[name] self._dirty = True def __contains__(self, name): return name in self._data def __len__(self): return len(self._data) def __getattr__(self, name): print('getattr %s' % name) return getattr(self._data, name) def __setattr__(self, name, value): print('__setattr__ %s '%name) if name in self.__slots__: print('setattr slots %s'%name) super(A, self).__setattr__(name, value) else: print('setattr data %'%name) self._dirty = True setattr(self._data, name, value) def __delattr__(self, name): print('__delattr__') delattr(self._data, name) self._dirty = True def __iter__(self): for key in self._data: yield key def __repr__(self): return str(self._data)
def respond(data, msg=None, bot=None): headers = { 'Host' : 'www.oschina.net', 'Connection' : 'Keep-Alive', 'User-Agent' : 'OSChina.NET/1.7.4_1/Android/4.1/Nexus S/12345678' } res = requests.get("http://www.oschina.net/action/api/news_list", headers=headers) parser = ElementTree.fromstring(res.content) news_list = parser.find('newslist') articles = [] i = 0 for news in news_list.iter('news'): if i > 9: break article = ObjectDict() article.title = news.find('title').text article.description = article.title article.url = "http://www.oschina.net/news/%s" % news.find('id').text article.picurl = '' articles.append(article) i += 1 return articles
def __init__(self, config_path): cfg = ObjectDict(read_config(config_path)) cfg.db_name = cfg['mongo.db'] cfg['num_processes'] = int(cfg.get('num_processes', 0)) cfg['stubo_version'] = version cfg['debug'] = asbool(cfg.get('debug', False)) max_workers = int(cfg.get('max_workers', 100)) log.info('started with {0} worker threads'.format(max_workers)) cfg['executor'] = ThreadPoolExecutor(max_workers) try: cfg['statsd_client'] = StatsClient(host=cfg.get('statsd.host', 'localhost'), prefix=cfg.get('statsd.prefix', 'stubo')) cfg['stats'] = StatsdStats() log.info('statsd host addr={0}, prefix={1}'.format( cfg['statsd_client']._addr, cfg['statsd_client']._prefix)) except socket.gaierror, e: log.warn("unable to connect to statsd: {0}".format(e))
def prepare(self): function = self.request.path if function.startswith("/stubo/api/"): function = function.partition("/stubo/api/")[-1] elif function == "/stubo/default/execCmds": # LEGACY function = "exec/cmds" args = self.skip_dotted_names(self.request.arguments) headers = self.skip_dotted_names(self.request.headers) self.track = ObjectDict(request_params=args, request_headers=headers, request_method=self.request.method) host_parts = self.request.host.partition(":") host = host_parts[0].lower() port = host_parts[-1] cache = Cache(host) track_setting = cache.get_stubo_setting("tracking_level") if not track_setting: track_setting = cache.get_stubo_setting("tracking_level", all_hosts=True) if not track_setting: track_setting = "normal" self.track.tracking_level = args.get("tracking_level", track_setting) request_size = len(self.request.body) # always track put/stub recordings if self.track.tracking_level == "full" or function == "put/stub": self.track.request_text = self.request.body if function == "get/response" and request_size <= 0: self.track.error = "NoTextInBody" self.track.update( dict( function=function, start_time=tsecs_to_date(self.request._start_time), host=host, port=port, remote_ip=self.request.remote_ip, server=socket.getfqdn(), request_size=request_size, ) ) log.debug("tracking: {0}:{1}".format(self.request.host, function))