Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4b7db9b

Browse files
committed
Fix#23; get parse.py at least mostly working under Python 3.
This could badly do with refactoring; it's likely with some set ofoptions this will still not work.
1 parent172380c commit4b7db9b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

‎parse.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ def parse():
3434
pass
3535
eliff=='-':
3636
f=sys.stdin
37+
ifsys.version_info[0]>=3:
38+
encoding=None
3739
else:
3840
try:
3941
# Try opening from file system
40-
f=open(f)
41-
exceptIOError:pass
42+
f=open(f,"rb")
43+
exceptIOErrorase:
44+
sys.stderr.write("Unable to open file: %s\n"%e)
45+
sys.exit(1)
4246
exceptIndexError:
4347
sys.stderr.write("No filename provided. Use -h for help\n")
4448
sys.exit(1)
@@ -76,12 +80,16 @@ def parse():
7680
t0=time.time()
7781
document=run(parseMethod,f,encoding)
7882
t1=time.time()
79-
printOutput(p,document,opts)
80-
t2=time.time()
81-
sys.stderr.write("\n\nRun took: %fs (plus %fs to print the output)"%(t1-t0,t2-t1))
83+
ifdocument:
84+
printOutput(p,document,opts)
85+
t2=time.time()
86+
sys.stderr.write("\n\nRun took: %fs (plus %fs to print the output)"%(t1-t0,t2-t1))
87+
else:
88+
sys.stderr.write("\n\nRun took: %fs"%(t1-t0))
8289
else:
8390
document=run(parseMethod,f,encoding)
84-
printOutput(p,document,opts)
91+
ifdocument:
92+
printOutput(p,document,opts)
8593

8694
defrun(parseMethod,f,encoding):
8795
try:
@@ -119,7 +127,11 @@ def printOutput(parser, document, opts):
119127
delkwargs['quote_char']
120128

121129
tokens=treewalkers.getTreeWalker(opts.treebuilder)(document)
122-
fortextinserializer.HTMLSerializer(**kwargs).serialize(tokens,encoding='utf-8'):
130+
ifsys.version_info[0]>=3:
131+
encoding=None
132+
else:
133+
encoding="utf-8"
134+
fortextinserializer.HTMLSerializer(**kwargs).serialize(tokens,encoding=encoding):
123135
sys.stdout.write(text)
124136
ifnottext.endswith('\n'):sys.stdout.write('\n')
125137
ifopts.error:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp