python: Remove unused imports and variables.
[cascardo/ovs.git] / python / ovs / unixctl / client.py
1 # Copyright (c) 2011, 2012 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import os
16 import types
17
18 import ovs.jsonrpc
19 import ovs.stream
20 import ovs.util
21
22
23 vlog = ovs.vlog.Vlog("unixctl_client")
24 strtypes = types.StringTypes
25
26
27 class UnixctlClient(object):
28     def __init__(self, conn):
29         assert isinstance(conn, ovs.jsonrpc.Connection)
30         self._conn = conn
31
32     def transact(self, command, argv):
33         assert isinstance(command, strtypes)
34         assert isinstance(argv, list)
35         for arg in argv:
36             assert isinstance(arg, strtypes)
37
38         request = ovs.jsonrpc.Message.create_request(command, argv)
39         error, reply = self._conn.transact_block(request)
40
41         if error:
42             vlog.warn("error communicating with %s: %s"
43                       % (self._conn.name, os.strerror(error)))
44             return error, None, None
45
46         if reply.error is not None:
47             return 0, str(reply.error), None
48         else:
49             assert reply.result is not None
50             return 0, None, str(reply.result)
51
52     def close(self):
53         self._conn.close()
54         self.conn = None
55
56     @staticmethod
57     def create(path):
58         assert isinstance(path, str)
59
60         unix = "unix:%s" % ovs.util.abs_file_name(ovs.dirs.RUNDIR, path)
61         error, stream = ovs.stream.Stream.open_block(
62             ovs.stream.Stream.open(unix))
63
64         if error:
65             vlog.warn("failed to connect to %s" % path)
66             return error, None
67
68         return 0, UnixctlClient(ovs.jsonrpc.Connection(stream))