Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit dc18d57

Browse files
committed
lint found minor stuff
1 parent c7658a3 commit dc18d57

File tree

6 files changed

+68
-68
lines changed

6 files changed

+68
-68
lines changed

cli4/cli4.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,37 @@
77
import keyword
88
import json
99

10-
my_yaml = None
11-
my_jsonlines = None
12-
1310
import CloudFlare
11+
1412
from .dump import dump_commands, dump_commands_from_web
1513
from . import converters
1614
from . import examples
1715

16+
my_yaml = None
17+
my_jsonlines = None
18+
19+
class CLI4InternalError(Exception):
20+
""" errors in cli4 """
21+
1822
def load_and_check_yaml():
1923
""" load_and_check_yaml() """
24+
# only called if user uses --yaml flag
2025
from . import myyaml
2126
global my_yaml
22-
my_yaml = myyaml.myyaml()
23-
if not my_yaml.available():
24-
sys.exit('cli4: install yaml support')
27+
try:
28+
my_yaml = myyaml.myyaml()
29+
except ImportError:
30+
sys.exit('cli4: install yaml support via: pip install pyyaml')
31+
32+
def load_and_check_jsonlines():
33+
""" load_and_check_yaml() """
34+
# only called if user uses --ndjson flag
35+
from . import myjsonlines
36+
global my_jsonlines
37+
try:
38+
my_jsonlines = myjsonlines.myjsonlines()
39+
except ImportError:
40+
sys.exit('cli4: install jsonlines support via: pip install jsonlines')
2541

2642
def strip_multiline(s):
2743
""" remove leading/trailing tabs/spaces on each line"""
@@ -56,7 +72,7 @@ def process_params_content_files(method, binary_file, args):
5672
with open(filename, 'rb') as f:
5773
content = f.read()
5874
else:
59-
with open(filename, 'r') as f:
75+
with open(filename, 'r', encoding="utf-8") as f:
6076
content = f.read()
6177
except IOError:
6278
sys.exit('cli4: %s - file open failure' % (filename))
@@ -86,8 +102,8 @@ def process_params_content_files(method, binary_file, args):
86102
value_string = strip_multiline(value_string)
87103
try:
88104
value = my_yaml.safe_load(value_string)
89-
except my_yaml.parser.ParserError as e:
90-
raise ValueError
105+
except my_yaml.parser.ParserError:
106+
raise ValueError from None
91107
except ValueError:
92108
sys.exit('cli4: %s="%s" - can\'t parse json value' % (tag_string, value_string))
93109
elif value_string[0] == '@':
@@ -203,8 +219,8 @@ def run_command(cf, method, command, params=None, content=None, files=None):
203219
elif (cmd[0] == 'user') and (cmd[1] == 'load_balancers') and (cmd[2] == 'pools'):
204220
identifier1 = converters.convert_load_balancers_pool_to_identifier(cf, element)
205221
else:
206-
raise Exception("/%s/%s :NOT CODED YET" % ('/'.join(cmd), element))
207-
except Exception as e:
222+
raise CLI4InternalError("/%s/%s :NOT CODED YET" % ('/'.join(cmd), element))
223+
except CLI4InternalError as e:
208224
sys.stderr.write('cli4: /%s - %s\n' % (command, e))
209225
raise e
210226
cmd.append(':' + identifier1)
@@ -229,8 +245,8 @@ def run_command(cf, method, command, params=None, content=None, files=None):
229245
identifier1,
230246
element)
231247
else:
232-
raise Exception("/%s/%s :NOT CODED YET" % ('/'.join(cmd), element))
233-
except Exception as e:
248+
raise CLI4InternalError("/%s/:%s :NOT CODED YET" % ('/'.join(cmd), element))
249+
except CLI4InternalError as e:
234250
sys.stderr.write('cli4: /%s - %s\n' % (command, e))
235251
raise e
236252
# identifier2 may be an array - this needs to be dealt with later
@@ -252,10 +268,11 @@ def run_command(cf, method, command, params=None, content=None, files=None):
252268
# raw string - used for workers script_names
253269
identifier3 = element[1:]
254270
else:
271+
# /accounts/:id/storage/kv/namespaces/:id/values/:key_name - it's a strange one!
255272
if len(cmd) >= 6 and cmd[0] == 'accounts' and cmd[2] == 'storage' and cmd[3] == 'kv' and cmd[4] == 'namespaces' and cmd[6] == 'values':
256273
identifier3 = element
257274
else:
258-
sys.stderr.write('/%s/%s :NOT CODED YET 3\n' % ('/'.join(cmd), element))
275+
sys.stderr.write('/%s/:%s :NOT CODED YET\n' % ('/'.join(cmd), element))
259276
raise e
260277
else:
261278
try:
@@ -332,9 +349,9 @@ def write_results(results, output):
332349
if len(results) == 1:
333350
results = results[0]
334351

