from os import path

from ffflash import RELEASE, log, now, timeout
from ffflash.lib.clock import epoch_repr
from ffflash.lib.data import merge_dicts
from ffflash.lib.files import read_json_file, write_json_file


class Container:
    def __init__(self, spec, filename):
        self._spec = spec
        self._location = path.abspath(filename)
        self.data = read_json_file(self._location, fallback={})

        self._info()

    def _info(self, info={}):
        self.data['_info'] = self.data.get('_info', {})
        self.data['_info']['generator'] = RELEASE

        self.data['_info']['access'] = self.data['_info'].get('access', {})
        if not self.data['_info']['access'].get('first', False):
            self.data['_info']['access']['first'] = now
        self.data['_info']['access']['last'] = now
        self.data['_info']['access']['overall'] = epoch_repr(
            abs(now - self.data['_info']['access']['first']),
            ms=True
        )
        self.data['_info']['access']['scrub'] = timeout

        if info:
            self.data['_info'] = merge_dicts(self.data['_info'], info)

    def save(self, info={}):
        self._info(info)
        if write_json_file(self._location, self.data):
            log.info('{} saved {}'.format(self._spec, self._location))

@@ -131,27 +131,23 @@
  import 
-merge_dicts
+Element
 %0Afrom ff
@@ -334,24 +334,25 @@
 lename)%0A
+%0A
         
 self.dat
@@ -343,25 +343,23 @@
         
-self.data
+content
  = read_
@@ -415,46 +415,48 @@
 elf.
-_
 info
-()%0A%0A    def _info(self, 
+ = Element(content.get('_
 info
-=%7B%7D):
+', %7B%7D))
 %0A   
@@ -473,75 +473,88 @@
 data
-%5B'_info'%5D = self.data.get('_info', %7B%7D)%0A        self.data%5B'_info'%5D%5B'
+ = Element(content.get(self._spec, %7B%7D))%0A%0A    def _info(self):%0A        self.info.
 gene
@@ -558,18 +558,16 @@
 enerator
-'%5D
  = RELEA
@@ -582,139 +582,37 @@
     
-self.data%5B'_info'%5D%5B'access'%5D = self.data%5B'_info'%5D.get('access', %7B%7D)%0A        if not self.data%5B'_info'%5D%5B'access'%5D.get('first', False)
+if not self.info.access.first
 :%0A  
@@ -630,40 +630,25 @@
 elf.
-data%5B'_info'%5D%5B'
+info.
 access
-'%5D%5B'
+.
 first
-'%5D
  = n
@@ -667,39 +667,24 @@
 elf.
-data%5B'_info'%5D%5B'
+info.
 access
-'%5D%5B'
+.
 last
-'%5D
  = n
@@ -703,42 +703,27 @@
 elf.
-data%5B'_info'%5D%5B'
+info.
 access
-'%5D%5B'
+.
 overall
-'%5D
  = e
@@ -764,54 +764,27 @@
 elf.
-data%5B'_info'%5D%5B'
+info.
 access
-'%5D%5B'
+.
 first
-'%5D),%0A           
+),
  ms=
@@ -815,40 +815,25 @@
 elf.
-data%5B'_info'%5D%5B'
+info.
 access
-'%5D%5B'
+.
 scrub
-'%5D
  = t
@@ -848,146 +848,140 @@
     
-    if info:%0A            self.data%5B'_info'%5D = merge_dicts(self.data%5B'_info'%5D, 
+def save(self):%0A        self._info()%0A        content = %7B%0A            '_info': self.
 info
-)%0A%0A
+,%0A 
     
-def save(self, info=%7B%7D):%0A        self._info(info)
+       self._spec: self.data%0A        %7D
 %0A   
@@ -1020,25 +1020,23 @@
 cation, 
-self.data
+content
 ):%0A     

Rewrite Container to dump/load two Elements (data,_info) into json file