-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsecrets.py
45 lines (35 loc) · 915 Bytes
/
secrets.py
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
from caching import cache
from copy import deepcopy
from functools import wraps
from getpass import getpass as _getpass
from magic import magic
@cache
def secretcls(cls):
def __repr__(self):
return '<{}#{}>'.format(
self.__class__.__name__, id(self))
return type('Secret[{}]'.format(cls.__name__), (cls,),
{'__repr__': __repr__})
@magic
class secret:
def __getitem__(self, key):
return secretcls(key)
def __call__(self, value):
secretcls = self[type(value)]
try:
cp = deepcopy(value)
cp.__class__ = secretcls
return cp
except TypeError:
return secretcls(value)
@wraps(_getpass)
def getpass(*args, **kwargs):
"""
>>> x = getpass()
Password:
>>> x
<Secret[str]#4332858128>
>>> print(x)
hunter2
"""
return secret(_getpass(*args, **kwargs))