Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3c90b9f965
Fetching contributors…

Cannot retrieve contributors at this time

file 91 lines (81 sloc) 2.844 kb
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
from paisley import CouchDB
from pc.secure import couchadmin, couchpassword

import os
import os.path

def pr(failure):
    pass
    # print failure

def getDesign(failure):
    d = None
    if str(failure.value).startswith('404'):
        d = couch.saveDoc('{}', design_id)
        d.addCallback(syncViews)
        d.addErrback(pr)
        return d


couch = CouchDB('127.0.0.1', dbName='pc', username=couchadmin, password=couchpassword)
design_id = "_design/pc"
designID = "pc"


this_file = __file__

def syncViews(designDoc):
    if 'views' not in designDoc:
        designDoc['views'] = {}
    jsondir = os.path.join(os.path.dirname(this_file), 'json')
    files = [f for f in os.listdir(jsondir) if f.endswith('.js')]
    new_design = {'views':{}}
    new_design['_id'] = design_id
    new_design['_rev'] = designDoc['_rev']
    if 'lists' in designDoc:
        new_design['lists'] = designDoc['lists']
    map_files = [f for f in files if 'map' in f.split('.')]
    for f in map_files:
        if '#' in f or '~' in f: continue
        view_name = f.split('/')[-1].split('.')[0]
        map_file = open(os.path.join(os.path.dirname(this_file), 'json', f))
        _map = map_file.read()
        map_file.close()
        _reduce = None
        reduce_file_name = f.replace("map","reduce")
        if os.path.isfile(os.path.join(os.path.dirname(this_file), 'json', reduce_file_name)):
            reduce_file = open(os.path.join(os.path.dirname(this_file), 'json', reduce_file_name))
            _reduce = reduce_file.read()
            reduce_file.close()
        view = {}
        view['map'] = _map
        if _reduce is not None:
            view['reduce'] = _reduce
        new_design['views'][view_name] = view

    def compDicts(di1, di2):
        changed = False
        def getcha(element):
            return True
        try:
            for el in di1:
                # something was deleted
                if el not in di2:
                    changed = getcha(el)
                    break
                for eel in di1[el]:
                    if eel not in di2[el]:
                        changed = getcha(eel)
                        break
                    if di1[el][eel] != di2[el][eel]:
                        changed = getcha((di1[el][eel],di2[el][eel]))
                        break
        except Exception, e:
            pass
        return changed
    res = None
    if compDicts(designDoc['views'], new_design['views']) or compDicts(new_design['views'], designDoc['views']):
        res = couch.saveDoc(new_design)
        def pr(res):
            pass
            # print res
        res.addCallback(pr)
        res.addErrback(pr)
    print "finish syncing"
    return res

def sync():
    d = couch.openDoc(design_id)
    d.addCallback(syncViews)
    d.addErrback(pr)
    return d
Something went wrong with that request. Please try again.