Skip to content

Commit

Permalink
new: dump functions are separate files now
Browse files Browse the repository at this point in the history
  • Loading branch information
kernel-dev committed Mar 26, 2022
1 parent 9dc5a3b commit bd74915
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/util/dump_functions/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import json
from src.managers.devicemanager import DeviceManager
from src.error.logger import Logger

def dump_json(dm, dir, logger):
data = None

if not isinstance(dm, DeviceManager):
raise TypeError("Parameter 'dm' is not of type 'DeviceManager'!")

if not os.path.isdir(dir):
raise ValueError("Parameter 'dir' is not a valid directory!")

if not isinstance(logger, Logger):
raise TypeError("Parameter 'logger' is not of type 'Logger'!")

try:
with open(os.path.join(dir, "info_dump.json"), "w") as _json:
_json.write(json.dumps(
dm.info, indent=4, sort_keys=False))
_json.close()
logger.info(
f'Successfully dumped "info_dump.json" into "{dir}"', __file__
)

data = f'Successfully dumped "info_dump.json" into "{dir}"\n'
except Exception as e:
logger.error(
f"Failed to dump to JSON!\n\t^^^^^^^^^{str(e)}", __file__)

return data
32 changes: 32 additions & 0 deletions src/util/dump_functions/plist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import plistlib
from src.managers.devicemanager import DeviceManager
from src.error.logger import Logger

def dump_plist(dm, dir, logger):
data = None

if not isinstance(dm, DeviceManager):
raise TypeError("Parameter 'dm' is not of type 'DeviceManager'!")

if not os.path.isdir(dir):
raise ValueError("Parameter 'dir' is not a valid directory!")

if not isinstance(logger, Logger):
raise TypeError("Parameter 'logger' is not of type 'Logger'!")

try:
with open(os.path.join(dir, "info_dump.plist"), "wb") as plist:
plistlib.dump(dm.info, plist, sort_keys=False)
plist.close()
logger.info(
f'Successfully dumped info "info_dump.plist" into "{dir}"', __file__
)

data = f'Successfully dumped info "info_dump.plist" into "{dir}"\n'
except Exception as e:
logger.error(
f"Failed to dump to Plist!\n\t^^^^^^^^^{str(e)}", __file__
)

return data
36 changes: 36 additions & 0 deletions src/util/dump_functions/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
from src.managers.tree import tree
from src.managers.devicemanager import DeviceManager
from src.error.logger import Logger

def dump_txt(dm, dir, logger):
data = None

if not isinstance(dm, DeviceManager):
raise TypeError("Parameter 'dm' is not of type 'DeviceManager'!")

if not os.path.isdir(dir):
raise ValueError("Parameter 'dir' is not a valid directory!")

if not isinstance(logger, Logger):
raise TypeError("Parameter 'logger' is not of type 'Logger'!")

try:
with open(
os.path.join(dir, "info_dump.txt"), "w", encoding="utf-8"
) as file:
for key in dm.info:
file.write(tree(key, dm.info[key], color=False))
file.write("\n")

file.close()
logger.info(
f'Successfully dumped "info_dump.txt" into "{dir}"', __file__
)

data = f'Successfully dumped "info_dump.txt" into "{dir}"\n'
except Exception as e:
logger.error(
f"Failed to dump to TXT!\n\t^^^^^^^^^{str(e)}", __file__)

return data
34 changes: 34 additions & 0 deletions src/util/dump_functions/xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import dicttoxml
import logging
from src.managers.devicemanager import DeviceManager
from src.error.logger import Logger

def dump_xml(dm, dir, logger):
data = None

if not isinstance(dm, DeviceManager):
raise TypeError("Parameter 'dm' is not of type 'DeviceManager'!")

if not os.path.isdir(dir):
raise ValueError("Parameter 'dir' is not a valid directory!")

if not isinstance(logger, Logger):
raise TypeError("Parameter 'logger' is not of type 'Logger'!")

try:
with open(os.path.join(dir, "info_dump.xml"), "wb") as xml:
# Disables debug prints from `dicttoxml`
dicttoxml.LOG.setLevel(logging.ERROR)
xml.write(dicttoxml.dicttoxml(dm.info, root=True))
xml.close()
logger.info(
f'Successfully dumped "info_dump.xml" into "{dir}"', __file__
)

data = f'Successfully dumped "info_dump.xml" into "{dir}"\n'
except Exception as e:
logger.error(
f"Failed to dump to XML!\n\t^^^^^^^^^{str(e)}", __file__)

return data

0 comments on commit bd74915

Please sign in to comment.