legacy.py 23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

'''
This module contains functionality to use old 'legacy' NOMAD CoE parsers with the
new nomad@fairdi infrastructure. This covers aspects like the new metainfo, a unifying
wrapper for parsers, parser logging, and a parser backend.
'''

Markus Scheidgen's avatar
Markus Scheidgen committed
21
from typing import Dict, List, Union, Any, Tuple, Type, cast
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
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
from abc import ABCMeta, abstractmethod
import importlib
import os.path
import inspect
from unittest.mock import patch
import logging
import glob
import sys

from nomad import utils, datamodel, config
from nomad.metainfo import (
    SubSection, Quantity, Section, Reference, MResource, MSection, MSectionBound, Property)
from nomad.metainfo.legacy import (
    LegacyMetainfoEnvironment, python_package_mapping, normalize_name)

from .parser import MatchingParser

logger = utils.get_logger(__name__)


class BackendError(Exception):
    pass


class BadContextUri(Exception):
    pass


class AbstractParserBackend(metaclass=ABCMeta):
    '''
    This ABS provides the parser backend interface used by the NOMAD-coe parsers.
    '''

    @abstractmethod
    def metaInfoEnv(self):
        ''' Returns the meta info used by this backend. '''
        pass

    @abstractmethod
    def startedParsingSession(
            self, mainFileUri, parserInfo, parserStatus=None, parserErrors=None):
        '''
        Should be called when the parsing starts.
        ParserInfo should be a valid json dictionary.
        '''
        pass

    @abstractmethod
    def finishedParsingSession(
            self, parserStatus, parserErrors, mainFileUri=None, parserInfo=None,
            parsingStats=None):
        ''' Called when the parsing finishes. '''
        pass

    @abstractmethod
    def openContext(self, contextUri: str):
        ''' Open existing archive data to introduce new data into an existing section. '''
        pass

    @abstractmethod
    def closeContext(self, contextUri: str):
        ''' Close priorly opened existing archive data again. '''
        pass

    @abstractmethod
    def openSection(self, metaName, parent_index=-1):
        ''' Opens a new section and returns its new unique gIndex. '''
        pass

    @abstractmethod
    def closeSection(self, metaName, gIndex):
        '''
        Closes the section with the given meta name and index. After this, no more
        value can be added to this section.
        '''
        pass

    @abstractmethod
    def openNonOverlappingSection(self, metaName):
        ''' Opens a new non overlapping section. '''
        pass

    @abstractmethod
    def setSectionInfo(self, metaName, gIndex, references):
        '''
        Sets info values of an open section references should be a dictionary with the
        gIndexes of the root sections this section refers to.
        '''
        pass

    @abstractmethod
    def closeNonOverlappingSection(self, metaName):
        '''
        Closes the current non overlapping section for the given meta name. After
        this, no more value can be added to this section.
        '''
        pass

    @abstractmethod
    def openSections(self):
        ''' Returns the sections that are still open as metaName, gIndex tuples. '''
        pass

    @abstractmethod
    def addValue(self, metaName, value, gIndex=-1):
        '''
        Adds a json value for the given metaName. The gIndex is used to identify
        the right parent section.
        '''
        pass

    @abstractmethod
    def addRealValue(self, metaName, value, gIndex=-1):
        '''
        Adds a float value for the given metaName. The gIndex is used to identify
        the right parent section.
        '''
        pass

    @abstractmethod
    def addArray(self, metaName, shape, gIndex=-1):
        '''
        Adds an unannitialized array of the given shape for the given metaName.
        The gIndex is used to identify the right parent section.
        This is neccessary before array values can be set with :func:`setArrayValues`.
        '''

    @abstractmethod
    def setArrayValues(self, metaName, values, offset=None, gIndex=-1):
        '''
        Adds values of the given numpy array to the last array added for the given
        metaName and parent gIndex.
        '''
        pass

    @abstractmethod
    def addArrayValues(self, metaName, values, gIndex=-1, override: bool = False):
        '''
        Adds an array with the given numpy array values for the given metaName and
        parent section gIndex. Override determines whether to rewrite exisiting values
        in the backend.
        '''
        pass

    @abstractmethod
    def pwarn(self, msg):
        ''' Used to catch parser warnings. '''
        pass

    @abstractmethod
    def get_sections(self, meta_name: str, g_index: int = -1) -> List[int]:
        ''' Return all gIndices for existing sections of the given meta_name and parent section index. '''
        pass

    @abstractmethod
    def get_value(self, metaName: str, g_index=-1) -> Any:
        '''
        Return the value set to the given meta_name in its parent section of the given index.
        An index of -1 (default) is only allowed if there is exactly one parent section.
        '''
        pass

    @abstractmethod
    def __getitem__(self, key):
        pass


