ADCT Python
Python client API implementation for publishing ADCT data
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Static Protected Attributes | List of all members
adctk.file_publisher.FilePublisher Class Reference

This plugin generates and writes each message to the configured file, with \<adct-json>\</adct-json> delimiters surrounding it. More...

Inheritance diagram for adctk.file_publisher.FilePublisher:
Inheritance graph
[legend]
Collaboration diagram for adctk.file_publisher.FilePublisher:
Collaboration graph
[legend]

Public Member Functions

 __init__ (self)
 
int config (self, **kwargs)
 
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).
 
- Public Member Functions inherited from adctk.publisher.Publisher
str name (self)
 Get the name which a factory or error message should use.
 
str version (self)
 
dict[str, str] get_option_defaults (self)
 
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)
 

Public Attributes

 defaults
 
 paused
 
 plugin_prefix
 
 debug
 
 fname
 
 fdir
 
 abspath
 
 fappend
 
 out
 
 mode
 
 state
 
- Public Attributes inherited from adctk.publisher.Publisher
 defaults
 
 plugin_prefix
 
 paused
 
 state
 
 mode
 

Static Protected Attributes

str _name = "file"
 
str _version = "1.0.0"
 
- Static Protected Attributes inherited from adctk.publisher.Publisher
str _name = "unknown"
 
str _version = "0.0.0"
 

Additional Inherited Members

- Static Public Member Functions inherited from adctk.publisher.Publisher
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.
 
- Static Public Attributes inherited from adctk.publisher.Publisher
 API_VERSION = adc_version.AdcVersion()._dict()
 
- Protected Member Functions inherited from adctk.publisher.Publisher
str _get (self, dict[str, str] opts, str field)
 
bool _get_bool (self, dict[str, str] opts, str field)
 

Detailed Description

This plugin generates and writes each message to the configured file, with \<adct-json>\</adct-json> delimiters surrounding it.

The output directory is "." by default, but may be overriden with a full path defined in env("ADC_FILE_PLUGIN_DIRECTORY"). The file name is adc.file_plugin.log by default, and may be overridden with a path name in env("ADC_FILE_PLUGIN_FILE"). The file is overwritten when the file_plugin is created, unless env("ADC_FILE_PLUGIN_APPEND") is "true". Debug output (normally to stderr) is enabled if env("ADC_FILE_PLUGIN_DEBUG") is a number greater than 0.

Multiple independent file publishers may be created; if the same file name and directory are used for distinct instances, output file content is undefined.

Environment Variables:

Constructor & Destructor Documentation

◆ __init__()

adctk.file_publisher.FilePublisher.__init__ (   self)

Reimplemented from adctk.publisher.Publisher.

45 def __init__(self):
46 super().__init__()
47 self.defaults = _defaults
48 self.paused = False
49 self.plugin_prefix = "ADC_FILE_PLUGIN_"
50 self.debug = 0
51 self.fname = None
52 self.fdir = None
53 self.abspath = None
54 self.fappend = False
55 self.out = None
56

Member Function Documentation

◆ config()

int adctk.file_publisher.FilePublisher.config (   self,
**  kwargs 
)
The config options for the file plugin are directory=<directory>,
   file=<leafname>, append="true" and debug=<int>.
   If not specified, then the environment variables ADC_FILE_PLUGIN_*
   are checked, where * is DIRECTORY, FILE, APPEND, and DEBUG.

Reimplemented from adctk.publisher.Publisher.

57 def config(self, **kwargs) -> int:
58 """The config options for the file plugin are directory=<directory>,
59 file=<leafname>, append="true" and debug=<int>.
60 If not specified, then the environment variables ADC_FILE_PLUGIN_*
61 are checked, where * is DIRECTORY, FILE, APPEND, and DEBUG.
62 """
63 try:
64 self.fappend = self._get_bool(kwargs, "APPEND")
65 self.fdir = self._get(kwargs, "DIRECTORY")
66 self.fname = self._get(kwargs, "FILE")
67 self.debug = int( self._get(kwargs, "DEBUG") )
68 if self.debug > 0:
69 log.logger.info("file plugin configured")
70 self.mode = publisher.Publisher.Mode.INIT
71 return 0
72 except ValueError as e:
73 log.logger.error("adctk.Filepublisher.Publisher.config: invalid options"\
74 "or environment %s* values: %s", self.plugin_prefix, e)
75 return 1
76

◆ finalize()

None adctk.file_publisher.FilePublisher.finalize (   self)

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

Reimplemented from adctk.publisher.Publisher.

