def getXMLTree(cls, filename, verbose): """ Opens a config file for reading. Returns XML Elementree object representing XML Config file. Argument(s): No arguments are required. Return value(s): ElementTree Restrictions: File must be named sites.xml and must be in same directory as caller. This Method is tagged as a Class Method """ if SitesFile.fileExists(filename): try: with open(filename) as f: sitetree = ElementTree() sitetree.parse(f) return sitetree except: SiteDetailOutput.PrintStandardOutput( 'There was an error reading from the {xmlfile} input file.\n' 'Please check that the {xmlfile} file is present and correctly ' 'formatted.'.format(xmlfile=filename), verbose=verbose) else: SiteDetailOutput.PrintStandardOutput( 'No local {xmlfile} file present.'.format(xmlfile=filename), verbose=verbose) return None
def TargetList(self, filename, verbose): """ Opens a file for reading. Returns each string from each line of a single or multi-line file. Argument(s): filename -- string based name of the file that will be retrieved and parsed. verbose -- boolean value representing whether output will be printed to stdout Return value(s): Iterator of string(s) found in a single or multi-line file. Restriction(s): This Method is tagged as a Class Method """ try: target = '' with open(filename) as f: li = f.readlines() for i in li: target = str(i).strip() yield target except IOError: SiteDetailOutput.PrintStandardOutput( 'There was an error reading from the target input file.', verbose=verbose)
def updateTekDefenseXMLTree(cls, prox, verbose): if prox: proxy = {'https': prox, 'http': prox} else: proxy = None remotemd5 = None localmd5 = None localfileexists = False try: localmd5 = SitesFile.getMD5OfLocalFile(__TEKDEFENSEXML__) localfileexists = True except IOError: SiteDetailOutput.PrintStandardOutput( 'Local file {xmlfile} not located. Attempting download.'. format(xmlfile=__TEKDEFENSEXML__), verbose=verbose) try: if localfileexists: remotemd5 = SitesFile.getMD5OfRemoteFile( __REMOTE_TEKD_XML_LOCATION__, proxy=proxy) if remotemd5 and remotemd5 != localmd5: SiteDetailOutput.PrintStandardOutput( 'There is an updated remote {xmlfile} file at {url}. ' 'Attempting download.'.format( url=__REMOTE_TEKD_XML_LOCATION__, xmlfile=__TEKDEFENSEXML__), verbose=verbose) SitesFile.getRemoteFile(__REMOTE_TEKD_XML_LOCATION__, proxy) else: SitesFile.getRemoteFile(__REMOTE_TEKD_XML_LOCATION__, proxy) except ConnectionError as ce: try: SiteDetailOutput.PrintStandardOutput( 'Cannot connect to {url}. Server response is {resp} Server error ' 'code is {code}'.format(url=__REMOTE_TEKD_XML_LOCATION__, resp=ce.message[0], code=ce.message[1][0]), verbose=verbose) except: SiteDetailOutput.PrintStandardOutput( 'Cannot connect to {url} to retreive the {xmlfile} for use.' .format(url=__REMOTE_TEKD_XML_LOCATION__, xmlfile=__TEKDEFENSEXML__), verbose=verbose) except HTTPError as he: try: SiteDetailOutput.PrintStandardOutput( 'Cannot connect to {url}. Server response is {resp}.'. format(url=__REMOTE_TEKD_XML_LOCATION__, resp=he.message), verbose=verbose) except: SiteDetailOutput.PrintStandardOutput( 'Cannot connect to {url} to retreive the {xmlfile} for use.' .format(url=__REMOTE_TEKD_XML_LOCATION__, xmlfile=__TEKDEFENSEXML__), verbose=verbose)