
das testen kommt noch. ich bin gerade am das paket neu packen.

und hättest du noch eine idee für eine neue funktion?
Code: Alles auswählen
STATUS_FILENAME = "/var/lib/dpkg/status"
def iter_packages():
"""The main function of module."""
info = []
with open(STATUS_FILENAME) as infofile:
for line in infofile:
if line.startswith(' '):
# continue content
info[-1][-1] += '\n' + line[1:].rstrip()
elif not line.strip():
# end of block
if info:
yield dict(info)
info = []
else:
key, content = line.split(":", 1)
info.append([key, content.strip()])
if info:
yield dict(info)
def package_exists(package_name):
"""This function checks if package exists."""
return any(info["Package"] == package_name
for info in iter_packages())
def get_package(package_name):
"""Returns all values of a package in a dictionary."""
for info in iter_packages():
if info["Package"] == package_name:
return info
raise KeyError("The package does not exist")
def list_package_names():
"""Returns all package names."""
return [
info["Package"]
for info in iter_packages()
]
def count_packages():
"""Counts all installed packages."""
return sum(1 for info in iter_packages())
Code: Alles auswählen
"""
The module provides information about installed debian packages.
The module can only be executed if the operating system supports debian packages!
"""
STATUS_FILENAME = "/var/lib/dpkg/status"
__author__ = "Ben Fässler <faesslerben@gmail.com>"
__license__ = "MIT"
__all__ = ["iter_packages (not for user)",
"package_exists",
"get_package",
"list_package_names",
"count_packages"
]
__version__ = "0.0.1"
import os
if not os.path.exists(STATUS_FILENAME):
raise FileNotFoundError("The information file was not found! Without this file, the module can not be executed!")
del os
def iter_packages():
"""The main function of module."""
info = []
with open(STATUS_FILENAME) as infofile:
for line in infofile:
if line.startswith(' '):
# continue content
info[-1][-1] += '\n' + line[1:].rstrip()
elif not line.strip():
# end of block
if info:
yield dict(info)
info = []
else:
key, content = line.split(":", 1)
info.append([key, content.strip()])
if info:
yield dict(info)
def package_exists(package_name):
"""This function checks if package exists.
Arguments:
package_name - The name of the package
"""
return any(info["Package"] == package_name
for info in iter_packages())
def get_package(package_name):
"""Returns all values of a package in a dictionary.
Arguments:
package_name - The name of the package
"""
for info in iter_packages():
if info["Package"] == package_name:
return info
raise KeyError("The package does not exist")
def list_package_names():
"""Returns all package names."""
return [
info["Package"]
for info in iter_packages()
]
def count_packages():
"""Counts all installed packages."""
return sum(1 for info in iter_packages())
Code: Alles auswählen
Auf Modulebene sollte so wenig wie möglich passieren
Code: Alles auswählen
if not os.path.exists(STATUS_FILENAME):
raise FileNotFoundError("The information file was not found! Without this file, the module can not be executed!")
else:
if not os.access(STATUS_FILENAME, os.R_OK):
raise PermissionError("No reading rights for the information file! Without this file, the module can not be executed!")
Code: Alles auswählen
if __name__ == "__main__":
Code: Alles auswählen
def print_documentation():
for i, a in zip([iter_packages, package_exists, get_package_values, list_package_names, count_packages], __all__):
print(a)
print("____________________")
print(i.__doc__ + "\n")
Code: Alles auswählen
iter_packages (not for user)
____________________
The main function of module.
package_exists
____________________
This function checks if package exists.
Arguments:
package_name - The name of the package
The function returns True or False.
get_package_values
____________________
Returns all values of a package in a dictionary.
Arguments:
package_name - The name of the package
list_package_names
____________________
Returns all names of installed packages.
The function returns the names in a list.
count_packages
____________________
Counts all installed packages.
The function returns the number as an integer.
Code: Alles auswählen
def package_created_paths(package_name):
"""Returns files and folders created by the package as a path.
Arguments:
package_name - The name of the package
The function returns the paths in a list.
"""
paths = []
filename = package_name + ".md5sums"
with open(pathlib.PurePath("/var/lib/dpkg/info", filename)) as infofile:
for info in infofile:
paths.append(info.split(" ")[1].strip())
return paths
Code: Alles auswählen
Die Einrückung vom Docstring stimmt nicht
Code: Alles auswählen
Wobei Dein Stil noch geringfügig abweicht. Ich weiss nicht ob das '-' statt des ':' bei den Argumenten erkannt wird.