settings.py 23.5 KB
Newer Older
1 2 3 4 5 6 7
# -*- coding: utf-8 -*-
#
# This file is part of the bliss project
#
# Copyright (c) 2016 Beamline Control Unit, ESRF
# Distributed under the GNU LGPLv3. See LICENSE for more info.

8
from .conductor import client
9
from bliss.common.utils import Null
10
from bliss import setup_globals
Matias Guijarro's avatar
Matias Guijarro committed
11 12 13
import weakref
import pickle

Matias Guijarro's avatar
Matias Guijarro committed
14 15 16 17 18
class InvalidValue(Null):
    def __str__(self):
        raise ValueError
    def __repr__(self):
        return '#ERR'
19

Matias Guijarro's avatar
Matias Guijarro committed
20
def get_cache():
21
    return client.get_cache(db=0)
Matias Guijarro's avatar
Matias Guijarro committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

def boolify(s,**keys):
    if s == 'True' or s == 'true':
            return True
    if s == 'False' or s == 'false':
            return False
    raise ValueError('Not Boolean Value!')

def auto_conversion(var):
    '''guesses the str representation of the variables type'''
    if var is None:
        return None
    for caster in (boolify,int, float):
        try:
            return caster(var)
37
        except (ValueError,TypeError):
Matias Guijarro's avatar
Matias Guijarro committed
38 39 40
            pass
    return var

41
def pickle_loads(var):
Matias Guijarro's avatar
Matias Guijarro committed
42 43
    if var is None:
        return None
Matias Guijarro's avatar
Matias Guijarro committed
44 45 46 47
    try:
        return pickle.loads(var)
    except Exception:
        return InvalidValue()
Matias Guijarro's avatar
Matias Guijarro committed
48 49 50 51 52 53 54 55 56

def ttl_func(cnx,name,value = -1):
    if value is None:
        return cnx.persist(name)
    elif value is -1:
        return cnx.ttl(name)
    else:
        return cnx.expire(name,value)

57 58
def read_decorator(func):
    def _read(self,*args,**keys):
Matias Guijarro's avatar
Matias Guijarro committed
59 60
        value = func(self,*args,**keys)
        if self._read_type_conversion:
61
            if isinstance(value,list):
Matias Guijarro's avatar
Matias Guijarro committed
62
                value = [self._read_type_conversion(x) for x in value]
63
            elif isinstance(value,dict):
Matias Guijarro's avatar
Matias Guijarro committed
64 65
                for k,v in value.iteritems():
                    value[k] = self._read_type_conversion(v)
66 67 68 69
                if hasattr(self,'default_values') and isinstance(self.default_values,dict):
                    tmp = dict(self._default_values)
                    tmp.update(value)
                    value = tmp
Matias Guijarro's avatar
Matias Guijarro committed
70 71
            else:
                value = self._read_type_conversion(value)
72 73 74 75 76 77
        if value is None:
            if hasattr(self,'_default_value'):
                value = self._default_value
            elif(hasattr(self,'_default_values') and 
                 hasattr(self._default_values,'get')):
                value = self._default_values.get(args[0])
Matias Guijarro's avatar
Matias Guijarro committed
78 79 80
        return value
    return _read

81 82
def write_decorator_dict(func):
    def _write(self,values,**keys):
Matias Guijarro's avatar
Matias Guijarro committed
83 84 85 86 87 88 89 90 91 92
        if self._write_type_conversion:
            if not isinstance(values,dict) and values is not None:
                raise TypeError('can only be dict')

            if values is not None:
                for k,v in values.iteritems():
                    values[k] = self._write_type_conversion(v)
        return func(self,values,**keys)
    return _write

93
def write_decorator_multiple(func):
Matias Guijarro's avatar
Matias Guijarro committed
94 95 96 97 98 99 100 101 102
    def _write(self,values,**keys):
        if self._write_type_conversion:
            if not isinstance(values,(list,tuple)) and values is not None:
                raise TypeError('can only be tuple or list')
            if values is not None:
                values = [self._write_type_conversion(x) for x in values]
        return func(self,values,**keys)
    return _write

103
def write_decorator(func):
Matias Guijarro's avatar
Matias Guijarro committed
104 105 106 107 108 109
    def _write(self,value,**keys):
        if self._write_type_conversion and value is not None:
            value = self._write_type_conversion(value)
        return func(self,value,**keys)
    return _write

