Skip to content

Commit

Permalink
Switch from mechanize to robobrowser to support python3
Browse files Browse the repository at this point in the history
  • Loading branch information
moobyfr committed Jun 17, 2016
1 parent 29b6708 commit 280b73b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 70 deletions.
4 changes: 2 additions & 2 deletions scripts/netmagis-add.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/python
import ConfigParser
import configparser
import sys

from netmagisclient import netmagisclient

config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
config.readfp(open('config.cfg'))
URL = config.get('netmagis', 'URL')
DOMAINE = config.get('netmagis', 'DOMAINE')
Expand Down
4 changes: 2 additions & 2 deletions scripts/netmagis-addvhost.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/python

import sys
import ConfigParser
import configparser
from netmagisclient import netmagisclient


config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
config.readfp(open('config.cfg'))
URL = config.get('netmagis', 'URL')
DOMAINE = config.get('netmagis', 'DOMAINE')
Expand Down
4 changes: 2 additions & 2 deletions scripts/netmagis-deleteip.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/python
import ConfigParser
import configparser
import sys

from netmagisclient import netmagisclient

config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
config.readfp(open('config.cfg'))
URL = config.get('netmagis', 'URL')
DOMAINE = config.get('netmagis', 'DOMAINE')
Expand Down
4 changes: 2 additions & 2 deletions scripts/netmagis-deletename.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/python
import ConfigParser
import configparser
import sys

from netmagisclient import netmagisclient

config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
config.readfp(open('config.cfg'))
URL = config.get('netmagis', 'URL')
DOMAINE = config.get('netmagis', 'DOMAINE')
Expand Down
8 changes: 3 additions & 5 deletions scripts/netmagis-exportcsv.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/python
import ConfigParser
import sys
import configparser

from netmagisclient import netmagisclient

config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
config.readfp(open('config.cfg'))
URL = config.get('netmagis', 'URL')
DOMAINE = config.get('netmagis', 'DOMAINE')
Expand All @@ -15,8 +14,7 @@
PASS = config.get('netmagis', 'PASS')
CAS_SERVER = config.get('netmagis', 'CAS_SERVER')

data = {
}
data = {'plage': ['50', '51']}

mynmc = netmagisclient.NetmagisClient(URL, CAS_SERVER)
mynmc.caslogin(LOGIN, PASS)
Expand Down
4 changes: 2 additions & 2 deletions scripts/netmagis-looklarge.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/python
import ConfigParser
import configparser
import sys

from netmagisclient import netmagisclient

config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
config.readfp(open('config.cfg'))
URL = config.get('netmagis', 'URL')
DOMAINE = config.get('netmagis', 'DOMAINE')
Expand Down
112 changes: 57 additions & 55 deletions scripts/netmagisclient/netmagisclient.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mechanize
from robobrowser import RoboBrowser


def is_id_form(form):
Expand All @@ -10,34 +10,37 @@ class NetmagisClient(object):
url = None # Netmagis's URL
casurl = None # CAS's URL
br = None # the browser reference
c = None
s = None

def __init__(self, url, casurl):
self.url = url
self.casurl = casurl
self.br = mechanize.Browser()
self.br = RoboBrowser()

# call the loginURL to authenticate
def caslogin(self, login, passwd):
uri = self.casurl+"?service="+self.url+"start"
print("LOGIN IN PROGRESS : %s", uri)
print("LOGIN IN PROGRESS : %s" % uri)
self.br.open(uri)
self.br.select_form(predicate=is_id_form)
self.br["username"] = login
self.br["password"] = passwd
response2 = self.br.submit()
form = self.br.get_form()
form['username'].value = login
form['password'].value = passwd
response2 = self.br.submit_form(form)
print(response2)
print("LOGIN ENDED")

def addvhost(self, data):
def addvhost(self, data): # XXX
uri = self.url+"add"
print("ADDVHOST IN PROGRESS : %s", uri)
self.br.open(uri)
self.br.select_form(None, None, 2)
self.br["name"] = data["name"]
self.br["domain"] = [data["domain"]]
self.br["nameref"] = data["nameref"]
self.br["domainref"] = [data["domainref"]]
r = self.br.submit()
returnaddvhost = r.read()
f = self.br.get_forms()[2]
f["name"] = data["name"]
f["domain"] = data["domain"]
f["nameref"] = data["nameref"]
f["domainref"] = data["domainref"]
self.br.submit_form(f)
returnaddvhost = self.br.response.content.decode('utf8')
if 'An error occurred in Netmagis application' in returnaddvhost:
print("Error")
returnvalue = 1
Expand All @@ -50,18 +53,18 @@ def add(self, data):
uri = self.url+"add"
print("ADD IN PROGRESS : %s", uri)
self.br.open(uri)
self.br.select_form(None, None, 0)
self.br["name"] = data["name"]
self.br["domain"] = [data["domain"]]
self.br["addr"] = data["addr"]
self.br["mac"] = data["mac"]
self.br["iddhcpprof"] = [data["iddhcpprof"]]
self.br["hinfo"] = [data["hinfo"]]
self.br["comment"] = data["comment"]
self.br["respname"] = data["respname"]
self.br["respmail"] = data["respmail"]
r = self.br.submit()
returnaddvhost = r.read()
f = self.br.get_forms()[0]
f["name"] = data["name"]
f["domain"] = data["domain"]
f["addr"] = data["addr"]
f["mac"] = data["mac"]
f["iddhcpprof"] = data["iddhcpprof"]
f["hinfo"] = data["hinfo"]
f["comment"] = data["comment"]
f["respname"] = data["respname"]
f["respmail"] = data["respmail"]
self.br.submit_form(f)
returnaddvhost = self.br.response.content.decode('utf8')
catchable_errors = ['There is already a host named',
'An error occurred in Netmagis application'
]
Expand All @@ -78,18 +81,18 @@ def deletename(self, data):
uri = self.url+"del"
print("DELNAME IN PROGRESS : %s", uri)
self.br.open(uri)
self.br.select_form(None, None, 0)
self.br["name"] = data["name"]
self.br["domain"] = [data["domain"]]
r = self.br.submit()
firstsubmit = r.read()
f = self.br.get_forms()[0]
f["name"] = data["name"]
f["domain"] = data["domain"]
self.br.submit_form(f)
firstsubmit = self.br.response.content.decode('utf8')
if 'An error occurred in Netmagis application' in firstsubmit:
print("Error")
returnvalue = 1
else:
self.br.select_form(None, None, 0)
r2 = self.br.submit()
secondsubmit = r2.read()
f2 = self.br.get_form()
self.br.submit_form(f2)
secondsubmit = self.br.response.content.decode('utf8')
if 'An error occurred in Netmagis application' in secondsubmit:
print("Error")
returnvalue = 1
Expand All @@ -102,17 +105,17 @@ def deleteip(self, data):
uri = self.url+"del"
print("DELIP IN PROGRESS : %s", uri)
self.br.open(uri)
self.br.select_form(None, None, 1)
self.br["addr"] = data["addr"]
r = self.br.submit()
firstsubmit = r.read()
form = self.br.get_forms()[1]
form["addr"] = data["addr"]
self.br.submit_form(form)
firstsubmit = self.br.response.content.decode('utf8')
if 'An error occurred in Netmagis application' in firstsubmit:
print("Error")
returnvalue = 1
else:
self.br.select_form(None, None, 0)
r2 = self.br.submit()
secondsubmit = r2.read()
f2 = self.br.get_form()
self.br.submit_form(f2)
secondsubmit = self.br.response.content.decode('utf8')
if 'An error occurred in Netmagis application' in secondsubmit:
print("Error")
returnvalue = 1
Expand All @@ -125,25 +128,24 @@ def exportcsv(self, data):
uri = self.url+"net"
print("EXPORT IN PROGRESS : %s", uri)
self.br.open(uri)
self.br.select_form(None, None, 0)
c = self.br.form.controls()
c.set_values(c.items, "plages")
r = self.br.submit('docsv')
print r.read()
form = self.br.get_form(action='net')
form['plages'].value = data['plage']
self.br.submit_form(form, submit=form['docsv'])
print(self.br.response.content.decode('utf8'))
print ("EXPORT ENDED")

def looklarge(self, data):
uri = self.url+"add"
print("LOOKLARGE IN PROGRESS : %s", uri)
self.br.open(uri)
self.br.select_form(None, None, 1)
self.br["naddr"] = data["naddr"]
self.br["plage"] = [data["plage"]]
r = self.br.submit()
returnlooklarge = r.read()
form = self.br.get_forms()[1]
form['naddr'] = data['naddr']
form['plage'] = data['plage']
self.br.submit_form(form, submit=form['dosearch'])
returnlooklarge = self.br.response.content.decode('utf8')
if 'Aucun bloc' in returnlooklarge:
returnvalue = 0
self.br.select_form(None, None, 0)
returnvalue = self.br.form["addr"]
return 0
f2 = self.br.get_forms()[0]
returnvalue = f2.fields['addr'].value
print("LOOKLARGE ENDED")
return returnvalue

0 comments on commit 280b73b

Please sign in to comment.