File: store.py

package info (click to toggle)
python-bumps 0.7.11-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,264 kB
  • sloc: python: 22,226; ansic: 4,973; cpp: 4,849; xml: 493; makefile: 163; perl: 108; sh: 101
file content (53 lines) | stat: -rw-r--r-- 1,373 bytes parent folder | download | duplicates (3)
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
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))