Completed
Branch master (e214b7)
by Philippe
36s
created

src.pipeline.Command   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %
Metric Value
dl 0
loc 52
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Command.store_file() 0 7 1
A Command._unzip_file() 0 8 2
A Command._zip_file() 0 9 2
A Command.get_file() 0 9 1
1
"""Command object
2
3
.. Authors:
4
    Philippe Dessauw
5
    [email protected]
6
7
.. Sponsor:
8
    Alden Dima
9
    [email protected]
10
    Information Systems Group
11
    Software and Systems Division
12
    Information Technology Laboratory
13
    National Institute of Standards and Technology
14
    http://www.nist.gov/itl/ssd/is
15
"""
16
from apputils.fileop import zip_directory, unzip_directory
17
from pipeline.files import FileManager
18
19
20
class Command(object):
21
    """Main command object
22
    """
23
24
    def __init__(self, filename, logger, app_config):
25
        self.logger = logger
26
27
        ip = app_config["machines"]["master"][0].split('@')
28
        master_ip = ip[-1:][0]
29
        master_queue_port = app_config["redis"]["port"]
30
        self.fman = FileManager(master_ip, master_queue_port)
31
32
        self.filename = filename
33
        self.unzipped = None
34
        self.config = app_config
35
36
    def get_file(self):
37
        """Retrieve file from redis and unzip it to the local filesystem
38
        """
39
        # Get hash from redis
40
        self.logger.debug("Retrieving "+self.filename+"...")
41
        self.fman.retrieve_file(self.filename)
42
43
        # Write it in the tmp folder
44
        self._unzip_file()
45
46
    def store_file(self):
47
        """Zip file on the local filesystem and store it to redis
48
        """
49
        self._zip_file()
50
51
        # Store it in redis
52
        self.fman.store_file(self.filename)
53
54
    def _zip_file(self):
55
        """Check if the file can be zipped and zip it
56
        """
57
        if self.unzipped is None:
58
            self.logger.error("Zipped directory has not been unzipped")
59
            return
60
61
        zip_directory(self.unzipped)
62
        self.unzipped = None
63
64
    def _unzip_file(self):
65
        """Check if the file can be unzipped and unzip it
66
        """
67
        if self.unzipped is not None:
68
            self.logger.error("Archive already unzipped")
69
            return
70
71
        self.unzipped = unzip_directory(self.filename)
72