-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from furkanonder/develop
New updates for version 0.2.0
- Loading branch information
Showing
13 changed files
with
313 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Test | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
pyv: ["3.8", "3.9", "3.10"] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.pyv }} | ||
|
||
- name: Test | ||
run: | | ||
python -m unittest discover -v |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,43 @@ | ||
|
||
repos: | ||
- repo: https://github.com/psf/black | ||
rev: 22.6.0 | ||
hooks: | ||
- id: black | ||
args: [--line-length=79] | ||
|
||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.0.1 | ||
- repo: https://github.com/PyCQA/isort | ||
rev: 5.10.1 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: end-of-file-fixer | ||
- id: check-yaml | ||
- id: check-added-large-files | ||
- id: isort | ||
args: ["--profile", "black", "--filter-files"] | ||
|
||
- repo: https://github.com/psf/black | ||
rev: 22.3.0 | ||
- repo: https://github.com/hakancelikdev/unimport | ||
rev: 0.10.0 | ||
hooks: | ||
- id: black | ||
- id: unimport | ||
args: [--remove, --include-star-import, --ignore-init] | ||
|
||
- repo: https://github.com/PyCQA/isort | ||
rev: 5.9.2 | ||
- repo: https://github.com/PyCQA/docformatter | ||
rev: v1.4 | ||
hooks: | ||
- id: isort | ||
- id: docformatter | ||
args: [--in-place] | ||
|
||
- repo: https://github.com/hakancelikdev/unimport | ||
rev: 0.8.4 | ||
- repo: https://github.com/pre-commit/mirrors-prettier | ||
rev: v2.7.1 | ||
hooks: | ||
- id: unimport | ||
args: [--remove, --include-star-import] | ||
- id: prettier | ||
args: [--prose-wrap=always, --print-width=88] | ||
|
||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.3.0 | ||
hooks: | ||
- id: end-of-file-fixer | ||
files: "\\.(py|.txt|.yaml|.json|.in|.md|.toml|.cfg|.html|.yml)$" | ||
|
||
- repo: https://github.com/asottile/pyupgrade | ||
rev: v2.37.3 | ||
hooks: | ||
- id: pyupgrade | ||
args: [--py36-plus] |
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from objerve.objerve import Hook, watch |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import sys | ||
|
||
BLACK = "\x1b[30m" | ||
RED = "\x1b[31m" | ||
GREEN = "\x1b[32m" | ||
YELLOW = "\x1b[33m" | ||
BLUE = "\x1b[34m" | ||
MAGENTA = "\x1b[35m" | ||
CYAN = "\x1b[36m" | ||
WHITE = "\x1b[37m" | ||
RESET = "\x1b[0m" | ||
|
||
|
||
def init_colors(): | ||
if sys.platform != "win32": | ||
return True | ||
|
||
try: | ||
"""Terminal coloring for Windows is written with Windows Console API | ||
Functions using the following resource. | ||
https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences | ||
""" | ||
|
||
from ctypes import POINTER, WINFUNCTYPE, WinError, windll | ||
from ctypes.wintypes import BOOL, DWORD, HANDLE | ||
|
||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 | ||
STD_OUTPUT_HANDLE = -11 | ||
|
||
def err_check(result, func, args) -> tuple: | ||
"""This function is a helper for the error checking. | ||
It is raises an exception when the API call failed. | ||
""" | ||
if not result: | ||
raise WinError() | ||
return args | ||
|
||
def get_std_handle() -> WINFUNCTYPE: | ||
"""GetStdHandle retrieves a handle to the specified standard device | ||
(standard input, standard output, or standard error).""" | ||
prototype = WINFUNCTYPE(HANDLE, DWORD) | ||
paramflags = ((1, "nStdHandle"),) | ||
function = prototype(("GetStdHandle", windll.kernel32), paramflags) | ||
function.errcheck = err_check | ||
return function | ||
|
||
def get_console_mode() -> WINFUNCTYPE: | ||
"""GetConsoleMode retrieves the current input mode of a console's | ||
input buffer or the current output mode of a console screen | ||
buffer.""" | ||
prototype = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD)) | ||
paramflags = ((1, "hConsoleHandle"), (2, "lpMode")) | ||
function = prototype( | ||
("GetConsoleMode", windll.kernel32), paramflags | ||
) | ||
function.errcheck = err_check | ||
return function | ||
|
||
def set_console_mode() -> WINFUNCTYPE: | ||
"""SetConsoleMode sets the input mode of a console's input buffer | ||
or the output mode of a console screen buffer.""" | ||
prototype = WINFUNCTYPE(BOOL, HANDLE, DWORD) | ||
paramflags = ((1, "hConsoleHandle"), (1, "dwMode")) | ||
function = prototype( | ||
("SetConsoleMode", windll.kernel32), paramflags | ||
) | ||
function.errcheck = err_check | ||
return function | ||
|
||
GetStdHandle = get_std_handle() | ||
GetConsoleMode = get_console_mode() | ||
SetConsoleMode = set_console_mode() | ||
|
||
h_out = GetStdHandle(STD_OUTPUT_HANDLE) | ||
dw_mode = GetConsoleMode(h_out) | ENABLE_VIRTUAL_TERMINAL_PROCESSING | ||
SetConsoleMode(h_out, dw_mode) | ||
except OSError: | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
USE_COLOR = init_colors() | ||
|
||
|
||
def set_color(color: str, text: str) -> str: | ||
if USE_COLOR: | ||
return f"{color}{text}{RESET}" | ||
else: | ||
return text |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import traceback | ||
from collections import defaultdict | ||
|
||
from objerve.color import CYAN, GREEN, YELLOW, set_color | ||
|
||
|
||
class Hook: | ||
def __init__(self, name, hooks, trace_limit): | ||
self.public_name = name | ||
self.private_name = f"_{name}" | ||
self.hooks = hooks | ||
self.trace_limit = trace_limit | ||
|
||
def __set__(self, obj, value): | ||
if "set" in self.hooks: | ||
self.print_stack(CYAN, f"Set | {self.public_name} = {value}") | ||
setattr(obj, self.private_name, value) | ||
|
||
def __get__(self, obj, objtype=None): | ||
if obj is None: | ||
return self | ||
else: | ||
value = getattr(obj, self.private_name) | ||
if "get" in self.hooks: | ||
self.print_stack(GREEN, f"Get | {self.public_name} = {value}") | ||
return value | ||
|
||
def __delete__(self, instance): | ||
if "delete" in self.hooks: | ||
self.print_stack(YELLOW, f"Delete | {self.public_name}") | ||
delattr(instance, self.private_name) | ||
|
||
def print_stack(self, color, msg): | ||
summary, *_ = traceback.extract_stack(limit=self.trace_limit) | ||
print( | ||
set_color( | ||
color, f"{msg}\n{' '.join(traceback.format_list([summary]))}" | ||
) | ||
) | ||
|
||
|
||
def watch(**kwargs): | ||
attrs = defaultdict(list) | ||
trace_limit = kwargs.pop("trace_limit", 3) | ||
|
||
for hook in kwargs: | ||
for attr in kwargs[hook]: | ||
attrs[attr].append(hook) | ||
|
||
def inner(cls): | ||
for attr in attrs: | ||
setattr(cls, attr, Hook(attr, attrs[attr], trace_limit)) | ||
return cls | ||
|
||
return inner |
This file contains 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
Empty file.
Oops, something went wrong.