Skip to content

Commit

Permalink
Added support for passing file names as pathlib.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey committed May 13, 2018
1 parent 50b765b commit f59ea9d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
13 changes: 12 additions & 1 deletion rarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,8 @@ def extract(self, member, path=None, pwd=None):
"""
if isinstance(member, RarInfo):
fname = member.filename
elif _have_pathlib and isinstance(member, Path):
fname = str(member)
else:
fname = member
self._extract([fname], path, pwd)
Expand Down Expand Up @@ -898,6 +900,8 @@ def _extract(self, fnlist, path=None, psw=None):

# destination path
if path is not None:
if _have_pathlib and isinstance(path, Path):
path = str(path)
cmd.append(path + os.sep)

# call
Expand Down Expand Up @@ -967,6 +971,8 @@ def getinfo(self, member):
"""
if isinstance(member, RarInfo):
fname = member.filename
elif _have_pathlib and isinstance(member, Path):
fname = str(member)
else:
fname = member

Expand Down Expand Up @@ -2738,7 +2744,12 @@ def _parse_xtime(flag, data, pos, basetime=None):
def is_filelike(obj):
"""Filename or file object?
"""
if isinstance(obj, (bytes, unicode)):
if _have_pathlib:
filename_types = (bytes, unicode, Path)
else:
filename_types = (bytes, unicode)

if isinstance(obj, filename_types):
return False
res = True
for a in ('read', 'tell', 'seek'):
Expand Down
26 changes: 20 additions & 6 deletions test/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""API tests.
"""

import sys
import io
import os
Expand All @@ -9,6 +8,9 @@

import rarfile

if rarfile._have_pathlib:
from pathlib import Path

#
# test start
#
Expand Down Expand Up @@ -49,6 +51,11 @@ def test_open_psw_late_rar5():
rf.open('stest1.txt', 'r', 'password').read()
rf.open('stest1.txt', 'r', u'password').read()

if rarfile._have_pathlib:
def test_open_pathlib_path():
rf = rarfile.RarFile('test/files/rar5-psw.rar')
rf.open(Path('stest1.txt'), 'r', 'password').read()

def test_read_psw_late_rar3():
rf = rarfile.RarFile('test/files/rar3-comment-psw.rar')
rf.read('file1.txt', 'password')
Expand All @@ -64,20 +71,21 @@ def test_open_psw_late():
rf = rarfile.RarFile('test/files/rar5-psw.rar')
rf.read('stest1.txt', 'password222')

def test_open_pathlib_path():
try:
from pathlib import Path
if rarfile._have_pathlib:
def test_create_from_pathlib_path():
# Make sure we can open both relative and absolute Paths
rarfile.RarFile(Path('test/files/rar5-psw.rar'))
rarfile.RarFile(Path('test/files/rar5-psw.rar').resolve())
except ImportError:
pass

def test_detection():
eq_(rarfile.is_rarfile('test/files/ctime4.rar.exp'), False)
eq_(rarfile.is_rarfile('test/files/ctime4.rar'), True)
eq_(rarfile.is_rarfile('test/files/rar5-crc.rar'), True)

if rarfile._have_pathlib:
eq_(rarfile.is_rarfile(Path('test/files/rar5-crc.rar')), True)


@raises(rarfile.BadRarFile)
def test_signature_error():
rarfile.RarFile('test/files/ctime4.rar.exp')
Expand Down Expand Up @@ -178,6 +186,12 @@ def test_extract():
rf.extractall('tmp/extract3', [rf.getinfo('stest2.txt')])
assert_true(os.path.isfile('tmp/extract3/stest2.txt'))

if rarfile._have_pathlib:
os.makedirs('tmp/extract1_pathlib')
rf.extractall(Path('tmp/extract1'))
assert_true(os.path.isfile('tmp/extract1/stest1.txt'))
assert_true(os.path.isfile('tmp/extract1/stest2.txt'))

@with_setup(clean_extract_dirs, clean_extract_dirs)
def test_extract_mem():
os.makedirs('tmp/extract1')
Expand Down

0 comments on commit f59ea9d

Please sign in to comment.