# 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

def tail(file, n=1, bs=1024):
    f = open(file,'rb')
    f.seek(-1,2)
    l = 1-f.read(1).decode('utf-8').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)
            x = f.read(block).decode('utf-8')
            l += x.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 avv():
  vstr=""
  v=[0.0 for i in range(numcells)]
  endlog=tail(config['files']['logfile'],11)
  for j in range(numcells):
    for i in range(10):
      x=float(endlog[i][j*6+15:j*6+20])
      v[j]=v[j]+x
  for i in range(len(v)):
    v[i]=v[i]/10
    vstr=vstr+str(i+1) + "=" +str(round(v[i],3)).ljust(5,'0') + ", "
  print(vstr)

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


# Path: calvfineall.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


def tail(file, n=1, bs=1024):
    f = open(file,'rb')
    f.seek(-1,2)
    l = 1-f.read(1).decode('utf-8').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)
            x = f.read(block).decode('utf-8')
            l += x.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 avv():
  vstr=""
  v=[0.0 for i in range(numcells)]
  endlog=tail(config['files']['logfile'],11)
  for j in range(numcells):
    for i in range(10):
      x=float(endlog[i][j*6+15:j*6+20])
      v[j]=v[j]+x
  for i in range(len(v)):
    v[i]=v[i]/10
    vstr=vstr+str(i+1) + "=" +str(round(v[i],3)).ljust(5,'0') + ", "
  print(vstr)

  print(config['calibrate']['delta'])