Skip to content

Commit

Permalink
Fix hidden directory issue
Browse files Browse the repository at this point in the history
Contents were being extracted to the a hidden directory. This PR
addresses this issue. Also updated the version number in the readme and
setup.py. Closes #27.
  • Loading branch information
brian-bates committed Oct 21, 2016
1 parent bbd05b5 commit 12987b6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $ kai yourfile
```

## Version
`v0.0.5`
`v0.0.6`

## Installation
```shell
Expand Down
33 changes: 12 additions & 21 deletions kai/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,22 @@ def __init__(self, filename, destination='.'):

@classmethod
def supports(cls, filename):
"""
Returns True if the extractor supports the given file
"""
"""Returns True if the extractor supports the given file"""
for supported_extension in cls.supported_extensions:
if filename.endswith(supported_extension):
return True
return False

def extract(self):
raise NotImplementedError()

@staticmethod
def strip_extension(full_path, extension):
return os.path.basename(full_path[:-len(extension)])

def extract(self):
raise NotImplementedError()


class TarExtractor(Extractor):
supported_extensions = ['tar', 'tar.gz', 'tgz', 'tar.bz2', 'tbz']
supported_extensions = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz']

def extract(self):
mode_map = {
Expand All @@ -72,12 +70,9 @@ def extract(self):
'.tar': 'r:'}
for file_extension, mode in mode_map.items():
if self.filename.endswith(file_extension):
destination = '{}{}'.format(
self.destination,
self.strip_extension(self.filename, file_extension))
with tarfile.open(self.filename, mode) as archive:
archive.extractall(path=destination)
return destination
archive.extractall(path=self.destination)
return self.destination
raise ExtractorError(
'Failed to extract {} as tar file.'.format(self.filename))

Expand All @@ -86,20 +81,16 @@ class ZipExtractor(Extractor):
supported_extensions = ['.zip']

def extract(self):
destination = '{}{}'.format(
self.destination, self.strip_extension(self.filename, '.zip'))
with zipfile.ZipFile(self.filename, 'r') as archive:
archive.extractall(path=destination)
return destination
archive.extractall(path=self.destination)
return self.destination


class RarExtractor(Extractor):
""" Extracting RAR file """
"""Extracting RAR file"""
supported_extensions = ['.rar']

def extract(self):
destination = '{}{}'.format(
self.destination, self.strip_extension(self.filename, '.rar'))
with rarfile.RarFile(self.filename, 'r') as archive:
archive.extractall(path=destination)
return destination
archive.extractall(path=self.destination)
return self.destination
15 changes: 4 additions & 11 deletions kai/kai.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@


def extract(filename, destination='.'):
"""
Attempts to extract the given archive to the given destination
"""
"""Attempts to extract the given archive to the given destination"""
extractor = ExtractorFactory.create(filename, destination)
return extractor.extract()


def parse_arguments():
"""
Create a custom command line parser and return the arguments
"""
"""Create a custom command line parser and return the arguments"""
parser = argparse.ArgumentParser(description="Easily extract anything")
parser.add_argument('filename', help='File to extract')
parser.add_argument('destination', nargs='?', default='.',
Expand All @@ -26,12 +22,9 @@ def parse_arguments():


def main():
"""
Parses command line aruments and extracts a given archive accordingly
"""
"""Parses command line aruments and extracts a given archive"""
args = parse_arguments()
destination = extract(args.filename, args.destination)
print('Contents extracted to {}'.format(destination))
extract(args.filename, args.destination)

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='kai',
version='0.0.5',
version='0.0.6',
description=description,
long_description=description,
author='Brian Bates',
Expand Down
2 changes: 1 addition & 1 deletion tests/features/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ def before_all(context):

def after_all(context):
"""Remove the directory after the tests are complete"""
shutil.rmtree(context.test_dir)
# shutil.rmtree(context.test_dir)
4 changes: 1 addition & 3 deletions tests/features/steps/basic_files_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ def step_invoke_kai(context):

@then(u'the file is extracted into the specified directory')
def step_verify_contents(context):
extracted_files = os.listdir(context.extracted_dir)
for filename in extracted_files:
for filename, expected in context.expected_hash.items():
path = os.path.join(context.extracted_dir, filename)
expected = context.expected_hash[filename]
actual = compute_hash(path)
assert expected == actual, (
'Failure: Extracted files are different:'
Expand Down

0 comments on commit 12987b6

Please sign in to comment.