148 def finalize(self) -> None:
149 self.state = publisher.Publisher.State.OK
150 self.paused = False
151 self.mode = publisher.Publisher.Mode.CONFIG
152 if self.out:
153 self.out.close()
154 self.out = None

◆ initialize()

int adctk.file_publisher.FilePublisher.initialize (   self)

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

Reimplemented from adctk.publisher.Publisher.

77 def initialize(self) -> int:
78 # pylint: disable=too-many-return-statements
79 if self.state == publisher.Publisher.State.ERR:
80 return errno.ENOTRECOVERABLE
81 if self.mode == publisher.Publisher.Mode.CONFIG:
82 self.config()
83 if self.mode != publisher.Publisher.Mode.INIT:
84 return errno.EINVAL
85 if self.state == publisher.Publisher.State.ERR:
86 log.logger.error("adctk.FilePublisher.initialize: unexpected 'state' found.")
87 return errno.ENOTRECOVERABLE
88 checkdir = pathlib.Path(self.fdir)
89 dirmade = False
90 try:
91 checkdir.mkdir(parents=True, exist_ok=True)
92 dirmade = True
93 except FileExistsError:
94 log.logger.error("adctk.FilePublisher.initialize: An existing file cannot be "\
95 "converted to a directory in path %s", self.fdir)
96 self.state = publisher.Publisher.State.ERR
97 return errno.EEXIST
98 except PermissionError:
99 log.logger.error("adctk.FilePublisher.initialize: permission denied "\
100 "along path %s", self.fdir)
101 self.state = publisher.Publisher.State.ERR
102 return errno.EPERM
103 except OSError as e:
104 log.logger.error("adctk.FilePublisher.initialize: unexpected OSError. %s", e)
105 self.state = publisher.Publisher.State.ERR
106 return errno.EIO
107 if not dirmade:
108 self.state = publisher.Publisher.State.ERR
109 return errno.EINVAL
110 self.abspath = (pathlib.Path(self.fdir) / self.fname).absolute()
111 if self.fappend:
112 filemode = "ab"
113 else:
114 filemode = "wb"
115 try:
116 # pylint: disable-next=consider-using-with
117 self.out = open(self.abspath, filemode)
118 if self.out is None:
119 self.state = publisher.Publisher.State.ERR
120 return errno.EBADF
121 self.mode = publisher.Publisher.Mode.PUB_OR_FINAL
122 return 0
123 except (FileNotFoundError, PermissionError, IsADirectoryError, IOError, OSError):
124 log.logger.error("adctk.FilePublisher.initialize: unable to open %s", self.abspath)
125 self.state = publisher.Publisher.State.ERR
126 self.out = None
127 return errno.EBADF
128

◆ publish()

int adctk.file_publisher.FilePublisher.publish (   self,
builder.Builder  b 
)

Publish json extracted from builder.

Reimplemented from adctk.publisher.Publisher.

129 def publish(self, b: builder.Builder) -> int:
130 if self.paused:
131 return 0
132 if self.state != publisher.Publisher.State.OK:
133 return 1
134 if self.mode != publisher.Publisher.Mode.PUB_OR_FINAL:
135 return 2
136 if self.out is None:
137 return 3
138 try:
139 message = f"<adct-json>{b.serialize()}</adct-json>\n"
140 self.out.write(message.encode('utf-8'))
141 except (FileNotFoundError, PermissionError, IsADirectoryError, IOError, OSError):
142 self.state = publisher.Publisher.State.ERR
143 log.logger.error("adctk.FilePublisher.publish: write failed to %s", self.abspath)
144 self.out.close()
145 self.out = None
146 return 0
147

Member Data Documentation

◆ _name

str adctk.file_publisher.FilePublisher._name = "file"
staticprotected

◆ _version

str adctk.file_publisher.FilePublisher._version = "1.0.0"
staticprotected

◆ abspath

adctk.file_publisher.FilePublisher.abspath

◆ debug

adctk.file_publisher.FilePublisher.debug

◆ defaults

adctk.file_publisher.FilePublisher.defaults

◆ fappend

adctk.file_publisher.FilePublisher.fappend

◆ fdir

adctk.file_publisher.FilePublisher.fdir

◆ fname

adctk.file_publisher.FilePublisher.fname

◆ mode

adctk.file_publisher.FilePublisher.mode

◆ out

adctk.file_publisher.FilePublisher.out

◆ paused

adctk.file_publisher.FilePublisher.paused

◆ plugin_prefix

adctk.file_publisher.FilePublisher.plugin_prefix

◆ state

adctk.file_publisher.FilePublisher.state

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