2014-03-31 3 views
0

CSV의 업로드를 처리하는 모듈이 있습니다. 모듈 내에 twzworkscsvreader.py라는 파일이 있습니다.이 파일에는 업로드 된 파일이 있는지 확인하는 클래스와 함수가 들어 있습니다. csv 파일 (정의 된) 헤더를보고하여 파일을 성공적으로 업로드하지 않은 경우 오류가 throw됩니다. 현재, 나는 콘솔에서 오류를 볼 수 있습니다. 이 오류를 콘솔에 인쇄하는 대신 브라우저의 페이지에 반환하는 기능을 내보기에 추가하고 싶습니다. 여기에이 오류장고보기 내에서 다른 모듈에 정의 된 클래스 및 메서드 사용

import logging 
import csv 
import time 
import warnings 
import datetime 
import dateutil.parser as dparser 
import os 

class CSVFileReader: 
    def __init__(self, filename, evidence, mysql, filetype): 
     """ Class constructor. 
     Args: 
     - filename: csv file name to process 
     - evidence: evidence number supplied from the command line 
     - mysql: instance of the mySQL database 
     - filetype: type of the file to process 
     """ 
     self.errorCount = 0 
     self.path = filename 
     self.evidence = evidence 
     self.mysql = mysql 
     self.dictInstance = {} 
     self.filetype = filetype 

     #TO DO: move this outside maybe back into function. I dont think this is right place this this. 
     self.artifacts = ['AuditPolicy','App Paths','exefile open\command','cmdfile open\command','batfile open\command','htafile open\command','piffile open\command',\ 
          'http open\command','browsers','Run keys','NetworkCards','Browser Helper Objects'] 

    def is_arbitrary_text(self,row): 
     """ 
     Check if an TZworks CSV header is present 
     Args: 
      row: the row as it was read from the csv 
     Returns: 
      Boolean: True or False 
     """ 
     ##TO DO CLEAN UP HOW THIS IS FORMATTED BELOW. MAYBE PULL ALL THIS INTO A SEPERATE FILE. 
     if self.filetype == "jmp" and str(row) == "['source path/filename', 'source type  ', 'appid   ', 'MRU/MFU', 'stream#', 'MRU date ', 'MRU-UTC  ', 'file mdate ', 'mtime-UTC ', 'file adate ', 'atime-UTC ', 'file cdate ', 'ctime-UTC ', 'tgt mdate ', 'mtime-UTC ', 'tgt adate ', 'atime-UTC] ', 'tgt cdate ', 'ctime-UTC ', 'ObjID cdate ', 'ctime-UTC ', 'tgt attrib', 'inode  ', 'seq# ', 'file size ', 'target name             ', 'idlist extra info           ', 'vol type', 'vol serial', 'vol label', 'local path', 'common path', 'network/device info', 'extra info', 'netbios name', 'volume id', 'object id', 'mac addr']": 
       return True 

     elif self.filetype == "usb" and str(row) == "['device name', 'vid/pid', ' time-utc', 'install', ' time-local', \ 
'disk dev', ' time-utc', 'vol dev', ' time-utc', 'type', 'vid', 'pid', 'hub', 'port', \ 
'vendor', 'product', 'rev', 'volume guid', 'vol', 'vol name', 'users [ date/time - utc]', \ 
'instance id/serial #', 'Other dates defined by explicit property keys', 'Readyboost (freeform list of EMDMgmt entries) vol serial#/vol name/last modify regtimes [utc] and * = test time']": 
      return True 

     elif self.filetype == "link" and str(row) == "['source path/filename', 'source type', 'file mdate', ' time-UTC', 'file adate', ' time-UTC', \ 
'file cdate', 'ctime-UTC', 'tgt mdate', ' time-UTC', 'tgt adate', ' time-UTC]', 'tgt cdate', ' time-UTC', 'ObjID cdate', ' ctime-UTC', 'tgt attrib', \ 
'inode', 'seq#', 'file size', 'target name', 'idlist extra info', 'vol type', 'vol serial', 'vol label', 'local path', 'common path', 'network/device info', \ 
'extra info', 'netbios name', 'volume id', 'object id', 'mac addr']": 
      return True 

     elif self.filetype == "prefetch" and str(row) == "['prefetch file name     ',' app name    ',' times ran',' last run ', \ 
