#Formate une table de valeur nutritive.
#À lancer dans le même répertoire que la base de donnée qui peut être téléchargée à:
#   http://www.nal.usda.gov/fnic/foodcomp/Data/SR16/dnload/sr16abbr.zip

#syntaxe:
#  python wikinut.py <NDB No> [title]

#exemple:
#  python wikinut.py  11190  Mâche potagère

FIELDS = ("num","desc","water", "energ", "protein", "lipid", "ash",
          "carbonhydr", "fiber", "sugar", "calcium", "iron", "magnesium",
          "phosphorus", "potassium", "sodium", "zinc", "copper", "manganese",
          "selenium", "vit_c", "thiamin", "riboflavin", "niacin", "panto_acid",
          "vit_b6", "folate_tot", "folic_acid", "food_folate", "folate_DFE",
          "vit_B12", "vit_A_IU", "vit_A_RAE", "retinol", "vit_E", "vit_K",
          "alha_carot", "beta_carot", "beta_crypt", "lycopene", "lut_zea",
          "fa_sat", "fa_mono", "fa_poly", "cholest", "gmwt_1", "gmwt-d1",
          "gmwt_2", "gmwt_d2", "refuse_pet")
     

PATTERN = """<!-- USDA Nutriment database: %(num)s %(desc)s -->

{| border="1" cellpadding="3" cellspacing="0" align="center"
|-----
| colspan="4" align="center" bgcolor="%(color)s" |
'''%(title)s'''<br /> <small>(valeur nutritive pour 100g)</small> 
|-----
| [[eau (nutriment)|eau]]: %(water)s g
| [[cendres totales]]: %(ash)s g
| [[fibre alimentaire|fibres]]: %(fiber)s g
| [[valeur énergétique]]: %(energ)s kcal
|-----
| [[protéine]]s: %(protein)s g || [[lipide]]s: %(lipid)s g
| [[glucide]]s: %(carbonhydr)s g
| [[sucre simple|sucres simples]]: %(sugar)s g
|-----
| colspan="4" align="center" bgcolor="%(color)s" | [[oligo-élément|oligo-éléments]]    
|-----
| [[calcium (nutriment)|calcium]]: %(calcium)s mg
| [[fer (nutriment)|fer]]: %(iron)s mg
| [[magnésium (nutriment)|magnésium]]: %(magnesium)s mg
| [[phosphore (nutriment)|phosphore]]: %(phosphorus)s mg
|-----
| [[potassium (nutriment)|potassium]]: %(potassium)s mg
| [[cuivre (nutriment)|cuivre]]: %(copper)s mg
| [[sodium (nutriment)|sodium]]: %(sodium)s mg
| [[zinc (nutriment)|zinc]]: %(zinc)s mg
|-----
| colspan="4" align="center" bgcolor="%(color)s" | [[vitamine]]s
|-----
| [[vitamine C]]: %(vit_c)s mg || [[vitamine B1]]: %(thiamin)s mg
| [[vitamine B2]]: %(riboflavin)s mg || [[vitamine B3]]: %(niacin)s mg
|-----
| [[vitamine B5]]: %(panto_acid)s mg || [[vitamine B6]]: %(vit_b6)s mg
| [[vitamine B9]]: %(folic_acid)s µg || [[vitamine B12]]: %(vit_B12)s µg
|-----
| [[vitamine A]]: %(vit_A_IU)s UI || [[rétinol]]: %(retinol)s µg
| [[vitamine E]]: %(vit_E)s µg || [[vitamine K]]: %(vit_K)s µg
|-----
| colspan="4" align="center" bgcolor="%(color)s" | [[acide gras|acides gras]]
|-----
| [[acide gras saturé|saturés]]: %(fa_sat)s g
| [[acide gras mono-insaturé|mono-insaturés]]: %(fa_mono)s g
| [[acide gras poly-insaturé|poly-insaturés]]: %(fa_poly)s g
| [[cholestérol]]: %(cholest)s mg
|}"""

ANIMALS = '01', '05', '07', '10', '13', '15', '17'
VEGETALS = '06', '08', '09', '11', '12', '16', '20' 

def print_table(l, title):
    data = dict([(i, "-") for i in FIELDS])
    data['title']=title

    items = l.split('^')
    assert len(items)==len(FIELDS)
    for f,i in zip(FIELDS, items):
        if i.startswith('~'):
            i=i[1:-1]
        if i:
            data[f]=i.replace('.', ',')
    kind = data['num'][:2]
    if kind in ANIMALS:
        data['color']='#FFDDDD'
    elif kind in VEGETALS:
        data['color']='#DDFFDD'
    else:
        data['color']='#EEEEFF'

    return PATTERN%data

def output(num, title):
    f = open("ABBREV.txt")
    for l in f:
        if l.startswith("~%s~"%num):
            return print_table(l, title)


if __name__ == '__main__':
    import sys
    id = sys.argv[1]
    title = ' '.join(sys.argv[2:])
    print output(id, title)