11#!/usr/bin/env python
22import os , sys , argparse
3+ from distutils .util import strtobool
34
45import pbincli .actions
56from pbincli .api import PrivateBin
1516elif sys .platform == "darwin" :
1617 CONFIG_PATHS .append (os .path .join (os .getenv ("HOME" ) or "~" , "Library" , "Application Support" , "pbincli" , "pbincli.conf" ))
1718
19+
1820def 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+
3338def 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