This forum is in archive mode. You will not be able to post new content.

Author Topic: [Python] Project P.I.N.N. - Module : OSEnv.py  (Read 440 times)

0 Members and 1 Guest are viewing this topic.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
[Python] Project P.I.N.N. - Module : OSEnv.py
« on: August 29, 2014, 03:22:53 PM »
I decided to share some modules that I am using in P.I.N.N.

OSEnv.py is a part of the package infogather what it does is, it collects information about the OS environment it is currently running on and then writes the data into an XML file named - osenv_data.xml.


So here's the Source :-

Code: (python) [Select]
"""
Created on Aug 27, 2014

@author: psychocoder
"""

import os
from xml.etree.ElementTree import Element, Comment, SubElement

from pinn.Constants import Constants
from pinn.FunctionUtil import FuncUtil


__author__ = "Animesh Shaw"
__copyright__ = "Copyright (C) 2014 Animesh Shaw"
__license__ = "Apache License"
__version__ = "1.0"


class OSEnv():

    """
    Class that gather information aboout the OS and its environment
    and writes them to an XML file
    """

    def __init__(self):
        """
        Initialize the constructor with necessary values.
        """

        self.__cons = Constants()

    def getOSEnvData(self):
        """

        @return: The os environment variables
        @rtype: collections.abc.ItemsView
        """

        return os.environ.items()

    def writeOSEnvDataToXML(self):
        """
        Methods that writes the OS Environment data into an XML file.
        """

        funcutil = FuncUtil()

        root = Element("osenv")
        root.append(
            Comment("Current Operating System Environment Data and variables"))

        for item in self.getOSEnvData():
            child = SubElement(root, item[0])
            child.text = item[1]

        with open(self.__cons.OSDATA_LOC + "/osenv_data.xml", mode="w") as f:
            f.writelines(str(funcutil.prettifyXML(root)))
            f.close()


def main():
    ins = OSEnv()
    ins.writeOSEnvDataToXML()


if __name__ == "__main__":
    main()


In the code above you will find some class instances like :- Constants or FuncUtil

Constants class is a class that contains data like path or keywords etc. as a constant which are used always. FuncUtil is a class that contains methods are that are always used. To avoid redundancy I punched them there. But basically that it. If you want to run the code then just edit the part where we open a file and give the location where you want to save. the prettifyXML code is given below.

Code: (python) [Select]
def prettifyXML(self, rootElement):
        """
        Prettifies the XML by parsing them again.
        """       
        dataString = ElementTree.tostring(rootElement, "UTF-8")
        parsedStr = minidom.parseString(dataString)
       
        return parsedStr.toprettyxml(indent="\t")


Final Output :-




The prettify method I got from here : http://pymotw.com/2/xml/etree/ElementTree/create.html#pretty-printing-xml
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.