#
# Copyright (C) 2007 by Johan De Taeye
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
# file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.8.0/contrib/django/freppledb/execute/export_file.py $
# revision : $LastChangedRevision: 1176 $ $LastChangedBy: jdetaeye $
# date : $LastChangedDate: 2010-02-14 22:27:13 +0100 (Sun, 14 Feb 2010) $
r'''
Exports frePPLe information to flat files.
The code in this file is executed NOT by Django, but by the embedded Python
interpreter from thefrePPLeengine. import
The code iterates over all objects in the C++ core engine, and writes this
information to a set of text files.
'''
from time import time
import csv
from threading import Thread
import inspect
import frepple
def exportProblems():
print "Exporting problems..."
starttime = time()
writer = csv.writer(open("problems.csv", "wb"), quoting=csv.QUOTE_ALL)
writer.writerow(('#entity','name','description','start date','end date','weight'))
for i in frepple.problems():
writer.writerow(
(i.entity, i.name, i.owner, i.description, i.start, i.end, i.weight)
)
print 'Exported problems in %.2f seconds' % (time() - starttime)
def exportOperationplans():
print "Exporting operationplans..."
starttime = time()
writer = csv.writer(open("operations.csv", "wb"), quoting=csv.QUOTE_ALL)
writer.writerow(('#id','operation','quantity','start date','end date','demand','locked'))
for i in frepple.operationplans():
writer.writerow(
( i.id, i.operation.name, i.quantity, i.start, i.end,
i.demand and i.demand.name or '', i.locked, i.unavailable,
i.owner and i.owner.id or None)
)
print 'Exported operationplans in %.2f seconds' % (time() - starttime)
def exportFlowplans():
print "Exporting flowplans..."
starttime = time()
writer = csv.writer(open("buffers.csv", "wb"), quoting=csv.QUOTE_ALL)
writer.writerow(('#operationplan id','buffer','quantity','date','on hand'))
for i in frepple.buffers():
for j in i.flowplans:
writer.writerow(
(j.operationplan.id, j.buffer.name, j.quantity,
j.date, j.onhand)
)
print 'Exported flowplans in %.2f seconds' % (time() - starttime)
def exportLoadplans():
print "Exporting loadplans..."
starttime = time()
writer = csv.writer(open("resources.csv", "wb"), quoting=csv.QUOTE_ALL)
writer.writerow(('#operationplan id','resource','quantity','start date','end date','setup'))
for i in frepple.resources():
for j in i.loadplans:
writer.writerow(
(j.operationplan.id, j.resource.name, j.quantity,
j.startdate, j.enddate, j.setup)
)
print 'Exported loadplans in %.2f seconds' % (time() - starttime)
def exportDemand():
def deliveries(d):
cumplanned = 0
n = d
while n.hidden and n.owner: n = n.owner
n = n and n.name or 'unspecified'
# Loop over all delivery operationplans
for i in d.operationplans:
cumplanned += i.quantity
cur = i.quantity
if cumplanned > d.quantity:
cur -= cumplanned - d.quantity
if cur < 0: cur = 0
yield (n, d.item.name, d.due, cur, i.end, i.quantity, i.id)
# Extra record if planned short
if cumplanned < d.quantity:
yield (n, d.item.name, d.customer and d.customer.name or None, d.due,
d.quantity - cumplanned, None, None, None)
print "Exporting demand plans..."
starttime = time()
writer = csv.writer(open("demands.csv", "wb"), quoting=csv.QUOTE_ALL)
writer.writerow(('#demand','item','customer','due date','requested quantity',
'plan date','plan quantity','operationplan id'))
for i in frepple.demands():
if i.quantity == 0: continue
for j in deliveries(i):
writer.writerow(j)
print 'Exported demand plans in %.2f seconds' % (time() - starttime)
def exportPegging():
print "Exporting pegging..."
starttime = time()
writer = csv.writer(open("demand_pegging.csv", "wb"), quoting=csv.QUOTE_ALL)
writer.writerow(('#demand','level','consuming operationplan id','consuming date',
'producing operationplan id','producing date','buffer','item','quantity demand',
'quantity buffer','pegged'))
for i in frepple.demands():
# Find non-hidden demand owner
n = i
while n.hidden and n.owner: n = n.owner
n = n and n.name or 'unspecified'
# Export pegging
for j in i.pegging:
writer.writerow((
n, j.level, j.consuming and j.consuming.id or '',
j.consuming_date,
j.producing and j.producing.id or '', j.producing_date,
j.buffer and j.buffer.name or '',
(j.buffer and j.buffer.item and j.buffer.item.name) or '',
j.quantity_demand, j.quantity_buffer, j.pegged
))
print 'Exported pegging in %.2f seconds' % (time() - starttime)
def exportForecast():
# Detect whether the forecast module is available
if not 'demand_forecastbucket' in [ a for a, b in inspect.getmembers(frepple) ]:
return
print "Exporting forecast plans..."
starttime = time()
writer = csv.writer(open("forecast.csv", "wb"), quoting=csv.QUOTE_ALL)
writer.writerow(('#forecast','start date','end date','total quantity',
'net quantity','consumed quantity'))
for i in frepple.demands():
if not isinstance(i, frepple.demand_forecastbucket) or i.total <= 0.0:
continue
writer.writerow((
i.name, i.startdate, i.enddate, i.total, i.quantity, i.consumed
))
print 'Exported forecast plans in %.2f seconds' % (time() - starttime)
def exportfrepple():
exportProblems()
exportOperationplans()
exportFlowplans()
exportLoadplans()
exportDemand()
exportPegging()
exportForecast()
|