def test_does_not_double_encode(self): x = u"λ" x_e = forcestr(x) assert forcestr(x_e) == x_e x_u = forceunicode(x_e) assert forceunicode(x_u) == x_u
def test_idempotent(self): x = u"a" assert forceunicode(forcestr(x)) == x x = u"λ" assert forceunicode(forcestr(x)) == x assert forceunicode(forcestr(SOURCE1)) == SOURCE1 x = "a" assert forcestr(forceunicode(x)) == x
def ssh_graph_server(sshargs): s1 = socket.socket() s1.bind(('127.0.0.1', socket.INADDR_ANY)) localhost, localport = s1.getsockname() if sshargs[0] != 'LOCAL': remoteport = random.randrange(10000, 20000) # ^^^ and just hope there is no conflict args = [ 'ssh', '-S', 'none', '-C', '-R%d:127.0.0.1:%d' % (remoteport, localport) ] args = args + sshargs + ['python -u -c "exec input()"'] else: remoteport = localport args = ['python', '-u', '-c', 'exec input()'] print(' '.join(args)) p = subprocess.Popen(args, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE) p.stdin.write( forcestr(repr('port=%d\n%s' % (remoteport, REMOTE_SOURCE)) + '\n')) line = p.stdout.readline() assert line == 'OK\n' graphserver.listen_server(None, s1=s1)
def reload(graph_id): page = getpage(graph_id) if save_tmp_file: f = open(save_tmp_file, 'w') f.write(forcestr(page.source)) f.close() messages.extend(page_messages(page, graph_id)) send_graph_messages(io, messages) del messages[:]
def splitline(line, re_word=re.compile( forcestr(r'[^\s"]\S*|["]["]|["].*?[^\\]["]'))): import ast result = [] for word in re_word.findall(line): if word.startswith(b'"'): word = ast.literal_eval(forceunicode(word)) result.append(word) return result
def test_forcestr_should_not_fail(self): garbage = u"\xef\xff\xbb\xbf\xce\xbb\xff\xff" # garbage result = forcestr(garbage) # should not raise
""" Graph file parsing. """ from __future__ import print_function, absolute_import import sys, re import subprocess from dotviewer import msgstruct from dotviewer.strunicode import forcestr, forceunicode re_nonword = re.compile(forcestr(r'([^0-9a-zA-Z_.]+)')) re_plain = re.compile(forcestr(r'graph [-0-9.]+ [-0-9.]+ [-0-9.]+$'), re.MULTILINE) re_digraph = re.compile(forcestr(r'\b(graph|digraph)\b'), re.IGNORECASE) def guess_type(content): # try to see whether it is a directed graph or not, # or already a .plain file # XXX not a perfect heursitic if re_plain.match(content): return 'plain' # already a .plain file # look for the word 'graph' or 'digraph' followed by a '{'. bracepos = None lastfound = '' for match in re_digraph.finditer(content): position = match.start() if bracepos is None: bracepos = content.find(b'{', position)
def page_messages(page, graph_id): from dotviewer import graphparse return graphparse.parse_dot(graph_id, forcestr(page.source), page.links, getattr(page, 'fixedfont', False))