forked from binux/pyspider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_module.py
68 lines (59 loc) · 2.13 KB
/
project_module.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: Binux<[email protected]>
# http://binux.me
# Created on 2014-02-16 22:24:20
import os
import sys
import logging
import inspect
import linecache
from libs import base_handler
from libs.log import SaveLogHandler
from libs.utils import hide_me
class ObjectDict(dict):
def __getattr__(self, name):
return self.__getitem__(name)
class ProjectModule(object):
def __init__(self, name, script, env={}):
self.name = name
self.name_fixed = name.replace('.', '_')
self.script = script
self.env = env
self.error = None
self.exc_info = None
self._log_buffer = []
self._logger = logging.Logger(self.name_fixed)
self._logger.addHandler(SaveLogHandler(self._log_buffer))
self._build_module()
def _build_module(self):
self._module = ObjectDict()
self._module.__dict__ = {
'logging': self._logger,
'logger': self._logger,
'__env__': self.env,
'__name__': self.name_fixed,
'__loader__': ObjectDict(get_source=lambda name: self.script),
}
try:
exec compile(self.script, self.name_fixed+'.py', 'exec') in self._module.__dict__
linecache.clearcache()
except Exception, e:
self.exc_info = sys.exc_info()
self.error = e
#logging.exception(e)
def rethrow(self):
if self.exc_info:
type, value, tb = self.exc_info
raise type, value, tb
def get(self, key='__class__', default=None):
if key is '__class__' and '__class__' not in self._module.__dict__:
for each in self._module.__dict__.values():
if inspect.isclass(each) and each is not base_handler.BaseHandler \
and issubclass(each, base_handler.BaseHandler):
self._module.__dict__['__class__'] = each
return self._module.__dict__.get(key, default)
@property
def logs(self):
return self._log_buffer