class Backend(AbstractParserBackend):
    '''
    A backend that uses the new metainfo to store all data.

    Arguments:
        metainfo: The main legacy metainfo package name or a legacy metainfo environment
            instance.
        logger: An optional logger.
        domain: An optional domain name.

    Attributes:
        domain: The domain that this backend contains data for.
        env: The metainfo environment (all available definitions).
        resource: The metainfo resource that contains all data.
203
        entry_archive: The root section of the archive behind this backend.
204
205
206
207
        logger: A logger that can be used to log metainfo and backend operation related
            warnings and errors.
    '''

Markus Scheidgen's avatar
Markus Scheidgen committed
208
209
    def __init__(self, metainfo: Union[str, LegacyMetainfoEnvironment], domain: str = None, logger=None):
        assert metainfo is not None
210
211
212
213
214
215

        if logger is None:
            logger = utils.get_logger(__name__)
        self.logger = logger
        self.domain = domain if domain is not None else 'dft'  # TODO

Markus Scheidgen's avatar
Markus Scheidgen committed
216
217
218
219
220
        if isinstance(metainfo, str):
            python_package_name, _ = python_package_mapping(metainfo)
            python_package_name = '.'.join(python_package_name.split('.')[:-1])
            python_module = importlib.import_module(python_package_name)
            metainfo = getattr(python_module, 'm_env')
221

Markus Scheidgen's avatar
Markus Scheidgen committed
222
        self.env: LegacyMetainfoEnvironment = cast(LegacyMetainfoEnvironment, metainfo)
223
224
        self.__legacy_env = None
        self.resource = MResource()
225
226
        self.entry_archive = datamodel.EntryArchive()
        self.resource.add(self.entry_archive)
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

        self.__open_sections: Dict[Tuple[Section, int], MSection] = {}
        self.strict = False  # TODO

        self.reset_status()
        # things that have no real purpose, but are required by some legacy code
        # self._unknown_attributes = {}
        # self._known_attributes = ['results']
        # self.fileOut = io.StringIO()

    def __getitem__(self, key):
        property_def = self.resolve_definition(key, Property)
        section_def = property_def.m_parent
        if section_def.extends_base_section:
            section_def = section_def.base_sections[0]

        if isinstance(property_def, Quantity):
            return self.__open_sections[(section_def, -1)].m_get(property_def)

        elif isinstance(property_def, SubSection):
            return self.__open_sections[(section_def, -1)].m_get_sub_sections(property_def)

    def metaInfoEnv(self):
        if self.__legacy_env is None:
            self.__legacy_env = self.env.legacy_info_env()
        return self.__legacy_env

    def resolve_definition(self, name, section_cls: Type[MSectionBound]) -> MSectionBound:
        return self.env.resolve_definition(normalize_name(name), section_cls)

    def resolve_context(self, context_uri: str):
        path = context_uri.strip('/').split('/')
        path.reverse()
        current = None
        while len(path) > 0:
            section = path.pop()
            if len(path) == 0:
                raise BadContextUri(context_uri)
            index = int(path.pop())
            if current is None:
                section_def = self.resolve_definition(section, Section)
                if section_def is None:
                    raise BadContextUri(context_uri)
                i = 0
271
                for content in self.entry_archive.m_contents():
272
273
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
                    if content.m_follows(section_def):
                        if i == index:
                            current = content
                            break

                        i += 1

            else:
                sub_section_def = self.resolve_definition(section, SubSection)
                if sub_section_def is None:
                    raise BadContextUri(context_uri)
                current = current.m_get_sub_section(sub_section_def, index)

        if current is None:
            raise BadContextUri(context_uri)

        return current

    def openContext(self, context_uri: str):
        ''' Open existing archive data to introduce new data into an existing section. '''
        section = self.resolve_context(context_uri)
        self.__open(section)

    def closeContext(self, context_uri: str):
        ''' Close priorly opened existing archive data again. '''
        section = self.resolve_context(context_uri)
        self.__close(section)

    def __open(self, section):
        if section.m_parent_index != -1:
            self.__open_sections[(section.m_def, section.m_parent_index)] = section

        # here -1 meaning the last opened section
        self.__open_sections[(section.m_def, -1)] = section

    def __close(self, section):
        # TODO
        pass
        # if section.m_parent_index != -1 and self.__open_sections.get((section.m_def, -1)) == section:
        #     del(self.__open_sections[(section.m_def, -1)])
        # del(self.__open_sections[(section.m_def, section.m_parent_index)])

    def openSection(self, name, parent_index: int = -1):
        '''
        It will assume that there is a sub-section def with the given name.
        It will use the latest opened section of the sub-sections parent as the parent
        for the new section.
        An Exception will be known root sections, e.g. 'section_run'.
        '''
        section_def = self.resolve_definition(name, Section)

        if name in datamodel.root_sections:
