quick-ini 1.0__tar.gz
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.
- quick_ini-1.0/PKG-INFO +8 -0
- quick_ini-1.0/README.md +0 -0
- quick_ini-1.0/pyproject.toml +3 -0
- quick_ini-1.0/quick_ini/__init__.py +1 -0
- quick_ini-1.0/quick_ini/quick_ini.py +101 -0
- quick_ini-1.0/quick_ini.egg-info/PKG-INFO +8 -0
- quick_ini-1.0/quick_ini.egg-info/SOURCES.txt +9 -0
- quick_ini-1.0/quick_ini.egg-info/dependency_links.txt +1 -0
- quick_ini-1.0/quick_ini.egg-info/top_level.txt +1 -0
- quick_ini-1.0/setup.cfg +4 -0
- quick_ini-1.0/setup.py +13 -0
quick_ini-1.0/PKG-INFO
ADDED
quick_ini-1.0/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .quick_ini import get_last_error, load_file, get_value
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from urllib.parse import urlparse
|
|
2
|
+
|
|
3
|
+
parsed_ini:dict[str,str|int|bool|float|None] = {}
|
|
4
|
+
error_message:str = ""
|
|
5
|
+
web_file:str = None
|
|
6
|
+
|
|
7
|
+
def grab_from_url(url:str) -> bool:
|
|
8
|
+
global web_file, error_message
|
|
9
|
+
import urllib.request, urllib.error
|
|
10
|
+
try:
|
|
11
|
+
with urllib.request.urlopen(url) as response:
|
|
12
|
+
if(response.status == 200):
|
|
13
|
+
web_file = response.read().decode('utf-8')
|
|
14
|
+
return True
|
|
15
|
+
else:
|
|
16
|
+
error_message = f"Failed to download file from {url}, got response: {response.status}"
|
|
17
|
+
return False
|
|
18
|
+
except urllib.error.HTTPError as e:
|
|
19
|
+
error_message = f"Could not get file from URL: '{e}'"
|
|
20
|
+
return False
|
|
21
|
+
|
|
22
|
+
def is_string_url(stri:str) -> bool:
|
|
23
|
+
try:
|
|
24
|
+
res = urlparse(stri)
|
|
25
|
+
return all([res.scheme, res.netloc])
|
|
26
|
+
except ValueError:
|
|
27
|
+
return False
|
|
28
|
+
|
|
29
|
+
def get_last_error() -> str:
|
|
30
|
+
"""
|
|
31
|
+
Returns the last error thrown by the library
|
|
32
|
+
Returns:
|
|
33
|
+
str: A formatted error message
|
|
34
|
+
"""
|
|
35
|
+
return error_message
|
|
36
|
+
|
|
37
|
+
def load_file(file_location:str, auto_type_convert:bool = True, empty_default = None) -> bool:
|
|
38
|
+
"""
|
|
39
|
+
Load a ini style file from the file system or from a URL.
|
|
40
|
+
\nUse get_value() or get_all() to access.
|
|
41
|
+
Args:
|
|
42
|
+
name (str): The path/URL of the file to load.
|
|
43
|
+
auto_type_convert (bool): Automaticly convert into recognized types such as True/False, int, float.
|
|
44
|
+
empty_default (Any): The default value when a value is left blank (overrides auto_type_convert).
|
|
45
|
+
Returns:
|
|
46
|
+
bool: True if successfull, False otherwise
|
|
47
|
+
Example:
|
|
48
|
+
>>> load_file("../config.ini", empty_default="")
|
|
49
|
+
True
|
|
50
|
+
"""
|
|
51
|
+
global error_message, parsed_ini
|
|
52
|
+
parsed_ini = {}
|
|
53
|
+
|
|
54
|
+
f_data:str = None
|
|
55
|
+
|
|
56
|
+
# Check if the provided location is a URL
|
|
57
|
+
if(is_string_url(file_location)):
|
|
58
|
+
if(not grab_from_url(file_location)):
|
|
59
|
+
# The grab_from_url sets the error message
|
|
60
|
+
return False
|
|
61
|
+
f_data = web_file
|
|
62
|
+
# Otherwise assume it's a file path
|
|
63
|
+
else:
|
|
64
|
+
try:
|
|
65
|
+
f_data = open(str(file_location), 'r', encoding='utf-8').read()
|
|
66
|
+
except FileNotFoundError:
|
|
67
|
+
error_message = f"Could not find file at: '{file_location}'"
|
|
68
|
+
return False
|
|
69
|
+
except IOError as e:
|
|
70
|
+
error_message = f"Error reading file: '{e}'"
|
|
71
|
+
return False
|
|
72
|
+
|
|
73
|
+
lines = f_data.splitlines()
|
|
74
|
+
|
|
75
|
+
for line in lines:
|
|
76
|
+
if(line.startswith("#") or "=" not in line): continue # Ignore comments and lines without "="
|
|
77
|
+
left, right = line.split("=", 1)
|
|
78
|
+
if(not right): right = empty_default
|
|
79
|
+
elif(not auto_type_convert): pass # If we arn't auto converting, pass through the rest of the IFs
|
|
80
|
+
elif(right.isnumeric()): right = int(right)
|
|
81
|
+
elif(right.count(".") == 1 and right.replace(".", "").isnumeric()): right = float(right)
|
|
82
|
+
elif(right.lower() == "false"): right = False
|
|
83
|
+
elif(right.lower() == "true"): right = True
|
|
84
|
+
|
|
85
|
+
parsed_ini[left] = right
|
|
86
|
+
return True
|
|
87
|
+
|
|
88
|
+
def get_value(name:str, default = None):
|
|
89
|
+
"""
|
|
90
|
+
Returns the value attached to the given name.
|
|
91
|
+
\nIf no setting is found, it will return the passed in default
|
|
92
|
+
Args:
|
|
93
|
+
name (str): The name of the setting
|
|
94
|
+
default (Any): What will be returned if nothing is found
|
|
95
|
+
Return:
|
|
96
|
+
Any: The value associated with the name
|
|
97
|
+
Example:
|
|
98
|
+
>>> get_value("do_debug_logs", False)
|
|
99
|
+
True
|
|
100
|
+
"""
|
|
101
|
+
return parsed_ini.get(name, default)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
quick_ini
|
quick_ini-1.0/setup.cfg
ADDED
quick_ini-1.0/setup.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="quick_ini",
|
|
5
|
+
version="1.0",
|
|
6
|
+
description="A library for reading from and writting to .ini formatted files",
|
|
7
|
+
long_description=open('README.md').read(),
|
|
8
|
+
long_description_content_type="text/markdown",
|
|
9
|
+
author="OllieZ-Mods",
|
|
10
|
+
author_email="socksinthewash@gmail.com",
|
|
11
|
+
url="",
|
|
12
|
+
packages=find_packages()
|
|
13
|
+
)
|