Skip to content

Commit

Permalink
Merge pull request #7 from nlesc-nano/devel
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
BvB93 authored Sep 6, 2019
2 parents 5609bc3 + 191613b commit 9d0690a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ Change Log
All notable changes to this project will be documented in this file.
This project adheres to `Semantic Versioning <http://semver.org/>`_.

1.0.1
*****

Changed
-------
* Trailing whitespaces are now removed from keys.
* Content of the `&COORD`_ block is now converted into a
dictionary with ``"_1"`` as key and a list of atomic symbols + coordinates
as values.

.. _`&COORD`: https://manual.cp2k.org/cp2k-6_1-branch/CP2K_INPUT/FORCE_EVAL/SUBSYS/COORD.html


1.0.0
*****

Added
-----

* Release of CP2K-Parser
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
.. image:: https://travis-ci.org/nlesc-nano/CP2K-Parser.svg?branch=master
:target: https://travis-ci.org/nlesc-nano/CP2K-Parser
|

|
.. image:: https://img.shields.io/badge/python-3.5-blue.svg
:target: https://www.python.org

.. image:: https://img.shields.io/badge/python-3.6-blue.svg
:target: https://www.python.org

.. image:: https://img.shields.io/badge/python-3.7-blue.svg
:target: https://www.python.org

CP2K-Parser 1.0.0
CP2K-Parser 1.0.1
#################

A package for converting CP2K_ input files into PLAMS_ compatible dictionaries.
Expand Down
2 changes: 1 addition & 1 deletion cp2kparser/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.0'
__version__ = '1.0.1'
40 changes: 32 additions & 8 deletions cp2kparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def split_str(item: str,
The first string is decapitalized.
"""
sep = sep or ' '
sep = sep if sep is not None else ' '
try:
key, value = item.split(sep, maxsplit=1)
except ValueError:
Expand Down Expand Up @@ -204,10 +204,10 @@ def parse_multi_keys(item: str,
Raised if **item** does not contain any whitespaces.
"""
sep = sep or ' '
sep = sep if sep is not None else ' '
i1, i2 = item.split(sep, maxsplit=1)
i1 = i1.lstrip('&').lower()
return ''.join((i1, sep, i2))
return sep.join((i1, i2))


def parse_header(input_gen: Generator[str, None, None],
Expand Down Expand Up @@ -238,7 +238,7 @@ def parse_header(input_gen: Generator[str, None, None],
container[key].append({})
recursive_update(input_gen, container[key][-1])

# A duplicate key; convert container[key] into a list of dictionaries
# container[key] already exists; convert container[key] into a list of dictionaries
elif key in container:
value_old = container[key]
container[key] = [value_old, {}]
Expand Down Expand Up @@ -271,6 +271,28 @@ def parse_block(item: str,
container[key] = value


def parse_coord_block(input_gen: Generator[str, None, None],
container: Union[dict, MutableSequence]) -> None:
"""Parse ``"coord"`` blocks.
Parameters
----------
item : :class:`str`
A string containing a key and value.
container : :class:`dict` or :class:`collections.abc.MutableSequence`
A to-be filled dictionary or mutable sequence.
"""
item = next(input_gen)
container['coord'] = {}
container['coord']['_1'] = coord = []
while item.lower() != '&end':
coord.append(item)
item = next(input_gen)
return


def recursive_update(input_gen: Generator[str, None, None],
container: Union[dict, MutableSequence]) -> None:
r"""Update **container** an a recursive manner.
Expand All @@ -286,11 +308,13 @@ def recursive_update(input_gen: Generator[str, None, None],
"""
for item in input_gen:
if item[0] == '&' and item[0:4].lower() != '&end': # Beginning of a block
if item.lower() == '&coord':
parse_coord_block(input_gen, container)
elif item[0] == '&' and item[0:4].lower() != '&end':
parse_header(input_gen, item, container)
elif item[0:4].lower() != '&end': # End of a block
elif item[0:4].lower() != '&end':
parse_block(item, container)
else:
else: # End of a block
return


Expand Down Expand Up @@ -403,7 +427,7 @@ def read_input(filename: Union[str, bytes, PurePath]) -> dict:
"""
def _sanitize_line(item: str) -> str:
"""Sanitize and return a single line from **filename**."""
return item.rstrip('\n').replace('\t', '').lstrip(' ')
return item.rstrip('\n').rstrip().replace('\t', '').lstrip(' ')

with open(filename, 'r') as f:
input_list = [_sanitize_line(item) for item in f if item != '\n']
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
'python-3-7',
'dictionary',
'parsing',
'cp2k'
'cp2k',
'plams'
],
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down

0 comments on commit 9d0690a

Please sign in to comment.