Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 24,25c24,25
- < def get_listing(conn, options, listing_command):
- < listing = ""
- ---
- > def get_power_status(conn, options):
- > listing = ""
- 27,28c27,28
- < try:
- < conn.send(listing_command+"\r\n")
- ---
- > try:
- > conn.send("/S"+"\r\n")
- 30,149c30,42
- < if isinstance(options["-c"], list):
- < re_all = list(options["-c"])
- < else:
- < re_all = [options["-c"]]
- < re_next = re.compile("Enter: ", re.IGNORECASE)
- < re_all.append(re_next)
- <
- < result = conn.log_expect(options, re_all, int(options["-Y"]))
- < listing = conn.before
- < if result == (len(re_all) - 1):
- < conn.send("\r\n")
- < conn.log_expect(options, options["-c"], int(options["-Y"]))
- < listing += conn.before
- <
- < except pexpect.EOF:
- < fail(EC_CONNECTION_LOST)
- < except pexpect.TIMEOUT:
- < fail(EC_TIMED_OUT)
- <
- < return listing
- <
- < def get_plug_status(conn, options):
- < listing = get_listing(conn, options, "/S")
- <
- < plug_section = 0
- < outlets = {}
- < for line in listing.splitlines():
- < if (plug_section == 2) and line.find("|") >= 0 and line.startswith("PLUG") == False:
- < plug_line = [x.strip().lower() for x in line.split("|")]
- < if len(plug_line) < len(plug_header):
- < plug_section = -1
- < pass
- < if ["list", "monitor"].count(options["-o"]) == 0 and options["-n"].lower() == plug_line[plug_index]:
- < return plug_line[status_index]
- < else:
- < ## We already believe that first column contains plug number
- < if len(plug_line[0]) != 0:
- < outlets[plug_line[0]] = (plug_line[name_index], plug_line[status_index])
- < elif (plug_section == 1):
- < plug_section = 2
- < pass
- < elif (line.upper().startswith("PLUG")):
- < plug_section = 1
- < plug_header = [x.strip().lower() for x in line.split("|")]
- < plug_index = plug_header.index("plug")
- < name_index = plug_header.index("name")
- < status_index = plug_header.index("status")
- <
- < if ["list", "monitor"].count(options["-o"]) == 1:
- < return outlets
- < else:
- < return "PROBLEM"
- <
- < def get_plug_group_status_from_list(status_list):
- < for status in status_list:
- < if status == "on":
- < return status
- < return "off"
- <
- < def get_plug_group_status(conn, options):
- < listing = get_listing(conn, options, "/SG")
- <
- < plug_section = 0
- < outlets = {}
- < current_outlet = ""
- < line_index = 0
- < lines = listing.splitlines()
- < while line_index < len(lines) and line_index >= 0:
- < line = lines[line_index]
- < if (line.find("|") >= 0 and line.lstrip().startswith("GROUP NAME") == False):
- < plug_line = [x.strip().lower() for x in line.split("|")]
- < if ["list", "monitor"].count(options["-o"]) == 0 and options["-n"].lower() == plug_line[name_index]:
- < line_index += 1
- < plug_status = []
- < while line_index < len(lines) and line_index >= 0:
- < plug_line = [x.strip().lower() for x in lines[line_index].split("|")]
- < if len(plug_line[plug_index]) > 0 and len(plug_line[name_index]) == 0:
- < plug_status.append(plug_line[status_index])
- < line_index += 1
- < else:
- < line_index = -1
- <
- < return get_plug_group_status_from_list(plug_status)
- <
- < else:
- < ## We already believe that first column contains plug number
- < if len(plug_line[0]) != 0:
- < group_name = plug_line[0]
- < plug_line_index = line_index + 1
- < plug_status = []
- < while plug_line_index < len(lines) and plug_line_index >= 0:
- < plug_line = [x.strip().lower() for x in lines[plug_line_index].split("|")]
- < if len(plug_line[name_index]) > 0:
- < plug_line_index = -1
- < break
- < if len(plug_line[plug_index]) > 0:
- < plug_status.append(plug_line[status_index])
- < plug_line_index += 1
- < else:
- < plug_line_index = -1
- < outlets[group_name] = (group_name, get_plug_group_status_from_list(plug_status))
- < line_index += 1
- <
- < elif (line.upper().lstrip().startswith("GROUP NAME")):
- < plug_header = [x.strip().lower() for x in line.split("|")]
- < name_index = plug_header.index("group name")
- < plug_index = plug_header.index("plug")
- < status_index = plug_header.index("status")
- < line_index += 2
- < else:
- < line_index += 1
- <
- <
- < if ["list", "monitor"].count(options["-o"]) == 1:
- < for group, status in outlet_groups:
- < outlets[group] = (group, status[0])
- <
- < return outlets
- < else:
- < return "PROBLEM"
- ---
- > if isinstance(options["-c"], list):
- > re_all = list(options["-c"])
- > else:
- > re_all = [options["-c"]]
- > re_next = re.compile("Enter: ", re.IGNORECASE)
- > re_all.append(re_next)
- >
- > result = conn.log_expect(options, re_all, int(options["-Y"]))
- > listing = conn.before
- > if result == (len(re_all) - 1):
- > conn.send("\r\n")
- > conn.log_expect(options, options["-c"], int(options["-Y"]))
- > listing += conn.before
- 151,155c44,71
- < def get_power_status(conn, options):
- < ret = get_plug_status(conn, options)
- <
- < if ret == "PROBLEM":
- < ret = get_plug_group_status(conn, options)
- ---
- > except pexpect.EOF:
- > fail(EC_CONNECTION_LOST)
- > except pexpect.TIMEOUT:
- > fail(EC_TIMED_OUT)
- >
- > plug_section = 0
- > outlets = {}
- > for line in listing.splitlines():
- > if (plug_section == 2) and line.find("|") >= 0 and line.startswith("PLUG") == False:
- > plug_line = [x.strip().lower() for x in line.split("|")]
- > if len(plug_line) < len(plug_header):
- > plug_section = -1
- > pass
- > if ["list", "monitor"].count(options["-o"]) == 0 and options["-n"].lower() == plug_line[plug_index]:
- > return plug_line[status_index]
- > else:
- > ## We already believe that first column contains plug number
- > if len(plug_line[0]) != 0:
- > outlets[plug_line[0]] = (plug_line[name_index], plug_line[status_index])
- > elif (plug_section == 1):
- > plug_section = 2
- > pass
- > elif (line.upper().startswith("PLUG")):
- > plug_section = 1
- > plug_header = [x.strip().lower() for x in line.split("|")]
- > plug_index = plug_header.index("plug")
- > name_index = plug_header.index("name")
- > status_index = plug_header.index("status")
- 157c73,76
- < return ret
- ---
- > if ["list", "monitor"].count(options["-o"]) == 1:
- > return outlets
- > else:
- > return "PROBLEM"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement