Created
June 1, 2016 15:18
-
-
Save kezabelle/9a09f84eccbd73d07e0f20e48d75f351 to your computer and use it in GitHub Desktop.
piddling around with trying to proxy objects to discover their API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import sys | |
from wrapt import CallableObjectProxy, wrap_object | |
def _dot_lookup(thing, comp, import_path): | |
try: | |
return getattr(thing, comp) | |
except AttributeError: | |
__import__(import_path) | |
return getattr(thing, comp) | |
def _importer(target): | |
components = target.split('.') | |
import_path = components.pop(0) | |
thing = __import__(import_path) | |
results = [] | |
for comp in components: | |
import_path += ".%s" % comp | |
thing = _dot_lookup(thing, comp, import_path) | |
results.append((thing, comp, import_path)) | |
return results | |
_NONE = object() | |
def replace_these(items): | |
for item in items: | |
imported_target = _importer(item) | |
target = imported_target.pop() | |
module = imported_target.pop() | |
# if target[0] is not _NONE: | |
# wrap_object('django.core.management', 'execute_from_command_line', factory=Erm) | |
def _runscript(filename): | |
# The script has to run in __main__ namespace (or imports from | |
# __main__ will break). | |
# | |
# So we clear up the __main__ and set several special variables | |
# (this gets rid of pdb's globals and cleans old variables on restarts). | |
import __main__ | |
import __builtin__ | |
__main__.__dict__.clear() | |
__main__.__dict__.update({"__name__" : "__main__", | |
"__file__" : filename, | |
"__builtins__": __builtin__, | |
}) | |
with open(filename, "rb") as fp: | |
exec(compile(fp.read(), filename, 'exec')) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--file', '-f', nargs='+') | |
known_args = parser.parse_known_args() | |
to_replace = known_args[0].file | |
remaining_args = known_args[1] | |
if remaining_args[0] == '--': | |
del remaining_args[0] | |
filename = remaining_args[0] | |
sys.path[0] = os.path.realpath(os.path.dirname(filename)) | |
sys.argv = remaining_args | |
replace_these(to_replace) | |
_runscript(filename) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import unicode_literals, absolute_import | |
import os | |
import wrapt | |
WRAPT_PATH = wrapt.__path__ | |
SITEPACKAGES = os.path.realpath(os.path.dirname(WRAPT_PATH[0])) | |
def twoddle(key): | |
import traceback, sys | |
result = traceback.extract_stack() | |
internal_frame = result.pop() | |
twoddle_frame = result.pop() | |
result2 = [result.pop()] | |
for filename, lineno, name, line in result2: | |
if filename.startswith(SITEPACKAGES): | |
filename = filename[len(SITEPACKAGES):].lstrip(os.path.sep) | |
print("`%s` by %s via '%s', line %d" % (key, name, filename, lineno)) | |
class showme(wrapt.ObjectProxy): | |
def __getattribute__(self, item): | |
twoddle(item) | |
return super(showme, self).__getattribute__(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment