def __call__(self, handler): # temp hack to handle languages and users during upstream-to-www migration if web.ctx.path.startswith("/l/"): raise web.seeother("/languages/" + web.ctx.path[len("/l/"):]) if web.ctx.path.startswith("/user/"): if not web.ctx.site.get(web.ctx.path): raise web.seeother("/people/" + web.ctx.path[len("/user/"):]) real_path, readable_path = get_readable_path(web.ctx.site, web.ctx.path, self.patterns, encoding=web.ctx.encoding) #@@ web.ctx.path is either quoted or unquoted depends on whether the application is running #@@ using builtin-server or lighttpd. Thats probably a bug in web.py. #@@ take care of that case here till that is fixed. # @@ Also, the redirection must be done only for GET requests. if readable_path != web.ctx.path and readable_path != urllib.quote(web.utf8(web.ctx.path)) and web.ctx.method == "GET": raise web.redirect(web.safeunicode(readable_path) + web.safeunicode(web.ctx.query)) web.ctx.readable_path = readable_path web.ctx.path = real_path web.ctx.fullpath = web.ctx.path + web.ctx.query out = handler() V2_TYPES = ['works', 'books', 'people', 'authors', 'publishers', 'languages', 'account'] if out and any(web.ctx.path.startswith('/%s/' % _type) for _type in V2_TYPES): out.v2 = True return out
def get_readable_path(site, path, patterns, encoding=None): """Returns real_path and readable_path from the given path. The patterns is a list of (path_regex, type, property_name, default_value) tuples. """ def match(path): for pat, type, property, default_title in patterns: m = web.re_compile('^' + pat).match(path) if m: prefix = m.group() extra = web.lstrips(path, prefix) tokens = extra.split("/", 2) # `extra` starts with "/". So first token is always empty. middle = web.listget(tokens, 1, "") suffix = web.listget(tokens, 2, "") if suffix: suffix = "/" + suffix return type, property, default_title, prefix, middle, suffix return None, None, None, None, None, None type, property, default_title, prefix, middle, suffix = match(path) if type is None: path = web.safeunicode(path) return (path, path) if encoding is not None \ or path.endswith(".json") or path.endswith(".yml") or path.endswith(".rdf"): key, ext = os.path.splitext(path) thing = _get_object(site, key) if thing: path = thing.key + ext path = web.safeunicode(path) return (path, path) thing = _get_object(site, prefix) # get_object may handle redirections. if thing: prefix = thing.key if thing and thing.type.key == type: title = thing.get(property) or default_title middle = '/' + h.urlsafe(title.strip()) else: middle = "" prefix = web.safeunicode(prefix) middle = web.safeunicode(middle) suffix = web.safeunicode(suffix) return (prefix + suffix, prefix + middle + suffix)
def cat(*args): buf = "" for i in args: full_path = web.safeunicode(i) if os.path.isfile(full_path): f = file(full_path) buf = "%s%s" % (buf, f.read().strip()) f.close() return web.safeunicode(buf)
def __call__(self, handler): real_path, readable_path = self.get_readable_path(web.ctx.path, encoding=web.ctx.encoding) #@@ web.ctx.path is either quoted or unquoted depends on whether the application is running #@@ using builtin-server or lighttpd. Thats probably a bug in web.py. #@@ take care of that case here till that is fixed. # @@ Also, the redirection must be done only for GET requests. if readable_path != web.ctx.path and readable_path != urllib.quote(web.utf8(web.ctx.path)) and web.ctx.method == "GET": raise web.seeother(web.safeunicode(readable_path) + web.safeunicode(web.ctx.query)) web.ctx.readable_path = readable_path web.ctx.path = real_path web.ctx.fullpath = web.ctx.path + web.ctx.query return handler()
def cat(*args): buf = "" for i in args: full_path = web.safeunicode(i) if os.path.isfile(full_path): f = file(full_path) buf += f.read().strip() f.close() try: buf = web.safeunicode(buf) except UnicodeDecodeError: msg = "expected %s file(s) in unicode, got un-know encoding" % ", ".join(args) sys.stderr.write("\n" + msg + "\n") buf = u"" return buf
def row(r): if isinstance(r, basestring): level = 0 label = "" title = web.safeunicode(r) pagenum = "" elif 'value' in r: level = 0 label = "" title = web.safeunicode(r['value']) pagenum = "" else: level = safeint(r.get('level', '0'), 0) label = r.get('label', '') title = r.get('title', '') pagenum = r.get('pagenum', '') r = web.storage(level=level, label=label, title=title, pagenum=pagenum) return r
def row(r): if isinstance(r, basestring): level = 0 label = "" title = web.safeunicode(r) pagenum = "" elif "value" in r: level = 0 label = "" title = web.safeunicode(r["value"]) pagenum = "" elif isinstance(r, dict): level = safeint(r.get("level", "0"), 0) label = r.get("label", "") title = r.get("title", "") pagenum = r.get("pagenum", "") else: return {} return dict(level=level, label=label, title=title, pagenum=pagenum)
def row(r): if isinstance(r, six.string_types): level = 0 label = "" title = web.safeunicode(r) pagenum = "" elif 'value' in r: level = 0 label = "" title = web.safeunicode(r['value']) pagenum = "" elif isinstance(r, dict): level = safeint(r.get('level', '0'), 0) label = r.get('label', '') title = r.get('title', '') pagenum = r.get('pagenum', '') else: return {} return dict(level=level, label=label, title=title, pagenum=pagenum)
def _format(self, d): if isinstance(d, dict): return dict((k, self._format(v)) for k, v in d.iteritems()) elif isinstance(d, list): return [self._format(v) for v in d] elif isinstance(d, common.Text): return {'type': '/type/text', 'value': web.safeunicode(d)} elif isinstance(d, Thing): return d._dictrepr() elif isinstance(d, datetime.datetime): return {'type': '/type/datetime', 'value': d.isoformat()} else: return d
def __call__(self, handler): # temp hack to handle languages and users during upstream-to-www migration if web.ctx.path.startswith("/l/"): raise web.seeother("/languages/" + web.ctx.path[len("/l/"):]) if web.ctx.path.startswith("/user/"): if not web.ctx.site.get(web.ctx.path): raise web.seeother("/people/" + web.ctx.path[len("/user/"):]) real_path, readable_path = self.get_readable_path(web.ctx.path, encoding=web.ctx.encoding) #@@ web.ctx.path is either quoted or unquoted depends on whether the application is running #@@ using builtin-server or lighttpd. Thats probably a bug in web.py. #@@ take care of that case here till that is fixed. # @@ Also, the redirection must be done only for GET requests. if readable_path != web.ctx.path and readable_path != urllib.quote(web.utf8(web.ctx.path)) and web.ctx.method == "GET": raise web.redirect(web.safeunicode(readable_path) + web.safeunicode(web.ctx.query)) web.ctx.readable_path = readable_path web.ctx.path = real_path web.ctx.fullpath = web.ctx.path + web.ctx.query return handler()
def datify(dtime=None, format=u"%x %H:%M", convert_to_utc=False, lang=None): if dtime is None: return "" if not type(dtime) in (datetime.datetime, datetime.date): dtime = parser.parse(dtime) if convert_to_utc: dtime = dtime.replace(tzinfo=TZ_LOCAL).astimezone(tz=TZ_UTC) return dtime.strftime(format) elif (lang or web.ctx.lang) == "ru": return pytils.dt.ru_strftime( web.safeunicode(format), inflected=True, date=dtime, ) else: return dtime.strftime(format)
def process_data(self, d, type, old_data=None): for k, v in d.items(): if v is None or v == [] or web.safeunicode(v).strip() == '': del d[k] else: if old_data and old_data.get(k) == v: continue p = self.get_property(type, k) if p: d[k] = self.process_value(v, p) else: d[k] = v if type: d['type'] = common.Reference(type.key) return d
def run(cmd): args = shlex.split(cmd) try: p_obj = subprocess.Popen(args, stdout = subprocess.PIPE, shell = True) # resp = p_obj.stdout.read().strip("\n") resp = p_obj.stdout.read() except TypeError: resp = None if not resp: # resp = os.popen(cmd).read().strip().split('\n') resp = os.popen(cmd).read().strip() resp = web.rstrips(resp, "\n") resp = web.safeunicode(resp) return resp
def convertSQLQueryRecords(qr=[]): """Convert SQL record value to avoid incorrect unicode handle in Jinja2. >>> db = web.DB(None, {}) >>> qr = db.query('SELECT * FROM msgs') >>> convertSQLQueryRecords(qr) >>> qr = db.select('msgs') >>> convertSQLQueryRecords(qr) """ rcds = [] for record in qr: for k in record: try: record[k] = web.safeunicode(record.get(k)) except UnicodeDecodeError: record[k] = '<<< DECODE FAILED >>>' rcds += [record] return rcds
def __init__(self): super(Foo, self).__init__() x, y, w, h = 100, 100, 900, 600 self.setGeometry(x, y, w, h) self.source = QtGui.QTextEdit(self) # self.preview = QtWebKit.QWebView(self) self.preview = QtGui.QTextEdit(self) self.preview.setReadOnly(True) self.preview.setFrameShape(QtGui.QFrame.NoFrame) qd = QtGui.QTextDocument() qd.setDefaultStyleSheet(DEFAULT_STYLE) self.preview.setDocument(qd) self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal) self.splitter.addWidget(self.source) self.splitter.addWidget(self.preview) # widget = self.splitter.widget(0) # policy = widget.sizePolicy() # policy.setHorizontalStretch(1) # policy.setVerticalStretch(1) # widget.setSizePolicy(policy) self.hbox = QtGui.QHBoxLayout(self) self.hbox.setContentsMargins(0, 0, 0, 0) self.hbox.setSpacing(0) self.hbox.addWidget(self.splitter) self.setLayout(self.hbox) self.font = QtGui.QFont("Monaco", 12) self.setFont(self.font) self.source.textChanged.connect(self.source_text_changed) self.source.setText(web.safeunicode(buf))
def __init__(self): super(Demo, self).__init__() x, y, w, h = 500, 200, 300, 400 self.setGeometry(x, y, w, h) # support basic rich text text1 = "Hello, <a href='http://www.pyside.org/'>PySide</a>" label1 = QtGui.QLabel(text1, self) x, y = 20, 20 label1.move(x, y) label1.linkActivated.connect(self._label1_linkActivated) label1.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Sunken) label2 = QtGui.QLabel(self) x, y = 20, 55 label2.move(x, y) text2 = u'\u4e2d\u6587' label2.setText(text2) label2.setFrameStyle(QtGui.QFrame.Panel) print label1.text(), type(label1.text()), label1.text() == text1 print label2.text(), type(label2.text()), label2.text() == web.safeunicode(text2)
def get_multi(self, keys): keys = [web.safestr(k) for k in keys] d = self._client.get_multi(keys) return dict((web.safeunicode(k), self.decompress(v)) for k, v in d.items())
def format(text): text = web.safeunicode(text) md = markdown.Markdown(source=text) md.postprocessors.append(wikilinks()) return web.safestr(md.convert())
def _get_input(self): title, msg = 'Get Input Dialog', 'Enter your name:' text, resp = QtGui.QInputDialog.getText(self, title, msg) if resp: self.label.setText(web.safeunicode(text))
def __init__(self, html): six.text_type.__init__(self, web.safeunicode(html))
def __init__(self, html): unicode.__init__(self, web.safeunicode(html))
def parse_boolean(value): return web.safeunicode(value).lower() in ["1", "true"]
def _format(text, safe_mode=False): text = web.safeunicode(text) md = get_markdown(text, safe_mode=safe_mode) html = web.safestr(md.convert()) return html, md.macros
def get_multi(self, keys): keys = [adapter.convert_key(k) for k in keys] keys = [web.safestr(k) for k in keys] d = self._client.get_multi(keys) return dict((web.safeunicode(adapter.unconvert_key(k)), self.decompress(v)) for k, v in d.items())
def get_readable_path(self, path, get_object=None, encoding=None): """ Returns (real_url, readable_url) for the given url. >>> fakes = {} >>> fakes['/b/OL1M'] = web.storage(title='fake', name='fake', type=web.storage(key='/type/edition')) >>> fakes['/a/OL1A'] = web.storage(title='fake', name='fake', type=web.storage(key='/type/author')) >>> def fake_get_object(key): return fakes[key] ... >>> get_readable_path = ReadableUrlProcessor().get_readable_path >>> get_readable_path('/b/OL1M', get_object=fake_get_object) (u'/b/OL1M', u'/b/OL1M/fake') >>> get_readable_path('/b/OL1M/foo', get_object=fake_get_object) (u'/b/OL1M', u'/b/OL1M/fake') >>> get_readable_path('/b/OL1M/fake', get_object=fake_get_object) (u'/b/OL1M', u'/b/OL1M/fake') >>> get_readable_path('/b/OL1M/foo/cover', get_object=fake_get_object) (u'/b/OL1M/cover', u'/b/OL1M/fake/cover') >>> get_readable_path('/a/OL1A/foo/cover', get_object=fake_get_object) (u'/a/OL1A/cover', u'/a/OL1A/fake/cover') When requested for .json nothing should be changed. >>> get_readable_path('/b/OL1M.json') (u'/b/OL1M.json', u'/b/OL1M.json') For deleted pages, the readable_path must be same as the real path. >>> fakes['/a/OL2A'] = web.storage(title='fake', name='fake', type=web.storage(key='/type/delete')) >>> get_readable_path('/a/OL2A', get_object=fake_get_object) (u'/a/OL2A', u'/a/OL2A') >>> get_readable_path('/a/OL2A/foo', get_object=fake_get_object) (u'/a/OL2A', u'/a/OL2A') """ def match(path): for pat, type, property, default_title in self.patterns: if web.re_compile('^' + pat).match(path): return type, property, default_title return None, None, None type, property, default_title = match(path) if type is None: path = web.safeunicode(path) return (path, path) if encoding is not None \ or path.endswith(".json") or path.endswith(".yml") or path.endswith(".rdf"): key, ext = os.path.splitext(path) get_object = get_object or self.get_object thing = get_object(key) if thing: path = thing.key + ext path = web.safeunicode(path) return (path, path) prefix, middle, suffix = self._split(path) get_object = get_object or self.get_object thing = get_object(prefix) # get_object may handle redirections. if thing: prefix = thing.key if thing and thing.type.key == type: title = thing.get(property) or default_title middle = '/' + _safepath(title.strip()) else: middle = "" prefix = web.safeunicode(prefix) middle = web.safeunicode(middle) suffix = web.safeunicode(suffix) return (prefix + suffix, prefix + middle + suffix)