Skip to content

Commit

Permalink
add debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
you-n-g committed Jul 7, 2020
1 parent 26b3924 commit 4394844
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,30 @@ This package is under development. We will release it soon in the future.
# Installation

[fzf](https://github.com/junegunn/fzf) is required

## config

Please config your [notifiers](https://github.com/liiight/notifiers).
`wan` will read the setting in ` ~/.dotfiles/.notifers.yaml` as the arguments for notifiers.

Here is a config example of telegram
```yaml
provider: telegram
kwargs:
chat_id: <Your Chat id from `@myidbot` by sending `/getid`>
token: <Your token from `@BotFather` by sending `/newbot`>
```
Other configs:
```yaml
log_level: DEBUG # the default level is INFO
```
# Usage
## Use in python code
```python
<Your code which takes a lot of time>
from wan import ntf; ntf('Finished')
```
48 changes: 35 additions & 13 deletions wan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .utils import get_pid_via_fzf
import psutil
import time
import sys


class Notifier:
Expand Down Expand Up @@ -34,39 +35,54 @@ def __init__(self, config_path: str = '~/.dotfiles/.notifers.yaml'):
else:
with path.open() as f:
self.config = yaml.load(f, Loader=yaml.FullLoader)
logger.remove()
log_level = self.config.get('log_level', 'INFO')
logger.add(sys.stderr, level=log_level)
logger.debug(f"log level: {log_level}")
self._provider = get_notifier(self.config['provider'])
kwargs = self.config['kwargs']
self._ntf = partial(self._provider.notify, **kwargs)

def ntf(self, message):
logger.debug("Sending message: {}".format(message))
self._ntf(message=message)

def wait(self, pid=None, message=None, idle=False, patience=20):
@staticmethod
def _get_process_info(pid):
process_info = ":"
try:
p = psutil.Process(pid)
except psutil.NoSuchProcess:
process_info = ""
else:
process_info = ":" + ' '.join(p.cmdline())
return process_info

def wait(self, pid=None, message=None, idle=False, patience=20, sleep=3):
"""wait.
wati the proces to stop or idle
Parameters
----------
pid :
pid
message :
message
idle :
will it notify me if the process become idle
patience :
How many idle status is ignored before reguard the process as stopped
sleep :
sleep
"""
logger.debug(f"Idle: {idle}")

process_info = ":"
if pid is None:
pid = get_pid_via_fzf()
if pid is None:
logger.info('No process selected')
return

try:
p = psutil.Process(pid)
except psutil.NoSuchProcess:
process_info = ""
else:
process_info = ":" + ' '.join(p.cmdline())
process_info = self._get_process_info(pid)

logger.info(f'Process[{pid}{process_info}] selected')

Expand All @@ -75,24 +91,30 @@ def wait(self, pid=None, message=None, idle=False, patience=20):
try:
p = psutil.Process(pid)
except psutil.NoSuchProcess:
logger.debug(f'The process[PID] has ended')
logger.info(f'The process[PID] has ended')
break
else:
# TODO: get the information of subprocess
# TODO: get the information of subprocesses
p_status = p.status()
logger.debug(f'status: {p_status}, patience: {cp}')
if idle and p_status not in {psutil.STATUS_RUNNING, psutil.STATUS_DISK_SLEEP}:
cp += 1
if cp > patience:
logger.debug(f'The process is not running, status: {p_status}')
logger.info(f'The process is not running, status: {p_status}')
break
else:
cp = 0
time.sleep(2)
time.sleep(sleep)
if message is None:
message = f'The Process[{pid}{process_info}] has stopped or become idle now.'
self.ntf(message)


def ntf(message):
# notify with the call stack
Notifier().ntf(message)


def run():
fire.Fire(Notifier)

Expand Down

0 comments on commit 4394844

Please sign in to comment.