335-
if isinstance(results, str) or isinstance(results, (bytes, bytearray)):
352+
if isinstance(results, (str, bytes, bytearray)):
336353
# if the results are a simple string, then it should be dumped directly
337-
# this is only used for /zones/:id/dns_records/export presently
354+
# this is only used for /zones/:id/dns_records/export, workers, and other calls
338355
# or
339356
# output is image or audio or video or something like that so we dump directly
340357
pass
@@ -441,11 +458,7 @@ def do_it(args):
441458
load_and_check_yaml()
442459
output = 'yaml'
443460
elif opt in ('-n', '--ndjson'):
444-
from . import myjsonlines
445-
global my_jsonlines
446-
my_jsonlines = myjsonlines.myjsonlines()
447-
if not my_jsonlines.available():
448-
sys.exit('cli4: install jsonlines support')
461+
load_and_check_jsonlines()
449462
output = 'ndjson'
450463
elif opt in ('-i', '--image'):
451464
output = 'image'

cli4/converters.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ def convert_zones_to_identifier(cf, zone_name):
1010
try:
1111
zones = cf.zones.get(params=params)
1212
except CloudFlare.exceptions.CloudFlareAPIError as e:
13-
raise ConverterError(int(e), '%s - %d %s' % (zone_name, int(e), e))
13+
raise ConverterError(int(e), '%s - %d %s' % (zone_name, int(e), e)) from None
1414
except Exception as e:
15-
raise ConverterError(0, '%s - %s' % (zone_name, e))
15+
raise ConverterError(0, '%s - %s' % (zone_name, e)) from e
1616

1717
if len(zones) == 1:
1818
return zones[0]['id']
1919

20-
raise ConverterError('%s: not found' % (zone_name))
20+
raise ConverterError('%s: not found' % (zone_name)) from None
2121

2222
def convert_accounts_to_identifier(cf, account_name):
2323
"""account names to numbers"""
2424
params = {'name':account_name, 'per_page':1}
2525
try:
2626
accounts = cf.accounts.get(params=params)
2727
except CloudFlare.exceptions.CloudFlareAPIError as e:
28-
raise ConverterError(int(e), '%s - %d %s' % (account_name, int(e), e))
28+
raise ConverterError(int(e), '%s - %d %s' % (account_name, int(e), e)) from None
2929
except Exception as e:
30-
raise ConverterError(0, '%s - %s' % (account_name, e))
30+
raise ConverterError(0, '%s - %s' % (account_name, e)) from e
3131

3232
if len(accounts) == 1:
3333
return accounts[0]['id']
3434

35-
raise ConverterError('%s: not found' % (account_name))
35+
raise ConverterError('%s: not found' % (account_name)) from None
3636

3737
def convert_dns_record_to_identifier(cf, zone_id, dns_name):
3838
"""dns record names to numbers"""
@@ -41,9 +41,9 @@ def convert_dns_record_to_identifier(cf, zone_id, dns_name):
4141
try:
4242
dns_records = cf.zones.dns_records.get(zone_id, params=params)
4343
except CloudFlare.exceptions.CloudFlareAPIError as e:
44-
raise ConverterError(int(e), '%s - %d %s' % (dns_name, int(e), e))
44+
raise ConverterError(int(e), '%s - %d %s' % (dns_name, int(e), e)) from None
4545
except Exception as e:
46-
raise ConverterError(0, '%s - %s' % (dns_name, e))
46+
raise ConverterError(0, '%s - %s' % (dns_name, e)) from e
4747

4848
r = []
4949
for dns_record in dns_records:
@@ -52,82 +52,82 @@ def convert_dns_record_to_identifier(cf, zone_id, dns_name):
5252
if len(r) > 0:
5353
return r
5454

