- # append to soma
- if pysoma:
- connection = pysoma.open_tcp('soma.panik', 12521, '', 0)
- palinsesto_xml = ET.fromstring(connection.get_palinsesto())
- palinsesto_xml.append(palinsesti_xml)
- with open('/tmp/soma.pl', 'w') as fd:
- fd.write(ET.tostring(palinsesto_xml))
-
- connection.new_palinsesto('/tmp/soma.pl')
- del connection
-
- # give it some time (...)
- time.sleep(3)
- connection = pysoma.open_tcp('soma.panik', 12521, '', 0)
- connection.set_default_palinsesto()
- del connection
+ # add to soma
+ def soma_connection():
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.connect(('soma', 12521))
+ s.recv(1024) # -> b'soma 2.5 NO_SSL\n'
+ s.sendall(b'100 - Ok\n')
+ s.recv(1024) # -> b'100 - Welcome to soma daemon\n'
+ s.sendall(b'\n') # (empty password)
+ if s.recv(1024) == b'100 - Ok\n':
+ raise SomaException('failed to initialize soma connection')
+ return s
+
+ with soma_connection() as s:
+ s.sendall(b'109 - Get the current palinsesto\n')
+ palinsesto_bytes = b''
+ while True:
+ new_bytes = s.recv(200000)
+ if not new_bytes:
+ break
+ palinsesto_bytes += new_bytes
+
+ palinsesto_xml = ET.fromstring(palinsesto_bytes)
+ palinsesto_xml.append(palinsesti_xml)
+ with soma_connection() as s:
+ s.sendall(b'106 - Switch to a New Palinsesto Request\n')
+ if s.recv(1024) == b'100 - Ok\n':
+ raise SomaException('failed to switch palinsesto')
+ s.sendall(ET.tostring(palinsesto_xml).encode('utf-8'))
+
+ # give it some time (...)
+ time.sleep(3)
+ with soma_connection() as s:
+ s.sendall(b'122 - Set the current Palinsesto as Default\n')
+ if s.recv(1024) == b'100 - Ok\n':
+ raise SomaException('failed to set current palinsesto as default')