204 lines
5.2 KiB
Python
204 lines
5.2 KiB
Python
|
#!/bin/bash
|
||
|
|
||
|
import sqlite3
|
||
|
import csv
|
||
|
import os
|
||
|
from uuid import uuid4
|
||
|
from os import path
|
||
|
|
||
|
RECIPES='recipes/'
|
||
|
SUPPLY='supply.csv'
|
||
|
|
||
|
con = sqlite3.connect('developers.sqlite')
|
||
|
c = con.cursor()
|
||
|
|
||
|
def uuid () :
|
||
|
return str(uuid4())
|
||
|
|
||
|
def getRecipe (name) :
|
||
|
query="SELECT recipe_id FROM recipes WHERE (name = LOWER(?)) LIMIT 1;"
|
||
|
res = c.execute(query, (name,))
|
||
|
return c.fetchone()
|
||
|
|
||
|
def hasRecipe (name) :
|
||
|
recipe = getRecipe(name)
|
||
|
if recipe is not None:
|
||
|
return recipe[0]
|
||
|
return ''
|
||
|
|
||
|
def createRecipe (name) :
|
||
|
id=uuid()
|
||
|
query="INSERT OR IGNORE INTO recipes (recipe_id, name) VALUES (?, LOWER(?));"
|
||
|
c.execute(query, (id, name,))
|
||
|
con.commit()
|
||
|
return id
|
||
|
|
||
|
def getChemical (chemical) :
|
||
|
query="SELECT chemical_id FROM chemicals WHERE (name = LOWER(?)) LIMIT 1;"
|
||
|
res = c.execute(query, (chemical,))
|
||
|
return c.fetchone()
|
||
|
|
||
|
def hasChemical (chemical) :
|
||
|
chemical = getChemical(chemical)
|
||
|
if chemical is not None:
|
||
|
return chemical[0]
|
||
|
return ''
|
||
|
|
||
|
def createChemical (chemical) :
|
||
|
id=uuid()
|
||
|
query="INSERT OR IGNORE INTO chemicals (chemical_id, name) VALUES (?, LOWER(?));"
|
||
|
c.execute(query, (id, chemical,))
|
||
|
con.commit()
|
||
|
return id
|
||
|
|
||
|
def ensureChemical (chemical) :
|
||
|
chemical_id = hasChemical(chemical)
|
||
|
if chemical_id == '' :
|
||
|
chemical_id = createChemical(chemical)
|
||
|
return chemical_id
|
||
|
|
||
|
def getSupply (url) :
|
||
|
query="SELECT supply_id FROM supply WHERE (url = ?) LIMIT 1;"
|
||
|
res = c.execute(query, (url,))
|
||
|
return c.fetchone()
|
||
|
|
||
|
def hasSupply (url) :
|
||
|
supply = getSupply(url)
|
||
|
if supply is not None:
|
||
|
return supply[0]
|
||
|
return ''
|
||
|
|
||
|
def createSupply (chemical_id, url, g, ml, price) :
|
||
|
id=uuid()
|
||
|
query="INSERT OR IGNORE INTO supply (supply_id,chemical_id,url,grams,milliliters,price) VALUES (?,?,?,?,?,?);"
|
||
|
c.execute(query, (id,chemical_id,url,g,ml,price,))
|
||
|
con.commit()
|
||
|
return id
|
||
|
|
||
|
def ensureSupply (chemical_id, url, g, ml, price) :
|
||
|
supply_id = hasSupply(url)
|
||
|
if supply_id == '' :
|
||
|
supply_id = createSupply(chemical_id, url, g, ml, price)
|
||
|
return supply_id
|
||
|
|
||
|
def displaySupply (supply_id, chemical) :
|
||
|
query="SELECT round((price/100.0)/grams, 2) FROM supply WHERE (supply_id = ?);"
|
||
|
res = c.execute(query, (supply_id,))
|
||
|
data = c.fetchone()
|
||
|
ratio = data[0]
|
||
|
print(f"{chemical}: {ratio} $/g")
|
||
|
|
||
|
def getRecipe (recipe) :
|
||
|
query="SELECT recipe_id FROM recipes WHERE (name = LOWER(?)) LIMIT 1;"
|
||
|
res = c.execute(query, (recipe,))
|
||
|
return c.fetchone()
|
||
|
|
||
|
def hasRecipe (recipe) :
|
||
|
recipe = getRecipe(recipe)
|
||
|
if recipe is not None:
|
||
|
return recipe[0]
|
||
|
return ''
|
||
|
|
||
|
def createComponent (chemical, recipe_id, chemical_id, g, ml, makes, note) :
|
||
|
id = uuid()
|
||
|
query="INSERT OR IGNORE INTO components (component_id,recipe_id,chemical_id,grams,milliliters,makes,note) VALUES (?,?,?,?,?,?,?);"
|
||
|
c.execute(query, (id, recipe_id, chemical_id, g, ml, makes, note))
|
||
|
con.commit()
|
||
|
val=f"{g}g" if g != 'NULL' else f"{ml}ml"
|
||
|
print(f"Added component {chemical} {val}")
|
||
|
|
||
|
def createRecipe (recipe) :
|
||
|
id=uuid()
|
||
|
query="INSERT OR IGNORE INTO recipes (recipe_id, name) VALUES (?, ?);"
|
||
|
c.execute(query, (id, recipe,))
|
||
|
con.commit()
|
||
|
return id
|
||
|
|
||
|
def importRecipe (filePath) :
|
||
|
name = path.basename(filePath).replace('.csv', '').replace('_', ' ')
|
||
|
recipe_id = hasRecipe(name)
|
||
|
|
||
|
if recipe_id != '' :
|
||
|
print(f"Recipe {name} already exists")
|
||
|
return
|
||
|
|
||
|
recipe_id = createRecipe(name)
|
||
|
|
||
|
with open(filePath, newline='') as csvfile:
|
||
|
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
||
|
for row in reader:
|
||
|
chemical=row[0]
|
||
|
if chemical == 'chemical' :
|
||
|
continue
|
||
|
g=float(row[1]) if row[1] != '' else 'NULL'
|
||
|
ml=float(row[2]) if row[2] != '' else 'NULL'
|
||
|
makes=int(row[3])
|
||
|
note=row[4]
|
||
|
chemical_id=ensureChemical(chemical)
|
||
|
createComponent(chemical, recipe_id, chemical_id, g, ml, makes, note)
|
||
|
print(f"Imported {name}")
|
||
|
|
||
|
def displayCents (val) :
|
||
|
return '${:,.2f}'.format(val / 100.0)
|
||
|
|
||
|
def displayCost (ml) :
|
||
|
query = "SELECT recipe_id,UPPER(name) FROM recipes ORDER BY name ASC;"
|
||
|
c.execute(query)
|
||
|
rows = c.fetchall()
|
||
|
for row in rows:
|
||
|
recipe = row[1]
|
||
|
print(f"{recipe}: {ml/1000} liters")
|
||
|
query = """SELECT a.name, (c.grams * (s.price/s.grams)) / c.makes, (c.grams/c.makes) FROM components AS c
|
||
|
INNER JOIN supply AS s
|
||
|
ON c.chemical_id = s.chemical_id
|
||
|
INNER JOIN chemicals AS a
|
||
|
ON c.chemical_id = a.chemical_id
|
||
|
WHERE c.recipe_id = ?;"""
|
||
|
|
||
|
c.execute(query, (row[0],))
|
||
|
components = c.fetchall()
|
||
|
|
||
|
total = 0
|
||
|
for component in components:
|
||
|
name = component[0]
|
||
|
price = component[1] * ml
|
||
|
grams = component[2] * ml
|
||
|
total += price
|
||
|
print(f"{name} : [{grams}g] {displayCents(price)}")
|
||
|
print("------")
|
||
|
print(f"Total: {displayCents(total)}")
|
||
|
print("------")
|
||
|
|
||
|
print('-------')
|
||
|
print('SUPPLY')
|
||
|
print('-------')
|
||
|
|
||
|
with open(SUPPLY, newline='') as csvfile:
|
||
|
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
||
|
for row in reader:
|
||
|
chemical=row[0]
|
||
|
if chemical == 'chemical' :
|
||
|
continue
|
||
|
url=row[1]
|
||
|
g=float(row[2]) if row[2] != '' else 'NULL'
|
||
|
ml=float(row[3]) if row[3] != '' else 'NULL'
|
||
|
price=int(row[4])
|
||
|
chemical_id = ensureChemical(chemical)
|
||
|
supply_id = ensureSupply(chemical_id, url, g, ml, price)
|
||
|
displaySupply(supply_id, chemical)
|
||
|
|
||
|
print('-------')
|
||
|
print('RECIPES')
|
||
|
print('-------')
|
||
|
|
||
|
for recipe in os.listdir(RECIPES):
|
||
|
if recipe.endswith('.csv') :
|
||
|
filePath=f'{RECIPES}{recipe}'
|
||
|
importRecipe(filePath)
|
||
|
|
||
|
print('----')
|
||
|
print('COST')
|
||
|
print('----')
|
||
|
|
||
|
displayCost(3785) #gallon
|