使用import从一个python脚本中提取变量值,并在另一个python脚本中使用它

时间:2014-12-23 09:36:49

标签: python import

我有一个python脚本“Parser_expected_xml.py”,它有一个可变的“技术”。我需要使用它的值来与另一个脚本“readingLogAndXmlGeneration.py”的xml标记内的值进行比较。

Parser_expected_xml.py的代码:

from xml.dom.minidom import parse
import subprocess
from subprocess import Popen
from os import pipe
import os
import sys 


dom=parse("C:/Users/dsoren/Documents/sample.xml")
technology=dom.getElementsByTagName("TechnologyDomain")
my_n_node = technology[0]
my_child = my_n_node.firstChild
tech = my_child.data
print tech  // it gives Mobile Access

代码

#!/usr/bin/python

import re
from sys import stdout
import datetime
from datetime import timedelta
import os.path
import sys
import xml.sax
import xml.etree.cElementTree as ET

############ Global Variable Declaration ##############

global Agent_Name
global jobId
global taskId
global AGENT_RESULT_CODE
global ERROR_CODE
global ERROR_MSG
global SESSION_ID
global RequestQueue
global ResponseQueue
global CORRELATION_ID
global SVC_RESULT
global SVC_RESPONSE
global BTS_SEGMENTS
global COMMAND_FAILURE
global txtErrorCode
global LOCATION_DOWN
global NSN2GServiceCallException
global ErrorCode
global Entering_Method
global Exiting_Method
global Executing_Method
global SESSION_ID_Ast_Log
global SESSION_ID_Ser_Log
global COMMAND_EXECUTED
global SERVICE_NAME
global Event_Value
global technology
global nef,alarm_index,parameter1,parameter2,parameter3
global Service,ServiceName,locationDownCheck,commandText
global command

### Gathering Logs from the Server and astro ########
'''
path="/opt/aor/regression/deepika"
pipe=subprocess.Popen(['perl','Automation_EA.pl',path],stdin=subprocess.PIPE)

ptr=open("/opt/aor/regression/time.txt",'r')
givenTime=ptr.read()

'''
######## Event Value Assignment ######################

Event_Value="E"


###### Reading the Log and Expected XML File###################

f_astro_log= open('C:/Users/dsoren/Desktop/End-End scenario(nokia e.g)/astro.log','r')
f_server_log=open('C:/Users/dsoren/Desktop/End-End scenario(nokia e.g)/service.log','r')

################root Element of XML


#



#################### Reading Astro Logs ###########################################################

def getTechnologyAndNef():

    for line in f_astro_log:
        tech=re.search(r'NETWORK=[\w+]+',line,re.I|re.M|re.S)

        if tech:

            tech=tech.group(0)
            tech=tech.split('=',2)
            technology=tech[1]
            TechnologyDomain=ET.SubElement(root,"TechnologyDomain")
            TechnologyDomain.text=technology   // this contains Mobile Access


        equipmentfamily=re.search(r'SYSTEM=[\w+]+',line,re.I|re.M|re.S)

        if equipmentfamily:

            equipmentfamily=equipmentfamily.group(0)
            equipmentfamily=equipmentfamily.split('=',2)
            nef=equipmentfamily[1]
            NormalizedEquipmentFamily=ET.SubElement(root,"NormalizedEquipmentFamily")
            NormalizedEquipmentFamily.text=nef    
    return

def getAlarmId():
    for line in f_server_log:

        alarm=re.search(r'T_ALARM_INDEX: [0-9]+',line,re.I|re.M|re.S)
        if alarm:

            alarm=alarm.group(0)
            alarm=alarm.split(':',2)
            alarm_index=alarm[1]
            AlarmID=ET.SubElement(Alarm,"AlarmID")
            AlarmID.text=alarm_index
    return

def getAlarmParameters():
    f_server_log.seek(0,0)
    for line in f_server_log:
        params=re.search(r'T_USER_INFO:[\w]+',line,re.I|re.M|re.S)
        if params:
            params=params.group()
            params=params.split(':',1)
            alarm_param=params[1]
    return

def getAlarmStatus():
    status='E'
    if status=='E':
        IfEntered=ET.SubElement(AlarmStatus,"IfEntered")
        IfEntered.text='True'

    if status=='F':
        IfFiltered=ET.SubElement(AlarmStatus,"IfFiltered")
        IfFiltered.text='True'

    if status=='I':
        IfInhibited=ET.SubElement(AlarmStatus,"IfInhibited")
        IfInhibited.text='True'

    return




