From 6b509dbce1d42a755b92fa89caa8f3a8d3981c47 Mon Sep 17 00:00:00 2001 From: frennkie Date: Fri, 22 May 2020 22:45:37 +0100 Subject: [PATCH] BlitzPy initial add --- home.admin/BlitzPy/CHANGELOG.md | 11 ++ home.admin/BlitzPy/LICENSE | 21 +++ home.admin/BlitzPy/MANIFEST.in | 1 + home.admin/BlitzPy/Makefile | 20 +++ home.admin/BlitzPy/README.md | 49 ++++++ home.admin/BlitzPy/blitzpy/__init__.py | 8 + home.admin/BlitzPy/blitzpy/config.py | 206 +++++++++++++++++++++++++ home.admin/BlitzPy/blitzpy/version.py | 8 + home.admin/BlitzPy/setup.cfg | 6 + home.admin/BlitzPy/setup.py | 35 +++++ 10 files changed, 365 insertions(+) create mode 100644 home.admin/BlitzPy/CHANGELOG.md create mode 100644 home.admin/BlitzPy/LICENSE create mode 100644 home.admin/BlitzPy/MANIFEST.in create mode 100644 home.admin/BlitzPy/Makefile create mode 100644 home.admin/BlitzPy/README.md create mode 100644 home.admin/BlitzPy/blitzpy/__init__.py create mode 100644 home.admin/BlitzPy/blitzpy/config.py create mode 100644 home.admin/BlitzPy/blitzpy/version.py create mode 100644 home.admin/BlitzPy/setup.cfg create mode 100644 home.admin/BlitzPy/setup.py diff --git a/home.admin/BlitzPy/CHANGELOG.md b/home.admin/BlitzPy/CHANGELOG.md new file mode 100644 index 00000000..bdc54ba5 --- /dev/null +++ b/home.admin/BlitzPy/CHANGELOG.md @@ -0,0 +1,11 @@ +# Changelog + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + + +## [0.1.0] - 2020-05-22 +### Added +- initial creation diff --git a/home.admin/BlitzPy/LICENSE b/home.admin/BlitzPy/LICENSE new file mode 100644 index 00000000..3eaa10fc --- /dev/null +++ b/home.admin/BlitzPy/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2018-2020 The RaspiBlitz developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/home.admin/BlitzPy/MANIFEST.in b/home.admin/BlitzPy/MANIFEST.in new file mode 100644 index 00000000..7b45c0e3 --- /dev/null +++ b/home.admin/BlitzPy/MANIFEST.in @@ -0,0 +1 @@ +include CHANGELOG.md diff --git a/home.admin/BlitzPy/Makefile b/home.admin/BlitzPy/Makefile new file mode 100644 index 00000000..1d477d46 --- /dev/null +++ b/home.admin/BlitzPy/Makefile @@ -0,0 +1,20 @@ +# Makefile + +build: + python setup.py sdist bdist_wheel + +clean: + rm -rf .eggs .tox .coverage .coverage.data .cache build + rm -rf BlitzPy.egg-info + find ./ -iname "*.pyc" -delete + find ./ -type d -iname "__pycache__" -delete + +test: + tox + +upload: + twine upload --skip-existing dist/* -r pypi + +upload-test: + twine upload --skip-existing dist/* -r pypitest + diff --git a/home.admin/BlitzPy/README.md b/home.admin/BlitzPy/README.md new file mode 100644 index 00000000..eec40e69 --- /dev/null +++ b/home.admin/BlitzPy/README.md @@ -0,0 +1,49 @@ +# BlitzPy + +BlitzPy is a part of the RaspiBlitz project and implements a few common use cases. + + +## Installation + +### Prerequisite + +None + +### Dependencies + +None + +### Install BlitzPy + +``` +cd ~/raspiblitz/home.admin/BlitzPy +pip install dist/BlitzPy-0.1.0-py2.py3-none-any.whl +OR +sudo -H python -m pip install dist/BlitzPy-0.1.0-py2.py3-none-any.whl +``` + +**or** consider using a virtual environment + +``` +python3 -m venv --system-site-packages venv +source venv/bin/activate +pip install BlitzPy +``` + +## Installation + +Import and use.. + +``` +from blitzpy import RaspiBlitzConfig +cfg = RaspiBlitzConfig() +cfg.reload() +print(cfg.__dict__) +print(cfg.hostname) +if cfg.run_behind_tor: + print("using TOR!") +``` + +## License + +[MIT License](http://en.wikipedia.org/wiki/MIT_License) diff --git a/home.admin/BlitzPy/blitzpy/__init__.py b/home.admin/BlitzPy/blitzpy/__init__.py new file mode 100644 index 00000000..c217ead1 --- /dev/null +++ b/home.admin/BlitzPy/blitzpy/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- + +from .config import RaspiBlitzConfig, RaspiBlitzInfo + +__all__ = [ + 'RaspiBlitzConfig', + 'RaspiBlitzInfo', +] diff --git a/home.admin/BlitzPy/blitzpy/config.py b/home.admin/BlitzPy/blitzpy/config.py new file mode 100644 index 00000000..f35e381a --- /dev/null +++ b/home.admin/BlitzPy/blitzpy/config.py @@ -0,0 +1,206 @@ +# -*- coding: utf-8 -*- + +import logging +import os +from configparser import ConfigParser, DEFAULTSECT + +log = logging.getLogger(__name__) + + +class LndConfig(object): + def __init__(self, abs_path="/mnt/hdd/lnd/lnd.conf"): + self.abs_path = abs_path + + # default values for LND Configuration + self.rpc_listen = "" + + @property + def rpc_listen_host(self): + return self.rpc_listen.split(":")[0] + + @property + def rpc_listen_port(self): + try: + return int(self.rpc_listen.split(":")[1]) + except (IndexError, TypeError, ValueError): + return 0 + + def reload(self): + """load config from file""" + parser = ConfigParser() + + log.debug("loading config from file: {}".format(self.abs_path)) + with open(self.abs_path) as f: + parser.read_string(f.read()) + + app_options = parser["Application Options"] + + self.rpc_listen = get_str_clean(app_options, "rpclisten", self.rpc_listen) + + +class RaspiBlitzConfig(object): + def __init__(self, abs_path="/mnt/hdd/raspiblitz.conf"): + self.abs_path = abs_path + + # default values for RaspiBlitz Configuration + self.auto_nat_discovery = False + self.auto_pilot = False + self.auto_unlock = False + self.chain = "" + self.dynDomain = "" + self.dyn_update_url = "" + self.hostname = "" + self.invoice_allow_donations = False + self.invoice_default_amount = 402 + self.lcd_rotate = False + self.lnd_address = "" + self.lnd_port = "" + self.network = "" + self.public_ip = "" + self.rtl_web_interface = False + self.run_behind_tor = False + self.ssh_tunnel = "" + self.touchscreen = False + self.version = "" + + def reload(self): + """load config from file""" + parser = ConfigParser() + + log.debug("loading config from file: {}".format(self.abs_path)) + with open(self.abs_path) as f: + parser.read_string("[{}]\n".format(DEFAULTSECT) + f.read()) + + default_s = parser[DEFAULTSECT] + + self.auto_nat_discovery = get_boolean_safe(default_s, "autoNatDiscovery", self.auto_nat_discovery) + self.auto_pilot = get_boolean_safe(default_s, "autoPilot", self.auto_pilot) + self.auto_unlock = get_boolean_safe(default_s, "autoUnlock", self.auto_unlock) + self.chain = get_str_clean(default_s, "chain", self.chain) + self.dynDomain = get_str_clean(default_s, "dynDomain", self.dynDomain) + self.dyn_update_url = get_str_clean(default_s, "dynUpdateUrl", self.dyn_update_url) + self.hostname = get_str_clean(default_s, "hostname", self.hostname) + self.invoice_allow_donations = get_boolean_safe(default_s, "invoiceAllowDonations", + self.invoice_allow_donations) + self.invoice_default_amount = get_int_safe(default_s, "invoiceDefaultAmount", self.invoice_default_amount) + self.lcd_rotate = get_boolean_safe(default_s, "lcdrotate", self.lcd_rotate) + self.lnd_address = get_str_clean(default_s, "lndAddress", self.lnd_address) + self.lnd_port = get_str_clean(default_s, "lndPort", self.lnd_port) + self.network = get_str_clean(default_s, "network", self.network) + self.public_ip = get_str_clean(default_s, "publicIP", self.public_ip) + self.rtl_web_interface = get_boolean_safe(default_s, "rtlWebinterface", self.rtl_web_interface) + self.run_behind_tor = get_boolean_safe(default_s, "runBehindTor", self.run_behind_tor) + self.ssh_tunnel = get_str_clean(default_s, "sshtunnel", self.ssh_tunnel) + self.touchscreen = get_boolean_safe(default_s, "touchscreen", self.touchscreen) + self.version = get_str_clean(default_s, "raspiBlitzVersion", self.version) + + +class RaspiBlitzInfo(object): + def __init__(self, abs_path="/home/admin/raspiblitz.info"): + self.abs_path = abs_path + + # default values for RaspiBlitz Info + self.base_image = "" + self.chain = "" + self.message = "" + self.network = "" + self.setup_step = 0 + self.state = "" + self.undervoltage_reports = 0 + + def reload(self): + """load config from file""" + parser = ConfigParser() + + log.debug("loading config from file: {}".format(self.abs_path)) + with open(self.abs_path) as f: + parser.read_string("[{}]\n".format(DEFAULTSECT) + f.read()) + + default_s = parser[DEFAULTSECT] + + self.base_image = get_str_clean(default_s, "baseimage", self.base_image) + self.chain = get_str_clean(default_s, "chain", self.chain) + self.message = get_str_clean(default_s, "message", self.message) + self.network = get_str_clean(default_s, "network", self.network) + self.setup_step = get_int_safe(default_s, "setupStep", self.setup_step) + self.state = get_str_clean(default_s, "state", self.state) + self.undervoltage_reports = get_int_safe(default_s, "undervoltageReports", self.undervoltage_reports) + + +def get_boolean_safe(cp_section, key, default_value): + """take a ConfigParser section, get key that might be string encoded boolean and return boolean""" + try: + value = cp_section.getboolean(key, default_value) + except ValueError: + _value = cp_section.get(key) + value = bool(_value.strip("'").strip('"')) # this will raise an Exception if int() fails! + return value + + +def get_int_safe(cp_section, key, default_value): + """take a ConfigParser section, get key that might be string encoded int and return int""" + try: + value = cp_section.getint(key, default_value) + except ValueError: + _value = cp_section.get(key) + value = int(_value.strip("'").strip('"')) # this will raise an Exception if int() fails! + return value + + +def get_str_clean(cp_section, key, default_value): + """take a ConfigParser section, get key and strip leading and trailing \' and \" chars""" + value = cp_section.get(key, default_value) + if not value: + return "" + + return value.lstrip('"').lstrip("'").rstrip('"').rstrip("'") + + +def main(): + lnd_cfg = LndConfig() + if os.path.exists(lnd_cfg.abs_path): + lnd_cfg.reload() + + print("=======\n= LND =\n=======") + print("rpc_list: \t\t{}".format(lnd_cfg.rpc_listen)) + print("rpc_list_host: \t\t{}".format(lnd_cfg.rpc_listen_host)) + print("rpc_list_port: \t\t{}".format(lnd_cfg.rpc_listen_port)) + print("") + + rb_cfg = RaspiBlitzConfig() + if os.path.exists(rb_cfg.abs_path): + rb_cfg.reload() + + print("====================\n= RaspiBlitzConfig =\n====================") + print("auto_nat_discovery: \t\t{}".format(rb_cfg.auto_nat_discovery)) + print("auto_pilot: \t\t\t{}".format(rb_cfg.auto_pilot)) + print("auto_unlock: \t\t\t{}".format(rb_cfg.auto_unlock)) + print("chain: \t\t\t\t{}".format(rb_cfg.chain)) + print("dynDomain: \t\t\t{}".format(rb_cfg.dynDomain)) + print("dyn_update_url: \t\t{}".format(rb_cfg.dyn_update_url)) + print("hostname: \t\t\t{}".format(rb_cfg.hostname)) + print("invoice_allow_donations: \t{}".format(rb_cfg.invoice_allow_donations)) + print("invoice_default_amount: \t{}".format(rb_cfg.invoice_default_amount)) + print("lcd_rotate: \t\t\t{}".format(rb_cfg.lcd_rotate)) + print("lnd_address: \t\t\t{}".format(rb_cfg.lnd_address)) + print("lnd_port: \t\t\t{}".format(rb_cfg.lnd_port)) + print("network: \t\t\t{}".format(rb_cfg.network)) + print("public_ip: \t\t\t{}".format(rb_cfg.public_ip)) + print("rtl_web_interface: \t\t{}".format(rb_cfg.rtl_web_interface)) + print("run_behind_tor: \t\t{}".format(rb_cfg.run_behind_tor)) + print("ssh_tunnel: \t\t\t{}".format(rb_cfg.ssh_tunnel)) + print("touchscreen: \t\t\t{}".format(rb_cfg.touchscreen)) + print("version: \t\t\t{}".format(rb_cfg.version)) + print("") + + rb_info = RaspiBlitzInfo() + if os.path.exists(rb_info.abs_path): + rb_info.reload() + + print("==================\n= RaspiBlitzInfo =\n==================") + print("state: \t\t{}".format(rb_info.state)) + print("") + + +if __name__ == "__main__": + main() diff --git a/home.admin/BlitzPy/blitzpy/version.py b/home.admin/BlitzPy/blitzpy/version.py new file mode 100644 index 00000000..e2145626 --- /dev/null +++ b/home.admin/BlitzPy/blitzpy/version.py @@ -0,0 +1,8 @@ +""" Store the version here so: +# 1) we don't load dependencies by storing it in __init__.py +# 2) we can import it in setup.py for the same reason +# 3) we can import it into your module module +""" + +__version_info__ = ('0', '1', '0') +__version__ = '.'.join(__version_info__) diff --git a/home.admin/BlitzPy/setup.cfg b/home.admin/BlitzPy/setup.cfg new file mode 100644 index 00000000..a968c050 --- /dev/null +++ b/home.admin/BlitzPy/setup.cfg @@ -0,0 +1,6 @@ +# content of setup.cfg +[tool:pytest] +addopts = -ra -q + +[bdist_wheel] +universal = 1 diff --git a/home.admin/BlitzPy/setup.py b/home.admin/BlitzPy/setup.py new file mode 100644 index 00000000..4310f2d6 --- /dev/null +++ b/home.admin/BlitzPy/setup.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +import setuptools + +with open("README.md", "r") as fh: + long_description = fh.read() + +with open("blitzpy/version.py") as f: + __version__ = "" + exec(f.read()) # set __version__ + +setuptools.setup( + name="BlitzPy", + version=__version__, + author="RaspiBlitz Developers", + author_email="raspiblitz@rhab.de", + description="Common Uses Cases for RaspiBlitz", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://github.com/rootzoll/raspiblitz", + packages=setuptools.find_packages(exclude=("tests", "docs")), + classifiers=[ + # How mature is this project? Common values are + # 3 - Alpha + # 4 - Beta + # 5 - Production/Stable + "Development Status :: 4 - Beta", + + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: POSIX :: Linux", + ], + python_requires='>=3.6', + install_requires=[ + ], +)