Weight-cli/weight-cli.py

224 lines
6.1 KiB
Python
Raw Normal View History

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
#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
# 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
"""
Une mesure est caractérisée par :
- sa valeur
- son type
- sa date
- sa métrique
- son identifiant
- un commentaire
- autre
"""
def save_mesure():
2020-04-21 16:37:26 +02:00
""" Affichage et enregistrement d'une mesure """
2020-04-19 23:17:19 +02:00
print("Votre mesure a été enregistrée : ")
with indent(4, quote=colored.blue('')):
puts(colored.green("-votre poids : {weight} kg").format(weight=args.weight))
puts("-son type : {typ}".format(typ=args.type))
puts("-la date : {dat}".format(dat=args.date))
puts("-la métrique : {metric}".format(metric=args.metric))
puts("-l'identifiant : {ide}".format(ide=args.id))
puts("-votre commentaire : {comment}".format(comment=args.comment))
puts("-autre : {other}".format(other=args.other))
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-21 16:37:26 +02:00
""" Affichage de l'ensemble des mesures """
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()
# insert the line item from list, by line number, starts from 0
contents.insert(int(store_temp[0]), store_temp[1])
f = open(args.data, "w")
contents = "".join(contents)
f.write(contents)
f.close()
2020-04-19 23:17:19 +02:00
def trace_graph():
2020-04-21 16:37:26 +02:00
""" Trace le graphe de l'évolution du poids avec Gnuplot """
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)
def tests_subproc():
2020-04-21 16:37:26 +02:00
""" Test du module subprocess """
2020-04-19 23:17:19 +02:00
process = Popen(['cat', 'data2.csv'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print(stdout)
### Main ###
2020-04-21 16:37:26 +02:00
""" Programme de relevé de poids
2020-04-19 23:17:19 +02:00
Attention, le fichier data.csv à une ligne d'entête à laquelle
il manque un séparateur | final pour que le nombre de champs soit
correct.
2020-04-21 16:37:26 +02:00
"""
2020-04-19 23:17:19 +02:00
# Création du parseur pour les arguments
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-19 23:17:19 +02:00
# Création des arguments
parser.add_argument('-da', '--data',
default='data2.csv',
2020-04-21 16:37:26 +02:00
help=_('optional : file data')
2020-04-19 23:17:19 +02:00
)
parser.add_argument('-w', '--weight',
type=float,
default=-1,
2020-04-21 16:37:26 +02:00
help=_('optional : weight')
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'
)
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-19 23:17:19 +02:00
# Récupération des arguments
args = parser.parse_args()
# Lancement des actions
if args.weight != -1:
save_mesure()
if args.read:
read_measures()
if args.remove >= 0:
remove_measure()
if args.graph:
trace_graph()
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