def getAgentInfo():

    f_astro_log.seek(0,0)
    for line in f_astro_log:
        agentname=re.search(r'agent:[\w+]+',line,re.I|re.M|re.S)
        if agentname:

            #print line
            Agent_Name=agentname.group()
            Agent_Name=Agent_Name.split(':',1)
            Agent_Name=Agent_Name[1]
            #print Agent_Name
            AgentName=ET.SubElement(Agent,"AgentName")
            AgentName.text=Agent_Name
            #print Agent_Name

        locationdown=re.search(r'LOCATION_DOWN',line,re.I|re.M|re.S)
        if locationdown:
            #print line
            locationdown1=re.search(r'LOCATION_DOWN Ticket not Exists.',line,re.I|re.M|re.S)
            #print line 
            '''
            if 'Ticket not Exists' in line:   // tbd
                #print line
                locationDownCheck='FALSE'
            else:
                locationDownCheck='TRUE'

            print locationDownCheck
            IsTroubleTicketAlreadyExisting=ET.SubElement(Agent,"IsTroubleTicketAlreadyExisting")
            IsTroubleTicketAlreadyExisting.text=locationDownCheck
            '''

    return

def getServiceInfo():
    f_astro_log.seek(0,0)
    for line in f_astro_log:
        process_name=re.search(r'Process Name::[\w-]+,',line,re.I|re.M|re.S)
        if process_name:

            servicename=process_name.group()
            servicename=servicename.split('::',1)
            servicename=servicename[1]
            ServiceName=ET.SubElement(Service,"ServiceName")
            ServiceName.text=servicename
            Command=ET.SubElement(ServiceName,"Command")
            CommandText=ET.SubElement(Command,"CommandText")
            if servicename=='P_SVC_COMM_InitSession,':
                CommandText.text='null'               
            #else servicename=='P_SVC_COMM_CloseSession,':
                #print "2"
                #CommandText.text='null'                
        errorCode=re.search(r'ERROR_CODE=[0-9]+',line,re.I|re.M|re.S)
        if errorCode:
            ERROR_CODE=errorCode.group()
            ERROR_CODE=ERROR_CODE.split('=',1)
            ERROR_CODE=ERROR_CODE[1]
            #Writing Error_code into XML
            ## Error Code Details
            ser_err_code=ET.SubElement(Command,"ErrorCode")
            ser_err_code.text=ERROR_CODE

        svc_result=re.search(r'SVC_RESULT=[\w-]+',line,re.I|re.M|re.S)
        if svc_result:
            SVC_RESULT=svc_result.group()
            SVC_RESULT=SVC_RESULT.split('=',1)
            SVC_RESULT=SVC_RESULT[1]
            #Writing SVC_RESULT into XML
            svc_rslt=ET.SubElement(Command,"SvcResult")
            svc_rslt.text=SVC_RESULT

    f_server_log.seek(0,0)
    for line in f_server_log:
        command_line=re.search(r'Executing Command:',line,re.I|re.M|re.S)
        if command_line:
            print line
            command=re.search(r'Executing Command:[\w]+:[\d]+',line,re.I|re.M|re.S)
            if command:
                command=command.group()
                command=command.split(':',1)
                command=command[1]
                print command
    return 


root=ET.Element("AlarmSequence")
getTechnologyAndNef()

Alarm=ET.SubElement(root,"Alarm")
getAlarmId()
AlarmNature=ET.SubElement(Alarm,"AlarmNature")

AlarmParameters=ET.SubElement(Alarm,"AlarmParameters")
AlarmParameter1=ET.SubElement(AlarmParameters,"AlarmParameter1")
#AlarmParameter1.text=parameter1
AlarmParameter2=ET.SubElement(AlarmParameters,"AlarmParameter2")
AlarmParameter3=ET.SubElement(AlarmParameters,"AlarmParameter3")

ExpectedResult=ET.SubElement(Alarm,"ExpectedResult")
AlarmStatus=ET.SubElement(ExpectedResult,"AlarmStatus")


IfRetained=ET.SubElement(AlarmStatus,"IfRetained")
IfDuplicate=ET.SubElement(AlarmStatus,"IfDuplicate")
ToBeTreated=ET.SubElement(AlarmStatus,"ToBeTreated")
TicketToBeCreated=ET.SubElement(AlarmStatus,"TicketToBeCreated")
getAlarmStatus()

Agent=ET.SubElement(ExpectedResult,"Agent")
getAgentInfo()
ActuationSequence=ET.SubElement(Agent,"ActuationSequence")
Service=ET.SubElement(ActuationSequence,"Service")
getServiceInfo()

tree = ET.ElementTree(root)
tree.write('C:/Users/dsoren/Documents/sample.xml')

我有另一个脚本调用这两个脚本并比较这样的值。

compare.py的代码

import sys
sys.path.append("C:/Users/dsoren/Documents")
import readingLogAndXmlGeneration 
import Parser_expected_xml

if readingLogAndXmlGeneration.TechnologyDomain == Parser_expected_xml.tech:
   print "Technology domain matched"

当我运行compare.py代码时,我收到此错误:

Traceback (most recent call last):
  File "C:\Users\dsoren\Documents\Alarm Scenarios\demo\work\ComparisonScript.py", line 22, in <module>
    if readingLogAndXmlGeneration.TechnologyDomain == Parser_expected_xml.tech:
**AttributeError: 'module' object has no attribute 'TechnologyDomain'**

我认为我做错了但无法得到它。

0 个答案:

没有答案