Module xalt_transmission_factory
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 from __future__ import print_function
22 import os, sys, json, base64
23
24 try:
25 from XALTdb import XALTdb
26 XALTdb_available = True
27 except ImportError:
28 XALTdb_available = False
29
30
31 from XALT_Rmap import Rmap
32 from xalt_global import *
35 """
36 This class is a factory to determine which way to "write" the json data.
37 """
39 """
40 This ctor saves away the system name and kind of record (link, run)
41
42 @param syshost: Name of the system.
43 @param kind: Type of record: link or run
44 """
45
46 self.__syshost = syshost
47 self.__kind = kind
48
50 """
51 returns the system name
52 @return returns the system name
53 """
54 return self.__syshost
55
57 """
58 Returns the kind of record: link or run
59 @return the kind of record.
60 """
61 return self.__kind
62
63 @staticmethod
64 - def build(name, syshost, kind, fn):
65 """
66 The static factory build routine that returns the transmission object.
67
68 @param name: Name of the factory: syslog, directdb, file
69 @param syshost: The system name. Should be stampede or darter not login1.stampede....
70 @param kind: Type of record: link, run
71 @param fn: file name (only used by file transport object
72 """
73
74 name = name.lower()
75 if (name == "syslog"):
76 obj = Syslog(syshost, kind)
77 elif (name == "directdb"):
78 obj = DirectDB(syshost, kind)
79 else:
80
81 obj = File(syshost, kind, fn)
82
83 return obj
84
85 -class Syslog(XALT_transmission_factory):
86 """
87 This class write the json record to syslog
88 """
89
91 """
92 This is the ctor for Syslog transmission method
93 @param syshost: Name of the system.
94 @param kind: Type of record: link or run
95 """
96
97 super(Syslog, self).__init__(syshost, kind)
98 - def save(self, resultT):
99 """
100 The json table is written to syslog with the text converted to base64.
101 @param resultT: The json record table
102 """
103 sA = []
104 sA.append("logger -t XALT_LOGGING")
105 sA.append(" \"")
106 sA.append(self._kind())
107 sA.append(":")
108 sA.append(self._syshost())
109 sA.append(":")
110 sA.append(base64.b64encode(json.dumps(resultT)))
111 sA.append("\"")
112 s = "".join(sA)
113 os.system(s)
114
115
116 -class File(XALT_transmission_factory):
117 """
118 This is the file transport class
119 """
120
122 """
123 This ctor is for the file transport method.
124 @param syshost: Name of the system.
125 @param kind: Type of record: link or run
126 @param fn: the file name to write the record to.
127 """
128 super(File, self).__init__(syshost, kind)
129 self.__fn = fn
130
131 - def save(self, resultT):
132 """
133 The json table is written to the file specified in the ctor.
134 @param resultT: The json record table
135 """
136 s = json.dumps(resultT, sort_keys=True,
137 indent=2, separators=(',',': '))
138 dirname, fn = os.path.split(self.__fn)
139 tmpFn = os.path.join(dirname, "." + fn)
140 if (not os.path.isdir(dirname)):
141 os.mkdir(dirname);
142
143 f = open(tmpFn,"w")
144 f.write(s)
145 f.close()
146 os.rename(tmpFn, self.__fn)
147
148
149 -class DirectDB(XALT_transmission_factory):
150 """
151 This class is the direct to db transmission method.
152 """
153
155 """
156 This is the ctor for Direct to DB transmission method
157 @param syshost: Name of the system.
158 @param kind: Type of record: link or run
159 """
160 super(DirectDB, self).__init__(syshost, kind)
161 - def save(self, resultT):
179