Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Switches configs parser
- """
- import glob
- import errno
- import os
- path = os.getcwd()
- files = glob.glob(path+"\sw\*")
- allFiles = dict() # read ports status
- switchNumbers = ["17","18","20","21","22","23","24","25","27","28","29","31","32"]
- switches = dict.fromkeys(switchNumbers,[]) # dict -> key: switch name, value: list with tuples (port, status)
- for name in files: # 'file' is a builtin type, 'name' is a less-ambiguous variable name.
- try:
- with open(name) as f: # No need to specify 'r': this is the default.
- allFiles[name[name.find("sw")+3:name.find(".txt")]] = f.readlines()
- except IOError as exc:
- if exc.errno != errno.EISDIR: # Do not fail if a directory is found, just ignore it.
- raise # Propagate other kinds of IOError.
- for fileName,content in allFiles.items():
- #print(fileName)
- switches[fileName[0:3]] = [(fileName[0], line[:7].strip(), "connected" in line)
- for line in content if line[0] in ["F","G","T"]]
- for switch in switchNumbers:
- print("\n\nSwitch: " + switch + "\n" + "---------------------------")
- for row in range(0,len(switches["1"+switch])):
- print("Port: {0} | status: {1} | no changes: {2}".format(switches["1"+switch][row][1],
- switches["1"+switch][row][2],
- all(switches["1"+switch][row][2] == switches[str(i)+switch][row][2] for i in range(2,6))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement