configReaders 25.7.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .readers import (read_config, read_json)
|
configReaders/readers.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import configparser
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
# use this for .cfg or .ini files
|
|
5
|
+
def read_config(file_path):
|
|
6
|
+
"""
|
|
7
|
+
Read from .cfg file
|
|
8
|
+
|
|
9
|
+
Parameters
|
|
10
|
+
----------
|
|
11
|
+
file_path (str) : the path to the config file
|
|
12
|
+
|
|
13
|
+
Returns
|
|
14
|
+
-------
|
|
15
|
+
see configparser docstring
|
|
16
|
+
"""
|
|
17
|
+
config = configparser.ConfigParser()
|
|
18
|
+
config.read(file_path)
|
|
19
|
+
return config
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def read_json(file_path):
|
|
25
|
+
"""
|
|
26
|
+
Reads .json files
|
|
27
|
+
|
|
28
|
+
Parameters
|
|
29
|
+
----------
|
|
30
|
+
file_path (str) : the path to the json file
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
--------
|
|
34
|
+
f (dict) : dictionary
|
|
35
|
+
|
|
36
|
+
"""
|
|
37
|
+
with open(file_path, 'r') as f:
|
|
38
|
+
return json.load(f)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: configReaders
|
|
3
|
+
Version: 25.7.1
|
|
4
|
+
Summary: Helper functions for reading keys from JSON and config files
|
|
5
|
+
Author: T. Werstlein
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
#configHelpers
|
|
15
|
+
_I wrote configHelpers to read .cfg, .ini or .json files that contain my api keys and passwords.
|
|
16
|
+
I found that I was recreating the same functions to perform these takes in multiple scripts and
|
|
17
|
+
decided to use this as a learning moment to dive into packages.
|
|
18
|
+
|
|
19
|
+
##configReaders
|
|
20
|
+
This is the package that contains readers.py.
|
|
21
|
+
|
|
22
|
+
##Readers.py
|
|
23
|
+
There are methods: read_config and read_json.
|
|
24
|
+
|
|
25
|
+
###read_config
|
|
26
|
+
The read_config method takes in a path to an .cfg or .ini file.
|
|
27
|
+
The .cfg or .ini sould be formatted into sections with your api keys and passwords.
|
|
28
|
+
How you segregate your keys and passwords is up to you, it is your .cfg or .ini after all.
|
|
29
|
+
|
|
30
|
+
-- usage example:
|
|
31
|
+
config_path = "default.cfg"
|
|
32
|
+
myKeys = read_config(config_path)
|
|
33
|
+
|
|
34
|
+
for key, value in myKeys['api_keys'].items():
|
|
35
|
+
print(key + ': ' + value)
|
|
36
|
+
|
|
37
|
+
print(myKeys['api_keys']['my_key'])
|
|
38
|
+
|
|
39
|
+
###read_json
|
|
40
|
+
The read_json method takes in a path to a .json file.
|
|
41
|
+
The .json file should be formatted into a dictionary of key/value sets.
|
|
42
|
+
|
|
43
|
+
-- usage example:
|
|
44
|
+
json_path = "openAI.json"
|
|
45
|
+
DB = read_json(json_path)
|
|
46
|
+
print(DB['KEYS']['api_key'])
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
-- config (.cfg) example:
|
|
50
|
+
[keys]
|
|
51
|
+
api_key1 = key_string1
|
|
52
|
+
api_key2 = key_string2
|
|
53
|
+
exit_keys = q, Q, exit, EXIT, quit, QUIT
|
|
54
|
+
|
|
55
|
+
[passwords]
|
|
56
|
+
api_pword1 = pass_string1
|
|
57
|
+
api_pword2 = pass_string2
|
|
58
|
+
|
|
59
|
+
-- json (.json) example:
|
|
60
|
+
{
|
|
61
|
+
'api1':{
|
|
62
|
+
'api_key': 'key_string',
|
|
63
|
+
'api_pass': 'pass_string',
|
|
64
|
+
}
|
|
65
|
+
'api2':{
|
|
66
|
+
'api_key': 'key_string',
|
|
67
|
+
'api_pass': 'pass_string',
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
configReaders/__init__.py,sha256=LhDqJ1yeoinK_mexynr7G1xEacuvyYT8c-3OUswWiO0,45
|
|
2
|
+
configReaders/readers.py,sha256=Ebrt5ajMSdElerow2HAZOICxK-SPyC4d5T02LwwNIn8,660
|
|
3
|
+
configreaders-25.7.1.dist-info/METADATA,sha256=K76tPixqp2Eb8kCIxZ_rPsdjes1p0oTezaLcZti1rvo,2005
|
|
4
|
+
configreaders-25.7.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
5
|
+
configreaders-25.7.1.dist-info/top_level.txt,sha256=4MCMZTsHGlBfeMIp6G1XnQz-U_1MU4Wxiv0bR5B-u3g,14
|
|
6
|
+
configreaders-25.7.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
configReaders
|