File: store.py

package info (click to toggle)
python-bumps 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,200 kB
  • sloc: python: 24,517; xml: 493; ansic: 373; makefile: 211; javascript: 99; sh: 94
file content (61 lines) | stat: -rw-r--r-- 1,409 bytes parent folder | download | duplicates (2)
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
import os
import json
import shutil
from tempfile import NamedTemporaryFile

ROOT = "/var/lib/jobqueue/server/%s"


def tempfile():
    create("temp")
    return NamedTemporaryFile(delete=False, dir=path("temp"))


def path(id):
    return ROOT % id


def create(id):
    # print "making %s"%path(id)
    if not os.path.exists(path(id)):
        os.makedirs(path(id))


def destroy(id):
    shutil.rmtree(path(id))


def put(id, key, value):
    value = json.dumps(value)
    datapath = path(id)
    datafile = os.path.join(datapath, "K-%s.json" % (key))
    try:
        open(datafile, "wb").write(value)
    except:
        raise KeyError("Could not store key %s-%s in %s" % (id, key, datafile))


def get(id, key):
    datapath = path(id)
    datafile = os.path.join(datapath, "K-%s.json" % (key))
    try:
        value = open(datafile, "rb").read()
    except:
        raise KeyError("Could not retrieve key %s-%s" % (id, key))
    # if value == "": print "key %s-%s is empty"%(id,key)
    return json.loads(value) if value != "" else None


def contains(id, key):
    datapath = path(id)
    datafile = os.path.join(datapath, "K-%s.json" % (key))
    return os.path.exists(datafile)


def delete(id, key):
    datapath = path(id)
    datafile = os.path.join(datapath, "K-%s.json" % (key))
    try:
        os.unlink(datafile)
    except:
        raise KeyError("Could not delete key %s-%s" % (id, key))