Skip to content

Commit

Permalink
fix reading options from config file (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: R4SAS <[email protected]>
  • Loading branch information
r4sas committed Dec 10, 2022
1 parent d8f3c44 commit b35fd9f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
15 changes: 7 additions & 8 deletions pbincli/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ def signal_handler(sig, frame):
print('Keyboard interrupt received, terminating…')
sys.exit(0)


signal.signal(signal.SIGINT, signal_handler)


def send(args, api_client, settings=None):
if args.short:
if settings['short']:
shortener = Shortener(settings)

if not args.notext:
Expand All @@ -42,7 +41,7 @@ def send(args, api_client, settings=None):
if args.verbose: print("Filling paste with data…")
# set compression type, works only on v2 pastes
if version == 2:
paste.setCompression(args.compression)
paste.setCompression(settings['compression'])

# add text in paste (if it provided)
paste.setText(text)
Expand All @@ -57,10 +56,10 @@ def send(args, api_client, settings=None):

if args.verbose: print("Encrypting paste…")
paste.encrypt(
formatter = args.format,
burnafterreading = args.burn,
discussion = args.discus,
expiration = args.expire)
formatter = settings['format'],
burnafterreading = settings['burn'],
discussion = settings['discus'],
expiration = settings['expire'])

if args.verbose: print("Sending request to server…")
request = paste.getJSON()
Expand Down Expand Up @@ -112,7 +111,7 @@ def send(args, api_client, settings=None):
else: # or here no status field in response or it is empty
PBinCLIError("Something went wrong…\nError: Empty response.")

if args.short:
if settings['short']:
print("\nQuerying URL shortening service…")
shortener.getlink("{}?{}#{}".format(
settings['server'],
Expand Down
37 changes: 23 additions & 14 deletions pbincli/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
import os, sys, argparse
from distutils.util import strtobool

import pbincli.actions
from pbincli.api import PrivateBin
Expand All @@ -15,6 +16,7 @@
elif sys.platform == "darwin":
CONFIG_PATHS.append(os.path.join(os.getenv("HOME") or "~", "Library", "Application Support", "pbincli", "pbincli.conf"))


def read_config(filename):
"""Read config variables from a file"""
settings = {}
Expand All @@ -24,12 +26,15 @@ def read_config(filename):
continue
try:
key, value = l.strip().split("=", 1)
settings[key.strip()] = value.strip()
if value.strip().lower() in ['true', 'false']:
settings[key.strip()] = bool(strtobool(value.strip()))
else:
settings[key.strip()] = value.strip()
except ValueError:
PBinCLIError("Unable to parse config file, please check it for errors.")

return settings


def main():
parser = argparse.ArgumentParser(description='Full-featured PrivateBin command-line client')
subparsers = parser.add_subparsers(title="actions", help="List of commands")
Expand All @@ -41,15 +46,15 @@ def main():
send_parser.add_argument("-p", "--password", help="Password for encrypting paste")
send_parser.add_argument("-E", "--expire", default="1day", action="store",
choices=["5min", "10min", "1hour", "1day", "1week", "1month", "1year", "never"], help="Paste lifetime (default: 1day)")
send_parser.add_argument("-B", "--burn", default=False, action="store_true", help="Set \"Burn after reading\" flag")
send_parser.add_argument("-D", "--discus", default=False, action="store_true", help="Open discussion for sent paste")
send_parser.add_argument("-B", "--burn", default=argparse.SUPPRESS, action="store_true", help="Set \"Burn after reading\" flag")
send_parser.add_argument("-D", "--discus", default=argparse.SUPPRESS, action="store_true", help="Open discussion for sent paste")
send_parser.add_argument("-F", "--format", default="plaintext", action="store",
choices=["plaintext", "syntaxhighlighting", "markdown"], help="Format of text (default: plaintext)")
send_parser.add_argument("-q", "--notext", default=False, action="store_true", help="Don't send text in paste")
send_parser.add_argument("-c", "--compression", default="zlib", action="store",
choices=["zlib", "none"], help="Set compression for paste (default: zlib). Note: works only on v2 paste format")
## URL shortener
send_parser.add_argument("-S", "--short", default=False, action="store_true", help="Use URL shortener")
send_parser.add_argument("-S", "--short", default=argparse.SUPPRESS, action="store_true", help="Use URL shortener")
send_parser.add_argument("--short-api", default=argparse.SUPPRESS, action="store",
choices=["tinyurl", "clckru", "isgd", "vgd", "cuttly", "yourls", "custom"], help="API used by shortener service")
send_parser.add_argument("--short-url", default=argparse.SUPPRESS, help="URL of shortener service API")
Expand All @@ -59,8 +64,8 @@ def main():
## Connection options
send_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)")
send_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)")
send_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation")
send_parser.add_argument("--no-insecure-warning", default=False, action="store_true",
send_parser.add_argument("--no-check-certificate", default=argparse.SUPPRESS, action="store_true", help="Disable certificate validation")
send_parser.add_argument("--no-insecure-warning", default=argparse.SUPPRESS, action="store_true",
help="Suppress InsecureRequestWarning (only with --no-check-certificate)")
##
send_parser.add_argument("-L", "--mirrors", default=argparse.SUPPRESS, help="Comma-separated list of mirrors of service with scheme (default: None)")
Expand All @@ -77,8 +82,8 @@ def main():
## Connection options
get_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/, ignored if URL used in pasteinfo)")
get_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)")
get_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation")
get_parser.add_argument("--no-insecure-warning", default=False, action="store_true",
get_parser.add_argument("--no-check-certificate", default=argparse.SUPPRESS, action="store_true", help="Disable certificate validation")
get_parser.add_argument("--no-insecure-warning", default=argparse.SUPPRESS, action="store_true",
help="Suppress InsecureRequestWarning (only with --no-check-certificate)")
##
get_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="Enable verbose output")
Expand All @@ -91,8 +96,8 @@ def main():
## Connection options
delete_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)")
delete_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)")
delete_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation")
delete_parser.add_argument("--no-insecure-warning", default=False, action="store_true",
delete_parser.add_argument("--no-check-certificate", default=argparse.SUPPRESS, action="store_true", help="Disable certificate validation")
delete_parser.add_argument("--no-insecure-warning", default=argparse.SUPPRESS, action="store_true",
help="Suppress InsecureRequestWarning (only with --no-check-certificate)")
##
delete_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="Enable verbose output")
Expand All @@ -102,11 +107,12 @@ def main():
# parse arguments
args = parser.parse_args()

# default configuration
CONFIG = {
'server': 'https://paste.i2pd.xyz/',
'mirrors': None,
'proxy': None,
'expire': None,
'expire': '1day',
'burn': False,
'discus': False,
'format': None,
Expand All @@ -125,11 +131,13 @@ def main():
# 1. Command line switches
# 2. Environment variables
# 3. Configuration file
# 4. Default values below
# 4. Defaults above

for p in CONFIG_PATHS:
if os.path.exists(p):
CONFIG.update(read_config(p))
fileconfig = read_config(p)
if args.debug: print("Configuration readed from file:\t{}".format(fileconfig))
CONFIG.update(fileconfig)
break

for key in CONFIG.keys():
Expand All @@ -143,6 +151,7 @@ def main():
# Re-validate PrivateBin instance URL
CONFIG['server'] = validate_url_ending(CONFIG['server'])

if args.debug: print("Whole configuration:\t\t{}".format(CONFIG))
api_client = PrivateBin(CONFIG)

if hasattr(args, "func"):
Expand Down

0 comments on commit b35fd9f

Please sign in to comment.