initial
This commit is contained in:
commit
7d4f8da59d
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.idea
|
||||
build/
|
||||
dist/
|
||||
ch1p.egg-info
|
||||
venv/
|
25
LICENSE
Normal file
25
LICENSE
Normal file
@ -0,0 +1,25 @@
|
||||
BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2021, Evgeny Zinoviev
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# ch1p-py
|
||||
|
||||
Personal collection of small but usable code snippets used here and there in various scripts.
|
||||
|
||||
## License
|
||||
|
||||
BSD-2c
|
2
ch1p/__init__.py
Normal file
2
ch1p/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from .functions import telegram_notify
|
||||
from .state import State
|
34
ch1p/functions.py
Normal file
34
ch1p/functions.py
Normal file
@ -0,0 +1,34 @@
|
||||
import os, requests
|
||||
|
||||
from typing import List, Tuple, AnyStr
|
||||
|
||||
|
||||
def _get_vars(params: List[Tuple], kw: dict) -> List[AnyStr]:
|
||||
result = []
|
||||
|
||||
for kw_name, env_name in params:
|
||||
env_name = f'MY_{env_name}'
|
||||
if kw_name in kw:
|
||||
result.append(kw[kw_name])
|
||||
elif env_name in os.environ:
|
||||
result.append(os.environ[env_name])
|
||||
else:
|
||||
raise RuntimeError("missing parameter %s or variable %s" % (kw_name, env_name))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def telegram_notify(text: str, parse_mode: str = 'html', **kwargs):
|
||||
token, chat_id = _get_vars([
|
||||
('chat_id', 'TELEGRAM_NOTIFY_CHAT_ID'),
|
||||
('token', 'TELEGRAM_NOTIFY_TOKEN')
|
||||
], kwargs)
|
||||
|
||||
r = requests.post('https://api.telegram.org/bot%s/sendMessage' % token, data={
|
||||
'chat_id': chat_id,
|
||||
'text': text,
|
||||
'parse_mode': parse_mode
|
||||
})
|
||||
|
||||
if r.status_code != 200:
|
||||
raise RuntimeError("telegram returned %d" % r.status_code)
|
28
ch1p/state.py
Normal file
28
ch1p/state.py
Normal file
@ -0,0 +1,28 @@
|
||||
import os, json
|
||||
|
||||
from functions import _get_vars
|
||||
|
||||
|
||||
class State:
|
||||
def __init__(self, default=None, **kwargs):
|
||||
file, = _get_vars([
|
||||
('file', 'STATE_FILE')
|
||||
], kwargs)
|
||||
|
||||
if default is None:
|
||||
default = {}
|
||||
|
||||
self.file = file
|
||||
self.default = default
|
||||
|
||||
def read(self) -> dict:
|
||||
if not os.path.exists(self.file):
|
||||
self.write(self.default)
|
||||
return self.default
|
||||
|
||||
with open(self.file, 'r') as f:
|
||||
return json.loads(f.read())
|
||||
|
||||
def write(self, state: dict):
|
||||
with open(self.file, 'w') as f:
|
||||
f.write(json.dumps(state))
|
24
setup.cfg
Normal file
24
setup.cfg
Normal file
@ -0,0 +1,24 @@
|
||||
[metadata]
|
||||
name = ch1p
|
||||
version = 0.0.1
|
||||
author = Evgeny Zinoviev
|
||||
author_email = me@ch1p.io
|
||||
description =
|
||||
long_description = file: README.md
|
||||
license = BSD
|
||||
license_file = LICENSE
|
||||
long_description_content_type = text/markdown
|
||||
url = https://github.com/gch1p/ch1p-py.git
|
||||
project_urls =
|
||||
Bug Tracker = https://github.com/gch1p/ch1p-py/issues
|
||||
classifiers =
|
||||
Programming Language :: Python :: 3
|
||||
License :: OSI Approved :: BSD License
|
||||
Operating System :: OS Independent
|
||||
|
||||
[options]
|
||||
packages = ch1p
|
||||
python_requires = >=3.6
|
||||
include_package_data = True
|
||||
install_requires =
|
||||
requests
|
Loading…
x
Reference in New Issue
Block a user