-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: dump functions are separate files now
- Loading branch information
1 parent
9dc5a3b
commit bd74915
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |