Correctly handle the case that the YAML python module is not installed
This commit is contained in:
@ -17,11 +17,18 @@
|
|||||||
# and Axel Kohlmeyer <akohlmey@gmail.com>
|
# and Axel Kohlmeyer <akohlmey@gmail.com>
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
import re, yaml
|
import re
|
||||||
|
|
||||||
|
has_yaml = False
|
||||||
|
try:
|
||||||
|
import yaml
|
||||||
|
has_yaml = True
|
||||||
try:
|
try:
|
||||||
from yaml import CSafeLoader as Loader
|
from yaml import CSafeLoader as Loader
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from yaml import SafeLoader as Loader
|
from yaml import SafeLoader as Loader
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
class LogFile:
|
class LogFile:
|
||||||
"""Reads LAMMPS log files and extracts the thermo information
|
"""Reads LAMMPS log files and extracts the thermo information
|
||||||
@ -66,6 +73,8 @@ class LogFile:
|
|||||||
style = LogFile.STYLE_YAML
|
style = LogFile.STYLE_YAML
|
||||||
yamllog += line;
|
yamllog += line;
|
||||||
current_run = {}
|
current_run = {}
|
||||||
|
if not has_yaml:
|
||||||
|
raise Exception('Cannot process YAML format logs without the PyYAML Python module')
|
||||||
|
|
||||||
elif re.match(r'^\.\.\.$', line):
|
elif re.match(r'^\.\.\.$', line):
|
||||||
thermo = yaml.load(yamllog, Loader=Loader)
|
thermo = yaml.load(yamllog, Loader=Loader)
|
||||||
|
|||||||
@ -2,11 +2,16 @@ import os
|
|||||||
import unittest
|
import unittest
|
||||||
from lammps.formats import LogFile, AvgChunkFile
|
from lammps.formats import LogFile, AvgChunkFile
|
||||||
|
|
||||||
|
has_yaml = False
|
||||||
|
try:
|
||||||
import yaml
|
import yaml
|
||||||
|
has_yaml = True
|
||||||
try:
|
try:
|
||||||
from yaml import CSafeLoader as Loader, CSafeDumper as Dumper
|
from yaml import CSafeLoader as Loader, CSafeDumper as Dumper
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from yaml import SafeLoader, SafeDumper
|
from yaml import SafeLoader, SafeDumper
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
EXAMPLES_DIR=os.path.abspath(os.path.join(__file__, '..', '..', '..', 'examples'))
|
EXAMPLES_DIR=os.path.abspath(os.path.join(__file__, '..', '..', '..', 'examples'))
|
||||||
|
|
||||||
@ -65,6 +70,7 @@ class Logfiles(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(run0["Step"], list(range(0,350, 50)))
|
self.assertEqual(run0["Step"], list(range(0,350, 50)))
|
||||||
|
|
||||||
|
@unittest.skipIf(not has_yaml,"Missing the PyYAML python module")
|
||||||
def testYamlLogFile(self):
|
def testYamlLogFile(self):
|
||||||
log = LogFile(os.path.join(EXAMPLES_DIR, YAML_STYLE_EXAMPLE_LOG))
|
log = LogFile(os.path.join(EXAMPLES_DIR, YAML_STYLE_EXAMPLE_LOG))
|
||||||
self.assertEqual(len(log.runs), 2)
|
self.assertEqual(len(log.runs), 2)
|
||||||
@ -127,7 +133,7 @@ try:
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@unittest.skipIf(not has_dump_yaml, "Either atom_style full or dump_style yaml are not available")
|
@unittest.skipIf(not (has_dump_yaml and has_yaml), "Either atom_style full, dump_style yaml, or the python PyYAML module are not available")
|
||||||
class PythonDump(unittest.TestCase):
|
class PythonDump(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
machine = None
|
machine = None
|
||||||
|
|||||||
Reference in New Issue
Block a user