aiida_vasp.parsers.content_parsers.win#

The .win parser interface.


Contains routines to parse Wannier90 input files. Will in the future utilize the parser in the Wannier90 plugin, but no input parser exists yet.

Module Contents#

Classes#

BaseKeyValueParser

Common codebase for all parser utilities.

KeyValueParser

Key and value parser.

WinParser

Parses wannier90 input files.

API#

class aiida_vasp.parsers.content_parsers.win.BaseKeyValueParser[source]#

Common codebase for all parser utilities.

This class provides utility methods for parsing key-value and line-based data.

empty_line = 'compile(...)'#
classmethod line(fobj_or_str: str | Any, d_type: type = str) Any[source]#

Grab a line from a file object or string and convert it to d_type (default: str).

Parameters:
  • fobj_or_str (file or str) – File object or string to read the line from.

  • d_type (type) – Type to convert each item in the line to. Defaults to str.

Returns:

Single value or list of values, depending on the number of items in the line.

classmethod splitlines(fobj_or_str: str | Any, d_type: type = float) list[Any][source]#

Split a chunk of text into a list of lines and convert each line to d_type (default: float).

Parameters:
  • fobj_or_str (file or str) – File object or string to split into lines.

  • d_type (type) – Type to convert each item in the line to. Defaults to float.

Returns:

List of values, one per line.

class aiida_vasp.parsers.content_parsers.win.KeyValueParser[source]#

Bases: aiida_vasp.parsers.content_parsers.win.BaseKeyValueParser

Key and value parser.

This base class provides utility functions for parsing files that are (mostly) in a key = value format.

Note

This class does not integrate with the VaspParser class currently.

Example usage:

import re
from aiida_vasp.parsers.file_parsers.parser import KeyValueParser

class ParamParser(KeyValueParser):
    def __init__(self, file_path):
        self._file_path = py.path.local(file_path)
        super().__init__()
        self.result = {}
    def convert_or_not(self, value):
        for converter in self.get_converter_iter():
            converted = self.try_convert(value, converter)
            if converted and 'value' in converted:
                return converted['value']
        return value
    def parse_file(self):
        assignments = re.findall(self.assignment, self._file_path.read())
        return {key: self.convert_or_not(value)}

Parses files like:

StrParam = value_1
FloatParam = 1.0
BoolParam = True
assignment = 'compile(...)'#
bool_true = 'compile(...)'#
bool_false = 'compile(...)'#
comment = True#
classmethod get_lines(filename: str) list[str][source]#

Read all lines from a file.

Parameters:

filename (str) – Path to the file.

Returns:

List of lines from the file.

Return type:

list[str]

classmethod retval(*args: Any, **kwargs: Any) dict[str, Any][source]#

Normalize return values from value conversion functions.

Returns:

Dictionary with the value and any additional keyword arguments.

Return type:

dict

classmethod flatten(lst: list[list[Any]]) list[Any][source]#

Flatten a list of lists into a single list.

Parameters:

lst (list) – List of lists.

Returns:

Flattened list.

Return type:

list

classmethod find_kv(line: str) list[tuple[str, str]][source]#

Find key-value pairs in a line using the assignment regex.

Parameters:

line (str) – Line to search for key-value pairs.

Returns:

List of (key, value) tuples.

Return type:

list[tuple]

classmethod float(string_: str) dict[str, Any][source]#

Parse a string into a float value followed by a comment.

Parameters:

string (str) – String to parse.

Returns:

Dictionary with value and comment.

Return type:

dict

classmethod float_unit(string_: str) dict[str, Any][source]#

Parse string into a float number with attached unit.

Parameters:

string (str) – String to parse.

Returns:

Dictionary with value, unit, and comment.

Return type:

dict

classmethod int(string_: str) dict[str, Any][source]#

Parse a string into an integer value followed by a comment.

Parameters:

string (str) – String to parse.

Returns:

Dictionary with value and comment.

Return type:

dict

classmethod int_unit(string_: str) dict[str, Any][source]#

Convert a string into a python value, associated unit and optional comment.

Parameters:

string (str) – String to parse.

Returns:

Dictionary with value, unit, and comment.

Return type:

dict

classmethod string(string_: str) dict[str, Any][source]#

Parse a string into value and comment, assuming only the first word is the value.

Parameters:

string (str) – String to parse.

Returns:

Dictionary with value and comment.

Return type:

dict

classmethod bool(string_: str) dict[str, Any][source]#

Parse string into a boolean value.

Parameters:

string (str) – String to parse.

Returns:

Dictionary with value and comment.

Return type:

dict

Raises:

ValueError – If the string does not match a boolean pattern.

classmethod kv_list(filename: str) list[Any][source]#

Read a file and return a list of key-value pairs for each line.

Parameters:

filename (str) – Path to the file.

Returns:

List of key-value pairs.

Return type:

list

classmethod kv_dict(kv_list: list[Any]) dict[str, Any][source]#

Convert a list of key-value pairs into a dictionary.

Parameters:

kv_list (list) – List of key-value pairs.

Returns:

Dictionary of key-value pairs.

Return type:

dict

classmethod clean_value(str_value: str) dict[str, Any][source]#

Get the converted python value from a string.

Parameters:

str_value (str) – String value to convert.

Returns:

Dictionary with the converted value.

Return type:

dict

classmethod get_converter_iter() Any[source]#

Get an iterator over the value converter functions in order.

Returns:

Iterator over converter functions.

classmethod try_convert(input_value: str, converter: Callable[[str], dict[str, Any]]) dict[str, Any] | None[source]#

Try to convert the input string into a python value given a conversion function.

Parameters:
  • input_value (str) – Value to convert.

  • converter (callable) – Converter function to use.

Returns:

Dictionary with the converted value, or None if conversion failed.

Return type:

dict or None

class aiida_vasp.parsers.content_parsers.win.WinParser(path: str)[source]#

Bases: aiida_vasp.parsers.content_parsers.win.KeyValueParser

Parses wannier90 input files.

This parser extracts keywords, blocks, and comments from a Wannier90 .win input file.

Initialization

Initialize the parser and parse the Wannier90 input file.

Parameters:

path (str) – Path to the Wannier90 .win file.

block = 'compile(...)'#
comment = 'compile(...)'#
classmethod parse_win(fobj_or_str: str | Any) tuple[dict[str, Any], dict[str, list[str]], list[str]][source]#

Parse a Wannier90 input file or string.

Parameters:

fobj_or_str (file or str) – File object or string containing the Wannier90 input.

Returns:

Tuple of (keywords dict, blocks dict, comments list).

Return type:

tuple