ADCT Python
Python client API implementation for publishing ADCT data
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Static Protected Attributes | List of all members
adctk.publisher.Publisher Class Reference
Inheritance diagram for adctk.publisher.Publisher:
Inheritance graph
[legend]

Classes

class  Mode
 For plugins managing external resources (files, pipes), this enum is used to track where in the lifecycle they are. More...
 
class  State
 For plugins where the output target becomes unrecoverable, state == ERR means no further output will be attempted. More...
 

Public Member Functions

 __init__ (self)
 
str name (self)
 Get the name which a factory or error message should use.
 
str version (self)
 
dict[str, str] get_option_defaults (self)
 
int config (self, **kwargs)
 Configure the plugin with the options given.
 
int initialize (self)
 Set up any resources needed for reuse in publish().
 
int publish (self, builder.Builder b)
 Publish json extracted from builder.
 
None finalize (self)
 Clean up any resources set up in initialize (files, pipes, etc).
 
None pause (self)
 Cause publish calls to be ignored until resume is called.
 
None resume (self)
 Cancel an prior calls to pause.
 
int publish_str (self, str json_string)
 
int publish_obj (self, obj)
 

Static Public Member Functions

tuple[float] get_timeout_defaults ()
 Get the adc server connect and response timeouts defined in the environment ADC_SERVER_CONNECT_TIMEOUT and ADC_SERVER_POST_TIMEOUT as a tuple or default values if not defined.
 
str get_url_default ()
 Get the alleged adc server url base from environment ADC_SERVER_URL or the default if it is not present.
 
str get_port_default ()
 Get the adc server port number to use if it is properly defined in the environment ADC_SERVER_PORT or the default if it is not.
 

Public Attributes

 defaults
 
 plugin_prefix
 
 paused
 
 state
 
 mode
 

Static Public Attributes

 API_VERSION = adc_version.AdcVersion()._dict()
 

Protected Member Functions

str _get (self, dict[str, str] opts, str field)
 
bool _get_bool (self, dict[str, str] opts, str field)
 

Static Protected Attributes

str _name = "unknown"
 
str _version = "0.0.0"
 

Detailed Description

 The API for various implementations which publish Builder objects.
    Subclasses need to implement __init__, config, initialize, publish, finalize only.
    In subclass __init__, data items
        self.defaults: dict[str,str]
        self.plugin_prefix: str
    need to be set for the plugin.
    In each subclass, data members
        _name: str
        _version: str
    need to be reassigned.
Particularly simple subclasses may not need to use data items: state, mode.

@ingroup pub_impl

Constructor & Destructor Documentation

◆ __init__()

adctk.publisher.Publisher.__init__ (   self)

Member Function Documentation

◆ _get()

str adctk.publisher.Publisher._get (   self,
dict[str,str]  opts,
str  field 
)
protected
 Check for short option named by field in opts and return if present.
    (In most cases the short name is in all capitals.)
    When named field is not present, use opts[env_prefix]
    (or if opts[env_prefix] is not present, then self.plugin_prefix)
    to search for env(prefix+field) and return that value if present.
    If no environment value found, return self.defaults[field].
210 def _get(self, opts: dict[str,str], field: str) -> str:
211 """ Check for short option named by field in opts and return if present.
212 (In most cases the short name is in all capitals.)
213 When named field is not present, use opts[env_prefix]
214 (or if opts[env_prefix] is not present, then self.plugin_prefix)
215 to search for env(prefix+field) and return that value if present.
216 If no environment value found, return self.defaults[field]."""
217 if field in opts:
218 return opts[field]
219 if "env_prefix" in opts:
220 prefix = opts["env_prefix"]
221 else:
222 prefix = self.plugin_prefix
223 env_name = prefix + field
224 env_val = os.getenv(env_name)
225 if not env_val:
226 return self.defaults[field]
227 return env_val
228

◆ _get_bool()

bool adctk.publisher.Publisher._get_bool (   self,
dict[str,str]  opts,
str  field 
)
protected
229 def _get_bool(self, opts: dict[str,str], field: str) -> bool:
230 s = self._get(opts, field)
231 if s in ["TRUE", "true", "True", "1", "t", "T", "y", "Yes", "YES"]:
232 return True
233 return False
234

◆ config()

int adctk.publisher.Publisher.config (   self,
**  kwargs 
)

Configure the plugin with the options given.

For plugin QQQ, Environment variables ADC_QQQ_PLUGIN_<key> will override the plugin default for any key not defined in kwargs. Here QQQ is plugin.name().upper(). Generic options:

Parameters
kwargsIf env_prefix is included in kwargs, replaces plugin environment variables with those which are prefixed using env_prefix.
env_prefixTypically, env_prefix is set to "PPP_ADC_QQQ_PLUGIN_" if application PPP wants to override the defaults of adctk plugin QQQ. Here QQQ is plugin.name().upper(). If no prefixed version of the variable is present, the plugin default is used. kwargs takes precedence over environment variables. Plugin defaults are used if not overridden by kwargs or an environment variable.
Returns
int non-zero if configuration cannot be completed using the arguments, environment, and default.

Reimplemented in adctk.debug_file_publisher.DebugFilePublisher, adctk.file_publisher.FilePublisher, adctk.http_publisher.HttpPublisher, adctk.ldms_publisher.LDMSPipePublisher, adctk.ldms_publisher.LDMSSystemPublisher, adctk.ldms_publisher.LDMSPipeMessagePublisher, adctk.ldms_publisher.LDMSSystemMessagePublisher, adctk.multifile_publisher.MultiFilePublisher, adctk.none_publisher.NonePublisher, adctk.stdout_publisher.StdoutPublisher, and adctk.syslog_publisher.SyslogPublisher.

135 def config(self, **kwargs) -> int:
136 """! @brief Configure the plugin with the options given.
137 For plugin QQQ, Environment variables ADC_QQQ_PLUGIN_<key> will override the plugin
138 default for any key not defined in kwargs. Here QQQ is plugin.name().upper().
139 Generic options:
140
141 @param kwargs If env_prefix is included in kwargs, replaces plugin environment variables
142 with those which are prefixed using env_prefix.
143
144 @param env_prefix Typically, env_prefix is set to "PPP_ADC_QQQ_PLUGIN_" if application PPP wants to
145 override the defaults of adctk plugin QQQ. Here QQQ is plugin.name().upper().
146 If no prefixed version of the variable is present, the plugin default is used.
147 kwargs takes precedence over environment variables. Plugin defaults are used
148 if not overridden by kwargs or an environment variable.
149
150 @return int non-zero if configuration cannot be completed using the arguments,
151 environment, and default.
152 """
153 raise NotImplementedError("Publisher.config not implemented")
154

◆ finalize()

None adctk.publisher.Publisher.finalize (   self)

Clean up any resources set up in initialize (files, pipes, etc).

Reimplemented in adctk.debug_file_publisher.DebugFilePublisher, adctk.file_publisher.FilePublisher, adctk.http_publisher.HttpPublisher, adctk.ldms_publisher.LDMSPipePublisher, adctk.ldms_publisher.LDMSPipeMessagePublisher, adctk.multifile_publisher.MultiFilePublisher, adctk.none_publisher.NonePublisher, adctk.stdout_publisher.StdoutPublisher, and adctk.syslog_publisher.SyslogPublisher.

163 def finalize(self) -> None:
164 """! Clean up any resources set up in initialize (files, pipes, etc)."""
165 raise NotImplementedError("Publisher.finalize not implemented")
166

◆ get_option_defaults()

dict[str,str] adctk.publisher.Publisher.get_option_defaults (   self)
Returns
modifiable copy of publisher option defaults
131 def get_option_defaults(self) -> dict[str,str]:
132 """! @return modifiable copy of publisher option defaults"""
133 return copy.deepcopy(self.defaults)
134

