Completed
Branch master (e214b7)
by Philippe
36s
created

FileManager.store_file()   A

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
1
"""Package defining Redis file manager
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
import base64
17
from os import remove
18
import redis
19
20
21
class FileManager(object):
22
    """Redis file manager
23
    """
24
25
    def __init__(self, host="127.0.0.1", port=6379, db=0):
26
        self.server = redis.StrictRedis(host, port, db)
27
        self.hashmap_name = "fman"
28
29
    def retrieve_file(self, filename):
30
        """Retrieve from redis hashmap
31
32
        Args
33
            filename (str): Filename to retrieve
34
        """
35
        b64_hash = self.server.hget(self.hashmap_name, filename)
36
        data = base64.b64decode(b64_hash)
37
38
        with open(filename, 'wb') as zip_file:
39
            zip_file.write(data)
40
41
    def store_file(self, filename):
42
        """Store file to redis hashmap
43
44
        Args
45
            filename (str): Filename to store
46
        """
47
        with open(filename, 'rb') as zip_file:
48
            b64_hash = base64.b64encode(zip_file.read())
49
50
        self.server.hset(self.hashmap_name, filename, b64_hash)
51
        remove(filename)
52
53
    def delete_file(self, filename):
54
        """Delete file from redis hashmap
55
56
        Args
57
            filename (str): Filename to delete
58
        """
59
        self.server.hdel(self.hashmap_name, filename)
60