324
            section = self.entry_archive.m_create(section_def.section_cls)
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514

        else:
            sub_section_def = self.resolve_definition(name, SubSection)
            parent_section_def = sub_section_def.m_parent_as(Section)
            if parent_section_def.extends_base_section:
                parent_section_def = parent_section_def.base_sections[0]

            parent = self.__open_sections[(parent_section_def, parent_index)]
            section = parent.m_create(section_def.section_cls, sub_section_def)

        self.__open(section)
        return section.m_parent_index

    def get_open_section_for_quantity(self, name, g_index):
        ''' Returns the open section that contains the quantity of the given name. '''

        quantity_def = self.resolve_definition(name, Quantity)
        section_def = quantity_def.m_parent_as(Section)
        if section_def.extends_base_section:
            section_def = section_def.base_sections[0]

        section = self.__open_sections[(section_def, g_index)]

        return section, quantity_def

    def closeSection(self, name, g_index):
        # TODO
        if self.strict:
            section_def = self.resolve_definition(name, Section)
            section = self.__open_sections[(section_def, g_index)]
            self.__close(section)

    def openNonOverlappingSection(self, metaName):
        return self.openSection(metaName)

    def setSectionInfo(self, metaName, gIndex, references):
        '''
        Sets info values of an open section references should be a dictionary with the
        gIndexes of the root sections this section refers to.
        '''
        # TODO might be necessary to make references work?
        pass

    def closeNonOverlappingSection(self, name):
        return self.closeSection(name, -1)

    def openSections(self):
        ''' Returns the sections that are still open as metaName, gIndex tuples. '''
        raise NotImplementedError()
        # for section_def, sub_sections in self.open_sections_by_def:
        #     for sub_section in sub_sections:
        #         yield section_def.name, sub_section.m_parent_index

    def addValue(self, name, value, g_index=-1):
        section, quantity_def = self.get_open_section_for_quantity(name, g_index)
        if isinstance(quantity_def.type, Reference):
            # quantity is a reference
            possible_targets = self.resource.all(quantity_def.type.target_section_def.section_cls)
            referenced_target = None
            for target in possible_targets:
                if target.m_parent_index == value:
                    referenced_target = target

            if referenced_target is None:
                raise BackendError('There is not section for the given reference index')

            value = referenced_target

        setattr(section, name, value)

    def addRealValue(self, name, value, g_index=-1):
        self.addValue(name, value, g_index)

    def addArray(self, name, shape, g_index=-1):
        '''
        Adds an uninitialized array of the given shape for the given metaName.
        The gIndex is used to identify the right parent section.
        This is neccessary before array values can be set with :func:`setArrayValues`.
        '''
        raise NotImplementedError()

    def setArrayValues(self, metaName, values, offset=None, gIndex=-1):
        '''
        Adds values of the given numpy array to the last array added for the given
        metaName and parent gIndex.
        '''
        raise NotImplementedError()

    def addArrayValues(self, name, values, gIndex=-1, override: bool = False):
        '''
        Adds an array with the given numpy array values for the given metaName and
        parent section gIndex. Override determines whether to rewrite exisiting values
        in the backend.
        '''
        section, quantity_def = self.get_open_section_for_quantity(name, gIndex)
        if isinstance(quantity_def.type, Reference):
            # quantity is a reference
            possible_targets = self.resource.all(quantity_def.type.target_section_def.section_cls)
            resolved_values = []
            for value in values:
                referenced_target = None
                for target in possible_targets:
                    if target.m_parent_index == value:
                        referenced_target = target

                if referenced_target is None:
                    raise BackendError('There is not section for the given reference index')

                resolved_values.append(referenced_target)
            values = resolved_values

        if self.strict and not override:
            quantity_def = section.m_def.all_quantities[name]
            assert not section.m_is_set(quantity_def)

        setattr(section, name, values)

    # The following are extensions to the origin NOMAD-coe parser backend. And allow
    # access to existing data

    def get_sections(self, meta_name: str, g_index: int = -1) -> List[int]:
        ''' Return all gIndices for existing sections of the given meta_name and parent index. '''
        section_def = self.resolve_definition(meta_name, Section)
        return [
            section.m_parent_index for section in self.resource.all(section_def.section_cls)
            if g_index == -1 or section.m_parent.m_parent_index == g_index]

    def get_value(self, meta_name: str, g_index=-1) -> Any:
        '''
        Return the value set to the given meta_name in its parent section of the given index.
        An index of -1 (default) is only allowed if there is exactly one parent section.
        '''
        section, quantity_def = self.get_open_section_for_quantity(meta_name, g_index)
        value = section.m_get(quantity_def)

        if value is None:
            raise KeyError()

        if isinstance(quantity_def.type, Reference):
            return value.m_parent_index

        return value

    def startedParsingSession(
            self, mainFileUri, parserInfo, parserStatus=None, parserErrors=None):
        self.reset_status()

    def finishedParsingSession(
            self, parserStatus, parserErrors, mainFileUri=None, parserInfo=None,
            parsingStats=None):
        self._status = parserStatus
        self._errors = parserErrors

    def addMatchTelemetry(self, match_telemetry, gIndex=-1):
        # TODO
        pass

    def pwarn(self, msg):
        self.logger.warn(msg)
        if len(self._warnings) < 10:
            self._warnings.append(msg)
        elif len(self._warnings) == 10:
            self._warnings.append('There are more warnings, check the processing logs.')

    @property
    def status(self) -> Tuple[str, List[str]]:
        ''' Returns status and potential errors. '''
        return (self._status, self._errors)

    def reset_status(self) -> None:
        self._status = 'ParseSuccess'
        self._errors = None
        self._warnings: List[str] = []


