# Path: config.py
# def editbatconfig(section,item,itemvalue):
# def loadconfig():


import sys
import time
from os import rename
from shutil import copy as filecopy
from copy import deepcopy
from ast import literal_eval
from configparser import SafeConfigParser
from config import config, loadconfig

summaryfile = SafeConfigParser()

def tail(file, n=1, bs=1024):
    f = open(file)
    f.seek(-1,2)
    l = 1-f.read(1).count('\n') # If file doesn't end in \n, count it anyway.
    B = f.tell()
    while n >= l and B > 0:
            block = min(bs, B)
            B -= block
            f.seek(B, 0)
            l += f.read(block).count('\n')
    f.seek(B, 0)
    l = min(l,n) # discard first (incomplete) line if l > n
    lines = f.readlines()[-l:]
    f.close()
    return lines

def getavi():
  istr=""
  avi = [ 0.0 for i in range(numiin)]  
  endlog=tail(config['files']['logfile'],11)
  for line in range(10):
    lineargs = str(endlog[line]).split(' ')
    for iin in range(numiin):
      avi[iin] = avi[iin] + float(lineargs[1+numcells+2+iin])/10
  for iin in range(numiin):
    istr =istr+str(round(avi[iin],3)) + ", "
  print('Average readings for last 100 seconds: ' + istr)

# Path: config.py
# def editbatconfig(section,item,itemvalue):
# def loadconfig():


# Path: caligain.py
import sys
import time
from os import rename
from shutil import copy as filecopy
from copy import deepcopy
from ast import literal_eval
from configparser import SafeConfigParser
from config import config, loadconfig


summaryfile = SafeConfigParser()

def tail(file, n=1, bs=1024):
    f = open(file)
    f.seek(-1,2)
    l = 1-f.read(1).count('\n') # If file doesn't end in \n, count it anyway.
    B = f.tell()
    while n >= l and B > 0:
            block = min(bs, B)
            B -= block
            f.seek(B, 0)
            l += f.read(block).count('\n')
    f.seek(B, 0)
    l = min(l,n) # discard first (incomplete) line if l > n
    lines = f.readlines()[-l:]
    f.close()
    return lines

def getavi():
  istr=""
  avi = [ 0.0 for i in range(numiin)]  
  endlog=tail(config['files']['logfile'],11)
  for line in range(10):
    lineargs = str(endlog[line]).split(' ')
    for iin in range(numiin):
      avi[iin] = avi[iin] + float(lineargs[1+numcells+2+iin])/10
  for iin in range(numiin):
    istr =istr+str(round(avi[iin],3)) + ", "
  print('Average readings for last 100 seconds: ' + istr)

  loadconfig()