Create a .txt list of IPs in a subnet - python

I'd like to make a very simple text (.txt) file. This python program needs to make a list of multiple ranges of IPs in a subnet, each taking up one line.
Example:
10.10.27.1
10.10.27.5
10.10.27.6
10.10.27.26
10.10.27.27
10.10.27.28
10.10.27.29
10.10.27.51
10.10.27.52
10.10.27.53
10.10.27.54
The subnet mask will essentially always be a /24, so providing mask input is not necessary. The program can even default to only supporting a standard class C.
Also, I'd like to support common ranges for devices that we use. A prompt for say, "Printers?" will include .26 - .30. "Servers?" will include .5 - .7. "DHCP?" prompt always will include .51 - .100 of the subnet. "Abnormal?" will include .100 - .254.
Subnet? 10.10.27.1
Servers? Y
Printers? Y
DHCP? Y
Abnormal? N
Output being:
10.10.27.1
10.10.27.5
10.10.27.6
10.10.27.7
10.10.27.26
10.10.27.27
10.10.27.28
10.10.27.29
10.10.27.30
10.10.27.51 (all the way to .100)
What is the best way to code this?

It looks like a few for loops are all you need:
network = '10.10.27'
for host in xrange(100, 255):
print("{network}.{host}".format(**locals()))

I would keep the script simple, just output all addresses as needed :)
def yesno(question):
output = input(question).lower()
if output == 'y':
return True
elif output == 'n':
return False
else:
print '%r is not a valid response, only "y" and "n" are allowed.' % output
return yesno(question)
addresses = []
subnet = input('Subnet? ')
# Remove the last digit and replace it with a %d for formatting later
subnet, address = subnet.rsplit('.', 1)
subnet += '%d'
addresses.append(int(address))
if yesno('Servers? '):
addresses += range(5, 8)
if yesno('Printers? '):
addresses += range(26, 31)
if yesno('DHCP? '):
addresses += range(51, 101)
if yesno('Abnormal? '):
addresses += range(100, 255)
for address in addresses:
print subnet % address

Here is a quick little script I threw together...
import socket
import sys
ranges = {'servers':(5, 8), 'printers':(26, 31), 'dhcp':(51, 101), 'abnormal':(101, 256)}
subnet = raw_input("Subnet? ")
try:
socket.inet_aton(subnet)
except socket.error:
print "Not a valid subnet"
sys.exit(1)
ip_parts = subnet.split(".")
if len(ip_parts) < 3:
print "Need at least three octets"
sys.exit(1)
ip_parts = ip_parts[:3]
ip = ".".join(ip_parts)
include_groups = []
last_octets = []
servers = raw_input("Servers? ")
printers = raw_input("Printers? ")
dhcp = raw_input("DHCP? ")
abnormal = raw_input("Abnormal? ")
if servers == "Y":
include_groups.append('servers')
if printers == "Y":
include_groups.append('printers')
if dhcp == "Y":
include_groups.append('dhcp')
if abnormal == "Y":
include_groups.append('abnormal')
for group in include_groups:
last_octets.extend([x for x in xrange(ranges[group][0], ranges[group][1])])
print "%s.1" %(ip)
for octet in last_octets:
print "%s.%s" %(ip, octet)

Related

Python: ping betwen two networks

I'm trying to make a simple python script to ping all devices in my company, but I stuck in pinging between two networks: 192.168.0.x and 192.168.10.x
Is there a way to ping addresses in 192.168.10.x from 192.168.0.x without making changes in LAN configuration?
from pythonping import ping
ips = ['192.168.0.88', '192.168.0.253', '192.168.0.182', '192.168.0.9', '192.168.0.1', '192.168.10.10',
'192.168.10.5']
def check_by_ping(ip=[]):
ret = []
for ip in ips:
p = ping(ip, count=1)
if p.rtt_max_ms == 2000:
ret.append([ip, False])
print('Wynik: ', ip, ' = ', 'Nieosiągalne!!!')
else:
ret.append([ip, p.rtt_max_ms])
print('Wynik: ', ip, ' = ', p.rtt_max_ms)
return ret
c = 0
while c<100:
print('\n Test nr: ',c,'\n')
check_by_ping(ips)
c += 1

