Output
cifflow.output.emit
CIF emission from a populated SQLite database.
emit(conn, schema, ...) reads structured tables and the _cif_fallback
table and produces a valid CIF string.
Assumption: by emission time, all data in the database is assumed to belong to a single coherent dataset. Namespace conflicts (e.g. short identifiers from unrelated sources) are not detected or resolved by the output layer.
emit(conn, schema, *, mode=EmitMode.ORIGINAL, version=CifVersion.CIF_2_0, plan=None, reconstruct_su=False, emit_defaults=True, line_ending='\n', pretty=True, line_limit=2048)
Emit CIF text from a populated SQLite database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
DuckDBPyConnection
|
Open |
required |
schema
|
SchemaSpec
|
The |
required |
mode
|
EmitMode
|
How the database is partitioned into CIF blocks. |
ORIGINAL
|
version
|
CifVersion
|
CIF version to emit. Controls quoting strategy. |
CIF_2_0
|
plan
|
OutputPlan | None
|
Optional ordering and grouping specification. |
None
|
reconstruct_su
|
bool
|
When |
False
|
emit_defaults
|
bool
|
When |
True
|
line_ending
|
str
|
Line terminator sequence written between every line and at the end of
the output. Use |
'\n'
|
pretty
|
bool
|
When |
True
|
line_limit
|
int | None
|
Maximum physical line length (in characters, before line endings are
applied). Default When a content line inside a semicolon-delimited text field exceeds
line_limit, the CIF 2.0 line-folding protocol (§5.3) is applied.
When Inline scalar values whose formatted line (tag + separator + token) would exceed line_limit are converted to semicolon-delimited fields. Loop data rows that exceed line_limit are wrapped across multiple physical lines using greedy token packing (tokens cannot be split). CIF 1.1 block codes, data names, and frame codes are independently limited to 75 characters by the CIF 1.1 specification; an exception is raised if this limit would be violated. |
2048
|
Returns:
| Type | Description |
|---|---|
str
|
Complete CIF text including magic line, terminated with |
Source code in src/cifflow/output/emit.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
cifflow.output.plan
Output plan dataclasses and EmitMode enum.
EmitMode
Bases: Enum
Controls how the database is partitioned into CIF blocks.
ONE_BLOCK
All data collapsed into a single CIF block named 'output'.
ALL_BLOCKS
One CIF block per schema category, plus one block per original
_cifflow_block_id from _cif_fallback.
ORIGINAL
Rows are grouped into blocks by their original _cifflow_block_id value,
reconstructing the CIF blocks as they were before ingestion. This is
the simple inverse of ingestion and the default.
GROUPED Rows are grouped by Set-category anchor key values. For each table the FK graph is searched (BFS) for the nearest Set-class ancestor:
- If a Set is reachable, that Set is the anchor. Tables with
composite keys — where some FK paths lead to Loop tables and others
lead to a Set — are correctly anchored to the Set even when the Set
path is not the first FK in the list.
- If no Set is reachable (the FK chain terminates at Loop tables only),
those tables fall back to ``_cifflow_block_id`` grouping (equivalent to
ORIGINAL for those tables).
- Keyless Set categories (those whose primary key is ``_cifflow_id``
rather than a domain key) carry no cross-block identity; they also
fall back to ``_cifflow_block_id`` grouping.
All tables that share the same Set anchor and the same anchor key
values are emitted in a single output block, merging rows from
multiple original data blocks that carry the same Set-level identity.
Source code in src/cifflow/output/plan.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
BlockSpec
dataclass
Emission specification for a group of output blocks.
Attributes:
| Name | Type | Description |
|---|---|---|
matches |
MatchPredicate
|
Predicate for block routing. Accepted forms:
First-match wins across the ordered list in |
category_order |
list[str | list[str]]
|
Categories in emission order within a block. A plain |
single_block |
bool
|
When |
column_order |
dict[str, list[str]]
|
|
block_namer |
Callable[[dict[str, list[str]]], str] | None
|
Optional per-spec block name override. Receives a dict mapping
|
attach_to |
MatchPredicate
|
When set, this block is not emitted standalone. Instead its table
rows are merged into the first already-resolved output block whose
anchor and tables frozensets satisfy this predicate (same forms as
|
Source code in src/cifflow/output/plan.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
__post_init__()
Normalise and validate fields after dataclass initialisation.
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
Source code in src/cifflow/output/plan.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
OutputPlan
dataclass
Optional ordering and grouping specification for :func:emit.
Attributes:
| Name | Type | Description |
|---|---|---|
specs |
list[BlockSpec]
|
Ordered list of :class: Emission order: all blocks assigned to An empty list (default) means all blocks use default ordering. |
block_namer |
Callable[[dict[str, list[str]]], str] | None
|
Global fallback block_namer (same signature as
|
Source code in src/cifflow/output/plan.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
match(anchors, tables)
Return (index, spec) of the first matching spec, or (None, None).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchors
|
frozenset[str]
|
Frozenset of Set-category table names that have rows in the block. |
required |
tables
|
frozenset[str]
|
Frozenset of all table names present in the block (Set + Loop). |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, BlockSpec] | tuple[None, None]
|
|
Source code in src/cifflow/output/plan.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
only(*categories)
Match blocks whose anchor set is exactly the given set — no more, no less.
Source code in src/cifflow/output/plan.py
93 94 95 96 | |
any_of(*categories)
Match blocks containing at least one of categories in the anchor frozenset.
Source code in src/cifflow/output/plan.py
99 100 101 102 | |
all_of(*categories)
Match blocks containing all of categories in the anchor frozenset.
Source code in src/cifflow/output/plan.py
105 106 107 108 | |
has(*categories)
Match blocks containing at least one of categories in the full tables frozenset.
Checks the Set or Loop tables frozenset. Use this to route loop-only blocks that have no Set anchor without writing a lambda.
Source code in src/cifflow/output/plan.py
111 112 113 114 115 116 117 118 | |
namer(*keys, prefix='', suffix='', sep='_', fallback='?')
Return a block_namer that builds a name from anchor key values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
str
|
Anchor key identifiers in For example, a block anchored to diffrn with id='D1' would receive: {'diffrn.id': ['D1']} A bridge block with both pd_phase and pd_diffractogram: {'pd_diffractogram.id': ['D1'], 'pd_phase.id': ['Al2O3']} |
()
|
prefix
|
str
|
String prepended to the result. |
''
|
suffix
|
str
|
String appended to the result. |
''
|
sep
|
str
|
Separator inserted between the extracted values. Default |
'_'
|
fallback
|
str
|
Value used when a key is absent from |
'?'
|
Returns:
| Type | Description |
|---|---|
Callable[[dict[str, list[str]]], str]
|
A |
Examples:
Single key with prefix:
>>> plan = OutputPlan(specs=[BlockSpec(matches='diffrn',
... block_namer=namer('diffrn.id', prefix='structure_'))])
'structure_
Multi-key bridge block:
>>> namer('pd_phase.id', 'pd_diffractogram.id')({'pd_phase.id': ['Al2O3'], 'pd_diffractogram.id': ['D1']})
'Al2O3_D1'
Source code in src/cifflow/output/plan.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
cifflow.output.quote
Value quoting for CIF output.
quote(stored, version) converts a value as stored in the SQLite database
back to a valid CIF token, selecting the least-restrictive delimiter that
produces a correctly round-trippable result.
Storage encoding (from ingest.encode_value):
- PLACEHOLDER . / ? → stored as . / ? (length 1)
- Quoted . / ? → stored as "." / "?" (length 3)
- Container (list / table) → stored as JSON text (CIF 2.0 only)
- Everything else → stored as raw string
quote(stored, version)
Return a valid CIF token for stored, suitable for the given version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stored
|
str
|
The value as retrieved from the SQLite database. Presence-state
encoding from
|
required |
version
|
CifVersion
|
|
required |
Returns:
| Type | Description |
|---|---|
str
|
A valid CIF token. Semicolon-delimited tokens begin with |
Source code in src/cifflow/output/quote.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
make_text_field(s, line_limit=None)
Produce a semicolon-delimited CIF text field for s.
Selects the correct wire format based on content requirements:
+--------------+-------------+-----------------------------+ | needs_prefix | needs_fold | format used | +==============+=============+=============================+ | False | False | plain semicolon | | True | False | prefix-only semicolon | | False | True | fold-only semicolon | | True | True | prefix + fold semicolon | +--------------+-------------+-----------------------------+
needs_prefix is True when s contains '\\n;', which would
otherwise prematurely terminate the field.
needs_fold is True when line_limit is given and at least one
content line in the text field would produce a physical line exceeding
line_limit characters.
Valid for both CIF 1.1 and CIF 2.0 (semicolon fields exist in both).
Source code in src/cifflow/output/quote.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |