-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.py
68 lines (53 loc) · 1.42 KB
/
source.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from abc import ABCMeta, abstractmethod
#from work import Work
from misc import dd, trace, lazy, isiterable
class Source:
def __init__(self, obj, index):
self.obj = obj
self.index = index
def __eq__(self, other):
return (
isinstance(other, Source)
and
self.obj == other.obj
and
self.index == other.index
)
@lazy
def __hash__(self):
return hash(self.index)
#TODO make a better hash
def __repr__(self):
return 'Source(%s, %s)' % (str(self.obj), str(self.index))
class HasSource(metaclass=ABCMeta):
@abstractmethod
def without_source(self):
pass
def whats_your(self, source_class):
me = self
while me.__class__ != source_class:
try:
me = me.source.obj
except AttributeError:
return None
return me
@property
def work(self):
import work
return self.whats_your(work.Work)
@property
def line_num(self):
return self.work.line_num
def force_source_from(obj):
try:
return obj.source
except AttributeError:
if type(obj) is str:
return None
elif isiterable(obj):
try:
return force_source_from(obj[0])
except IndexError:
return None
else:
return None