How to prevents a AssertionError from stop Python code?

I want to do a system using a Autorules fuzzy controller where i create rules from process real data.
My problem is when i use the new data to simulate the fuzzy controller with the rules extracted of the older data, i get a error about the crisp output that cannot be calculated because do not exist rules enough, what is totally normal because my fuzzy system needs more rules and that's my point! I want to implement a new routine that analyses the crisp input/output and create new rules from this data, after that, i want to go back in my code and simulate again.
Is there a function that blocks the AssertionError from stop the code and redirect to another def or to a previously code line?
I tried to find some lib with a function that allows me to redirect the error steady to stop the code but no sucess. I think i will have to change the skfuzzy defuzz def code to allow to make it.
Thank you very much.
''' python
step = stp_ini
k = 0
delay = stp_ini
end = stp_fim - stp_ini
Tout_sim = pd.DataFrame(columns=['val'])
Vent_sim = pd.DataFrame(columns=['val'])
start = timeit.default_timer()
for step in range(end-k):
clear_output(wait=True)
simulation.input['PCA1'] = comp1n[step+delay]
simulation.input['PCA2'] = comp2n[step+delay]
simulation.input['PCA3'] = comp3n[step+delay]
simulation.input['PCA4'] = comp4n[step+delay]
simulation.input['Vent'] = dataoutf.NumVentOn[step+delay]
simulation.compute()
Tout_sim = Tout_sim.append({'val':simulation.output['Tout']},ignore_index=True)
stop = timeit.default_timer()
if ((step/(stp_fim-k-1))*100) < 5:
expected_time = "Calculating..."
else:
time_perc = timeit.default_timer()
expected_time = np.round( ( (time_perc-start)/(step/(end-k-1)) )/60,2)
'''
~\AppData\Local\Continuum\anaconda3\lib\site-packages\skfuzzy\control\controlsystem.py in defuzz(self)
587 self.var.defuzzify_method)
588 except AssertionError:
--> 589 raise ValueError("Crisp output cannot be calculated, likely "
590 "because the system is too sparse. Check to "
591 "make sure this set of input values will "
ValueError: Crisp output cannot be calculated, likely because the system is too sparse. Check to make sure this set of input values will activate at least one connected Term in each Antecedent via the current set of Rules.
edit:
I try to wrap the line code ValueError by try but the ValueError is activated yet
def defuzz(self):
"""Derive crisp value based on membership of adjective(s)."""
if not self.sim._array_inputs:
ups_universe, output_mf, cut_mfs = self.find_memberships()
if len(cut_mfs) == 0:
raise ValueError("No terms have memberships. Make sure you "
"have at least one rule connected to this "
"variable and have run the rules calculation.")
try:
return defuzz(ups_universe, output_mf,
self.var.defuzzify_method)
except AssertionError:
try:
new_c1 = []
new_c2 = []
new_c3 = []
new_c4 = []
new_vent = []
new_tout = []
newcondition1 = []
newcondition2 = []
newcondition3 = []
newcondition4 = []
newcondition5 = []
newcondition6 = []
#input
n = 0
for n in range(len(namespca)):
new_c1.append(fuzz.interp_membership(PCA1.universe, PCA1[namespcapd.name.loc[n]].mf, comp1n[step]))
new_c2.append(fuzz.interp_membership(PCA2.universe, PCA2[namespcapd.name.loc[n]].mf, comp2n[step]))
new_c3.append(fuzz.interp_membership(PCA3.universe, PCA3[namespcapd.name.loc[n]].mf, comp3n[step]))
new_c4.append(fuzz.interp_membership(PCA4.universe, PCA4[namespcapd.name.loc[n]].mf, comp4n[step]))
n = 0
for n in range(len(namesvent)):
new_vent.append(fuzz.interp_membership(Vent.universe, Vent[namesventpd.name.loc[n]].mf, dataoutf.NumVentOn[step]))
#output
n = 0
for n in range(len(namestemp)):
new_tout.append(fuzz.interp_membership(Tout.universe, Tout[namestemppd.name.loc[n]].mf, dataoutf.TsaidaHT[step]))
#new_c1 = np.transpose(new_c1)
new_c1_conv = pd.DataFrame(new_c1)
#new_c2 = np.transpose(new_c2)
new_c2_conv = pd.DataFrame(new_c2)
#new_c3 = np.transpose(new_c3)
new_c3_conv = pd.DataFrame(new_c3)
#new_c4 = np.transpose(new_c4)
new_c4_conv = pd.DataFrame(new_c4)
#new_vent = np.transpose(new_vent)
new_vent_conv = pd.DataFrame(new_vent)
#new_tout = np.transpose(new_tout)
new_tout_conv = pd.DataFrame(new_tout)
i=0
for i in range(pcamf):
newcondition1.append([new_c1_conv.idxmax(axis=0) == i])
newcondition2.append([new_c2_conv.idxmax(axis=0) == i])
newcondition3.append([new_c3_conv.idxmax(axis=0) == i])
newcondition4.append([new_c4_conv.idxmax(axis=0) == i])
i=0
for i in range(ventmf):
newcondition5.append([new_vent_conv.idxmax(axis=0) == i])
i=0
for i in range(tempmf):
newcondition6.append([new_tout_conv.idxmax(axis=0) == i])
choicelistpca = namespca
choicelistvent = namesvent
choicelisttout = namestemp
new_c1_rules = np.select(newcondition1, choicelistpca)
new_c2_rules = np.select(newcondition2, choicelistpca)
new_c3_rules = np.select(newcondition3, choicelistpca)
new_c4_rules = np.select(newcondition4, choicelistpca)
new_vent_rules = np.select(newcondition5, choicelistvent)
new_tout_rules = np.select(newcondition6, choicelisttout)
new_rules = np.vstack([new_c1_rules,new_c2_rules,new_c3_rules,new_c4_rules,new_vent_rules,new_tout_rules])
new_rules = new_rules.T
new_rulespd = pd.DataFrame(new_rules,columns=['PCA1','PCA2','PCA3','PCA4','Vent','Tout'])
#Checar se a nova regra está dentro do conjunto de regras fuzzy atual
if pd.merge(new_rulespd,AutoRules, on=['PCA1','PCA2','PCA3','PCA4','Vent','Tout'],how='inner').empty:
print('Nova regra não encontrada no conjunto atual de regras fuzzy!')
else:
pd.merge(new_rulespd,AutoRules, on=['PCA1','PCA2','PCA3','PCA4','Vent','Tout'],how='inner')
"""except AssertionError:
raise ValueError("Crisp output cannot be calculated, likely "
"because the system is too sparse. Check to "
"make sure this set of input values will "
"activate at least one connected Term in each "
"Antecedent via the current set of Rules.")"""
else:
# Calculate using array-aware version, one cut at a time.
output = np.zeros(self.sim._array_shape, dtype=np.float64)
it = np.nditer(output, ['multi_index'], [['writeonly', 'allocate']])
for out in it:
universe, mf = self.find_memberships_nd(it.multi_index)
out[...] = defuzz(universe, mf, self.var.defuzzify_method)
return output
Wrap the line of code that raises ValueError in a try. And decide what to do in its except ValueError: clause. Perhaps continue-ing on to the next iteration might be reasonable.

how to increment mac address in python

I am trying to send a request with name, id.no and mac address. This process i need to do for 100 times. i mean 100 different names and id.no's and mac address. I tried doing this. I want output as name1, 1234567679 and aa:bb:cc:dd:11:22. can someone please help me. I have pasted my code and output below
for i in range (1,10):
name = 'name'+str(i)
print name
id = 121333445500000
id += 1
print id
mac = "aa:bb:cc:dd:"
ee = 0
for j in range(0,9):
ee1 = '0'+str(ee + j)+':'
ff = 0
for k in range(0,10):
ff1 = ff+k
if ff1 < 10:
mac1 = mac + ee1 + '0' + str(ff1)
print mac1
else:
mac1 = mac + ee1 + str(ff1)
print mac1
Output:
name1
121333445500001
aa:bb:cc:dd:00:00
aa:bb:cc:dd:00:01
aa:bb:cc:dd:00:02
aa:bb:cc:dd:00:03
aa:bb:cc:dd:00:04
aa:bb:cc:dd:00:05
aa:bb:cc:dd:00:06
aa:bb:cc:dd:00:07
aa:bb:cc:dd:00:08
aa:bb:cc:dd:00:09
aa:bb:cc:dd:01:00
aa:bb:cc:dd:01:01
aa:bb:cc:dd:01:02
aa:bb:cc:dd:01:03
aa:bb:cc:dd:01:04
aa:bb:cc:dd:01:05
aa:bb:cc:dd:01:06
aa:bb:cc:dd:01:07
aa:bb:cc:dd:01:08
aa:bb:cc:dd:01:09
aa:bb:cc:dd:02:00
aa:bb:cc:dd:02:01
aa:bb:cc:dd:02:02
aa:bb:cc:dd:02:03
aa:bb:cc:dd:02:04
aa:bb:cc:dd:02:05
aa:bb:cc:dd:02:06
aa:bb:cc:dd:02:07
aa:bb:cc:dd:02:08
aa:bb:cc:dd:02:09
aa:bb:cc:dd:03:00
aa:bb:cc:dd:03:01
aa:bb:cc:dd:03:02
aa:bb:cc:dd:03:03
aa:bb:cc:dd:03:04
aa:bb:cc:dd:03:05
aa:bb:cc:dd:03:06
aa:bb:cc:dd:03:07
aa:bb:cc:dd:03:08
aa:bb:cc:dd:03:09
aa:bb:cc:dd:04:00
aa:bb:cc:dd:04:01
aa:bb:cc:dd:04:02
aa:bb:cc:dd:04:03
aa:bb:cc:dd:04:04
aa:bb:cc:dd:04:05
aa:bb:cc:dd:04:06
aa:bb:cc:dd:04:07
aa:bb:cc:dd:04:08
aa:bb:cc:dd:04:09
aa:bb:cc:dd:05:00
aa:bb:cc:dd:05:01
aa:bb:cc:dd:05:02
aa:bb:cc:dd:05:03
aa:bb:cc:dd:05:04
aa:bb:cc:dd:05:05
aa:bb:cc:dd:05:06
aa:bb:cc:dd:05:07
aa:bb:cc:dd:05:08
aa:bb:cc:dd:05:09
aa:bb:cc:dd:06:00
aa:bb:cc:dd:06:01
aa:bb:cc:dd:06:02
aa:bb:cc:dd:06:03
aa:bb:cc:dd:06:04
aa:bb:cc:dd:06:05
aa:bb:cc:dd:06:06
aa:bb:cc:dd:06:07
aa:bb:cc:dd:06:08
aa:bb:cc:dd:06:09
aa:bb:cc:dd:07:00
aa:bb:cc:dd:07:01
aa:bb:cc:dd:07:02
aa:bb:cc:dd:07:03
aa:bb:cc:dd:07:04
aa:bb:cc:dd:07:05
aa:bb:cc:dd:07:06
aa:bb:cc:dd:07:07
aa:bb:cc:dd:07:08
aa:bb:cc:dd:07:09
aa:bb:cc:dd:08:00
aa:bb:cc:dd:08:01
aa:bb:cc:dd:08:02
aa:bb:cc:dd:08:03
aa:bb:cc:dd:08:04
aa:bb:cc:dd:08:05
aa:bb:cc:dd:08:06
aa:bb:cc:dd:08:07
aa:bb:cc:dd:08:08
aa:bb:cc:dd:08:09
name2
121333445500001
aa:bb:cc:dd:00:00
aa:bb:cc:dd:00:01
aa:bb:cc:dd:00:02
aa:bb:cc:dd:00:03
aa:bb:cc:dd:00:04
aa:bb:cc:dd:00:05
aa:bb:cc:dd:00:06
aa:bb:cc:dd:00:07
aa:bb:cc:dd:00:08
aa:bb:cc:dd:00:09
aa:bb:cc:dd:01:00
aa:bb:cc:dd:01:01
aa:bb:cc:dd:01:02
aa:bb:cc:dd:01:03
aa:bb:cc:dd:01:04
aa:bb:cc:dd:01:05
aa:bb:cc:dd:01:06
aa:bb:cc:dd:01:07
aa:bb:cc:dd:01:08
aa:bb:cc:dd:01:09
aa:bb:cc:dd:02:00
aa:bb:cc:dd:02:01
aa:bb:cc:dd:02:02
aa:bb:cc:dd:02:03
aa:bb:cc:dd:02:04
aa:bb:cc:dd:02:05
aa:bb:cc:dd:02:06
aa:bb:cc:dd:02:07
aa:bb:cc:dd:02:08
aa:bb:cc:dd:02:09
aa:bb:cc:dd:03:00
aa:bb:cc:dd:03:01
aa:bb:cc:dd:03:02
aa:bb:cc:dd:03:03
aa:bb:cc:dd:03:04
aa:bb:cc:dd:03:05
aa:bb:cc:dd:03:06
aa:bb:cc:dd:03:07
aa:bb:cc:dd:03:08
aa:bb:cc:dd:03:09
aa:bb:cc:dd:04:00
aa:bb:cc:dd:04:01
aa:bb:cc:dd:04:02
aa:bb:cc:dd:04:03
aa:bb:cc:dd:04:04
aa:bb:cc:dd:04:05
aa:bb:cc:dd:04:06
aa:bb:cc:dd:04:07
aa:bb:cc:dd:04:08
aa:bb:cc:dd:04:09
aa:bb:cc:dd:05:00
aa:bb:cc:dd:05:01
aa:bb:cc:dd:05:02
aa:bb:cc:dd:05:03
aa:bb:cc:dd:05:04
aa:bb:cc:dd:05:05
aa:bb:cc:dd:05:06
aa:bb:cc:dd:05:07
aa:bb:cc:dd:05:08
aa:bb:cc:dd:05:09
aa:bb:cc:dd:06:00
aa:bb:cc:dd:06:01
aa:bb:cc:dd:06:02
aa:bb:cc:dd:06:03
aa:bb:cc:dd:06:04
aa:bb:cc:dd:06:05
aa:bb:cc:dd:06:06
aa:bb:cc:dd:06:07
aa:bb:cc:dd:06:08
aa:bb:cc:dd:06:09
aa:bb:cc:dd:07:00
aa:bb:cc:dd:07:01
aa:bb:cc:dd:07:02
aa:bb:cc:dd:07:03
aa:bb:cc:dd:07:04
aa:bb:cc:dd:07:05
aa:bb:cc:dd:07:06
aa:bb:cc:dd:07:07
aa:bb:cc:dd:07:08
aa:bb:cc:dd:07:09
aa:bb:cc:dd:08:00
aa:bb:cc:dd:08:01
aa:bb:cc:dd:08:02
aa:bb:cc:dd:08:03
aa:bb:cc:dd:08:04
aa:bb:cc:dd:08:05
aa:bb:cc:dd:08:06
aa:bb:cc:dd:08:07
aa:bb:cc:dd:08:08
aa:bb:cc:dd:08:09
name3
121333445500001
Hope this Helps
mac="0xaabbccdd0000"
for i in range(101):
mac = "{:012X}".format(int(mac, 16) + 1)
print(':'.join(mac[i]+mac[i+1] for i in range(0, len(mac), 2)))
Try this code
import time, random
mac = "aa:bb:cc:dd:"
for i in range(1,100):
name = 'name'+ str(i)
# getting random number for id
id = int(time.time()) + random.randrange(0, 100, 1)
# quotient in part 1
mac_end1 = "0" + str(int(i/10))
# remainder in part 2
mac_end2 = "0" + str(int(i%10))
mac1 = mac + mac_end1 + ":" + mac_end2
print name, id, mac1
Based on #tcpip, here is a more generic way
Script filename: mac_generator.py
#%%
import argparse
import sys
# sys.argv = ['']
parser = argparse.ArgumentParser()
parser.add_argument('-t',
'--total',
type=int,
default=100,
help="The total number of mac address to be generated")
parser.add_argument('-f',
'--file',
action="store_true",
help="Generate in filename mac_address.txt")
args = parser.parse_args()
print("Total number of MAC address to be generated is: {}".format(args.total))
#%%
mac="0x000000000001"
def print_mac():
for i in range(args.total - 1):
global mac
mac = "{:012X}".format(int(mac, 16) + 1)
print(':'.join(mac[i]+mac[i+1] for i in range(0, len(mac), 2)))
original_stdout = sys.stdout # Save a reference to the original standard output
if(args.file):
with open('mac_address.txt', 'w') as f:
sys.stdout = f # Change the standard output to the file we created.
print_mac()
sys.stdout = original_stdout # Reset the standard output to its origin
else:
print_mac()
Usage
usage: mac_generator.py [-h] [-t TOTAL] [-f]
optional arguments:
-h, --help show this help message and exit
-t TOTAL, --total TOTAL
The total number of mac address to be generated
-f, --file Generate in filename mac_address.txt
So, if you want to generate 100 MAC address, run ./mac_generator -t 100
I used the netaddr library, it makes it quite neat:
base = netaddr.EUI(emac2)
for m in range(1, count2):
value = base.value
value += 1
base._set_value(value)
macs.append(str(base))

Python unified diff with line numbers from both "files"

I'm trying to figure out a way to create unified diffs with line numbers only showing N lines of context. I have been unable to do this with difflib.unified_diff. I need to show changes in both files.
The closest I can come is using diff on the command line like so:
/usr/bin/diff
--unchanged-line-format=' %.2dn %L'
--old-line-format="-%.2dn %L"
--new-line-format="+%.2dn %L"
file1.py
file2.py
BUT I only want to show N lines of context, and /usr/bin/diff doesn't seem to support context with a custom line format (eg. -U2 is not compatible with --line-format "conflicting output style options").
Below is an example of what I'd like to accomplish (the same output as the above diff, but only showing 1 line of context surrounding changes):
+01: def renamed_function()
-01: def original_function():
02:
+03: """ Neat stuff here """
04:
21:
+22: # Here's a new comment
23:
85: # Output the value of foo()
+86: print "Foo is %s"%(foo())
-86: print foo()
87:
I was able to figure out something very close to what I wanted to do. It's slower than regular diff, though. Here's the entire code, from my project GitGate.
def unified_diff(to_file_path, from_file_path, context=1):
""" Returns a list of differences between two files based
on some context. This is probably over-complicated. """
pat_diff = re.compile(r'## (.[0-9]+\,[0-9]+) (.[0-9]+,[0-9]+) ##')
from_lines = []
if os.path.exists(from_file_path):
from_fh = open(from_file_path,'r')
from_lines = from_fh.readlines()
from_fh.close()
to_lines = []
if os.path.exists(to_file_path):
to_fh = open(to_file_path,'r')
to_lines = to_fh.readlines()
to_fh.close()
diff_lines = []
lines = difflib.unified_diff(to_lines, from_lines, n=context)
for line in lines:
if line.startswith('--') or line.startswith('++'):
continue
m = pat_diff.match(line)
if m:
left = m.group(1)
right = m.group(2)
lstart = left.split(',')[0][1:]
rstart = right.split(',')[0][1:]
diff_lines.append("## %s %s ##\n"%(left, right))
to_lnum = int(lstart)
from_lnum = int(rstart)
continue
code = line[0]
lnum = from_lnum
if code == '-':
lnum = to_lnum
diff_lines.append("%s%.4d: %s"%(code, lnum, line[1:]))
if code == '-':
to_lnum += 1
elif code == '+':
from_lnum += 1
else:
to_lnum += 1
from_lnum += 1
return diff_lines

Two issue about python OpenOPC library

Issues description and environments
The OpenOPC library is friendly and easy to use, the api is simple too, but I have found two issues during the development of a tool to record real time OPC items data.
The development environment is: Window 8.1, Python 2.7.6, wxpython 2.8 unicode
The testing environment is: Window XP SP3, Python 2.7.6, wxpython 2.8 unicode, Rockwell's soft logix as OPC Server
The deploy environment is: Window XP SP3, connected with Rockwell's real PLC, installed RSLogix 5000 and RSLinx Classic Gateway
Questions
the opc.list function doesn't list all the item of specify node both in testing and workstaion environment. The question is how to list the 't' from the opc server?
An int array 'dint100' and a dint 't' is added with RS logix 5000 at the scope of soft_1
With the default OPC client test tool from Rockwell it could list the new added 't'
With OpenOPC library, I couldn't find out how to list the item 't', but I could read it's value by opc.read('[soft_1]t') with it's tag.
If the 't' could be listed, it could be added into the IO tree of my tool.
The opc.servers function will encounter an OPCError on the deploy environment, but the client could connect the 'RSLinx OPC Server' directly with the server name. Does opc.servers function dependent on some special dll or service?
Any suggestions will be appreciated! Thanks in advance!
Consider that the browsing problems ("opc.list") may not be on your side. RSLinx is notorious for its broken OPC browsing. Try some test/simulation server from a different vendor, to test this hypothesis.
I realize that I'm really late to this game. I found what was causing this issue. OpenOPC.py assumes that there cannot be both a "Leaf" and a "Branch" on the same level. Replace the function ilist with this:
def ilist(self, paths='*', recursive=False, flat=False, include_type=False):
"""Iterable version of list()"""
try:
self._update_tx_time()
pythoncom.CoInitialize()
try:
browser = self._opc.CreateBrowser()
# For OPC servers that don't support browsing
except:
return
paths, single, valid = type_check(paths)
if not valid:
raise TypeError("list(): 'paths' parameter must be a string or a list of strings")
if len(paths) == 0: paths = ['*']
nodes = {}
for path in paths:
if flat:
browser.MoveToRoot()
browser.Filter = ''
browser.ShowLeafs(True)
pattern = re.compile('^%s$' % wild2regex(path) , re.IGNORECASE)
matches = filter(pattern.search, browser)
if include_type: matches = [(x, node_type) for x in matches]
for node in matches: yield node
continue
queue = []
queue.append(path)
while len(queue) > 0:
tag = queue.pop(0)
browser.MoveToRoot()
browser.Filter = ''
pattern = None
path_str = '/'
path_list = tag.replace('.','/').split('/')
path_list = [p for p in path_list if len(p) > 0]
found_filter = False
path_postfix = '/'
for i, p in enumerate(path_list):
if found_filter:
path_postfix += p + '/'
elif p.find('*') >= 0:
pattern = re.compile('^%s$' % wild2regex(p) , re.IGNORECASE)
found_filter = True
elif len(p) != 0:
pattern = re.compile('^.*$')
browser.ShowBranches()
# Branch node, so move down
if len(browser) > 0:
try:
browser.MoveDown(p)
path_str += p + '/'
except:
if i < len(path_list)-1: return
pattern = re.compile('^%s$' % wild2regex(p) , re.IGNORECASE)
# Leaf node, so append all remaining path parts together
# to form a single search expression
else:
###################################### JG Edit - Flip the next two rows comment/uncommented
p = '.'.join(path_list[i:])
# p = string.join(path_list[i:], '.')
pattern = re.compile('^%s$' % wild2regex(p) , re.IGNORECASE)
break
###################################### JG Edit - Comment this to return to original
browser.ShowBranches()
node_types = ['Branch','Leaf']
if len(browser) == 0:
lowest_level = True
node_types.pop(0)
else:
lowest_level = False
for node_type in node_types:
if node_type=='Leaf':
browser.ShowLeafs(False)
matches = filter(pattern.search, browser)
if not lowest_level and recursive:
queue += [path_str + x + path_postfix for x in matches]
else:
###################################### JG Edit - Flip the next two rows comment/uncommented
if lowest_level or node_type=='Leaf': matches = [exceptional(browser.GetItemID,x)(x) for x in matches]
# if lowest_level: matches = [exceptional(browser.GetItemID,x)(x) for x in matches]
if include_type: matches = [(x, node_type) for x in matches]
for node in matches:
if not node in nodes: yield node
nodes[node] = True
###################################### Uncomment this to return to original
# browser.ShowBranches()
# if len(browser) == 0:
# browser.ShowLeafs(False)
# lowest_level = True
# node_type = 'Leaf'
# else:
# lowest_level = False
# node_type = 'Branch'
# matches = filter(pattern.search, browser)
# if not lowest_level and recursive:
# queue += [path_str + x + path_postfix for x in matches]
# else:
# if lowest_level: matches = [exceptional(browser.GetItemID,x)(x) for x in matches]
# if include_type: matches = [(x, node_type) for x in matches]
# for node in matches:
# if not node in nodes: yield node
# nodes[node] = True
except pythoncom.com_error as err:
error_msg = 'list: %s' % self._get_error_str(err)
raise OPCError(error_msg)

Categories

Resources