settings.py 18.9 KB
Newer Older
1
from .conductor import client
Matias Guijarro's avatar
Matias Guijarro committed
2 3 4
import weakref
import pickle

5 6 7
class Null(object):
    __slots__ = []

Matias Guijarro's avatar
Matias Guijarro committed
8
def get_cache():
9
    return client.get_cache(db=0)
Matias Guijarro's avatar
Matias Guijarro committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

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)
        except ValueError:
            pass
    return var

29
def pickle_loads(var):
Matias Guijarro's avatar
Matias Guijarro committed
30 31 32 33 34 35 36 37 38 39 40 41
    if var is None:
        return None
    return pickle.loads(var)

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)

42 43
def read_decorator(func):
    def _read(self,*args,**keys):
Matias Guijarro's avatar
Matias Guijarro committed
44 45
        value = func(self,*args,**keys)
        if self._read_type_conversion:
46
            if isinstance(value,list):
Matias Guijarro's avatar
Matias Guijarro committed
47
                value = [self._read_type_conversion(x) for x in value]
48
            elif isinstance(value,dict):
Matias Guijarro's avatar
Matias Guijarro committed
49 50
                for k,v in value.iteritems():
                    value[k] = self._read_type_conversion(v)
51 52 53 54
                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
55 56
            else:
                value = self._read_type_conversion(value)
57 58 59 60 61 62
        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
63 64 65
        return value
    return _read

66 67
def write_decorator_dict(func):
    def _write(self,values,**keys):
Matias Guijarro's avatar
Matias Guijarro committed
68 69 70 71 72 73 74 75 76 77
        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

78
def write_decorator_multiple(func):
Matias Guijarro's avatar
Matias Guijarro committed
79 80 81 82 83 84 85 86 87
    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

88
def write_decorator(func):
Matias Guijarro's avatar
Matias Guijarro committed
89 90 91 92 93 94
    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

95
class SimpleSetting(object):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
96 97
    def __init__(self,name,connection = None,
                 read_type_conversion = auto_conversion,
98
                 write_type_conversion = None,
99
                 default_value = None):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
100 101 102 103 104 105
        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
106
        self._default_value = default_value
Matias Guijarro's avatar
Matias Guijarro committed
107

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
108
    @read_decorator
