2020-04-19 23:17:19 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import csv
|
|
|
|
import argparse
|
|
|
|
from clint.textui import puts, colored, indent
|
|
|
|
from clint.textui import columns
|
|
|
|
#import pytz
|
|
|
|
#import tzlocal
|
|
|
|
import time
|
|
|
|
from datetime import datetime, timedelta, date
|
|
|
|
import dateutil.parser
|
|
|
|
from dateutil.rrule import rrulestr
|
|
|
|
#from icalendar import Calendar,Event,Todo,Journal,Alarm
|
|
|
|
#import caldav
|
|
|
|
#import uuid
|
|
|
|
#import json
|
|
|
|
import os
|
2020-04-21 16:37:26 +02:00
|
|
|
#os.environ['LANGUAGE']='fr'
|
2020-04-19 23:17:19 +02:00
|
|
|
#import logging
|
|
|
|
import sys
|
|
|
|
#import re
|
|
|
|
#import urllib3
|
|
|
|
#from getpass import getpass
|
|
|
|
#from sympy import symbols
|
|
|
|
#from sympy.plotting import textplot
|
|
|
|
import subprocess
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
import numpy as np
|
2020-04-21 16:37:26 +02:00
|
|
|
import gettext
|
|
|
|
#gettext.bindtextdomain('base', 'locale/en/LC_MESSAGES/base.mo')
|
|
|
|
#gettext.textdomain('base')
|
|
|
|
# Voir : https://docs.python.org/fr/3/library/gettext.html
|
2020-04-21 16:47:16 +02:00
|
|
|
en = gettext.translation('base', localedir='locale/', languages=['en'])
|
|
|
|
en.install()
|
|
|
|
_ = en.gettext # english
|
|
|
|
# fr = gettext.translation('base', localedir='locale/', languages=['fr'])
|
|
|
|
# fr.install()
|
|
|
|
# _ = fr.gettext # english
|
2020-04-21 16:37:26 +02:00
|
|
|
# Voir : https://medium.com/i18n-and-l10n-resources-for-developers/how-to-translate-python-applications-with-the-gnu-gettext-module-5c1c085041
|
2020-04-19 23:17:19 +02:00
|
|
|
|
|
|
|
"""
|
2020-05-01 22:50:32 +02:00
|
|
|
This program allow you to store and visualise your weight.
|
|
|
|
No database is necessary because all is stored in files.
|
|
|
|
Three files are important :
|
|
|
|
- weight-cli.py, the principal program
|
|
|
|
- data2.csv, the data file and
|
|
|
|
- Fonctionnel.txt, the code for Gnuplot
|
|
|
|
But other files are present, as personal.txt with your other
|
|
|
|
parameters.
|
|
|
|
|
|
|
|
A measure is characterized by :
|
2020-04-30 23:33:25 +02:00
|
|
|
- its value
|
|
|
|
- its type
|
|
|
|
- its date
|
|
|
|
- its metrics
|
|
|
|
- its identifier
|
|
|
|
- one comment
|
|
|
|
- other
|
2020-05-01 22:50:32 +02:00
|
|
|
|
|
|
|
Actually, weight-cli allow to add or remove a measure,
|
|
|
|
visualise all the measure, plot a graph of these.
|
|
|
|
|
|
|
|
To know witch parameters are available, type :
|
|
|
|
weight-cli --help or weight-cli -h
|
2020-04-19 23:17:19 +02:00
|
|
|
"""
|
|
|
|
|
2020-04-21 23:41:21 +02:00
|
|
|
def save_file_data():
|
2020-04-30 23:33:25 +02:00
|
|
|
""" Saving the data file name """
|
2020-04-21 23:41:21 +02:00
|
|
|
f = open("Fonctionnel.txt", "r")
|
|
|
|
contents = f.readlines()
|
|
|
|
f.close()
|
|
|
|
linenum = 0
|
|
|
|
for row in contents:
|
|
|
|
if row.split(' ',1)[0] == 'plot':
|
2020-05-08 10:10:33 +02:00
|
|
|
contents.insert(linenum, "plot '"+args.data+"' using 3:1 pt '.' title '', f(x) lt 7 title 'bmi'\n")
|
2020-04-21 23:41:21 +02:00
|
|
|
del(contents[linenum+1])
|
|
|
|
linenum += 1
|
|
|
|
f = open("Fonctionnel.txt", "w")
|
|
|
|
contents = "".join(contents)
|
|
|
|
f.write(contents)
|
|
|
|
f.close()
|
|
|
|
|
2020-04-19 23:17:19 +02:00
|
|
|
def save_mesure():
|
2020-04-30 23:33:25 +02:00
|
|
|
""" Displaying and recording a measure """
|
|
|
|
print(_("Your measure has been recorded"))
|
2020-04-19 23:17:19 +02:00
|
|
|
with indent(4, quote=colored.blue('')):
|
2020-04-30 23:33:25 +02:00
|
|
|
puts(colored.green("-your weight : {weight} kg").format(weight=args.weight))
|
|
|
|
puts("-its type : {typ}".format(typ=args.type))
|
|
|
|
puts("-the date : {dat}".format(dat=args.date))
|
|
|
|
puts("-the metric : {metric}".format(metric=args.metric))
|
|
|
|
puts("-the identifier : {ide}".format(ide=args.id))
|
|
|
|
puts("-your comment : {comment}".format(comment=args.comment))
|
|
|
|
puts("-other : {other}".format(other=args.other))
|
2020-04-19 23:17:19 +02:00
|
|
|
with open(str(args.data), 'a', newline='\n') as csvfile:
|
|
|
|
weightwriter = csv.writer(csvfile, delimiter='|', quotechar='"', quoting=csv.QUOTE_MINIMAL)
|
|
|
|
weightwriter.writerow([str(args.weight), \
|
|
|
|
str(args.type), \
|
|
|
|
str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")), \
|
|
|
|
str(args.metric), str(args.id), \
|
|
|
|
str(args.comment), \
|
|
|
|
str(args.other)])
|
|
|
|
|
|
|
|
def read_measures():
|
2020-04-30 23:33:25 +02:00
|
|
|
""" Display of all measures """
|
2020-04-19 23:17:19 +02:00
|
|
|
data_file = open(str(args.data), 'r')
|
|
|
|
data = csv.reader(data_file, delimiter='|')
|
|
|
|
linenum = 0
|
|
|
|
for row in data:
|
|
|
|
with indent(2, quote=colored.blue('')):
|
|
|
|
puts(columns([(colored.red(str(linenum))), 4], \
|
|
|
|
[(colored.green(str(row[0])+' kg')), 15], \
|
|
|
|
[(str(row[1])),10], \
|
|
|
|
[(colored.yellow(str(row[2]))),25], \
|
|
|
|
[(str(row[3])),6], \
|
|
|
|
[(str(row[4])),4], \
|
|
|
|
[(colored.magenta(str(row[5]))),20])), \
|
|
|
|
[(str(row[6])),10]
|
|
|
|
linenum += 1
|
|
|
|
data_file.close()
|
|
|
|
|
|
|
|
def remove_measure():
|
2020-04-21 16:37:26 +02:00
|
|
|
""" Remove a measure """
|
2020-04-19 23:17:19 +02:00
|
|
|
f = open(args.data, "r")
|
|
|
|
contents = f.readlines()
|
|
|
|
f.close()
|
|
|
|
# remove the line item from list, by line number, starts from 0
|
2020-04-20 17:42:54 +02:00
|
|
|
store_temp = str(args.remove)+' '+contents[args.remove]
|
|
|
|
g = open('remove.txt', "w")
|
|
|
|
g.write(str(store_temp))
|
|
|
|
g.close()
|
2020-04-19 23:17:19 +02:00
|
|
|
contents.pop(args.remove)
|
|
|
|
|
|
|
|
f = open(args.data, "w")
|
|
|
|
contents = "".join(contents)
|
|
|
|
f.write(contents)
|
|
|
|
f.close()
|
|
|
|
|
2020-04-20 17:42:54 +02:00
|
|
|
def undo_remove():
|
2020-04-21 16:37:26 +02:00
|
|
|
""" Undo remove the last line removed """
|
2020-04-20 17:42:54 +02:00
|
|
|
f = open(args.data, "r")
|
|
|
|
contents = f.readlines()
|
|
|
|
f.close()
|
|
|
|
g = open('remove.txt', 'r')
|
|
|
|
store_temp = g.readline().split(' ',1)
|
|
|
|
g.close()
|
2020-04-21 23:41:21 +02:00
|
|
|
# insert the line item from list, by its line number
|
2020-04-20 17:42:54 +02:00
|
|
|
contents.insert(int(store_temp[0]), store_temp[1])
|
|
|
|
|
|
|
|
f = open(args.data, "w")
|
|
|
|
contents = "".join(contents)
|
|
|
|
f.write(contents)
|
|
|
|
f.close()
|
|
|
|
|
2020-04-30 23:33:25 +02:00
|
|
|
def plot_graph():
|
|
|
|
""" Plot the graph of the weight evolution with Gnuplot """
|
2020-04-23 17:14:27 +02:00
|
|
|
if args.dates != None:
|
|
|
|
print(args.dates)
|
|
|
|
# set xrange [*:*]
|
|
|
|
f = open("Fonctionnel.txt", "r")
|
|
|
|
contents = f.readlines()
|
|
|
|
f.close()
|
|
|
|
linenum = 0
|
|
|
|
for row in contents:
|
|
|
|
motif = row.split(' ',2)[0]+' '+row.split(' ',2)[1]
|
|
|
|
if motif == 'set xrange':
|
2020-04-23 20:30:59 +02:00
|
|
|
if args.dates[0] == '*':
|
|
|
|
ligne = "set xrange [*:'"+args.dates[1]+"']\n"
|
|
|
|
elif args.dates[1] == '*':
|
|
|
|
ligne = "set xrange ['"+args.dates[0]+"':*]\n"
|
|
|
|
else:
|
|
|
|
ligne = "set xrange ['"+args.dates[0]+"':'"+args.dates[1]+"']\n"
|
|
|
|
contents.insert(linenum, ligne)
|
2020-04-23 17:14:27 +02:00
|
|
|
del(contents[linenum+1])
|
|
|
|
linenum += 1
|
|
|
|
f = open("Fonctionnel.txt", "w")
|
|
|
|
contents = "".join(contents)
|
|
|
|
f.write(contents)
|
|
|
|
f.close()
|
|
|
|
else:
|
|
|
|
f = open("Fonctionnel.txt", "r")
|
|
|
|
contents = f.readlines()
|
|
|
|
f.close()
|
|
|
|
linenum = 0
|
|
|
|
for row in contents:
|
|
|
|
motif = row.split(' ',2)[0]+' '+row.split(' ',2)[1]
|
|
|
|
if motif == 'set xrange':
|
|
|
|
contents.insert(linenum, "set xrange [*:*]\n")
|
|
|
|
del(contents[linenum+1])
|
|
|
|
linenum += 1
|
|
|
|
f = open("Fonctionnel.txt", "w")
|
|
|
|
contents = "".join(contents)
|
|
|
|
f.write(contents)
|
|
|
|
f.close()
|
2020-04-19 23:17:19 +02:00
|
|
|
#process = Popen(['gnuplot', 'Fonctionnel.txt'], stdout=PIPE, stderr=PIPE)
|
|
|
|
process = Popen(['gnuplot', 'Fonctionnel.txt'])#, stdout=PIPE, stderr=PIPE)
|
|
|
|
stdout, stderr = process.communicate()
|
|
|
|
#print(stdout)
|
2020-05-01 22:50:32 +02:00
|
|
|
|
|
|
|
def save_size():
|
2020-05-08 10:10:33 +02:00
|
|
|
print(bmi_weight_max())
|
2020-05-01 22:50:32 +02:00
|
|
|
""" Store the size """
|
|
|
|
f = open('personal.txt', "w")
|
|
|
|
f.write(str(args.size))
|
|
|
|
f.close()
|
2020-04-19 23:17:19 +02:00
|
|
|
|
2020-05-08 10:10:33 +02:00
|
|
|
f = open("Fonctionnel.txt", "r")
|
|
|
|
contents = f.readlines()
|
|
|
|
f.close()
|
|
|
|
linenum = 0
|
|
|
|
for row in contents:
|
|
|
|
motif = row.split(' ',2)[0]+' '+row.split(' ',2)[1]
|
|
|
|
if motif == 'f(x) =':
|
|
|
|
contents.insert(linenum, "f(x) = "+str(bmi_weight_max())+"\n")
|
|
|
|
del(contents[linenum+1])
|
|
|
|
linenum += 1
|
|
|
|
f = open("Fonctionnel.txt", "w")
|
|
|
|
contents = "".join(contents)
|
|
|
|
f.write(contents)
|
|
|
|
f.close()
|
|
|
|
|
2020-04-19 23:17:19 +02:00
|
|
|
def tests_subproc():
|
2020-04-30 23:33:25 +02:00
|
|
|
""" Test of the subprocess module """
|
2020-04-19 23:17:19 +02:00
|
|
|
process = Popen(['cat', 'data2.csv'], stdout=PIPE, stderr=PIPE)
|
|
|
|
stdout, stderr = process.communicate()
|
|
|
|
print(stdout)
|
2020-05-01 22:50:32 +02:00
|
|
|
|
|
|
|
def bmi(the_weight):
|
2020-05-08 10:10:33 +02:00
|
|
|
""" Return the BMI for a given size """
|
2020-05-01 22:50:32 +02:00
|
|
|
f = open('personal.txt', "r")
|
|
|
|
the_size = float(f.read())
|
|
|
|
f.close()
|
|
|
|
bmi = the_weight / pow(the_size, 2)
|
|
|
|
return(bmi)
|
|
|
|
|
|
|
|
def bmi_weight_max():
|
|
|
|
""" Return the weight for BMI max """
|
|
|
|
f = open('personal.txt', "r")
|
|
|
|
the_size = float(f.read())
|
|
|
|
f.close()
|
|
|
|
weight_max = 29.9 * pow(the_size, 2)
|
|
|
|
return(weight_max)
|
2020-04-19 23:17:19 +02:00
|
|
|
|
|
|
|
### Main ###
|
2020-04-30 23:33:25 +02:00
|
|
|
""" Weight Recording Program
|
|
|
|
Be careful, the data.csv file has a header line that lacks
|
|
|
|
a final | separator for the number of fields to be correct.
|
2020-04-21 16:37:26 +02:00
|
|
|
"""
|
2020-04-19 23:17:19 +02:00
|
|
|
|
2020-04-30 23:33:25 +02:00
|
|
|
# Creating the parser for arguments
|
2020-04-19 23:17:19 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
2020-04-21 16:37:26 +02:00
|
|
|
texte = _('A script to manage your weight in command line.')
|
|
|
|
parser.description=texte
|
2020-04-30 23:33:25 +02:00
|
|
|
# Creating arguments
|
2020-04-19 23:17:19 +02:00
|
|
|
parser.add_argument('-da', '--data',
|
2020-05-08 10:10:33 +02:00
|
|
|
default='data.csv',
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : file data')
|
2020-04-19 23:17:19 +02:00
|
|
|
)
|
2020-05-01 22:50:32 +02:00
|
|
|
parser.add_argument('-s', '--size',
|
|
|
|
type=float,
|
|
|
|
default=-1,
|
|
|
|
help=_('optional : size in meter')
|
|
|
|
)
|
2020-04-19 23:17:19 +02:00
|
|
|
parser.add_argument('-w', '--weight',
|
|
|
|
type=float,
|
|
|
|
default=-1,
|
2020-05-01 22:50:32 +02:00
|
|
|
help=_('optional : weight in kilogram')
|
2020-04-19 23:17:19 +02:00
|
|
|
)
|
|
|
|
parser.add_argument('-t', '--type',
|
|
|
|
default='WEIGHT',
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : type')
|
2020-04-19 23:17:19 +02:00
|
|
|
)
|
|
|
|
parser.add_argument('-d', '--date',
|
|
|
|
default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : date')
|
2020-04-19 23:17:19 +02:00
|
|
|
)
|
|
|
|
parser.add_argument('-i', '--id',
|
|
|
|
default='',
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : id')
|
2020-04-19 23:17:19 +02:00
|
|
|
)
|
|
|
|
parser.add_argument('-m', '--metric',
|
|
|
|
default='true',
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : metric')
|
2020-04-19 23:17:19 +02:00
|
|
|
)
|
|
|
|
parser.add_argument('-c', '--comment',
|
|
|
|
default=' ',
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : comment')
|
2020-04-19 23:17:19 +02:00
|
|
|
)
|
|
|
|
parser.add_argument('-o', '--other',
|
|
|
|
default='',
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : other')
|
2020-04-19 23:17:19 +02:00
|
|
|
)
|
|
|
|
parser.add_argument('-r', '--read',
|
|
|
|
help='optional read measures',
|
|
|
|
action='store_true'
|
|
|
|
)
|
|
|
|
parser.add_argument('-rem', '--remove',
|
|
|
|
type=int,
|
|
|
|
default='-1',
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : delete a measure by its number in the list')
|
2020-04-19 23:17:19 +02:00
|
|
|
)
|
|
|
|
parser.add_argument('-g', '--graph',
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : plot graph'),
|
2020-04-19 23:17:19 +02:00
|
|
|
action='store_true'
|
|
|
|
)
|
2020-04-23 17:14:27 +02:00
|
|
|
parser.add_argument('-ds', '--dates',
|
2020-04-23 20:30:59 +02:00
|
|
|
nargs='+',
|
|
|
|
help=_("optional : min_date max_date -> 'Y-m-d hh:mm:ss' or '*' for an automatic date.")
|
2020-04-23 17:14:27 +02:00
|
|
|
)
|
2020-04-19 23:17:19 +02:00
|
|
|
parser.add_argument('-tst', '--tests',
|
|
|
|
help='optional tests operations',
|
|
|
|
action='store_true'
|
|
|
|
)
|
2020-04-20 17:42:54 +02:00
|
|
|
parser.add_argument('-ur', '--undoremove',
|
2020-04-21 16:37:26 +02:00
|
|
|
help=_('optional : undo remove the last line removed'),
|
2020-04-20 17:42:54 +02:00
|
|
|
action='store_true'
|
|
|
|
)
|
2020-04-21 16:37:26 +02:00
|
|
|
parser.add_argument('-v', '--version',
|
|
|
|
version='0.0.5',
|
|
|
|
action='version'
|
|
|
|
)
|
2020-04-30 23:33:25 +02:00
|
|
|
# Argument retrieval
|
2020-04-19 23:17:19 +02:00
|
|
|
args = parser.parse_args()
|
2020-04-30 23:33:25 +02:00
|
|
|
# Launching of actions
|
2020-04-23 21:28:25 +02:00
|
|
|
if args.data != 'data2.csv':
|
2020-04-21 23:41:21 +02:00
|
|
|
save_file_data()
|
2020-05-01 22:50:32 +02:00
|
|
|
if args.size != -1:
|
|
|
|
save_size()
|
2020-04-19 23:17:19 +02:00
|
|
|
if args.weight != -1:
|
|
|
|
save_mesure()
|
|
|
|
if args.read:
|
|
|
|
read_measures()
|
|
|
|
if args.remove >= 0:
|
|
|
|
remove_measure()
|
|
|
|
if args.graph:
|
2020-04-30 23:33:25 +02:00
|
|
|
plot_graph()
|
2020-04-19 23:17:19 +02:00
|
|
|
if args.tests:
|
|
|
|
tests_subproc()
|
2020-04-20 17:42:54 +02:00
|
|
|
if args.undoremove:
|
|
|
|
undo_remove()
|
2020-04-19 23:17:19 +02:00
|
|
|
|