Fix for Python 3

This commit is contained in:
Phyks 2014-08-03 23:34:17 +02:00
parent 5bf247205f
commit d059005946
3 changed files with 17 additions and 15 deletions

10
bmc.py
View File

@ -50,11 +50,11 @@ def checkBibtex(filename, bibtex_string):
while check.lower() == 'n': while check.lower() == 'n':
with tempfile.NamedTemporaryFile(suffix=".tmp") as tmpfile: with tempfile.NamedTemporaryFile(suffix=".tmp") as tmpfile:
tmpfile.write(bibtex_string) tmpfile.write(bibtex_string.encode('utf-8'))
tmpfile.flush() tmpfile.flush()
subprocess.call([EDITOR, tmpfile.name]) subprocess.call([EDITOR, tmpfile.name])
tmpfile.seek(0) tmpfile.seek(0)
bibtex = BibTexParser(tmpfile.read()+"\n") bibtex = BibTexParser(tmpfile.read().decode('utf-8')+"\n")
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
try: try:
@ -176,7 +176,7 @@ def addFile(src, filetype, manual, autoconfirm, tag):
bibtex = BibTexParser(bibtex) bibtex = BibTexParser(bibtex)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
if len(bibtex) > 0: if len(bibtex) > 0:
bibtex_name = list(bibtex.keys()[0] bibtex_name = list(bibtex.keys())[0]
bibtex = bibtex[bibtex_name] bibtex = bibtex[bibtex_name]
bibtex_string = tools.parsed2Bibtex(bibtex) bibtex_string = tools.parsed2Bibtex(bibtex)
else: else:
@ -214,7 +214,7 @@ def addFile(src, filetype, manual, autoconfirm, tag):
# Remove first page of IOP papers # Remove first page of IOP papers
try: try:
if 'IOP' in bibtex['publisher'] and bibtex['type'] == 'article': if 'IOP' in bibtex['publisher'] and bibtex['type'] == 'article':
tearpages.main(new_name) tearpages.tearpage(new_name)
except (KeyError, shutil.Error, IOError): except (KeyError, shutil.Error, IOError):
pass pass
@ -288,7 +288,7 @@ def downloadFile(url, filetype, manual, autoconfirm, tag):
print('Download finished') print('Download finished')
tmp = tempfile.NamedTemporaryFile(suffix='.'+contenttype) tmp = tempfile.NamedTemporaryFile(suffix='.'+contenttype)
with open(tmp.name, 'w+') as fh: with open(tmp.name, 'w+', encoding='utf-8') as fh:
fh.write(dl) fh.write(dl)
new_name = addFile(tmp.name, filetype, manual, autoconfirm, tag) new_name = addFile(tmp.name, filetype, manual, autoconfirm, tag)
if new_name is False: if new_name is False:

View File

@ -75,7 +75,7 @@ def download(url):
try: try:
size = int(dict(r.info())['Content-Length'].strip()) size = int(dict(r.info())['Content-Length'].strip())
except KeyError: except KeyError:
size = 1 size = 0
dl = b"" dl = b""
dl_size = 0 dl_size = 0
while True: while True:
@ -83,6 +83,7 @@ def download(url):
if buf: if buf:
dl += buf dl += buf
dl_size += len(buf) dl_size += len(buf)
if size != 0:
done = int(50 * dl_size / size) done = int(50 * dl_size / size)
sys.stdout.write("\r[%s%s]" % ('='*done, ' '*(50-done))) sys.stdout.write("\r[%s%s]" % ('='*done, ' '*(50-done)))
sys.stdout.write(" "+str(int(float(done)/52*100))+"%") sys.stdout.write(" "+str(int(float(done)/52*100))+"%")

View File

@ -16,6 +16,11 @@ import re
import sys import sys
from termios import tcflush, TCIOFLUSH from termios import tcflush, TCIOFLUSH
try:
input = raw_input
except NameError:
pass
_slugify_strip_re = re.compile(r'[^\w\s-]') _slugify_strip_re = re.compile(r'[^\w\s-]')
_slugify_hyphenate_re = re.compile(r'[\s]+') _slugify_hyphenate_re = re.compile(r'[\s]+')
@ -63,11 +68,7 @@ def replaceAll(text, dic):
def rawInput(string): def rawInput(string):
"""Flush stdin and then prompt the user for something""" """Flush stdin and then prompt the user for something"""
tcflush(sys.stdin, TCIOFLUSH) tcflush(sys.stdin, TCIOFLUSH)
try: return input(string)
input = raw_input
except NameError:
pass
return input(string).decode('utf-8')
def warning(*objs): def warning(*objs):