Skip to content

Commit

Permalink
migrate FormatOverview
Browse files Browse the repository at this point in the history
  • Loading branch information
jandom committed Aug 20, 2023
1 parent d9cf5e3 commit 9727694
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 42 deletions.
12 changes: 8 additions & 4 deletions doc/source/scripts/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import print_function

import collections
import os
import pathlib
import sys
Expand Down Expand Up @@ -75,10 +76,12 @@ def _run_method(self, method, *args, **kwargs):
def get_lines(self, *args, **kwargs):
lines = []
for items in self.input_items or self._set_up_input():
try:
lines.append(self.get_line(*items))
except TypeError: # one argument
lines.append(self.get_line(items))
line = (
self.get_line(*items)
if isinstance(items, collections.Iterable)
else self.get_line(items)
)
lines.append(line)
if self.sort:
lines = sorted(lines)
self.lines = lines
Expand All @@ -88,6 +91,7 @@ def get_line(self, *args):
for p in self.preprocess:
self._run_method(p, *args)
for h in self.headings:
print("headings", (h, args))
line.append(self._run_method(h, *args))
for p in self.postprocess:
self._run_method(p, *args)
Expand Down
96 changes: 59 additions & 37 deletions doc/source/scripts/gen_format_overview_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from collections import defaultdict

import base
from base import TableWriter
from core import DESCRIPTIONS
from MDAnalysis import _CONVERTERS, _PARSERS, _READERS, _SINGLEFRAME_WRITERS
Expand All @@ -31,51 +32,72 @@
FAIL = ""


class FormatOverview(TableWriter):
filename = "formats/format_overview.txt"
include_table = "Table of all supported formats in MDAnalysis"
preprocess = ["keys"]
headings = ["File type", "Description", "Topology", "Coordinates", "Read", "Write"]
class FormatOverview:
def __init__(self) -> None:
def _file_type(fmt, handlers, key):
return base.sphinx_ref(txt=fmt, label=key, suffix="-format")

def _set_up_input(self):
return sorted_types

def _file_type(self, fmt, handlers):
return self.sphinx_ref(fmt, self.keys[-1], suffix="-format")

def _keys(self, fmt, handlers):
if fmt in DESCRIPTIONS:
key = fmt
else:
key = list(handlers.values())[0].format[0]

# raise an informative error
if key not in DESCRIPTIONS:
def _keys(fmt, handlers):
if fmt in DESCRIPTIONS:
key = fmt
return key
else:
key = list(handlers.values())[0].format[0]

def _description(self, fmt, handlers):
return DESCRIPTIONS[self.keys[-1]]
# raise an informative error
if key not in DESCRIPTIONS:
key = fmt
return key

def _topology(self, fmt, handlers):
if "Topology parser" in handlers:
return SUCCESS
return FAIL
def _description(fmt, handlers, key):
return DESCRIPTIONS[key]

def _coordinates(self, fmt, handlers):
if "Coordinate reader" in handlers:
return SUCCESS
return FAIL
def _topology(fmt, handlers, key):
if "Topology parser" in handlers:
return SUCCESS
return FAIL

def _read(self, fmt, handlers):
return SUCCESS
def _coordinates(fmt, handlers, key):
if "Coordinate reader" in handlers:
return SUCCESS
return FAIL

def _write(self, fmt, handlers):
if "Coordinate writer" in handlers:
return SUCCESS
if "Converter" in handlers:
def _read(fmt, handlers, key):
return SUCCESS
return FAIL

def _write(fmt, handlers, key):
if "Coordinate writer" in handlers:
return SUCCESS
if "Converter" in handlers:
return SUCCESS
return FAIL

input_items = [
(format, handlers, _keys(format, handlers))
for format, handlers in sorted_types
]

self.table_writer = TableWriter(
filename="formats/format_overview.txt",
include_table="Table of all supported formats in MDAnalysis",
headings=[
"File type",
"Description",
"Topology",
"Coordinates",
"Read",
"Write",
],
input_items=input_items,
columns={
"File type": _file_type,
"Description": _description,
"Topology": _topology,
"Coordinates": _coordinates,
"Read": _read,
"Write": _write,
},
)
self.table_writer.get_lines_and_write_table()


class CoordinateReaders(FormatOverview):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_FILE_TYPES():
def test_FormatOverview(snapshot):
with patch("builtins.open"):
ov = FormatOverview()
assert ov.lines == snapshot
assert ov.table_writer.lines == snapshot


def test_CoordinateReaders(snapshot):
Expand Down

0 comments on commit 9727694

Please sign in to comment.