#!/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 #os.environ['LANGUAGE']='fr' #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 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 """ Une mesure est caractérisée par : - sa valeur - son type - sa date - sa métrique - son identifiant - un commentaire - autre """ def save_mesure(): """ Affichage et enregistrement d'une mesure """ 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(): """ Affichage de l'ensemble des mesures """ 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(): """ Remove a measure """ f = open(args.data, "r") contents = f.readlines() f.close() # remove the line item from list, by line number, starts from 0 store_temp = str(args.remove)+' '+contents[args.remove] g = open('remove.txt', "w") g.write(str(store_temp)) g.close() contents.pop(args.remove) f = open(args.data, "w") contents = "".join(contents) f.write(contents) f.close() def undo_remove(): """ Undo remove the last line removed """ 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() def trace_graph(): """ Trace le graphe de l'évolution du poids avec Gnuplot """ #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(): """ Test du module subprocess """ process = Popen(['cat', 'data2.csv'], stdout=PIPE, stderr=PIPE) stdout, stderr = process.communicate() print(stdout) ### Main ### """ Programme de relevé de poids 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. """ # Création du parseur pour les arguments parser = argparse.ArgumentParser() texte = _('A script to manage your weight in command line.') parser.description=texte # Création des arguments parser.add_argument('-da', '--data', default='data2.csv', help=_('optional : file data') ) parser.add_argument('-w', '--weight', type=float, default=-1, help=_('optional : weight') ) parser.add_argument('-t', '--type', default='WEIGHT', help=_('optional : type') ) parser.add_argument('-d', '--date', default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), help=_('optional : date') ) parser.add_argument('-i', '--id', default='', help=_('optional : id') ) parser.add_argument('-m', '--metric', default='true', help=_('optional : metric') ) parser.add_argument('-c', '--comment', default=' ', help=_('optional : comment') ) parser.add_argument('-o', '--other', default='', help=_('optional : other') ) parser.add_argument('-r', '--read', help='optional read measures', action='store_true' ) parser.add_argument('-rem', '--remove', type=int, default='-1', help=_('optional : delete a measure by its number in the list') ) parser.add_argument('-g', '--graph', help=_('optional : plot graph'), action='store_true' ) parser.add_argument('-tst', '--tests', help='optional tests operations', action='store_true' ) parser.add_argument('-ur', '--undoremove', help=_('optional : undo remove the last line removed'), action='store_true' ) parser.add_argument('-v', '--version', version='0.0.5', action='version' ) # 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() if args.undoremove: undo_remove()