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

Commit aa31bb3

Browse files
committed
Added cli4 -e option to display example file path names
1 parent 0ea52bc commit aa31bb3

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,34 @@ $
500500

501501
The [examples](https://github.com/cloudflare/python-cloudflare/tree/master/examples) folder contains many examples in both simple and verbose formats.
502502

503+
You can see the installed path of these files directly via `cli4 -e` command.
504+
505+
```bash
506+
$ cli4 -e
507+
Python .py files:
508+
...
509+
/opt/homebrew/lib/python3.11/site-packages/examples/example_always_use_https.py
510+
...
511+
Bash .sh files:
512+
...
513+
/opt/homebrew/lib/python3.11/site-packages/examples/example_paging_thru_zones.sh
514+
...
515+
$
516+
```
517+
518+
The exact path will vary depending on your system.
519+
The above example is MacOS and Python 3.9 hence the `/opt/homebrew/lib/python3.11/site-packages/` path.
520+
One Linux, the Python pip command may install the code is a system location like `/usr/lib/python3/dist-packages` or `~/.local/lib/python3.9/site-packages/` or something different.
521+
The `cli4 -e` command will try to decode the location and display the example files.
522+
523+
If you are running release before Python 3.9 then you will be asked to install the following:
524+
525+
```bash
526+
$ pip install importlib_resources
527+
...
528+
$
529+
```
530+
503531
## A DNS zone code example
504532

505533
```python

cli4/cli4.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import CloudFlare
1414
from .dump import dump_commands, dump_commands_from_web
1515
from . import converters
16+
from . import examples
1617

1718
def load_and_check_yaml():
1819
""" load_and_check_yaml() """
@@ -365,6 +366,7 @@ def do_it(args):
365366

366367
verbose = False
367368
output = 'json'
369+
example = False
368370
raw = False
369371
dump = False
370372
openapi_url = None
@@ -374,6 +376,7 @@ def do_it(args):
374376

375377
usage = ('usage: cli4 '
376378
+ '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] '
379+
+ '[-e|--examples] '
377380
+ '[-j|--json] [-y|--yaml] [-n|ndjson] '
378381
+ '[-r|--raw] '
379382
+ '[-d|--dump] '
@@ -386,10 +389,10 @@ def do_it(args):
386389

387390
try:
388391
opts, args = getopt.getopt(args,
389-
'VhvqjyrdA:bp:GPOUD',
392+
'VhvqejyrdA:bp:GPOUD',
390393
[
391394
'version',
392-
'help', 'verbose', 'quiet', 'json', 'yaml', 'ndjson',
395+
'help', 'verbose', 'quiet', 'examples', 'json', 'yaml', 'ndjson',
393396
'raw',
394397
'dump', 'openapi=',
395398
'binary',
@@ -407,6 +410,8 @@ def do_it(args):
407410
verbose = True
408411
elif opt in ('-q', '--quiet'):
409412
output = None
413+
elif opt in ('-e', '--examples'):
414+
example = True
410415
elif opt in ('-j', '--json'):
411416
output = 'json'
412417
elif opt in ('-y', '--yaml'):
@@ -440,6 +445,13 @@ def do_it(args):
440445
elif opt in ('-D', '--delete'):
441446
method = 'DELETE'
442447

448+
if example:
449+
try:
450+
examples.display()
451+
except ModuleNotFoundError as e:
452+
sys.exit(e)
453+
sys.exit(0)
454+
443455
try:
444456
cf = CloudFlare.CloudFlare(debug=verbose, raw=raw, profile=profile)
445457
except Exception as e:

cli4/examples.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Cloudflare API via command line"""
2+
3+
import os
4+
import sys
5+
6+
if sys.version_info < (3, 9):
7+
# importlib.resources either doesn't exist or lacks the files()
8+
# function, so use the PyPI version:
9+
try:
10+
import importlib_resources
11+
except ModuleNotFoundError as e:
12+
importlib_resources = None
13+
else:
14+
# importlib.resources has files(), so use that:
15+
import importlib.resources as importlib_resources
16+
17+
examples_package_name = 'examples'
18+
19+
def display():
20+
""" display() """
21+
22+
if not importlib_resources:
23+
raise ModuleNotFoundError('Module "importlib_resources" missing - please "pip install importlib_resources" as your Python version is lower than 3.9')
24+
25+
try:
26+
pkg = importlib_resources.files(examples_package_name)
27+
except ModuleNotFoundError as e:
28+
raise e
29+
30+
for ext,name in {'c': 'C', 'h': 'C', 'cc': 'C++', 'py':'Python', 'sh':'Bash', 'awk':'AWK'}.items():
31+
l = sorted(pkg.glob('**/*.' + ext))
32+
if len(l) == 0:
33+
continue
34+
print('%s .%s files:' % (name, ext))
35+
for f in sorted(pkg.glob('**/*.' + ext)):
36+
if '__init__.py' in os.fspath(f):
37+
continue
38+
print('\t%s' % (os.fspath(f)))
39+

0 commit comments

Comments
 (0)