Browse Source

Add uuid generation for rnd function. Change name net to cidr.

develop
Mike Hiretsky 11 years ago
parent
commit
6470f9ef72
  1. BIN
      i18n/cl_lib_ru.mo
  2. 18
      pym/cl_template.py
  3. 20
      pym/utils/ip.py

BIN
i18n/cl_lib_ru.mo

18
pym/cl_template.py

@ -2751,6 +2751,7 @@ class templateFunction(_error, _shareTemplate, _shareTermsFunction):
первый аргумент:
'num' - числа,
'pas' - цифры и буквы
'uuid' - цифры и строчные буквы a-f
второй аргумент:
количество символов
"""
@ -2760,11 +2761,13 @@ class templateFunction(_error, _shareTemplate, _shareTermsFunction):
len(terms)!=2:
self.printErrTemplate()
cl_overriding.exit(1)
fArgvNames = ['num','pas']
fArgvNames = {'num':string.digits,
'pas':string.ascii_letters + string.digits,
'uuid':string.ascii_lowercase[:6] + string.digits}
if not terms[0] in fArgvNames:
self.printErrTemplate()
cl_overriding.printERROR(\
_("first argument function is not 'num' or 'pas'"))
_("first argument function must be 'num','pas' or 'uuid'"))
cl_overriding.exit(1)
try:
lenStr = int(terms[1])
@ -2773,15 +2776,8 @@ class templateFunction(_error, _shareTemplate, _shareTermsFunction):
cl_overriding.printERROR(\
_("two argument function is not number"))
cl_overriding.exit(1)
if terms[0] == fArgvNames[0]:
replace=''.join([random.choice(string.digits)\
for i in xrange(lenStr)])
elif terms[0] == fArgvNames[1]:
replace=''.join([random.choice(string.ascii_letters + \
string.digits) for i in xrange(lenStr)])
else:
self.printErrTemplate()
cl_overriding.exit(1)
choiceStr = fArgvNames[terms[0]]
replace = ''.join([random.choice(choiceStr) for i in xrange(lenStr)])
textTemplateTmp = textTemplateTmp[:resS.start()] + replace +\
textTemplateTmp[resS.end():]
return textTemplateTmp

20
pym/utils/ip.py

@ -62,8 +62,8 @@ def checkNet(net):
"""Check net"""
if not reNet.match(net):
return False
mask = strIpToIntIp(netToMask(int(net)))
ip,op,net = net.partition('/')
ip,op,cidr = net.partition('/')
mask = strIpToIntIp(cidrToMask(int(cidr)))
return (strIpToIntIp(ip)&mask) == (strIpToIntIp(ip))
maskDigs = map(lambda x:str(x),(0b10000000,0b11000000,0b11100000,0b11110000,
@ -106,19 +106,23 @@ def intIpToStrIp(addr):
return "{0}.{1}.{2}.{3}".format(
addr>>24,(addr>>16)&0xff,(addr>>8)&0xff,addr&0xff)
def maskToNet(mask):
def maskToCidr(mask):
"""Convert mask specified by str to net"""
mask = strIpToIntIp(mask)
return 32-int(math.log(((~mask) & 0xffffffff)+1,2))
def netToMask(net):
def cidrToMask(cidr):
"""Convert net to mask specified by str"""
return intIpToStrIp((2**net-1)<<(32-net))
return intIpToStrIp((2**cidr-1)<<(32-cidr))
def getIpNet(ip,mask):
def getIpNet(ip,mask=None,cidr=None):
"""Get net (xx.xx.xx.xx/xx) by ip address and mask"""
ip = strIpToIntIp(ip)
net = maskToNet(mask)
if not mask is None:
net = maskToCidr(mask)
else:
net = int(cidr)
mask = cidrToMask(net)
mask = strIpToIntIp(mask)
return "{ip}/{net}".format(ip=intIpToStrIp(ip&mask),
net=net)
@ -127,7 +131,7 @@ def isIpInNet(checkip,*ipnets):
"""Check is ip in specified nets"""
return map(lambda x:x[0],
filter(lambda x:strIpToIntIp(checkip)&x[2] == strIpToIntIp(x[1])&x[2],
map(lambda x:(x[0],x[1][0],strIpToIntIp(netToMask(int(x[1][1])))),
map(lambda x:(x[0],x[1][0],strIpToIntIp(cidrToMask(int(x[1][1])))),
map(lambda x:(x,x.partition('/')[0::2]),
ipnets))))

Loading…
Cancel
Save