Commit 0cae6ca1 by willmcgugan@gmail.com

Fixes for fs commands in PY3

parent 747c7b9b
......@@ -84,7 +84,7 @@ List contents of [PATH]"""
file_paths = filter(None, [fs.getpathurl(path, allow_none=True) for path in file_paths])
dirs = frozenset(dir_paths)
paths = sorted(dir_paths + file_paths, key=lambda p:p.lower())
paths = sorted(dir_paths + file_paths, key=lambda p: p.lower())
if not options.all:
paths = [path for path in paths if not isdotfile(path)]
......@@ -109,6 +109,7 @@ List contents of [PATH]"""
wrap_filename = self.wrap_filename
wrap_dirname = self.wrap_dirname
def wrap(path):
if path in dirs:
return wrap_dirname(path.ljust(max_width))
......@@ -125,7 +126,6 @@ List contents of [PATH]"""
return padded_columns
def condense_columns(columns):
max_column_height = max([len(col) for col in columns])
lines = [[] for _ in xrange(max_column_height)]
......@@ -147,18 +147,18 @@ List contents of [PATH]"""
smallest_paths = min(path_widths)
num_paths = len(paths)
num_cols = min(terminal_width / (smallest_paths + 2), num_paths)
num_cols = min(terminal_width // (smallest_paths + 2), num_paths)
while num_cols:
col_height = (num_paths + num_cols - 1) / num_cols
col_height = (num_paths + num_cols - 1) // num_cols
line_width = 0
for col_no in xrange(num_cols):
try:
col_width = max(path_widths[col_no*col_height:(col_no + 1) * col_height])
col_width = max(path_widths[col_no * col_height: (col_no + 1) * col_height])
except ValueError:
continue
line_width += col_width
if line_width > terminal_width:
break;
break
line_width += 2
else:
if line_width - 1 <= terminal_width:
......@@ -173,4 +173,3 @@ def run():
if __name__ == "__main__":
sys.exit(run())
\ No newline at end of file
......@@ -8,7 +8,7 @@ from fs.errors import FSError
from fs.path import splitext, pathsplit, isdotfile, iswildcard
import platform
from collections import defaultdict
import re
import six
if platform.system() == 'Windows':
......@@ -76,6 +76,10 @@ class Command(object):
version = ''
def __init__(self, usage='', version=''):
if six.PY3:
self.output_file = sys.stdout.buffer
self.error_file = sys.stderr.buffer
else:
self.output_file = sys.stdout
self.error_file = sys.stderr
self.encoding = getattr(self.output_file, 'encoding', 'utf-8') or 'utf-8'
......@@ -210,11 +214,9 @@ class Command(object):
return raw_input('%s: %s ' % (self.name, msg))
def text_encode(self, text):
if not isinstance(text, unicode):
text = text.decode('ascii', 'replace')
text = text.encode(self.encoding, 'replace')
return text
def output(self, msgs, verbose=False):
......@@ -226,10 +228,8 @@ class Command(object):
self.output_file.write(self.text_encode(msg))
def output_table(self, table, col_process=None, verbose=False):
if verbose and not self.verbose:
return
if col_process is None:
col_process = {}
......@@ -248,7 +248,9 @@ class Command(object):
td = col_process[col_no](td)
out_col.append(td)
lines.append(self.text_encode('%s\n' % ' '.join(out_col).rstrip()))
self.output(''.join(lines))
for l in lines:
self.output_file.write(l)
#self.output(''.join(lines))
def error(self, *msgs):
for msg in msgs:
......@@ -275,7 +277,7 @@ class Command(object):
desc = getattr(fs_opener, 'desc', '')
opener_table.append((names, desc))
opener_table.sort(key = lambda r:r[0])
opener_table.sort(key=lambda r: r[0])
def wrap_line(text):
......@@ -298,14 +300,13 @@ class Command(object):
for names, desc in opener_table:
self.output(('-' * self.terminal_width, '\n'))
proto = ', '.join([n+'://' for n in names])
proto = ', '.join([n + '://' for n in names])
self.output((self.wrap_dirname('[%s]' % proto), '\n\n'))
if not desc.strip():
desc = "No information available"
wrap_line(desc)
self.output('\n')
def run(self):
parser = self.get_optparse()
options, args = parser.parse_args()
......@@ -340,6 +341,7 @@ class Command(object):
opener.add(new_opener)
if not six.PY3:
args = [unicode(arg, sys.getfilesystemencoding()) for arg in args]
self.verbose = options.verbose
try:
......
......@@ -18,15 +18,16 @@ __all__ = ['copyfile',
'find_duplicates',
'print_fs']
import os
import sys
import stat
import six
from fs.mountfs import MountFS
from fs.path import pathjoin
from fs.errors import DestinationExistsError, RemoveRootError
from fs.base import FS
def copyfile(src_fs, src_path, dst_fs, dst_path, overwrite=True, chunk_size=64*1024):
"""Copy a file from one filesystem to another. Will use system copyfile, if both files have a syspath.
Otherwise file will be copied a chunk at a time.
......@@ -491,7 +492,7 @@ def print_fs(fs,
terminal_colors = hasattr(file_out, 'isatty') and file_out.isatty()
def write(line):
file_out.write(line.encode(file_encoding, 'replace')+'\n')
file_out.write(line.encode(file_encoding, 'replace') + b'\n')
def wrap_prefix(prefix):
if not terminal_colors:
......@@ -511,11 +512,7 @@ def print_fs(fs,
def wrap_filename(fname):
if not terminal_colors:
return fname
# if '.' in fname:
# name, ext = os.path.splitext(fname)
# fname = '%s\x1b[36m%s\x1b[0m' % (name, ext)
if fname.startswith('.'):
#fname = '\x1b[2m%s\x1b[0m' % fname
fname = '\x1b[33m%s\x1b[0m' % fname
return fname
dircount = [0]
......
......@@ -15,6 +15,16 @@ changedir=.tox
commands = nosetests fs.tests -v \
[]
[testenv:py31]
commands = nosetests fs.tests -v \
[]
deps = distribute
six
dexml
nose
winpdb
[testenv:py32]
commands = nosetests fs.tests -v \
[]
......@@ -24,3 +34,12 @@ deps = distribute
nose
winpdb
[testenv:py33]
commands = nosetests fs.tests -v \
[]
deps = distribute
six
dexml
nose
winpdb
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment