Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

"""File archive classes.""" 

from __future__ import absolute_import, print_function, unicode_literals 

 

import magic 

import os 

import shutil 

import sys 

 

from xtract.utils import FileTypeNotSupported, register_xtract, TYPES 

 

 

if sys.version_info[0] < 3: 

IsADirectoryError = IOError 

 

 

def get_file_type(path, types=TYPES): 

"""Get the magic mimetype and return according file extension.""" 

18 ↛ 26line 18 didn't jump to line 26, because the condition on line 18 was never false if os.path.exists(path): 

try: 

mime = magic.from_file(path, mime=True) 

except IsADirectoryError: 

return 

for cls in types: 

if cls().has_mimetype(mime): 

return cls 

for cls in types: 

27 ↛ 28line 27 didn't jump to line 28, because the condition on line 27 was never true if cls().check_extension(path): 

return cls 

 

 

def _xtract(source, destination, overwrite, delete_source, types): 

cls = get_file_type(source, types=types) 

 

if cls is None: 

raise FileTypeNotSupported(source) 

 

return cls().xtract(source, destination=destination, overwrite=overwrite, delete_source=delete_source) 

 

 

def xtract(source, destination=None, overwrite=False, all=False, keep_intermediate=False, types=TYPES): 

final_destination = destination 

first_cycle = True 

while True: 

destination = None 

delete_source = not first_cycle and not keep_intermediate 

overwrite_current = not all and overwrite 

try: 

destination = _xtract(source, destination, overwrite_current, delete_source, types) 

except FileTypeNotSupported as e: 

if first_cycle: 

raise e 

break 

# if not all: 

# raise e 

# if not first_cycle: 

# break 

source = destination 

if not all: 

break 

first_cycle = False 

 

if final_destination: 

if os.path.exists(final_destination): 

64 ↛ 66line 64 didn't jump to line 66, because the condition on line 64 was never false if not overwrite: 

return source 

try: 

shutil.move(source, final_destination) 

except shutil.Error: 

pass 

else: 

source = final_destination 

return source 

 

 

xtract.register = register_xtract