110 111 112 113 114 115 116 117 118 119 120 121
def scan(match='*',count=1000,connection=None):
    if connection is None:
        connection = get_cache()
    cursor = 0
    while 1:
        cursor,values = connection.scan(cursor=cursor,
                                        match=match,count=count)
        for val in values:
            yield val
        if int(cursor) == 0:
            break
        
122
class SimpleSetting(object):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
123 124
    def __init__(self,name,connection = None,
                 read_type_conversion = auto_conversion,
125
                 write_type_conversion = None,
126
                 default_value = None):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
127 128 129 130 131 132
        if connection is None:
            connection = get_cache()
        self._cnx = weakref.ref(connection)
        self._name = name
        self._read_type_conversion = read_type_conversion
        self._write_type_conversion = write_type_conversion
133
        self._default_value = default_value
Matias Guijarro's avatar
Matias Guijarro committed
134

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
135
    @read_decorator
136
    def get(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
137 138 139 140 141
        cnx = self._cnx()
        value = cnx.get(self._name)
        return value

    @write_decorator
142
    def set(self,value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
143 144 145 146 147 148
        cnx = self._cnx()
        cnx.set(self._name,value)

    def ttl(self,value = -1):
        return ttl_func(self._cnx(),self._name,value)

149
    def __add__(self,other):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
150
        value = self.get()
151
        if isinstance(other,SimpleSetting):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
152 153 154 155 156 157
            other = other.get()
        return value + other

    def __iadd__(self,other):
        cnx = self._cnx()
        if cnx is not None:
158
            if isinstance(other,int):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
159 160
                if other == 1:
                    cnx.incr(self._name)
Matias Guijarro's avatar
Matias Guijarro committed
161
                else:
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
162
                    cnx.incrby(self._name,other)
163
            elif isinstance(other,float):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
164 165 166 167
                cnx.incrbyfloat(self._name,other)
            else:
                cnx.append(self._name,other)
            return self
Matias Guijarro's avatar
Matias Guijarro committed
168

169
    def __isub__(self,other):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
170 171 172
        if isinstance(other,basestring):
            raise TypeError("unsupported operand type(s) for -=: %s" % type(other).__name__)
        return self.__iadd__(-other)
Matias Guijarro's avatar
Matias Guijarro committed
173

174
    def __getitem__(self,ran):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        cnx = self._cnx()
        if cnx is not None:
            step = None
            if isinstance(ran,slice):
                i,j = ran.start,ran.stop
                step = ran.step
            elif isinstance(ran,int):
                i = j = ran
            else:
                raise TypeError('indices must be integers')

            value = cnx.getrange(self._name,i,j)
            if step is not None:
                value = value[0:-1:step]
            return value


    def __repr__(self):
        cnx = self._cnx()
        value = cnx.get(self._name)
195
        return '<SimpleSetting name=%s value=%s>' % (self._name,value)
Matias Guijarro's avatar
Matias Guijarro committed
196

197
class SimpleSettingProp(object):
Matias Guijarro's avatar
Matias Guijarro committed
198 199 200
    def __init__(self,name,connection = None,
                 read_type_conversion = auto_conversion,
                 write_type_conversion = None,
201
                 default_value = None,
202
                 use_object_name = True):
Matias Guijarro's avatar
Matias Guijarro committed
203 204 205 206
        self._name = name
        self._cnx = connection or get_cache()
        self._read_type_conversion = read_type_conversion
        self._write_type_conversion = write_type_conversion
207
        self._default_value = default_value
Matias Guijarro's avatar
Matias Guijarro committed
208 209
        self._use_object_name = use_object_name

210
    def __get__(self,obj,type = None):
Matias Guijarro's avatar
Matias Guijarro committed
211 212 213 214
        if self._use_object_name:
            name = obj.name + ':' + self._name
        else:
            name = self._name
215 216 217 218
        return SimpleSetting(name,self._cnx,
                             self._read_type_conversion,
                             self._write_type_conversion,
                             self._default_value)
Matias Guijarro's avatar
Matias Guijarro committed
219

220
    def __set__(self,obj,value):
221
        if isinstance(value,SimpleSetting): return
Matias Guijarro's avatar
Matias Guijarro committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235

        if self._use_object_name:
            name = obj.name + ':' + self._name
        else:
            name = self._name

        if value is None:
            self._cnx.delete(name)
        else:
            if self._write_type_conversion:
                value = self._write_type_conversion(value)
            self._cnx.set(name,value)

class QueueSetting(object):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
236 237
    def __init__(self,name,connection = None,
                 read_type_conversion = auto_conversion,
238
                 write_type_conversion = None):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
239 240 241 242 243
        if connection is None: connection = get_cache()
        self._cnx = weakref.ref(connection)
        self._name = name
        self._read_type_conversion = read_type_conversion
        self._write_type_conversion = write_type_conversion
Matias Guijarro's avatar
Matias Guijarro committed
244

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
245
    @read_decorator
246
    def get(self,first=0,last=-1):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
247 248 249 250
        cnx = self._cnx()
        if first == last:
            l = cnx.lindex(self._name,first)
        else:
251 252
            if last != -1:
                last -= 1
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
253 254
            l = cnx.lrange(self._name,first,last)
        return l
Matias Guijarro's avatar
Matias Guijarro committed
255

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
256
    @write_decorator
257
    def append(self,value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
258
        cnx = self._cnx()
259
        return cnx.rpush(self._name,value)
Matias Guijarro's avatar
Matias Guijarro committed
260

261 262 263 264
    def clear(self):
        cnx = self._cnx()
        cnx.delete(self._name)

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
265
    @write_decorator
266
    def prepend(self,value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
267
        cnx = self._cnx()
268
        return cnx.lpush(self._name,value)
Matias Guijarro's avatar
Matias Guijarro committed
269

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
270
    @write_decorator_multiple
271
    def extend(self,values):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
272
        cnx = self._cnx()
273
        return cnx.rpush(self._name,*values)
Matias Guijarro's avatar
Matias Guijarro committed
274

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
275
    @write_decorator
276
    def remove(self,value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
277
        cnx = self._cnx()
278
        cnx.lrem(self._name,value)
Matias Guijarro's avatar
Matias Guijarro committed
279

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
280
    @write_decorator_multiple
281
    def set(self,values):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
282 283
        if not isinstance(values,(list,tuple)) and values is not None:
            raise TypeError('can only be tuple or list')
Matias Guijarro's avatar
Matias Guijarro committed
284

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
285 286 287 288
        cnx = self._cnx()
        cnx.delete(self._name)
        if values is not None:
            cnx.rpush(self._name,*values)
Matias Guijarro's avatar
Matias Guijarro committed
289

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
290
    @write_decorator
291
    def set_item(self,value,pos = 0):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
292 293
        cnx = self._cnx()
        cnx.lset(self._name,pos,value)
Matias Guijarro's avatar
Matias Guijarro committed
294

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
295
    @read_decorator
296
    def pop_front(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
297 298 299 300 301 302 303
        cnx = self._cnx()
        value = cnx.lpop(self._name)
        if self._read_type_conversion:
            value = self._read_type_conversion(value)
        return value

    @read_decorator
304
    def pop_back(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
305 306 307 308 309 310 311 312 313
        cnx = self._cnx()
        value = cnx.rpop(self._name)
        if self._read_type_conversion:
            value = self._read_type_conversion(value)
        return value

    def ttl(self,value = -1):
        return ttl_func(self._cnx(),self._name,value)

314
    def __len__(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
315 316 317
        cnx = self._cnx()
        return cnx.llen(self._name)

318
    def __repr__(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
319 320
        cnx = self._cnx()
        value = cnx.lrange(self._name,0,-1)
321
        return '<QueueSetting name=%s value=%s>' % (self._name,value)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
322 323 324 325 326

    def __iadd__(self,other):
        self.extend(other)
        return self

327
    def __getitem__(self,ran):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
328
        if isinstance(ran,slice):
329 330
            i = ran.start is not None and ran.start or 0
            j = ran.stop is not None and ran.stop or -1
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
331 332 333 334
        elif isinstance(ran,int):
            i = j = ran
        else:
            raise TypeError('indices must be integers')
335 336 337 338 339
        value = self.get(first = i,last = j)
        if value is None:
            raise StopIteration
        else:
            return value
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
340

341
    def __iter__(self):
342 343
        cnx = self._cnx()
        lsize = cnx.llen(self._name)
344
        for first in xrange(0,lsize,1024):
345 346 347 348 349
            last = first + 1024
            if last >= lsize: last = -1
            for value in self.get(first,last):
                yield value
        
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
350
    def __setitem__(self,ran,value):
351 352
        if isinstance(ran,slice):
            for i,v in zip(range(ran.start,ran.stop),value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
353 354 355 356 357 358 359 360
                self.set_item(v,pos=i)
        elif isinstance(ran,int):
            self.set_item(value,pos=ran)
        else:
            raise TypeError('indices must be integers')
        return self

class QueueSettingProp(object):
Matias Guijarro's avatar
Matias Guijarro committed
361 362 363
    def __init__(self,name,connection = None,
                 read_type_conversion = auto_conversion,
                 write_type_conversion = None,
364
                 use_object_name = True):
Matias Guijarro's avatar
Matias Guijarro committed
365 366 367 368 369 370
        self._name = name
        self._cnx = connection or get_cache()
        self._read_type_conversion = read_type_conversion
        self._write_type_conversion = write_type_conversion
        self._use_object_name = use_object_name

371
    def __get__(self,obj,type = None):
Matias Guijarro's avatar
Matias Guijarro committed
372 373 374 375 376
        if self._use_object_name:
            name = obj.name + ':' + self._name
        else:
            name = self._name

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
377 378 379
        return QueueSetting(name,self._cnx,
                            self._read_type_conversion,
                            self._write_type_conversion)
Matias Guijarro's avatar
Matias Guijarro committed
380

381
    def __set__(self,obj,values):
382
        if isinstance(values,QueueSetting): return
Matias Guijarro's avatar
Matias Guijarro committed
383 384 385 386 387 388

        if self._use_object_name:
            name = obj.name + ':' + self._name
        else:
            name = self._name

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
389 390 391
        proxy = QueueSetting(name,self._cnx,
                             self._read_type_conversion,
                             self._write_type_conversion)
Matias Guijarro's avatar
Matias Guijarro committed
392 393 394 395 396
        proxy.set(values)

class HashSetting(object):
    def __init__(self,name,connection=None,
                 read_type_conversion=auto_conversion,
397 398
                 write_type_conversion=None,
                 default_values = {}):
Matias Guijarro's avatar
Matias Guijarro committed
399 400 401 402 403 404
        if connection is None:
            connection = get_cache()
        self._cnx = weakref.ref(connection)
        self._name = name
        self._read_type_conversion = read_type_conversion
        self._write_type_conversion = write_type_conversion
405
        self._default_values = default_values
Matias Guijarro's avatar
Matias Guijarro committed
406

407 408
    def __repr__(self):
        value = self.get_all()
Matias Guijarro's avatar
Matias Guijarro committed
409
        return '<HashSetting name=%s value=%s>' % (self._name,value)
410
    
411
    def __delitem__(self,key):
412 413
        cnx = self._cnx()
        cnx.hdel(self._name,key)
Matias Guijarro's avatar
Matias Guijarro committed
414

415
    def __len__(self):
Matias Guijarro's avatar
Matias Guijarro committed
416 417 418 419 420 421
        cnx = self._cnx()
        return cnx.hlen(self._name)

    def ttl(self,value = -1):
        return ttl_func(self._cnx(),self._name,value)

Matias Guijarro's avatar
Matias Guijarro committed
422
    def raw_get(self, key):
Matias Guijarro's avatar
Matias Guijarro committed
423
        cnx = self._cnx()
Matias Guijarro's avatar
Matias Guijarro committed
424 425 426 427 428 429
        return cnx.hget(self._name, key)

    @read_decorator
    def get(self, key, default = None):
        v = self.raw_get(key)
        if v is None:
430 431 432 433
            if self._write_type_conversion:
                v = self._write_type_conversion(default)
            else:
                v = default
Matias Guijarro's avatar
Matias Guijarro committed
434
        return v 
Matias Guijarro's avatar
Matias Guijarro committed
435

Matias Guijarro's avatar
Matias Guijarro committed
436 437 438 439 440 441 442 443 444 445 446 447 448
    def _raw_get_all(self):
        cnx = self._cnx()
        return cnx.hgetall(self._name)
    
    def get_all(self):
        all_dict = dict(self._default_values)
        for k, raw_v in self._raw_get_all().iteritems():
          v = self._read_type_conversion(raw_v)
          if isinstance(v, InvalidValue):
              raise ValueError("%s: Invalid value '%s` (cannot deserialize %r)" % (self._name, k, raw_v))
          all_dict[k] = v
        return all_dict
    
Matias Guijarro's avatar
Matias Guijarro committed
449
    @read_decorator
Matias Guijarro's avatar
Matias Guijarro committed
450
    def pop(self, key, default = Null()):
451 452 453 454 455 456 457 458 459
        cnx = self._cnx().pipeline()
        cnx.hget(self._name,key)
        cnx.hdel(self._name,key)
        (value,worked) = cnx.execute()
        if not worked:
            if isinstance(default,Null):
                raise KeyError(key)
            else:
                value = default
Matias Guijarro's avatar
Matias Guijarro committed
460 461
        return value
    
Matias Guijarro's avatar
Matias Guijarro committed
462
    def remove(self, *keys):
Matias Guijarro's avatar
Matias Guijarro committed
463
        cnx = self._cnx()
Matias Guijarro's avatar
Matias Guijarro committed
464
        cnx.hdel(self._name, *keys)
Matias Guijarro's avatar
Matias Guijarro committed
465

466
    def keys(self):
467
        return list(self.iterkeys())
Matias Guijarro's avatar
Matias Guijarro committed
468

469
    def values(self):
470
        return list(self.itervalues())
Matias Guijarro's avatar
Matias Guijarro committed
471

472
    def clear(self):
Matias Guijarro's avatar
Matias Guijarro committed
473 474 475
        cnx = self._cnx()
        cnx.delete(self._name)

476
    def copy(self):
Matias Guijarro's avatar
Matias Guijarro committed
477 478 479
        return self.get()

    @write_decorator_dict
480
    def set(self,values):
Matias Guijarro's avatar
Matias Guijarro committed
481 482 483 484 485 486
        cnx = self._cnx()
        cnx.delete(self._name)
        if values is not None:
            cnx.hmset(self._name,values)

    @write_decorator_dict
487
    def update(self,values):
Matias Guijarro's avatar
Matias Guijarro committed
488
        cnx = self._cnx()
489 490
        if values:
            cnx.hmset(self._name,values)
Matias Guijarro's avatar
Matias Guijarro committed
491

492
    def items(self):
Matias Guijarro's avatar
Matias Guijarro committed
493 494 495 496
        values = self.get_all()
        return [(k,v) for k,v in values.iteritems()]

    @read_decorator
497
    def fromkeys(self,keys):
Matias Guijarro's avatar
Matias Guijarro committed
498 499 500
        cnx = self._cnx()
        return cnx.hmget(self._name,keys)
    
501
    def has_key(self,key):
Matias Guijarro's avatar
Matias Guijarro committed
502
        cnx = self._cnx()
503
        return cnx.hexists(self._name,key) or self._default_values.has_key(key)
Matias Guijarro's avatar
Matias Guijarro committed
504 505
    
    def iterkeys(self):
506 507 508
        for k,v in self.iteritems():
            yield k

Matias Guijarro's avatar
Matias Guijarro committed
509
    def itervalues(self):
510 511
        for k,v in self.iteritems():
            yield v
Matias Guijarro's avatar
Matias Guijarro committed
512

513
    def iteritems(self):
Matias Guijarro's avatar
Matias Guijarro committed
514 515
        cnx = self._cnx()
        next_id = 0
516
        seen_keys = set()
Matias Guijarro's avatar
Matias Guijarro committed
517 518 519 520 521
        while True:
            next_id,pd = cnx.hscan(self._name,next_id)
            for k,v in pd.iteritems():
                if self._read_type_conversion:
                    v = self._read_type_conversion(v)
522
                seen_keys.add(k)
Matias Guijarro's avatar
Matias Guijarro committed
523
                yield k,v
524
            if not next_id or next_id is '0':
Matias Guijarro's avatar
Matias Guijarro committed
525 526
                break

527 528 529 530
        for k,v in self._default_values.iteritems():
            if k in seen_keys: continue
            yield k,v

Matias Guijarro's avatar
Matias Guijarro committed
531 532 533
    def __getitem__(self,key):
        value = self.get(key)
        if value is None:
534 535
            if not self._default_values.has_key(key):
                raise KeyError(key)
Matias Guijarro's avatar
Matias Guijarro committed
536 537
        return value

538
    def __setitem__(self,key,value):
Matias Guijarro's avatar
Matias Guijarro committed
539
        cnx = self._cnx()
540 541 542
        if value is None:
            cnx.hdel(self._name,key)
            return
Matias Guijarro's avatar
Matias Guijarro committed
543 544 545 546 547 548 549 550
        if self._write_type_conversion:
            value = self._write_type_conversion(value)
        cnx.hset(self._name,key,value)

class HashSettingProp(object):        
    def __init__(self,name,connection = None,
                 read_type_conversion = auto_conversion,
                 write_type_conversion = None,
551
                 default_values = {},
552
                 use_object_name = True):
Matias Guijarro's avatar
Matias Guijarro committed
553 554 555 556
        self._name = name
        self._cnx = connection or get_cache()
        self._read_type_conversion = read_type_conversion
        self._write_type_conversion = write_type_conversion
557
        self._default_values = default_values
Matias Guijarro's avatar
Matias Guijarro committed
558 559 560 561 562 563 564 565 566 567
        self._use_object_name = use_object_name

    def __get__(self,obj,type = None):
        if self._use_object_name:
            name = obj.name + ':' + self._name
        else:
            name = self._name
    
        return HashSetting(name,self._cnx,
                           self._read_type_conversion,
568 569
                           self._write_type_conversion,
                           self._default_values)
Matias Guijarro's avatar
Matias Guijarro committed
570

571
    def __set__(self,obj,values):
Matias Guijarro's avatar
Matias Guijarro committed
572 573 574 575 576
        if self._use_object_name:
            name = obj.name + ':' + self._name
        else:
            name = self._name

577
        if isinstance(values,HashSetting): return
Matias Guijarro's avatar
Matias Guijarro committed
578

579 580 581 582
        proxy = HashSetting(name,self._cnx,
                            self._read_type_conversion,
                            self._write_type_conversion,
                            self._default_values)
Matias Guijarro's avatar
Matias Guijarro committed
583 584
        proxy.set(values)
    
585
    def get_proxy(self):
Matias Guijarro's avatar
Matias Guijarro committed
586 587
        return HashSetting(self._name,self._cnx,
                           self._read_type_conversion,
588 589
                           self._write_type_conversion,
                           self._default_values)
Matias Guijarro's avatar
Matias Guijarro committed
590 591
#helper

592
def _change_to_obj_marshalling(keys):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
593 594 595 596 597
    read_type_conversion = keys.pop('read_type_conversion',pickle_loads)
    write_type_conversion = keys.pop('write_type_conversion',pickle.dumps)
    keys.update({'read_type_conversion':read_type_conversion,
                 'write_type_conversion':write_type_conversion})

598 599 600
class HashObjSetting(HashSetting):
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
601 602
        HashSetting.__init__(self,name,**keys)

603
class HashObjSettingProp(HashSettingProp):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
604
    def __init__(self,name,**keys):
605
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
606 607
        HashSettingProp.__init__(self,name,**keys)

608 609 610
class QueueObjSetting(QueueSetting):
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
611 612 613
        QueueSetting.__init__(self,name,**keys)

class QueueObjSettingProp(QueueSettingProp):
614 615
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
616 617
        QueueSettingProp.__init__(self,name,**keys)

618 619 620
class SimpleObjSetting(SimpleSetting):
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
621 622 623
        SimpleSetting.__init__(self,name,**keys)

class SimpleObjSettingProp(SimpleSettingProp):
624 625
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
626
        SimpleSettingProp.__init__(self,name,**keys)
Matias Guijarro's avatar
Matias Guijarro committed
627

628 629 630
class Struct(object):
    def __init__(self,name,**keys):
        self._proxy = HashSetting(name,**keys)
631

632 633
    def __dir__(self):
        return self._proxy.keys()
634

635 636
    def __repr__(self):
        return "<Struct with attributes: %s>" % self._proxy.keys()
637

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    def __getattribute__(self, name):
        if name.startswith('_'):
            return object.__getattribute__(self,name)
        else:
            return self._proxy.get(name)
            
    def __setattr__(self,name,value):
        if name.startswith('_'):
            return object.__setattr__(self,name,value)
        else:
            self._proxy[name] = value
 
    def __delattr__(self,name):
        if name.startswith('_'):
            return object.__delattr__(self,name)
        else:
            self._proxy.remove(name)
655 656


657
class ParametersType(type):
658 659 660 661 662 663 664 665
    def __call__(cls, *args, **kwargs):
        class_dict = { '__slots__': tuple(cls.SLOTS), 'SLOTS':cls.SLOTS }
        new_cls = type(cls.__name__, (cls,), class_dict)
        return type.__call__(new_cls, *args, **kwargs)

    def __new__(cls, name, bases, attrs):
        attrs['__slots__'] = tuple(attrs['SLOTS'])
        return type.__new__(cls, name, bases, attrs)
Matias Guijarro's avatar
Matias Guijarro committed
666

667

668 669
class ParamDescriptor(object):
    OBJECT_PREFIX = 'object:'
670

671 672 673 674 675
    def __init__(self, proxy, name, value, assign = True):
        self.proxy = proxy 
        self.name = name
        if assign:
            self.assign(value)
676

677 678 679
    def assign(self, value):
        if hasattr(value, 'name') and hasattr(setup_globals, value.name):
            value = '%s%s' % (ParamDescriptor.OBJECT_PREFIX, value.name)
Matias Guijarro's avatar
Matias Guijarro committed
680 681 682 683
        try:
            self.proxy[self.name] = value
        except Exception:
            raise ValueError("%s.%s: cannot set value" % (self.proxy._name, self.name))
684 685 686 687 688 689

    def __get__(self, obj, obj_type):
        value = self.proxy[self.name]
        if isinstance(value, (str,unicode)) and value.startswith(ParamDescriptor.OBJECT_PREFIX):
            value = value[len(ParamDescriptor.OBJECT_PREFIX):]
            return getattr(setup_globals, value)
Matias Guijarro's avatar
Matias Guijarro committed
690
        return value
691

692 693 694 695 696
    def __set__(self, obj, value):
        return self.assign(value)

    def __delete__(self, *args):
        del self.proxy[self.name]
697 698


699 700
class Parameters(object):
    __metaclass__ = ParametersType
701
    DESCRIPTOR = ParamDescriptor
702 703
    SLOTS = ['_proxy','__current_config']

704 705
    def __init__(self, name, **keys):
        self.__current_config = SimpleSetting(name, default_value='default')
706 707 708 709
        hash_name = '%s:%s' % (name, self.__current_config.get())
        self._proxy = HashSetting(hash_name, **keys)
        for key in self._proxy.iterkeys():
            self.add(key)
710 711

    def __dir__(self):
712 713
        return self._proxy.keys() + ['add','remove','switch','configs']
    
714
    def __repr__(self):
715
        rep_str = "Parameters (%s)\n" % self.__current_config.get()
716 717
        d = dict(self._proxy.iteritems())
        max_len = max((len(x) for x in d.keys()))
718
        str_format = '  .%-' + '%ds' % max_len + ' = %r\n'
719
        for key,value in sorted(d.iteritems()):
720 721
            rep_str += str_format % (key,value)
        return rep_str
722 723 724 725 726 727 728 729
    
    def add(self, name, value = None):
        setattr(self.__class__, name, self.DESCRIPTOR(self._proxy, name, value,
                                                      value is not None))

    def remove(self, name):
        self._proxy.remove(name)
        delattr(self.__class__, name)
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

    def switch(self,name):
        for key,value in dict(self.__class__.__dict__).iteritems():
            if isinstance(value, self.DESCRIPTOR):
                delattr(self.__class__,key)
                
        self.__current_config.set(name)

        basename = ":".join(self._proxy._name.split(':')[:-1])
        self._proxy._name = '%s:%s' % (basename, name)
        
        for key in self._proxy.keys():
            self.add(key)
        
    @property
    def configs(self):
        basename = ":".join(self._proxy._name.split(':')[:-1])
        return list((x.split(':')[-1] for x in scan(match='%s:*' % basename)))
Matias Guijarro's avatar
Matias Guijarro committed
748

749 750
if __name__ == "__main__":
    class A(object):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
751 752 753 754 755 756
        x = SimpleSettingProp('counter')
        y = SimpleObjSettingProp('obj')
        q = QueueSettingProp('seb')
        ol = QueueObjSettingProp('seb-list')
        h = HashSettingProp('seb-hash')
        oh = HashObjSettingProp('seb-hash-object')
Matias Guijarro's avatar
Matias Guijarro committed
757 758 759 760 761
        def __init__(self,name):
            self.name = name

    a = A('m0')
    p = Struct('optics:zap:params')