◆ get_port_default()

str adctk.publisher.Publisher.get_port_default ( )
static

Get the adc server port number to use if it is properly defined in the environment ADC_SERVER_PORT or the default if it is not.

99 def get_port_default() -> str:
100 """! Get the adc server port number to use if it is properly
101 defined in the environment ADC_SERVER_PORT or the default
102 if it is not."""
103 env_name = _ENV_PREFIX + "PORT"
104 env_val = os.getenv(env_name)
105 if not env_val:
106 return _PORT_DEFAULT
107 try:
108 i = int(env_val)
109 except ValueError:
110 i = int(_PORT_DEFAULT)
111 return str(i)
112

◆ get_timeout_defaults()

tuple[float] adctk.publisher.Publisher.get_timeout_defaults ( )
static

Get the adc server connect and response timeouts defined in the environment ADC_SERVER_CONNECT_TIMEOUT and ADC_SERVER_POST_TIMEOUT as a tuple or default values if not defined.

60 def get_timeout_defaults() -> tuple[float]:
61 """! Get the adc server connect and response timeouts
62 defined in the environment ADC_SERVER_CONNECT_TIMEOUT and
63 ADC_SERVER_POST_TIMEOUT as a tuple or default values if not
64 defined.
65 """
66 env_name = _ENV_PREFIX + "CONNECT_TIMEOUT"
67 env_val = os.getenv(env_name)
68 if env_val:
69 try:
70 ct = float(env_val)
71 except ValueError:
72 log.logger.error("adctk.Publisher.get_timeout_defaults: unable to "\
73 "convert %s to float from %s", env_val, env_name)
74 else:
75 ct = _CONNECT_TIMEOUT_DEFAULT
76 env_name = _ENV_PREFIX + "POST_TIMEOUT"
77 env_val = os.getenv(env_name)
78 if env_val:
79 try:
80 pt = float(env_val)
81 except ValueError:
82 log.logger.error("adctk.Publisher.get_timeout_defaults: unable to "\
83 "convert %s to float from %s", env_val, env_name)
84 else:
85 pt = _POST_TIMEOUT_DEFAULT
86 return (ct, pt)
87

◆ get_url_default()

str adctk.publisher.Publisher.get_url_default ( )
static

Get the alleged adc server url base from environment ADC_SERVER_URL or the default if it is not present.

89 def get_url_default() -> str:
90 """! Get the alleged adc server url base from environment ADC_SERVER_URL
91 or the default if it is not present."""
92 env_name = _ENV_PREFIX + "URL"
93 env_val = os.getenv(env_name)
94 if not env_val:
95 return _SERVER_DEFAULT
96 return env_val
97

◆ initialize()

int adctk.publisher.Publisher.initialize (   self)

Set up any resources needed for reuse in publish().

Reimplemented in adctk.debug_file_publisher.DebugFilePublisher, adctk.file_publisher.FilePublisher, adctk.http_publisher.HttpPublisher, adctk.ldms_publisher.LDMSPipePublisher, adctk.ldms_publisher.LDMSPipeMessagePublisher, adctk.multifile_publisher.MultiFilePublisher, adctk.none_publisher.NonePublisher, adctk.stdout_publisher.StdoutPublisher, and adctk.syslog_publisher.SyslogPublisher.

155 def initialize(self) -> int:
156 """! Set up any resources needed for reuse in publish()."""
157 raise NotImplementedError("Publisher.initialize not implemented")
158

◆ name()

str adctk.publisher.Publisher.name (   self)

Get the name which a factory or error message should use.

123 def name(self) ->str:
124 """! Get the name which a factory or error message should use."""
125 return self._name
126

◆ pause()

None adctk.publisher.Publisher.pause (   self)

Cause publish calls to be ignored until resume is called.

167 def pause(self) -> None:
168 """! Cause publish calls to be ignored until resume is called."""
169 self.paused = True
170