109
    def get(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
110 111 112 113 114
        cnx = self._cnx()
        value = cnx.get(self._name)
        return value

    @write_decorator
115
    def set(self,value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
116 117 118 119 120 121
        cnx = self._cnx()
        cnx.set(self._name,value)

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

122
    def __add__(self,other):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
123
        value = self.get()
124
        if isinstance(other,SimpleSetting):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
125 126 127 128 129 130
            other = other.get()
        return value + other

    def __iadd__(self,other):
        cnx = self._cnx()
        if cnx is not None:
131
            if isinstance(other,int):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
132 133
                if other == 1:
                    cnx.incr(self._name)
Matias Guijarro's avatar
Matias Guijarro committed
134
                else:
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
135
                    cnx.incrby(self._name,other)
136
            elif isinstance(other,float):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
137 138 139 140
                cnx.incrbyfloat(self._name,other)
            else:
                cnx.append(self._name,other)
            return self
Matias Guijarro's avatar
Matias Guijarro committed
141

142
    def __isub__(self,other):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
143 144 145
        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
146

147
    def __getitem__(self,ran):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        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)
168
        return '<SimpleSetting name=%s value=%s>' % (self._name,value)
Matias Guijarro's avatar
Matias Guijarro committed
169

170
class SimpleSettingProp(object):
Matias Guijarro's avatar
Matias Guijarro committed
171 172 173
    def __init__(self,name,connection = None,
                 read_type_conversion = auto_conversion,
                 write_type_conversion = None,
174
                 default_value = None,
175
                 use_object_name = True):
Matias Guijarro's avatar
Matias Guijarro committed
176 177 178 179
        self._name = name
        self._cnx = connection or get_cache()
        self._read_type_conversion = read_type_conversion
        self._write_type_conversion = write_type_conversion
180
        self._default_value = default_value
Matias Guijarro's avatar
Matias Guijarro committed
181 182
        self._use_object_name = use_object_name

183
    def __get__(self,obj,type = None):
Matias Guijarro's avatar
Matias Guijarro committed
184 185 186 187
        if self._use_object_name:
            name = obj.name + ':' + self._name
        else:
            name = self._name
188 189 190 191
        return SimpleSetting(name,self._cnx,
                             self._read_type_conversion,
                             self._write_type_conversion,
                             self._default_value)
Matias Guijarro's avatar
Matias Guijarro committed
192

193
    def __set__(self,obj,value):
194
        if isinstance(value,SimpleSetting): return
Matias Guijarro's avatar
Matias Guijarro committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208

        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
209 210
    def __init__(self,name,connection = None,
                 read_type_conversion = auto_conversion,
211
                 write_type_conversion = None):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
212 213 214 215 216
        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
217

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
218
    @read_decorator
219
    def get(self,first=0,last=-1):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
220 221 222 223 224 225
        cnx = self._cnx()
        if first == last:
            l = cnx.lindex(self._name,first)
        else:
            l = cnx.lrange(self._name,first,last)
        return l
Matias Guijarro's avatar
Matias Guijarro committed
226

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
227
    @write_decorator
228
    def append(self,value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
229
        cnx = self._cnx()
230
        return cnx.rpush(self._name,value)
Matias Guijarro's avatar
Matias Guijarro committed
231

232 233 234 235
    def clear(self):
        cnx = self._cnx()
        cnx.delete(self._name)

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
236
    @write_decorator
237
    def prepend(self,value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
238
        cnx = self._cnx()
239
        return cnx.lpush(self._name,value)
Matias Guijarro's avatar
Matias Guijarro committed
240

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
241
    @write_decorator_multiple
242
    def extend(self,values):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
243
        cnx = self._cnx()
244
        return cnx.rpush(self._name,*values)
Matias Guijarro's avatar
Matias Guijarro committed
245

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
246
    @write_decorator
247
    def remove(self,value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
248 249
        cnx = self._cnx()
        cnx.lrem(self._name,1,value)
Matias Guijarro's avatar
Matias Guijarro committed
250

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
251
    @write_decorator_multiple
252
    def set(self,values):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
253 254
        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
255

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
256 257 258 259
        cnx = self._cnx()
        cnx.delete(self._name)
        if values is not None:
            cnx.rpush(self._name,*values)
Matias Guijarro's avatar
Matias Guijarro committed
260

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
261
    @write_decorator
262
    def set_item(self,value,pos = 0):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
263 264
        cnx = self._cnx()
        cnx.lset(self._name,pos,value)
Matias Guijarro's avatar
Matias Guijarro committed
265

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
266
    @read_decorator
267
    def pop_front(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
268 269 270 271 272 273 274
        cnx = self._cnx()
        value = cnx.lpop(self._name)
        if self._read_type_conversion:
            value = self._read_type_conversion(value)
        return value

    @read_decorator
275
    def pop_back(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
276 277 278 279 280 281 282 283 284
        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)

285
    def __len__(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
286 287 288
        cnx = self._cnx()
        return cnx.llen(self._name)

289
    def __repr__(self):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
290 291
        cnx = self._cnx()
        value = cnx.lrange(self._name,0,-1)
292
        return '<QueueSetting name=%s value=%s>' % (self._name,value)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
293 294 295 296 297

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

298
    def __getitem__(self,ran):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
299
        if isinstance(ran,slice):
300 301
            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
302 303 304 305
        elif isinstance(ran,int):
            i = j = ran
        else:
            raise TypeError('indices must be integers')
306 307 308 309 310
        value = self.get(first = i,last = j)
        if value is None:
            raise StopIteration
        else:
            return value
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
311

312
    def __iter__(self):
313 314
        cnx = self._cnx()
        lsize = cnx.llen(self._name)
315
        for first in xrange(0,lsize,1024):
316 317 318 319 320
            last = first + 1024
            if last >= lsize: last = -1
            for value in self.get(first,last):
                yield value
        
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
321
    def __setitem__(self,ran,value):
322 323
        if isinstance(ran,slice):
            for i,v in zip(range(ran.start,ran.stop),value):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
324 325 326 327 328 329 330 331
                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
332 333 334
    def __init__(self,name,connection = None,
                 read_type_conversion = auto_conversion,
                 write_type_conversion = None,
335
                 use_object_name = True):
Matias Guijarro's avatar
Matias Guijarro committed
336 337 338 339 340 341
        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

342
    def __get__(self,obj,type = None):
Matias Guijarro's avatar
Matias Guijarro committed
343 344 345 346 347
        if self._use_object_name:
            name = obj.name + ':' + self._name
        else:
            name = self._name

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
348 349 350
        return QueueSetting(name,self._cnx,
                            self._read_type_conversion,
                            self._write_type_conversion)
Matias Guijarro's avatar
Matias Guijarro committed
351

352
    def __set__(self,obj,values):
353
        if isinstance(values,QueueSetting): return
Matias Guijarro's avatar
Matias Guijarro committed
354 355 356 357 358 359

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

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
360 361 362
        proxy = QueueSetting(name,self._cnx,
                             self._read_type_conversion,
                             self._write_type_conversion)
Matias Guijarro's avatar
Matias Guijarro committed
363 364 365 366 367
        proxy.set(values)

class HashSetting(object):
    def __init__(self,name,connection=None,
                 read_type_conversion=auto_conversion,
368 369
                 write_type_conversion=None,
                 default_values = {}):
Matias Guijarro's avatar
Matias Guijarro committed
370 371 372 373 374 375
        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
376
        self._default_values = default_values
Matias Guijarro's avatar
Matias Guijarro committed
377

378 379
    def __repr__(self):
        value = self.get_all()
Matias Guijarro's avatar
Matias Guijarro committed
380
        return '<HashSetting name=%s value=%s>' % (self._name,value)
381
    
382
    def __delitem__(self,key):
383 384
        cnx = self._cnx()
        cnx.hdel(self._name,key)
Matias Guijarro's avatar
Matias Guijarro committed
385

386
    def __len__(self):
Matias Guijarro's avatar
Matias Guijarro committed
387 388 389 390 391 392 393
        cnx = self._cnx()
        return cnx.hlen(self._name)

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

    @read_decorator
394
    def get(self,key,default = None):
Matias Guijarro's avatar
Matias Guijarro committed
395
        cnx = self._cnx()
396 397 398 399
        return_val = cnx.hget(self._name,key)
        if return_val is None:
            return_val = default
        return return_val
Matias Guijarro's avatar
Matias Guijarro committed
400 401

    @read_decorator
402 403 404 405 406 407 408 409 410 411
    def pop(self,key,default = Null()):
        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
412 413
        return value
    
414
    def remove(self,key):
Matias Guijarro's avatar
Matias Guijarro committed
415 416 417 418 419 420 421 422
        cnx = self._cnx()
        cnx.hdel(self._name,key)

    @read_decorator
    def get_all(self):
        cnx = self._cnx()
        return cnx.hgetall(self._name)
    
423
    def keys(self):
424
        return list(self.iterkeys())
Matias Guijarro's avatar
Matias Guijarro committed
425

426
    def values(self):
427
        return list(self.itervalues())
Matias Guijarro's avatar
Matias Guijarro committed
428

429
    def clear(self):
Matias Guijarro's avatar
Matias Guijarro committed
430 431 432
        cnx = self._cnx()
        cnx.delete(self._name)

433
    def copy(self):
Matias Guijarro's avatar
Matias Guijarro committed
434 435 436
        return self.get()

    @write_decorator_dict
437
    def set(self,values):
Matias Guijarro's avatar
Matias Guijarro committed
438 439 440 441 442 443
        cnx = self._cnx()
        cnx.delete(self._name)
        if values is not None:
            cnx.hmset(self._name,values)

    @write_decorator_dict
444
    def update(self,values):
Matias Guijarro's avatar
Matias Guijarro committed
445 446 447
        cnx = self._cnx()
        cnx.hmset(self._name,values)

448
    def items(self):
Matias Guijarro's avatar
Matias Guijarro committed
449 450 451 452
        values = self.get_all()
        return [(k,v) for k,v in values.iteritems()]

    @read_decorator
453
    def fromkeys(self,keys):
Matias Guijarro's avatar
Matias Guijarro committed
454 455 456
        cnx = self._cnx()
        return cnx.hmget(self._name,keys)
    
457
    def has_key(self,key):
Matias Guijarro's avatar
Matias Guijarro committed
458
        cnx = self._cnx()
459
        return cnx.hexists(self._name,key) or self._default_values.has_key(key)
Matias Guijarro's avatar
Matias Guijarro committed
460 461
    
    def iterkeys(self):
462 463 464
        for k,v in self.iteritems():
            yield k

Matias Guijarro's avatar
Matias Guijarro committed
465
    def itervalues(self):
466 467
        for k,v in self.iteritems():
            yield v
Matias Guijarro's avatar
Matias Guijarro committed
468

469
    def iteritems(self):
Matias Guijarro's avatar
Matias Guijarro committed
470 471
        cnx = self._cnx()
        next_id = 0
472
        seen_keys = set()
Matias Guijarro's avatar
Matias Guijarro committed
473 474 475 476 477
        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)
478
                seen_keys.add(k)
Matias Guijarro's avatar
Matias Guijarro committed
479
                yield k,v
480
            if not next_id or next_id is '0':
Matias Guijarro's avatar
Matias Guijarro committed
481 482
                break

483 484 485 486
        for k,v in self._default_values.iteritems():
            if k in seen_keys: continue
            yield k,v

Matias Guijarro's avatar
Matias Guijarro committed
487 488 489 490 491 492
    def __getitem__(self,key):
        value = self.get(key)
        if value is None:
            raise KeyError(key)
        return value

493
    def __setitem__(self,key,value):
Matias Guijarro's avatar
Matias Guijarro committed
494
        cnx = self._cnx()
495 496 497
        if value is None:
            cnx.hdel(self._name,key)
            return
Matias Guijarro's avatar
Matias Guijarro committed
498 499 500 501 502 503 504 505
        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,
506
                 default_values = {},
507
                 use_object_name = True):
Matias Guijarro's avatar
Matias Guijarro committed
508 509 510 511
        self._name = name
        self._cnx = connection or get_cache()
        self._read_type_conversion = read_type_conversion
        self._write_type_conversion = write_type_conversion
512
        self._default_values = default_values
Matias Guijarro's avatar
Matias Guijarro committed
513 514 515 516 517 518 519 520 521 522
        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,
523 524
                           self._write_type_conversion,
                           self._default_values)
Matias Guijarro's avatar
Matias Guijarro committed
525

526
    def __set__(self,obj,values):
Matias Guijarro's avatar
Matias Guijarro committed
527 528 529 530 531
        if self._use_object_name:
            name = obj.name + ':' + self._name
        else:
            name = self._name

532
        if isinstance(values,HashSetting): return
Matias Guijarro's avatar
Matias Guijarro committed
533

534 535 536 537
        proxy = HashSetting(name,self._cnx,
                            self._read_type_conversion,
                            self._write_type_conversion,
                            self._default_values)
Matias Guijarro's avatar
Matias Guijarro committed
538 539
        proxy.set(values)
    
540
    def get_proxy(self):
Matias Guijarro's avatar
Matias Guijarro committed
541 542
        return HashSetting(self._name,self._cnx,
                           self._read_type_conversion,
543 544
                           self._write_type_conversion,
                           self._default_values)
Matias Guijarro's avatar
Matias Guijarro committed
545 546
#helper

547
def _change_to_obj_marshalling(keys):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
548 549 550 551 552
    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})

553 554 555
class HashObjSetting(HashSetting):
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
556 557
        HashSetting.__init__(self,name,**keys)

558
class HashObjSettingProp(HashSettingProp):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
559
    def __init__(self,name,**keys):
560
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
561 562
        HashSettingProp.__init__(self,name,**keys)

563 564 565
class QueueObjSetting(QueueSetting):
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
566 567 568
        QueueSetting.__init__(self,name,**keys)

class QueueObjSettingProp(QueueSettingProp):
569 570
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
571 572
        QueueSettingProp.__init__(self,name,**keys)

573 574 575
class SimpleObjSetting(SimpleSetting):
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
576 577 578
        SimpleSetting.__init__(self,name,**keys)

class SimpleObjSettingProp(SimpleSettingProp):
579 580
    def __init__(self,name,**keys):
        _change_to_obj_marshalling(keys)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
581
        SimpleSettingProp.__init__(self,name,**keys)
Matias Guijarro's avatar
Matias Guijarro committed
582 583

class Struct(object):
584
    def __init__(self,name,**keys):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
585
        self._proxy = HashSetting(name,**keys)
Matias Guijarro's avatar
Matias Guijarro committed
586

587
    def __dir__(self):
Matias Guijarro's avatar
Matias Guijarro committed
588 589
        return self._proxy.keys()

590
    def __repr__(self):
Matias Guijarro's avatar
Matias Guijarro committed
591 592 593 594 595 596 597 598
        return "<Struct with attributes: %s>" % self._proxy.keys()

    def __getattribute__(self, name):
        if name.startswith('_'):
            return object.__getattribute__(self,name)
        else:
            return self._proxy.get(name)
            
599
    def __setattr__(self,name,value):
Matias Guijarro's avatar
Matias Guijarro committed
600 601 602 603 604
        if name.startswith('_'):
            return object.__setattr__(self,name,value)
        else:
            self._proxy[name] = value
 
605
    def __delattr__(self,name):
Matias Guijarro's avatar
Matias Guijarro committed
606 607 608 609 610
        if name.startswith('_'):
            return object.__delattr__(self,name)
        else:
            self._proxy.remove(name)

611 612
if __name__ == "__main__":
    class A(object):
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
613 614 615 616 617 618
        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
619 620 621 622 623
        def __init__(self,name):
            self.name = name

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