class LegacyParser(MatchingParser):
    '''
    A parser implementation for legacy NOMAD-coe parsers. It assumes that parsers
    are installed to the python environment.

    Arguments:
        parser_class_name: the main parser class that implements NOMAD-coe's
        backend_factory: a callable that returns a backend, takes meta_info and logger as argument
    '''
    def __init__(self, parser_class_name: str, *args, backend_factory=None, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        self.parser_class_name = parser_class_name
        self.backend_factory = backend_factory

Markus Scheidgen's avatar
Markus Scheidgen committed
515
516
517
518
519
        module_name = self.parser_class_name.split('.')[:-1]
        parser_class = self.parser_class_name.split('.')[-1]
        module = importlib.import_module('.'.join(module_name))
        self.parser_class = getattr(module, parser_class)

520
521
522
        module = importlib.import_module('.'.join(module_name + ['metainfo']))
        self.metainfo_env = getattr(module, 'm_env')

523
524
525
526
    def run(self, mainfile: str, logger=None) -> Backend:
        # TODO we need a homogeneous interface to parsers, but we dont have it right now.
        # There are some hacks to distinguish between ParserInterface parser and simple_parser
        # using hasattr, kwargs, etc.
Markus Scheidgen's avatar
Markus Scheidgen committed
527
528
529
530
531
532

        if issubclass(self.parser_class, CoEParser):
            # TODO reuse parser
            parser = self.parser_class()
            return parser.run(mainfile, logger)

533
534
535
536
537
538
        def create_backend(meta_info):
            if self.backend_factory is not None:
                return self.backend_factory(meta_info, logger=logger)

            return Backend(meta_info, logger=logger, domain=self.domain)

Markus Scheidgen's avatar
Markus Scheidgen committed
539
        init_signature = inspect.getargspec(self.parser_class.__init__)
540
541
542
543
        kwargs = dict(backend=create_backend, log_level=logging.DEBUG, debug=True)
        kwargs = {key: value for key, value in kwargs.items() if key in init_signature.args}

        with utils.legacy_logger(logger):
Markus Scheidgen's avatar
Markus Scheidgen committed
544
            self.parser = self.parser_class(**kwargs)
545
546
547
548
549
550
551
552
553
554
555

            with patch.object(sys, 'argv', []):
                backend = self.parser.parse(mainfile)
                os.chdir(config.fs.working_directory)

            if backend is None or not hasattr(backend, 'status'):
                backend = self.parser.parser_context.super_backend

        return backend


Markus Scheidgen's avatar
Markus Scheidgen committed
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
class CoEParser(metaclass=ABCMeta):

    @abstractmethod
    def run(self, mainfile, logger) -> Backend:
        pass


class CoEInterfaceParser(CoEParser):

    def __init__(self, interface_class):
        super().__init__()
        self.interface_class = interface_class
        self.__interface = None

    def run(self, mainfile, logger):
        if self.__interface is None:
            self.__interface = self.interface_class()

        self.__interface.setup_logger(logger)
        self.__interface.parse(mainfile)
        return self.__interface.parser_context.super_backend


class CoESimpleMatcherParser(CoEParser):

    def __init__(self):
        super().__init__()
        self.parser_description = self.create_parser_description()
        self.simple_matcher = self.create_simple_matcher()

        self._metainfo_env = self.metainfo_env()
        self.__legacy_metainfo_env = None

        self.caching_levels = self.create_caching_levels()

    @abstractmethod
    def metainfo_env(self) -> LegacyMetainfoEnvironment:
        pass

    @property
    def metaInfoEnv(self) -> LegacyMetainfoEnvironment:
        if self.__legacy_metainfo_env is None:
            self.__legacy_metainfo_env = self._metainfo_env.legacy_info_env()
        return self.__legacy_metainfo_env

    def create_caching_levels(self) -> dict:
        return dict()

    @abstractmethod
    def create_simple_matcher(self):
        pass

    @abstractmethod
    def create_parser_description(self) -> dict:
        raise NotImplementedError()

    @abstractmethod
    def create_super_context(self):
        pass

    def simple_parser(self, mainfile, logger) -> Backend:
        from nomadcore.simple_parser import mainFunction
        backend = Backend(self._metainfo_env, logger=logger)
        from unittest.mock import patch
        with patch.object(sys, 'argv', ['<exe>', '--uri', 'nmd://uri', mainfile]):
            mainFunction(
                mainFileDescription=self.simple_matcher,
                metaInfoEnv=self.metaInfoEnv,
                parserInfo=self.parser_description,
                cachingLevelForMetaName=self.caching_levels,
                superContext=self.create_super_context(),
                superBackend=backend)

        return backend

    def run(self, mainfile, logger) -> Backend:
        with utils.legacy_logger(logger):
            return self.simple_parser(mainfile, logger)


636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
class VaspOutcarParser(LegacyParser):
    '''
    LegacyParser that only matches mailfiles, if there is no .xml in the
    same directory, i.e. to use the VASP OUTCAR parser in absence of .xml
    output file.
    '''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.name = 'parsers/vaspoutcar'

    def is_mainfile(self, filename: str, *args, **kwargs) -> bool:
        is_mainfile = super().is_mainfile(filename, *args, **kwargs)

        if is_mainfile:
            directory = os.path.dirname(filename)
            if len(glob.glob('%s/*.xml*' % directory)) > 0:
                return False

        return is_mainfile