1# Copyright (C) 2001-2019 Artifex Software, Inc.
2# All Rights Reserved.
3#
4# This software is provided AS-IS with no warranty, either express or
5# implied.
6#
7# This software is distributed under license and may not be copied,
8# modified or distributed except as expressly authorized under the terms
9# of the license contained in the file LICENSE in this distribution.
10#
11# Refer to licensing information at http://www.artifex.com or contact
12# Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13# CA 94945, U.S.A., +1(415)492-9861, for further information.
14#
15
16
17# rasterdb.py
18#
19# methods to put and retrieve files to and from the raster database
20
21import os, gzip
22from stat import *
23import gsconf
24
25def exists(file, dbdir=gsconf.rasterdbdir):
26    x = 0
27    filename=dbdir + file + '.gz'
28    try:
29        mode = os.stat(dbdir + file + '.gz')[ST_MODE]
30        if S_ISREG(mode):
31            x = 1
32    except:
33        pass
34
35    return x
36
37def get_file(file, dbdir=gsconf.rasterdbdir, output=None):
38    if exists(file, dbdir):
39        if output:
40            ofile = output
41        else:
42            ofile = file
43        zf = gzip.open(dbdir + file + '.gz')
44        f = open(ofile, 'w')
45        data = zf.read(1024)
46        while data:
47            f.write(data)
48            data = zf.read(1024)
49        zf.close()
50        f.close()
51    else:
52        print "rasterdb.get_file: does not exist",file
53
54def put_file(file, dbdir=gsconf.rasterdbdir):
55    mode = os.stat(file)[ST_MODE]
56    if S_ISREG(mode):
57        f = open(file)
58        zf = gzip.open(dbdir + file + '.gz', 'w')
59        data = f.read(1024)
60        while data:
61            zf.write(data)
62            data = f.read(1024)
63        f.close()
64        zf.close()
65
66