' time-utc ',' mdate  ',' time-utc',' adate  ',' time-utc',' cdate  ',' time-utc',' path/appname  ',' Num Vols',' volume(s)', \ 
' volume serial(s)',' volume date/time(s) (utc)',' Num modules',' Module list']": 
      return True 

     elif self.filetype == "sbag" and str(row) == "['regdate', ' reg-UTC', 'mru', 'mdate', ' time-UTC', 'adate', ' time-UTC', 'cdate', ' time-UTC', \ 
'type', 'bag', 'file size', 'inode', 'seq#', 'full path', 'source subkey/value name', 'user acct', 'extra metadata', 'bag registry dates related to this entry']": 
      return True 

     elif self.filetype == "AuditPolicy" and row == "regdate, reg-UTC,policy name,type audit": 
      return True 

     elif self.filetype == "App Paths" and row == "reg date, reg-UTC,subkey,value name,value data": 
      return True 

     elif self.filetype == "exefile open\command" and row == "reg date, reg-UTC,value name,value data": 
      return True 

     elif self.filetype == "cmdfile open\command" and row == "reg date, reg-UTC,value name,value data": 
      return True 

     elif self.filetype == "batfile open\command" and row == "reg date, reg-UTC,value name,value data": 
      return True 

     elif self.filetype == "htafile open\command" and row == "reg date, reg-UTC,value name,value data": 
      return True 

     elif self.filetype == "piffile open\command" and row == "reg date, reg-UTC,value name,value data": 
      return True 

     elif self.filetype == "http open\command" and row == "reg date, reg-UTC,value name,value data": 
      return True 

     elif self.filetype == "browsers" and row == "reg date, reg-UTC,subkey,value name,value data": 
      return True 

     elif self.filetype == "Run keys" and row == "reg date, reg-UTC,value name,value data": 
      return True 

     elif self.filetype == "NetworkCards" and row == "reg date, reg-UTC,subkey,value name,value data": 
      return True 

     elif self.filetype == "Browser Helper Objects" and row == "reg date, reg-UTC,subkey,value name,value data": 
      return True 

     else: 
      return False 

    #TODO: Move this to its own file 
    def StringToDatetime(self, datetime, dt_type): 
     """Converts a string timestamp into a datetime object. 

     Args: 
      datetime: A string formatted as a timestamp (i.e 1970-01-01 00:00:00.000). 
      dt_type: which datetime field we are parsing. Used for logging purposes 

     Returns: 
      A datetime object or None if the parsing fails.""" 
     # Here some dates have space (like "install" and some are just empty (like "userDateTime) 
     # so we need to check for both conditions 
     if datetime == " " or datetime == "" or datetime == "       ": 
      self.errorCount += 1 
      #TO DO: Use %s 
      message = dt_type + " is missing the date at line " 
      message += str(self.rowcount) 
      logging.warn(message) 
      return None 
     else: 
      try: 
       datetimeobject = dparser.parse(str(datetime)) 
       return datetimeobject 
      except: 
       self.errorCount += 1 
       #TO DO: Use %s 
       message = dt_type + " has incorrect date at line " 
       message += str(self.rowcount) 
       logging.error(message) 
       return None 

    def readyBoostParse(self, string): 
     """Parse the last field of the csv file 
      Args: 
       string: string representation of the last field in the csv 
      Returns: 
       readyboostDict: dictionary of the items in the readyboost field 

     """ 
     #Readyboost (freeform list of EMDMgmt entries) vol serial#/vol name/last modify regtimes [utc] and * = test time 

     readyboostDict = {} 
     readyboost_items = string.split(';') 
     item_counter = 1 
     for item in readyboost_items: 
      if len(readyboost_items) > 5: 
       self.errorCount += 1 
       logging.error("Too many readyboost items at line " + str(self.rowcount)) 
      try: 
       vol_sn_and_vol_name , _, last_mod_regdatetime = item.partition("[") 
       vol_sn, _, vol_name = vol_sn_and_vol_name.partition("/") 
       readyboostDict['volume_serial' + str(item_counter)] = vol_sn.replace('-', '') 
       readyboostDict['volume_name' + str(item_counter)] = vol_name 
       readyboostDict['last_modify' + str(item_counter)] = self.StringToDatetime(last_mod_regdatetime.strip("]"), "readyboost") 
       item_counter += 1 

      except Exception as e: 
       self.errorCount += 1 
       #TO DO add better error handeling and logging. 
       readyboostDict['volume_serial' + str(item_counter)] = "" 
       readyboostDict['volume_name' + str(item_counter)] = "" 
       readyboostDict['last_modify' + str(item_counter)] = None 
       item_counter += 1 

     return readyboostDict 

    def readAdditionalData(self): 
     """ Read the data from the additional CSV file 
      Args: 
       None 
      Return: 
       None 
     """ 
     in_header = False 
     headerPassed = False 
     internalHeader = False 
     #Why not use the other readData? 
     with open(self.path, "rb") as csvfile: 
      for line in csvfile: 
       line = line.rstrip('\r\n') 
       if not line: 
        continue 
       if internalHeader and headerPassed: 
        if line != "--------------------------------------------------------------------------------------------------------- ":# and not in_header: 
         if self.filetype == "AuditPolicy": 
          self.processAuditPolicyData(line) 
         if self.filetype == "App Paths": 
          self.processAppPathData(line) 
         if self.filetype == "exefile open\command": 
          self.processExeFileOpenCommand(line) 
         if self.filetype == "cmdfile open\command": 
          self.processCmdFileOpenCommand(line) 
         if self.filetype == "batfile open\command": 
          self.processBatFileOpenCommand(line) 
         if self.filetype == "htafile open\command": 
          self.processHtaFileOpenCommand(line) 
         if self.filetype == "piffile open\command": 
          self.processPifFileOpenCommand(line) 
         if self.filetype == "http open\command": 
          self.processHttpOpenCommand(line) 
         if self.filetype == "browsers": 
          self.processBrowsers(line) 
         if self.filetype == "Run keys": 
          self.processRunKeys(line) 
         if self.filetype == "NetworkCards": 
          self.processNetworkCards(line) 
         if self.filetype == "Browser Helper Objects": 
          self.processBrowserHelperObject(line) 
        else: 
         internalHeader = False 
         headerPassed = False 
         self.filetype = "" 
       if line == "--------------------------------------------------------------------------------------------------------- ":# and not in_header: 
        if not in_header: 
         in_header = True 
        else: 
         in_header = False 
         headerPassed = True 
        continue 
       #if line == "--------------------------------------------------------------------------------------------------------- " and in_header: 
        #in_header = False 
        #headerPassed = True 
        #continue 
       if in_header: 
        (key,value) = line.split(':') 
        value = value.strip() 
        if key == "Artifact": 
         if value not in self.artifacts: 
          print "Unknown artifact found." 
          logging.error("Unknown artifact found in file: {0:s}".format(self.path)) 
          return 
         else: 
          self.filetype = value 
          continue 
        else: 
         continue 
       elif headerPassed and not internalHeader: 
        if not self.is_arbitrary_text(line): 
         return False 
        else: 
         internalHeader = True 

    def readData(self): 
     """ Read the data from the CSV file. 
      Args: 
       None 
      Return: 
       None 
     """ 
     try: 
      import codecs 
      with open(self.path, "rU") as data_initial: 
       reader = csv.reader((line.replace('\0','') for line in data_initial), delimiter=",") 

       ##oldcode 
       #data_initial = open("staff.csv", "rU") 
       #reader = csv.reader((line.replace('\0','') for line in csvfile), delimiter=",") 
       #reader = csv.reader(csvfile, delimiter=',', quotechar='"') 


       #Row number where header exists 
       rangecount = 0 
       #Check if more then one header exists in file. 
       header = False 
       #Count number of rows. Need to test to see what happens if error on one row. 
       self.rowcount = 1 

       for row in reader: 
        #Check if expected header is in place and if there are no other headers in file. 
        if self.is_arbitrary_text(row) == True and header == False: 
         for _ in range(rangecount): 
          next(reader, None) 
          self.is_arbitrary_text(reader) 
         reader = csv.reader((line.replace('\0','') for line in data_initial), delimiter=",") 
         #Do processing for each row 
         for row in reader: 
          if self.filetype == "usb": 
           self.processUSBData(row) 
          if self.filetype == "lnk": 
           self.processLNKData(row) 
          if self.filetype == "prefetch": 
           self.processPreFetchData(row) 
          if self.filetype == "sbag": 
           self.processSBagData(row) 
          if self.filetype == "jmp": 
           self.processJmpData(row) 
          self.rowcount += 1 
         header = True 
        else: 
         rangecount +=1 

       if header == False: 
        logging.error("Did not find expected header in CSV file: " + self.path) 
        print "Did not find expected header in CSV file %s." % (self.path) 


     except IOError as e: 
      print "Error parsing file: %s". e.args[1] 

사람이 나에게 내보기 내에서이 클래스를 사용하는 프로토 타입을 쓸 수를 처리하는 twzworkscsvreader.py 부분이 너무이 페이지에 오류가 표시되는지? 당신이 장고를 사용하는 경우 클래스는 내가 mysq_db_loader 및

답변

관련 문제