#
# Copyright (C) 2021 Satoru SATOH <satoru.satoh@gmail.com>
# License: MIT
#
"""File based test data collector.
"""
import inspect
import pathlib
import typing

from . import (
    constants, datatypes, utils
)


DICT_0 = dict()


class TDataCollector:
    """File based test data collector.
    """
    target: str = ''  # Initial value will be replaced in self.init.
    kind: str = 'basics'
    pattern: str = '*.json'  # input file name pattern

    # sub dir names of expected data files should be found always.
    should_exist: typing.Iterable[str] = ('e', )

    # True if you want to keep the order of keys of dicts loaded.
    ordered: bool = False

    root: typing.Optional[pathlib.Path] = None
    datasets: typing.List[datatypes.TData] = []
    initialized: bool = False

    @classmethod
    def resolve_target(cls) -> str:
        """
        Resolve target by this file path.
        """
        return utils.target_by_parent(inspect.getfile(cls))

    def init(self) -> None:
        """Initialize its members.
        """
        if not self.target:
            self.target = self.resolve_target()

        if not self.root:
            self.root = constants.RES_DIR / self.target / self.kind

        self.datasets = self.load_datasets()
        self.initialized = True

    def load_datasets(self) -> typing.List[datatypes.TData]:
        """Load test data from files.
        """
        _datasets = [
            (datadir,
             [datatypes.TData(
                data.datadir, data.inp,
                utils.load_data(data.inp, ordered=self.ordered),
                utils.load_data(data.exp, ordered=self.ordered),
                utils.load_data(data.opts, default=DICT_0),
                data.scm,
                utils.load_data(data.query, default=''),
                utils.load_data(data.ctx, default=DICT_0, ordered=self.ordered)
              )
              for data in utils.each_data_from_dir(
                  datadir, self.pattern, self.should_exist
              )]
             )
            for datadir in sorted(self.root.glob('*'))
        ]
        if not _datasets:
            raise ValueError(f'No data: {self.root!s}')

        for datadir, data in _datasets:
            if not data:
                raise ValueError(
                    f'No data in subdir: {datadir!s}, '
                    f'pattern={self.pattern}, '
                    f'should_exist={self.should_exist!r}'
                )

        return _datasets

    def each_data(self) -> typing.Iterable[datatypes.TData]:
        """Yields test data.
        """
        if not self.initialized:
            self.init()

        for _datadir, data in self.datasets:
            for tdata in data:
                yield tdata

# vim:sw=4:ts=4:et:

@@ -1306,17 +1306,19 @@
 data
-sets
 (self
+, data
 ) -%3E
@@ -1363,105 +1363,88 @@
  %22%22%22
-Load test data from files.%0A        %22%22%22%0A        _datasets = %5B%0A            (datadir,%0A             %5B
+%0A        Load dataset and make an object holding it.%0A        %22%22%22%0A        return 
 data
@@ -1456,20 +1456,16 @@
 .TData(%0A
-    
         
@@ -1496,36 +1496,32 @@
 np,%0A            
- 
-   
 utils.load_data(
@@ -1545,36 +1545,32 @@
 =self.ordered),%0A
-    
             util
@@ -1606,36 +1606,32 @@
 =self.ordered),%0A
-    
             util
@@ -1678,28 +1678,24 @@
             
-    
 data.scm,%0A  
@@ -1692,20 +1692,16 @@
 ta.scm,%0A
-    
         
@@ -1749,36 +1749,32 @@
 '),%0A            
-  
-  
 utils.load_data(
@@ -1825,32 +1825,236 @@
 ed)%0A        
+)%0A%0A
     
+def load_datasets(self) -%3E typing.List%5Bdatatypes.TData%5D:%0A        %22%22%22Load test data from files.%0A        %22%22%22%0A        _datasets = %5B%0A            (datadir,%0A             %5B%0A                self.load_data(data)%0A
   
-)%0A
             
@@ -2107,24 +2107,26 @@
             
+ 
+ 
 datadir, sel
@@ -2172,10 +2172,11 @@
     
+  
 )
-%5D
 %0A   
@@ -2177,32 +2177,33 @@
  )%0A             
+%5D
 )%0A            fo

add a method to load and make data object in tests.base.collector