Debian Codegraph @

avogadro/qtplugins/scriptfileformats/fileformatscript.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_QTPLUGINS_FILEFORMATSCRIPT_H
7 #define AVOGADRO_QTPLUGINS_FILEFORMATSCRIPT_H
8
9 #include <avogadro/io/fileformat.h>
10
11 #include <avogadro/core/avogadrocore.h>
12
13 #include <QtCore/QString>
14 #include <QtCore/QVariantMap>
15
16 namespace Avogadro {
17 namespace QtGui {
18 class PythonScript;
19 }
20
21 namespace QtPlugins {
22
23 /**
24 * @brief The FileFormatScript class interfaces with external scripts that
25 * implement chemical file reader/writers.
26 *
27 * Script Entry Points
28 * ===================
29 *
30 * The script must handle the following command line arguments:
31 * - `--metadata` Print metadata describing the format and the script's
32 * abilities and exit.
33 * - `--read` Read data from standard input and produce a standard
34 * representation on standard output.
35 * - `--write` Read a standard representation from standard input and write
36 * the formatted result to standard output.
37 *
38 * Identify the Format with `--metadata`
39 * =====================================
40 *
41 * Running the script with the `--metadata` option should print a JSON object
42 * of the following form:
43 ~~~{.js}
44 {
45 "inputFormat": "cml",
46 "outputFormat": "cml",
47 "operations": ["read", "write"],
48 "identifier": "Unique Name",
49 "name": "User-friendly Name",
50 "description": "Description of format.",
51 "specificationUrl": "http://url.specifying.format/if/any/exist",
52 "fileExtensions": ["ext"],
53 "mimeTypes": ["chemical/x-ext"]
54 }
55 ~~~
56 *
57 * Details:
58 * - `inputFormat` indicates the format that the script can convert to the
59 * implemented format by the `--write` command. Allowed values are `"cml"`,
60 * `"cjson"`, or `"xyz"`. See the `--write` documentation for more detail.
61 * - `outputFormat` indicates the format that the script can convert to from the
62 * implemented format by the `--read` command. Allowed values are `"cml"`,
63 * `"cjson"`, `"sdf"`, `"pdb"` or `"xyz"`. See the `--read` documentation for
64 more detail.
65 * - `operations` specifies the scripts capabilities. The array should contain
66 * `"read"` if the script implements the `--read` option, and/or `"write"` if
67 * `--write` is available.
68 * - `identifier` is a unique identifier. The value must only be unique amongst
69 * script formats, as it will be prefixed with "User Script: " internally by
70 * Avogadro.
71 * - `name` is a user-friendly name for the format.
72 * - `description` is a description of the format, along with any relevant help
73 * text for users.
74 * - `specificationUrl` is the URL of the format specification if available
75 * (or relevant web page/wiki otherwise).
76 * - `fileExtensions` is an array specifying the file extensions that this
77 * format supports.
78 * - `mimeTypes` is an array specifying the mime types that this format
79 * supports.
80 * - `bond` is a boolean indicating whether the format expects Avogadro
81 * to perceive bonds after reading the file.
82 *
83 * Required members are
84 * - `operations`
85 * - `inputFormat` (if `"write"` is specified in `operations`)
86 * - `outputFormat` (if `"read"` is specified in `operations`)
87 * - `identifier`
88 * - `name`
89 *
90 * Optional members are
91 * - `description`
92 * - `specificationUrl`
93 * - `fileExtensions`
94 * - `mimeTypes`
95 *
96 * Reading a format with `--read`
97 * ==============================
98 *
99 * If `"read"` is specified in the `operations` `--metadata` output along with
100 * a valid `outputFormat`, Avogadro will call the script with `--read` and
101 * write the implemented format to the script's standard input. The script shall
102 * convert the input to `outputFormat` and print it to standard output.
103 *
104 * Writing a format with `--write`
105 * ===============================
106 *
107 * If `"write"` is specified in the `operations` `--metadata` output along with
108 * a valid `inputFormat`, Avogadro will call the script with `--write` and
109 * write the `inputFormat` to the script's standard input. The script shall
110 * convert the input to the implemented format and print it to standard output.
111 */
112 class FileFormatScript : public Avogadro::Io::FileFormat
113 {
114 public:
115 /** Formats that may be written to the script's input/output formats. */
116 enum Format
117 {
118 NotUsed,
119 Cjson,
120 Cml,
121 Mdl,
122 Pdb,
123 Sdf,
124 Xyz
125 };
126
127 FileFormatScript(const QString& scriptFileName = "");
128 ~FileFormatScript() override;
129
130 /**
131 * Configure the interpreter for package-based (pixi) execution.
132 */
133 void setPackageInfo(const QString& packageDir, const QString& command,
134 const QString& identifier);
135
136 /**
137 * Populate metadata fields from a QVariantMap (e.g. from pyproject.toml)
138 * instead of calling the script with --metadata.
139 */
140 void readMetaData(const QVariantMap& metadata);
141
142 QString scriptFilePath() const;
143
144 Format inputFormat() const { return m_inputFormat; }
145
146 Format outputFormat() const { return m_outputFormat; }
147
148 bool isValid() const { return m_valid; }
149
150 FileFormat* newInstance() const override;
151
152 bool read(std::istream& in, Core::Molecule& molecule) override;
153
154 bool write(std::ostream& out, const Core::Molecule& molecule) override;
155
156 bool readFile(const std::string& fileName, Core::Molecule& molecule) override;
157
158 bool writeFile(const std::string& fileName,
159 const Core::Molecule& molecule) override;
160
161 Operations supportedOperations() const override { return m_operations; }
162
163 std::string identifier() const override { return m_identifier; }
164
165 std::string name() const override { return m_name; }
166
167 std::string description() const override { return m_description; }
168
169 std::string specificationUrl() const override { return m_specificationUrl; }
170
171 std::vector<std::string> fileExtensions() const override
172 {
173 return m_fileExtensions;
174 }
175
176 std::vector<std::string> mimeTypes() const override { return m_mimeTypes; }
177
178 private:
179 static Format stringToFormat(const std::string& str);
180 static std::string formatToString(Format fmt);
181 static Io::FileFormat* createFileFormat(Format fmt);
182 void resetMetaData();
183 void copyMetaDataFrom(const FileFormatScript& other);
184
185 private:
186 QtGui::PythonScript* m_interpreter;
187 bool m_valid;
188 bool m_bondOnRead;
189 bool m_inputModeFile;
190 bool m_outputModeFile;
191 Operations m_operations;
192 Format m_inputFormat;
193 Format m_outputFormat;
194 std::string m_identifier;
195 std::string m_name;
196 std::string m_description;
197 std::string m_specificationUrl;
198 std::vector<std::string> m_fileExtensions;
199 std::vector<std::string> m_mimeTypes;
200 };
201
202 } // namespace QtPlugins
203 } // namespace Avogadro
204
205 #endif // AVOGADRO_QTPLUGINS_FILEFORMATSCRIPT_H