55-
raise ConverterError('%s: not found' % (dns_name))
55+
raise ConverterError('%s: not found' % (dns_name)) from None
5656

5757
def convert_certificates_to_identifier(cf, certificate_name):
5858
"""certificate names to numbers"""
5959
try:
6060
certificates = cf.certificates.get()
6161
except CloudFlare.exceptions.CloudFlareAPIError as e:
62-
raise ConverterError(int(e), '%s - %d %s' % (certificate_name, int(e), e))
62+
raise ConverterError(int(e), '%s - %d %s' % (certificate_name, int(e), e)) from None
6363
except Exception as e:
64-
raise ConverterError(0, '%s - %s' % (certificate_name, e))
64+
raise ConverterError(0, '%s - %s' % (certificate_name, e)) from e
6565

6666
for certificate in certificates:
6767
if certificate_name in certificate['hostnames']:
6868
return certificate['id']
6969

70-
raise ConverterError('%s: not found' % (certificate_name))
70+
raise ConverterError('%s: not found' % (certificate_name)) from None
7171

7272
def convert_organizations_to_identifier(cf, organization_name):
7373
"""organizations names to numbers"""
7474
try:
7575
organizations = cf.user.organizations.get()
7676
except CloudFlare.exceptions.CloudFlareAPIError as e:
77-
raise ConverterError(int(e), '%s - %d %s' % (organization_name, int(e), e))
77+
raise ConverterError(int(e), '%s - %d %s' % (organization_name, int(e), e)) from None
7878
except Exception as e:
79-
raise ConverterError(0, '%s - %s' % (organization_name, e))
79+
raise ConverterError(0, '%s - %s' % (organization_name, e)) from e
8080

8181
for organization in organizations:
8282
if organization_name == organization['name']:
8383
return organization['id']
8484

85-
raise ConverterError('%s not found' % (organization_name))
85+
raise ConverterError('%s not found' % (organization_name)) from None
8686

8787
def convert_invites_to_identifier(cf, invite_name):
8888
"""invite names to numbers"""
8989
try:
9090
invites = cf.user.invites.get()
9191
except CloudFlare.exceptions.CloudFlareAPIError as e:
92-
raise ConverterError(int(e), '%s - %d %s' % (invite_name, int(e), e))
92+
raise ConverterError(int(e), '%s - %d %s' % (invite_name, int(e), e)) from None
9393
except Exception as e:
94-
raise ConverterError(0, '%s - %s' % (invite_name, e))
94+
raise ConverterError(0, '%s - %s' % (invite_name, e)) from e
9595

9696
for invite in invites:
9797
if invite_name == invite['organization_name']:
9898
return invite['id']
9999

100-
raise ConverterError('%s: not found' % (invite_name))
100+
raise ConverterError('%s: not found' % (invite_name)) from None
101101

102102
def convert_virtual_dns_to_identifier(cf, virtual_dns_name):
103103
"""virtual dns names to numbers"""
104104
try:
105105
virtual_dnss = cf.user.virtual_dns.get()
106106
except CloudFlare.exceptions.CloudFlareAPIError as e:
107-
raise ConverterError(int(e), '%s - %d %s' % (virtual_dns_name, int(e), e))
107+
raise ConverterError(int(e), '%s - %d %s' % (virtual_dns_name, int(e), e)) from None
108108
except Exception as e:
109-
raise ConverterError(0, '%s - %s' % (virtual_dns_name, e))
109+
raise ConverterError(0, '%s - %s' % (virtual_dns_name, e)) from e
110110

111111
for virtual_dns in virtual_dnss:
112112
if virtual_dns_name == virtual_dns['name']:
113113
return virtual_dns['id']
114114

115-
raise ConverterError('%s: not found' % (virtual_dns_name))
115+
raise ConverterError('%s: not found' % (virtual_dns_name)) from None
116116

117117
def convert_load_balancers_pool_to_identifier(cf, pool_name):
118118
"""load balancer pool names to numbers"""
119119
try:
120120
pools = cf.user.load_balancers.pools.get()
121121
except CloudFlare.exceptions.CloudFlareAPIError as e:
122-
raise ConverterError(int(e), '%s - %d %s' % (pool_name, int(e), e))
122+
raise ConverterError(int(e), '%s - %d %s' % (pool_name, int(e), e)) from None
123123
except Exception as e:
124-
raise ConverterError(0, '%s - %s' % (pool_name, e))
124+
raise ConverterError(0, '%s - %s' % (pool_name, e)) from e
125125

126126
for p in pools:
127127
if pool_name == p['description']:
128128
return p['id']
129129

130-
raise ConverterError('%s: not found' % (pool_name))
130+
raise ConverterError('%s: not found' % (pool_name)) from None
131131

132132
def convert_custom_hostnames_to_identifier(cf, zone_id, custom_hostname):
133133
"""custom_hostnames to numbers"""
@@ -136,9 +136,9 @@ def convert_custom_hostnames_to_identifier(cf, zone_id, custom_hostname):
136136
try:
137137
custom_hostnames_records = cf.zones.custom_hostnames.get(zone_id, params=params)
138138
except CloudFlare.exceptions.CloudFlareAPIError as e:
139-
raise ConverterError(int(e), '%s - %d %s' % (custom_hostname, int(e), e))
139+
raise ConverterError(int(e), '%s - %d %s' % (custom_hostname, int(e), e)) from None
140140
except Exception as e:
141-
raise ConverterError(0, '%s - %s' % (custom_hostname, e))
141+
raise ConverterError(0, '%s - %s' % (custom_hostname, e)) from e
142142

143143
r = []
144144
for custom_hostnames_record in custom_hostnames_records:
@@ -147,4 +147,4 @@ def convert_custom_hostnames_to_identifier(cf, zone_id, custom_hostname):
147147
if len(r) > 0:
148148
return r
149149

150-
raise ConverterError('%s: not found' % (custom_hostname))
150+
raise ConverterError('%s: not found' % (custom_hostname)) from None

cli4/dump.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@ def dump_commands_from_web(cf, url):
2222
else:
2323
a.append('%-6s %s' % (r['action'], r['cmd']))
2424
return '\n'.join(a) + '\n'
25-
26-

cli4/examples.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
# function, so use the PyPI version:
99
try:
1010
import importlib_resources
11-
except ModuleNotFoundError as e:
11+
except ModuleNotFoundError:
1212
importlib_resources = None
1313
else:
1414
# importlib.resources has files(), so use that:
1515
import importlib.resources as importlib_resources
1616

17-
examples_package_name = 'examples'
17+
EXAMPLES_PACKAGE_NAME = 'examples'
1818

1919
def display():
2020
""" display() """
@@ -23,7 +23,7 @@ def display():
2323
raise ModuleNotFoundError('Module "importlib_resources" missing - please "pip install importlib_resources" as your Python version is lower than 3.9')
2424

2525
try:
26-
pkg = importlib_resources.files(examples_package_name)
26+
pkg = importlib_resources.files(EXAMPLES_PACKAGE_NAME)
2727
except ModuleNotFoundError as e:
2828
raise e
2929

@@ -36,4 +36,3 @@ def display():
3636
if '__init__.py' in os.fspath(f):
3737
continue
3838
print('\t%s' % (os.fspath(f)))
39-

cli4/myjsonlines.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ class myjsonlines():
77

88
def __init__(self):
99
""" __init__ """
10-
pass
11-
12-
def available(self):
13-
""" available() """
1410
if not myjsonlines._jsonlines:
1511
try:
1612
import jsonlines
1713
myjsonlines._jsonlines = jsonlines
18-
except ImportError:
19-
return False
20-
return True
14+
except ImportError as e:
15+
raise ImportError from e
2116

2217
def Writer(self, fd):
2318
""" Writer() """

cli4/myyaml.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@ class myyaml():
88

99
def __init__(self):
1010
""" __init__ """
11-
pass
12-
13-
def available(self):
14-
""" available() """
1511
if not myyaml._yaml:
1612
try:
1713
import yaml
1814
myyaml._yaml = yaml
1915
myyaml.parser = yaml.parser
20-
except ImportError:
21-
return False
22-
return True
16+
except ImportError as e:
17+
raise ImportError from e
2318

2419
def safe_load(self,value_string):
2520
""" safe_load() """

0 commit comments

Comments
 (0)