◆ publish()

int adctk.publisher.Publisher.publish (   self,
builder.Builder  b 
)

Publish json extracted from builder.

Reimplemented in adctk.http_publisher.HttpPublisher, adctk.debug_file_publisher.DebugFilePublisher, adctk.file_publisher.FilePublisher, adctk.ldms_publisher.LDMSPipePublisher, adctk.ldms_publisher.LDMSPipeMessagePublisher, adctk.multifile_publisher.MultiFilePublisher, adctk.none_publisher.NonePublisher, adctk.stdout_publisher.StdoutPublisher, and adctk.syslog_publisher.SyslogPublisher.

159 def publish(self, b: builder.Builder) -> int:
160 """! Publish json extracted from builder"""
161 raise NotImplementedError("Publisher.publish not implemented")
162

◆ publish_obj()

int adctk.publisher.Publisher.publish_obj (   self,
  obj 
)
 Deprecated function. Call publish_str with the result of json.dumps(obj), if possible.
Returns EINVAL when dumps fails.
Use of this method is discouraged

Reimplemented in adctk.debug_file_publisher.DebugFilePublisher, and adctk.http_publisher.HttpPublisher.

199 def publish_obj(self, obj) -> int:
200 """ Deprecated function. Call publish_str with the result of json.dumps(obj), if possible.
201 Returns EINVAL when dumps fails.
202 Use of this method is discouraged"""
203 try:
204 js = json.dumps(obj)
205 return self.publish_str(js)
206 except TypeError:
207 return errno.EINVAL
208 return 0
209

◆ publish_str()

int adctk.publisher.Publisher.publish_str (   self,
str  json_string 
)
 Deprecated function; prefer publish() Publish json string supplied by user,
by creating a Builder and
defining the application block to contain the string given as a single
field. Use of this method is discouraged, as it hides important
datatype information from downstream consumers and it must generate
an application name.

Reimplemented in adctk.debug_file_publisher.DebugFilePublisher, and adctk.http_publisher.HttpPublisher.

175 def publish_str(self, json_string: str) -> int:
176 """ Deprecated function; prefer publish() Publish json string supplied by user,
177 by creating a Builder and
178 defining the application block to contain the string given as a single
179 field. Use of this method is discouraged, as it hides important
180 datatype information from downstream consumers and it must generate
181 an application name."""
182 from . import factory # break import loop
183 if json_string is None:
184 return errno.ENODATA
185 af = factory.Factory()
186 b = af.get_builder()
187 # make header
188 app = f"{getpass.getuser()}.publish_str"
189 b.add_header_section(app)
190 # make 'application' section
191 ads = factory.Factory.get_builder()
192 ads.add_json_string("payload", json_string)
193 ads.add("argv", sys.argv)
194 b.add_app_data_section(ads)
195 # send the composed message
196 self.publish(builder)
197 return 0
198

◆ resume()

None adctk.publisher.Publisher.resume (   self)

Cancel an prior calls to pause.

171 def resume(self) -> None:
172 """! Cancel an prior calls to pause."""
173 self.paused = False
174

◆ version()

str adctk.publisher.Publisher.version (   self)
Returns
publisher version
127 def version(self) ->str:
128 """! @return publisher version"""
129 return self._version
130

Member Data Documentation

◆ _name

str adctk.publisher.Publisher._name = "unknown"
staticprotected

◆ _version

str adctk.publisher.Publisher._version = "0.0.0"
staticprotected

◆ API_VERSION

adctk.publisher.Publisher.API_VERSION = adc_version.AdcVersion()._dict()
static

◆ defaults

adctk.publisher.Publisher.defaults

◆ mode

adctk.publisher.Publisher.mode

◆ paused

adctk.publisher.Publisher.paused

◆ plugin_prefix

adctk.publisher.Publisher.plugin_prefix

◆ state

adctk.publisher.Publisher.state

The documentation for this class was generated from the following file: