Skip to content

Commit b35fd9f

Browse files
committed
fix reading options from config file (#37)
Signed-off-by: R4SAS <r4sas@i2pmail.org>
1 parent d8f3c44 commit b35fd9f

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

pbincli/actions.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ def signal_handler(sig, frame):
1010
print('Keyboard interrupt received, terminating…')
1111
sys.exit(0)
1212

13-
1413
signal.signal(signal.SIGINT, signal_handler)
1514

1615

1716
def send(args, api_client, settings=None):
18-
if args.short:
17+
if settings['short']:
1918
shortener = Shortener(settings)
2019

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

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

5857
if args.verbose: print("Encrypting paste…")
5958
paste.encrypt(
60-
formatter = args.format,
61-
burnafterreading = args.burn,
62-
discussion = args.discus,
63-
expiration = args.expire)
59+
formatter = settings['format'],
60+
burnafterreading = settings['burn'],
61+
discussion = settings['discus'],
62+
expiration = settings['expire'])
6463

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

115-
if args.short:
114+
if settings['short']:
116115
print("\nQuerying URL shortening service…")
117116
shortener.getlink("{}?{}#{}".format(
118117
settings['server'],

pbincli/cli.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
import os, sys, argparse
3+
from distutils.util import strtobool
34

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

19+
1820
def read_config(filename):
1921
"""Read config variables from a file"""
2022
settings = {}
@@ -24,12 +26,15 @@ def read_config(filename):
2426
continue
2527
try:
2628
key, value = l.strip().split("=", 1)
27-
settings[key.strip()] = value.strip()
29+
if value.strip().lower() in ['true', 'false']:
30+
settings[key.strip()] = bool(strtobool(value.strip()))
31+
else:
32+
settings[key.strip()] = value.strip()
2833
except ValueError:
2934
PBinCLIError("Unable to parse config file, please check it for errors.")
30-
3135
return settings
3236

37+
3338
def main():
3439
parser = argparse.ArgumentParser(description='Full-featured PrivateBin command-line client')
3540
subparsers = parser.add_subparsers(title="actions", help="List of commands")
@@ -41,15 +46,15 @@ def main():
4146
send_parser.add_argument("-p", "--password", help="Password for encrypting paste")
4247
send_parser.add_argument("-E", "--expire", default="1day", action="store",
4348
choices=["5min", "10min", "1hour", "1day", "1week", "1month", "1year", "never"], help="Paste lifetime (default: 1day)")
44-
send_parser.add_argument("-B", "--burn", default=False, action="store_true", help="Set \"Burn after reading\" flag")
45-
send_parser.add_argument("-D", "--discus", default=False, action="store_true", help="Open discussion for sent paste")
49+
send_parser.add_argument("-B", "--burn", default=argparse.SUPPRESS, action="store_true", help="Set \"Burn after reading\" flag")
50+
send_parser.add_argument("-D", "--discus", default=argparse.SUPPRESS, action="store_true", help="Open discussion for sent paste")
4651
send_parser.add_argument("-F", "--format", default="plaintext", action="store",
4752
choices=["plaintext", "syntaxhighlighting", "markdown"], help="Format of text (default: plaintext)")
4853
send_parser.add_argument("-q", "--notext", default=False, action="store_true", help="Don't send text in paste")
4954
send_parser.add_argument("-c", "--compression", default="zlib", action="store",
5055
choices=["zlib", "none"], help="Set compression for paste (default: zlib). Note: works only on v2 paste format")
5156
## URL shortener
52-
send_parser.add_argument("-S", "--short", default=False, action="store_true", help="Use URL shortener")
57+
send_parser.add_argument("-S", "--short", default=argparse.SUPPRESS, action="store_true", help="Use URL shortener")
5358
send_parser.add_argument("--short-api", default=argparse.SUPPRESS, action="store",
5459
choices=["tinyurl", "clckru", "isgd", "vgd", "cuttly", "yourls", "custom"], help="API used by shortener service")
5560
send_parser.add_argument("--short-url", default=argparse.SUPPRESS, help="URL of shortener service API")
@@ -59,8 +64,8 @@ def main():
5964
## Connection options
6065
send_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)")
6166
send_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)")
62-
send_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation")
63-
send_parser.add_argument("--no-insecure-warning", default=False, action="store_true",
67+
send_parser.add_argument("--no-check-certificate", default=argparse.SUPPRESS, action="store_true", help="Disable certificate validation")
68+
send_parser.add_argument("--no-insecure-warning", default=argparse.SUPPRESS, action="store_true",
6469
help="Suppress InsecureRequestWarning (only with --no-check-certificate)")
6570
##
6671
send_parser.add_argument("-L", "--mirrors", default=argparse.SUPPRESS, help="Comma-separated list of mirrors of service with scheme (default: None)")
@@ -77,8 +82,8 @@ def main():
7782
## Connection options
7883
get_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/, ignored if URL used in pasteinfo)")
7984
get_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)")
80-
get_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation")
81-
get_parser.add_argument("--no-insecure-warning", default=False, action="store_true",
85+
get_parser.add_argument("--no-check-certificate", default=argparse.SUPPRESS, action="store_true", help="Disable certificate validation")
86+
get_parser.add_argument("--no-insecure-warning", default=argparse.SUPPRESS, action="store_true",
8287
help="Suppress InsecureRequestWarning (only with --no-check-certificate)")
8388
##
8489
get_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="Enable verbose output")
@@ -91,8 +96,8 @@ def main():
9196
## Connection options
9297
delete_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)")
9398
delete_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)")
94-
delete_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation")
95-
delete_parser.add_argument("--no-insecure-warning", default=False, action="store_true",
99+
delete_parser.add_argument("--no-check-certificate", default=argparse.SUPPRESS, action="store_true", help="Disable certificate validation")
100+
delete_parser.add_argument("--no-insecure-warning", default=argparse.SUPPRESS, action="store_true",
96101
help="Suppress InsecureRequestWarning (only with --no-check-certificate)")
97102
##
98103
delete_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="Enable verbose output")
@@ -102,11 +107,12 @@ def main():
102107
# parse arguments
103108
args = parser.parse_args()
104109

110+
# default configuration
105111
CONFIG = {
106112
'server': 'https://paste.i2pd.xyz/',
107113
'mirrors': None,
108114
'proxy': None,
109-
'expire': None,
115+
'expire': '1day',
110116
'burn': False,
111117
'discus': False,
112118
'format': None,
@@ -125,11 +131,13 @@ def main():
125131
# 1. Command line switches
126132
# 2. Environment variables
127133
# 3. Configuration file
128-
# 4. Default values below
134+
# 4. Defaults above
129135

130136
for p in CONFIG_PATHS:
131137
if os.path.exists(p):
132-
CONFIG.update(read_config(p))
138+
fileconfig = read_config(p)
139+
if args.debug: print("Configuration readed from file:\t{}".format(fileconfig))
140+
CONFIG.update(fileconfig)
133141
break
134142

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

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

148157
if hasattr(args, "func"):

0 commit comments

Comments
 (0)