Skip to content

Commit

Permalink
Added --warnings flag to cli4 and associated warnings= flag to cloudf…
Browse files Browse the repository at this point in the history
…lare() call. Provides an ability to control warning messages on or after 2.20.* release
  • Loading branch information
mahtin committed May 8, 2024
1 parent 676d258 commit 2720d21
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
20 changes: 11 additions & 9 deletions CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CloudFlare():
class _v4base():
""" :meta private: """

def __init__(self, config):
def __init__(self, config, warnings=True):
""" :meta private: """

self.network = None
Expand Down Expand Up @@ -89,12 +89,14 @@ def __init__(self, config):

self.logger = CFlogger(config['debug']).getLogger() if 'debug' in config and config['debug'] else None

warning = warning_2_20()
if warning:
if self.logger:
self.logger.warning('\n' + warning)
else:
print_warning_2_20(warning)
if warnings:
# After 2.20.* there is a warning message posted to handle un-pinned versions
warning = warning_2_20()
if warning:
if self.logger:
self.logger.warning(''.join(['\n ' + v for v in warning.split('\n')]))
else:
print_warning_2_20(warning)

def __del__(self):
if self.network:
Expand Down Expand Up @@ -1032,7 +1034,7 @@ def api_from_openapi(self, url=None):

return self._base.api_from_openapi(url)

def __init__(self, email=None, key=None, token=None, certtoken=None, debug=False, raw=False, use_sessions=True, profile=None, base_url=None, global_request_timeout=None, max_request_retries=None, http_headers=None):
def __init__(self, email=None, key=None, token=None, certtoken=None, debug=False, raw=False, use_sessions=True, profile=None, base_url=None, global_request_timeout=None, max_request_retries=None, http_headers=None, warnings=True):
""" :meta private: """

self._base = None
Expand Down Expand Up @@ -1093,7 +1095,7 @@ def __init__(self, email=None, key=None, token=None, certtoken=None, debug=False
if v == '':
config[k] = None

self._base = self._v4base(config)
self._base = self._v4base(config, warnings=warnings)

# add the API calls
try:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ $ cli4 [-V|--version] [-h|--help] [-v|--verbose] \
[-b|--binary] \
[-p|--profile profile-name] \
[-h|--header additional-header] \
[-w|--warnings [True|False]] \
[--get|--patch|--post|--put|--delete] \
[item=value|item=@filename|@filename ...] /command ...
```
Expand Down
16 changes: 14 additions & 2 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ def do_it(args):
binary_file = False
profile = None
http_headers = None
warnings = True
method = 'GET'

usage = ('usage: cli4 '
Expand All @@ -410,13 +411,14 @@ def do_it(args):
+ '[-b|--binary] '
+ '[-p|--profile profile-name] '
+ '[-h|--header additional-header] '
+ '[-w|--warnings [True|False]] '
+ '[--get|--patch|--post|--put|--delete] '
+ '[item=value|item=@filename|@filename ...] '
+ '/command ...')

try:
opts, args = getopt.getopt(args,
'VhveqjynirdA:bp:h:GPOUD',
'VhveqjynirdA:bp:h:w:GPOUD',
[
'version', 'help', 'verbose',
'examples',
Expand All @@ -428,6 +430,7 @@ def do_it(args):
'binary',
'profile=',
'header=',
'warnings=',
'get', 'patch', 'post', 'put', 'delete'
])
except getopt.GetoptError:
Expand Down Expand Up @@ -461,6 +464,15 @@ def do_it(args):
if http_headers is None:
http_headers = []
http_headers.append(arg)
elif opt in ('-w', '--warnings'):
if arg is None or arg == '':
warnings = None
elif arg.lower() in ('yes', 'true', '1'):
warnings = True
elif arg.lower() in ('no', 'false', '0'):
warnings = False
else:
sys.exit('cli4: --warnings takes boolean True/False argument')
elif opt in ('-d', '--dump'):
do_dump = True
elif opt in ('-A', '--openapi'):
Expand All @@ -487,7 +499,7 @@ def do_it(args):
sys.exit(0)

try:
cf = CloudFlare.CloudFlare(debug=verbose, raw=raw, profile=profile, http_headers=http_headers)
cf = CloudFlare.CloudFlare(debug=verbose, raw=raw, profile=profile, http_headers=http_headers, warnings=warnings)
except Exception as e:
sys.exit(e)

Expand Down

0 comments on commit 2720d21

Please sign in to comment.