#!/usr/local/bin/python import sys,os,string,regex,regsub,time import pgext from HTMLgen import * # Other constants... # NO = FALSE = 0 YES = TRUE = 1 text_bar = 60* "-" RED = "#993300" GREEN = "#336600" BLACK = "#000000" WHITE = "#FFFFFF" LIGHT_GREY = "#E7E7E7" GREY = "#C0C0C0" DARK_GREY = "#6F6F6F" # ==================================================================================================== # Define directories where things come from and go to... # in_base_dir = "/home/kevan/computer_collection" in_picture_dir = os.path.join(in_base_dir,"pictures") in_html_dir = os.path.join(in_base_dir,"html") base_url = "/kevan" base_dir = "/home/kevan/public_html" base_url_collection = os.path.join (base_url,"collection") base_dir_collection = os.path.join (base_dir,"collection") base_url_images = os.path.join (base_url_collection,"images") base_dir_images = os.path.join (base_dir_collection,"images") base_url_pictures = os.path.join (base_url_collection,"pictures") base_dir_pictures = os.path.join (base_dir_collection,"pictures") # functions that return specific filenames and urls... def make_manufacturer_dir (resource): directory = os.path.join (base_dir_collection, "manufacturer-"+resource) if not os.path.isdir (directory): os.mkdir (directory, 0755) def manufacturer_index_href (resource,ptype): dir = os.path.join (base_url_collection,"manufacturer-"+resource) return os.path.join (dir,"index-%s.html"%(ProductType[ptype][ProductType_resource])) def manufacturer_index_file (resource,ptype): dir = os.path.join (base_dir_collection,"manufacturer-"+resource) return os.path.join (dir,"index-%s.html"%(ProductType[ptype][ProductType_resource])) def product_page_href (mres,pres): dir = os.path.join (base_url_collection,"manufacturer-"+mres) return os.path.join (dir,"%s.html"%(pres)) def product_page_file (mres,pres): dir = os.path.join (base_dir_collection,"manufacturer-"+mres) return os.path.join (dir,"%s.html"%(pres)) def product_picture_href (mres,pres,size): return os.path.join (base_url_pictures,"%s-%s-%s.jpg"%(mres,pres,size)) def product_picture_file (mres,pres,size): return os.path.join (base_dir_pictures,"%s-%s-%s.jpg"%(mres,pres,size)) def ptype_index_href (name): return os.path.join (base_url_collection,name) def ptype_index_file (name): return os.path.join (base_dir_collection,name) def static_page_href (name): return os.path.join (base_url_collection,name) def static_page_file (name): return os.path.join (base_dir_collection,name) def other_collectors_href (): return os.path.join (base_url_collection,"other_collectors.html") def other_collectors_file (): return os.path.join (base_dir_collection,"other_collectors.html") def plain_text_href (): return os.path.join (base_url_collection,"catalogue.txt") def plain_text_file (): return os.path.join (base_dir_collection,"catalogue.txt") def dot_image_size_file (): return os.path.join (base_dir_collection,".image_sizes") # ==================================================================================================== # The db_query function keeps track of the queries made so that we can do some # performance tuning... # dq_diff = {} dq_count = 0 dq_queries = {} def db_query (str): global dq_diff,dq_count,dq_queries dq_count = dq_count + 1 dq_diff[str] = 1 try: dq_queries[str] = dq_queries[str] + 1 except KeyError: dq_queries[str] = 1 result = dbase_connection.query(str) return result.getresult() # ==================================================================================================== # There are a number of static tables that only need to be read once... # def read_static_tables (): print "Reading static tables..." # -------------------------------------------------- print " item_status" global ItemStatus,ItemStatus_name,ItemStatus_desc ItemStatus = {} for its_id,its_nm,its_desc in db_query ("SELECT its_id,its_nm,its_desc from item_status"): ItemStatus[its_id] = (its_nm,its_desc) ItemStatus_name = 0 ItemStatus_desc = 1 # -------------------------------------------------- print " product_type" global ProductType,ProductTypeIdList,ProductType_name,ProductType_file,ProductType_resource ProductType = {} ProductTypeIdList = [] for ptyp_id,ptyp_nm,ptyp_file,ptyp_res in db_query ("SELECT ptyp_id,ptyp_nm,ptyp_file,ptyp_res " + "FROM product_type " + "ORDER BY ptyp_id"): ptyp_id = string.atoi(ptyp_id) ProductType[ptyp_id] = (ptyp_nm,ptyp_file,ptyp_res) ProductTypeIdList.append (ptyp_id) ProductType_name = 0 ProductType_file = 1 ProductType_resource = 2 # -------------------------------------------------- print " product counts" global ProductCount,ProductCount_name ProductCount = {} result = db_query ("select p.ptyp_id,pt.ptyp_nm,count(prd_id) " + "from product p,product_type pt " + "where p.ptyp_id = pt.ptyp_id " + "group by p.ptyp_id,pt.ptyp_nm ") for ptyp_id,ptyp_nm,count in result: ProductCount[ptyp_id] = (ptyp_nm,count) ProductCount_name = 0 ProductCount_count = 1 # -------------------------------------------------- print " product_subtype" global ProductSubType,ProductSubType_name,ProductSubType_desc ProductSubType = {} for pst_id,pst_nm,pst_desc in db_query ("SELECT pst_id,pst_nm,pst_desc from product_subtype"): ProductSubType[pst_id] = (pst_nm,pst_desc) ProductSubType_name = 0 ProductSubType_desc = 1 # -------------------------------------------------- print " manufacturer" global ManufacturerName,Manufacturer_CommonName,Manufacturer_FullName ManufacturerName = {} for man_id, man_full_nm, man_cmn_nm in db_query ("SELECT man_id, man_full_nm, man_cmn_nm FROM manufacturer"): ManufacturerName[man_id] = (man_cmn_nm,man_full_nm) Manufacturer_CommonName = 0 Manufacturer_FullName = 1 # ==================================================================================================== class MyContainer (Container): def start_tag (self): return "" def end_tag (self): return "" class Big(Text): def __repr__(self): return '%s' % self.text class Small(Text): def __repr__(self): return '%s' % self.text # ==================================================================================================== def heading_one (doc,title): if 1: doc.append (Paragraph(Font (Big(Bold(Italic(title))), face = "Helvetica, Arial", color = RED))) else: htable = TableLite (border = 0, cellspacing = 0, cellpadding = 2, width="100%") htable.append (TR(TD(Font (Big(Bold(title)), face = "Helvetica, Arial", color = RED), bgcolor=WHITE))) doc.append (htable) # ==================================================================================================== class CompColDoc (Document): """A document class specifically designed for my computer collection pages.""" # -------------------------------------------------------------------------------- def __init__ (self): self.contents = [] self.heading = "" self.has_menu = YES self.title = "" self.description = "" self.keywords = [] self.dir_level = None self.manufacturer_list = [] self.product_list = [] self.current_product = None self.menu_selection = 202 self.main_sections = ( (202, 'Home', 'index.html'), (101, 'Computers', 'prod_computers.html'), (102, 'Calculators', 'prod_calculators.html'), (103, 'Peripherals', 'prod_peripherals.html'), (104, 'Video Games', 'prod_video_games.html'), (107, 'Hand Held Games', 'prod_hand_held_games.html'), ) self.other_pages = ( (203, 'Can You Help', 'can_you_help.html'), (301, 'Other Collectors', 'other_collectors.html'), (201, 'Creating These Pages', 'creation.html'), ) # -------------------------------------------------------------------------------- def __repr__(self): global base_url_images s = DOCTYPE # first work out what level of sub directory we are in and create relative links for graphics... if self.dir_level == None: base_url_images = os.path.join (base_url_collection,"images") else: if self.dir_level == 0: base_url_images = "images" elif self.dir_level == 1: base_url_images = "../images" # ------------------------------------------------------------ # Obviously we start with the head element... s = s + "\n" # ------------------------------------------------------------ # Output the title, and some meta, link elements... s = s + "Kevan's Computer Bits - %s\n"%(self.title) if self.description: s = s + "\n"%(self.description) if self.keywords: s = s + "\n"%(string.joinfields(self.keywords,", ")) s = s + "\n" s = s + "\n" s = s + "\n" s = s + "\n" s = s + "\n" s = s + "\n" # ------------------------------------------------------------ # Start the body... s = s + '\n'%(GREEN,GREEN,GREEN) s = s + '\n' # everything is wrapped in a table... s = s + '\n' s = s + '\n' # ------------------------------------------------------------ # first column contains the menus... s = s + '\n" # second column contains a vertical bar... s = s + '\n'%(WHITE,base_url_images) # the third column contains the main content... s = s + '\n' s = s + '\n' s = s + "
\n' s = s + '\n'%(base_url_images) s = s + '\n' if self.product_list: cspan_arrow = 1 cspan_text = 2 else: cspan_arrow = 2 cspan_text = 1 for list in (self.main_sections,self.other_pages): if list is self.main_sections: s = s + '\n' else: s = s + '\n' for id,name,page in list: s = s + '' s = s + '' s = s + '\n' if self.product_list: s = s + '\n' for ptype,pitems in self.product_list: if pitems: s = s + '' s = s + '' s = s + '\n' for pid,pname,pres in pitems: s = s + '' s = s + '' s = s + '\n' s = s + "
'%(RED) s = s + 'Main Sections...'%(WHITE) s = s + '
'%(RED) s = s + 'Other Pages...'%(WHITE) s = s + '
' if id == self.menu_selection: s = s + '*'%(base_url_images) else: s = s + ''%(base_url_images) s = s + ''%(base_url_images) s = s + ''%(base_url_collection,page) s = s + '%s'%(GREEN,name) s = s + '' s = s + '
'%(RED) s = s + 'Current Manufacturer...'%(WHITE) s = s + '
' if self.current_product_type == ptype: s = s + '*'%(base_url_images) else: s = s + ''%(base_url_images) s = s + ''%(base_url_images) s = s + ''%(manufacturer_index_href(self.current_manufacturer_res,ptype)) s = s + '%ss'%(GREEN,ProductType[ptype][ProductType_name]) s = s + '' s = s + '
' s = s + ''%(base_url_images) if self.current_product_id == pid: s = s + '*'%(base_url_images) else: s = s + ''%(base_url_images) s = s + ''%(base_url_images) s = s + ''%(product_page_href(self.current_manufacturer_res,pres)) if self.current_manufacturer_cnam: pname = regsub.sub (self.current_manufacturer_cnam,"",pname) s = s + '%s'%(GREEN,pname) s = s + '' s = s + '
\n' s = s + '\n' s = s + '' s = s + '' s = s + '\n' s = s + '' # If we are doing a heading... s = s + '' s = s + '\n' s = s + '\n'%(WHITE,base_url_images) s = s + '\n'%(GREEN,base_url_images) s = s + '\n'%(base_url_images) s = s + '\n' s = s + '
' s = s + '\n'%(base_url_images) s = s + 'Kevans Computer Bits...'%(base_url_images) s = s + '
' s = s + '\n'%(base_url_images) if self.heading: s = s + '%s'%(RED,self.heading) else: s = s + ' ' s = s + '
\n' for object in self.contents: s = s + str(object) s = s + '
\n' s = s + '
\n" # ------------------------------------------------------------ # and a new table for the copyrights etc. tstr = time.asctime(time.localtime (time.time())) s = s + '\n' s = s + '\n'%(base_url_images) s = s + '\n'%(GREEN,base_url_images) s = s + '\n'%(base_url_images) s = s + "\n" s = s + "\n" s = s + "\n" s = s + '
\n" s = s + '''
Created by: Kevan Heydon on %s
\n'''%(tstr) s = s + '''

The pictures of my computers in these pages are Copyright Kevan Heydon, 1996,1997,1998. You may use them for non-commercial purposes as long as they are properly attributed to me.\n''' s = s + "

\n' s = s + '\n' s = s + '\n' # that's it so return what we have... return s # -------------------------------------------------------------------------------- def write(self, filename): # print " writing:",filename fd = open(filename, 'w') fd.write(str(self)) fd.close() # -------------------------------------------------------------------------------- class ImageSizes: """During the processing of the source image files I create a text file that contains lines of the form 'filename x-size y-size bytes'. Each output file has an entry in this file and the information is used for the WIDTH and HEIGHT attributes of the IMG tag. This class puts a dictionary type wrapper around the data from the file allowing me to look up image sizes based on filename.""" def __init__ (self): self.images = {} fd = open (dot_image_size_file(),"r") while 1: raw_line = fd.readline() if not raw_line: break [manufacturer, product, text, x, y, sz] = string.split (raw_line) self.images[manufacturer, product, text] = (x,y,sz) fd.close () def __getitem__ (self,name): return self.images[name] def get_image_data (self,man_res,prd_res,text): try: return self.images[man_res,prd_res,text] except: return None # -------------------------------------------------------------------------------- def do_manufacturer_pages (): print "Doing manufacturer pages..." # build the list of all products print " getting product information..." result = db_query ("SELECT m.man_id, m.man_full_nm, m.man_cmn_nm, m.man_nts, m.man_addr, m.man_res, " " p.prd_id,p.ptyp_id, p.prd_nm, p.prd_desc, p.prd_res " "FROM manufacturer m, product p " "WHERE m.man_id = p.man_id AND prd_include = 't'" "ORDER BY m.man_cmn_nm,p.ptyp_id,p.prd_nm;") product_data = {} product_list = [] manufacturer_data = {} manufacturer_list = [] man_prod_data = {} for (m_man_id, m_man_full_nm, m_man_cmn_nm, m_man_nts, m_man_addr, m_man_res, p_prd_id, p_ptyp_id, p_prd_nm, p_prd_desc, p_prd_res) in result: m_man_id = string.atoi (m_man_id) p_prd_id = string.atoi (p_prd_id) p_ptyp_id = string.atoi(p_ptyp_id) if m_man_id not in manufacturer_list: manufacturer_list.append (m_man_id) manufacturer_data[m_man_id] = (m_man_full_nm, m_man_cmn_nm, m_man_nts, m_man_addr, m_man_res) try: man_prod_data[m_man_id].append (p_prd_id) except KeyError: man_prod_data[m_man_id] = [p_prd_id] if p_prd_id not in product_list: product_list.append (p_prd_id) product_data[p_prd_id] = (p_ptyp_id, p_prd_nm, p_prd_desc, p_prd_res) # build table of transfered items... print " getting transfer information..." result = db_query ("SELECT prd_id,hwi_transfered FROM hwitem") transfered_table = {} for prd_id,hwi_transfered in result: if hwi_transfered == 'f': transfered_table[string.atoi(prd_id)] = 1 # Get resource data... print " getting resource data..." result = db_query ("SELECT res_id,man_id,res_nm,res_url,res_author,res_desc FROM resource") resource_table = {} for res_id,man_id,res_nm,res_url,res_author,res_desc in result: res_id = string.atoi(res_id) man_id = string.atoi(man_id) try: resource_table[man_id].append (res_id,res_nm,res_url,res_author,res_desc) except KeyError: resource_table[man_id] = [(res_id,res_nm,res_url,res_author,res_desc)] # process each manufacturer in turn... for m_man_id in manufacturer_list: (m_man_full_nm, m_man_cmn_nm, m_man_nts, m_man_addr, m_man_res) = manufacturer_data[m_man_id] print " ",m_man_full_nm,"'%s'"%(m_man_res) # build the list of all products for this manufacturer menu_product_list = [] for prod_type in ProductTypeIdList: (title,filename,resource) = ProductType[prod_type] menu_idnames = [] for product_id in man_prod_data[m_man_id]: if not transfered_table.has_key(product_id): continue if product_data[product_id][0] == prod_type: menu_idnames.append (product_id,product_data[product_id][1],product_data[product_id][3]) menu_product_list.append (prod_type,menu_idnames) # create the optput directory if it doesn't already exist... make_manufacturer_dir (m_man_res) # process each product type for this manufacturer... for prod_type in ProductTypeIdList: (title,filename,resource) = ProductType[prod_type] # list the products I have for this manufacturer for the current product type... # XXX this could be done as a table, as per the main product type tables, where we could include # product sub type information, and picture information etc. list = UL() product_count = 0 for p_prd_id in man_prod_data[m_man_id]: (p_ptyp_id, p_prd_nm, p_prd_desc, p_prd_res) = product_data[p_prd_id] if p_ptyp_id != prod_type: continue if not transfered_table.has_key(p_prd_id): continue product_count = product_count + 1 list.append (Href(product_page_href(m_man_res, p_prd_res), Bold(p_prd_nm))) # if we don't have any products of the current type for this manufacturer then # don't bother continuing... if product_count == 0: continue print " %ss"%(ProductType[prod_type][ProductType_name]) outfile = manufacturer_index_file (m_man_res,prod_type) doc = CompColDoc () doc.dir_level = 1 doc.title = m_man_full_nm doc.heading = m_man_full_nm doc.product_list = menu_product_list doc.current_manufacturer_res = m_man_res doc.current_manufacturer_cnam = m_man_cmn_nm doc.current_product_id = None doc.current_product_type = prod_type doc.menu_selection = None if m_man_nts: doc.append (Paragraph("%s"%(m_man_nts))) else: doc.append (Paragraph("")) doc.append (Paragraph("")) heading_one (doc,"Products I Have...") doc.append (Paragraph("""I have the following %ss in my collection for this manufacturer. """%(string.lower(ProductType[prod_type][ProductType_name])))) doc.append (Paragraph(list)) # Do Resources... heading_one (doc,"Resources...") try: resource_data = resource_table[m_man_id] except KeyError: doc.append ("Nothing :-(\n") else: rlist = List() for res_id,res_nm,res_url,res_author,res_desc in resource_data: line = MyContainer (Href(res_url,Emphasis(Strong(res_nm))), " - ", Emphasis(res_author)) if res_desc: line.append (BR()) line.append (Font(res_desc,size="-1")) line.append (P()) else: line.append (BR()) line.append (P()) rlist.append(line) doc.append(rlist) doc.append (BR()) doc.append (Font(Emphasis(MyContainer ("If you know of a resource that would be suitable for inclusion into this list please ", Href("mailto:collector@heydon.org","email me"), "and I will add it.")), size="-2")) doc.write (outfile) do_product_pages (manufacturer_data,man_prod_data,product_data,transfered_table,menu_product_list, m_man_id,prod_type) # -------------------------------------------------------------------------------- def do_product_pages (manufacturer_data,man_prod_data,product_data,transfered_table,menu_product_list, m_man_id,prod_type): (m_man_full_nm, m_man_cmn_nm, m_man_nts, m_man_addr, m_man_res) = manufacturer_data[m_man_id] # Now create the individual product pages... # for p_prd_id in man_prod_data[m_man_id]: (p_ptyp_id, p_prd_nm, p_prd_desc, p_prd_res) = product_data[p_prd_id] # ignore products of the wrong type and transfered products... if prod_type != p_ptyp_id: continue if not transfered_table.has_key(p_prd_id): continue print " ",p_prd_nm # -------------------------------------------------- # begin the document... doc = CompColDoc () doc.dir_level = 1 doc.title = "Product %s"%(p_prd_nm) doc.heading = p_prd_nm doc.product_list = menu_product_list doc.current_manufacturer_res = m_man_res doc.current_manufacturer_cnam = m_man_cmn_nm doc.current_product_id = p_prd_id doc.current_product_type = None doc.menu_selection = None # -------------------------------------------------- # do first picture... small_img_data = image_size_table.get_image_data (m_man_res,p_prd_res,"small") large_img_data = image_size_table.get_image_data (m_man_res,p_prd_res,"large") if small_img_data: (sm_w,sm_h,sm_s) = small_img_data if large_img_data: (la_w,la_h,la_s) = large_img_data doc.append (Href(product_picture_href (m_man_res,p_prd_res,"large"), Image (product_picture_href (m_man_res,p_prd_res,"small"), width=sm_w,height=sm_h,border=0, alt="Link to a large picture... (%sx%s - %sKb)"%(la_w,la_h,la_s)))) else: doc.append (Image (product_picture_href (m_man_res,p_prd_res,"small"), width=sm_w,height=sm_h,border=0,alt="picture of item")) # -------------------------------------------------- # do the description if we have one... if p_prd_desc: doc.append (Paragraph(p_prd_desc)) # -------------------------------------------------- # do pictures... picture_data = db_query ("SELECT pict_suffix,pict_desc FROM picture " "WHERE prd_id = %s ORDER BY pict_suffix"%(p_prd_id)) if picture_data: heading_one (doc,"Other Pictures...") picts = List() for suffix,description in picture_data: img_data = image_size_table.get_image_data (m_man_res,p_prd_res,suffix) if img_data: picts.append (Href(product_picture_href (m_man_res,p_prd_res,suffix), description)) doc.append (picts) # -------------------------------------------------- # do items... item_data = db_query ("SELECT hwi_id,hwi_p_dte,its_id,hwi_serial_num,hwi_nte " + "FROM hwitem " + "WHERE prd_id = %s AND "%(p_prd_id) + " hwi_transfered = 'f' " "ORDER BY hwi_serial_num") if item_data: heading_one (doc,"My Items...") itbl = TableLite (border=0,cellpadding=0,width="100%") count = 0 for hwi_id,hwi_p_dte,its_id,hwi_serial_num,hwi_nte in item_data: count = count + 1 history_data = db_query ("SELECT hist_dte,hwi_id,hist_nts " + "FROM history " + "WHERE hwi_id = %s AND hist_visible = 't' "%(hwi_id) + "ORDER BY hist_dte") row_span = 3 if hwi_nte: row_span = row_span + 1 if history_data: row_span = row_span + 1 itbl.append (TR(TD(Image("/kevan/collection/images/blank.gif",width=1,height=1,alt=" "), height=1,bgcolor=GREEN,colspan=4))) itbl.append (TR(TD("%d) "%(count),rowspan = row_span,valign="top"), TD(Strong("Ser. No."),align="left",nowrap = 1,valign="top"), TD(Strong(":"),valign="top"), TD(hwi_serial_num,align="left",nowrap = 1,width="100%",valign="top") )) if hwi_p_dte: tm_str = regsub.gsub ("-","/",hwi_p_dte) else: tm_str = "unknown" itbl.append (TR(TD(Strong("Acquired"),align="left",nowrap = 1,valign="top"), TD(Strong(":"),valign="top"), TD(tm_str,align="left",nowrap = 1,width="100%",valign="top"))) itbl.append (TR(TD(Strong("Status"),align="left",nowrap = 1,valign="top"), TD(Strong(":"),valign="top"), TD(ItemStatus[its_id][ItemStatus_name],align="left",nowrap = 1,width="100%",valign="top"))) if hwi_nte: itbl.append (TR(TD(Strong("Notes"),align="left",nowrap = 1,valign="top"), TD(Strong(":"),valign="top"), TD(hwi_nte,colspan = 2,valign="top"))) if history_data: print history_data hist_row = TR() hist_cell = TD("",colspan=2, valign="top") hist_row.append (TD(Strong("History"),align="left",nowrap = 1,valign="top")) hist_row.append (TD(Strong(":"),valign="top")) for hist_dte,h_hwi_id,hist_nts in history_data: hist_cell.append (Strong(regsub.gsub ("-","/",hist_dte))) hist_cell.append (" - ") hist_cell.append (hist_nts) hist_cell.append (BR()) hist_row.append (hist_cell) itbl.append (hist_row) itbl.append (TR(TD(Image("/kevan/collection/images/blank.gif",width=1,height=2,alt=" "), height = 2,colspan=3))) doc.append (itbl) # -------------------------------------------------- # do software... cart_data = db_query ("select s.sfw_id, s.man_id, s.prd_id, s.sfw_nm, s.sfw_code, " + "i.sfw_id, i.mda_id, i.swi_manuals, i.swi_box " + "from software s, switem i " + "where s.prd_id = %s AND s.sfw_id = i.sfw_id "%(p_prd_id) + "order by s.sfw_code") if cart_data: heading_one (doc,"Software...") ctbl = TableLite (border=0,width="100%") ctbl.append (TR(TD(Strong("Name"), bgcolor = GREY, align="left"), TD(Strong("Code"), bgcolor = GREY, align="left"), TD(Strong("Manufacturer"), bgcolor = GREY, align="left"), TD(Strong("Box"), bgcolor = GREY, align="center"), TD(Strong("Man"), bgcolor = GREY, align="center"))) for (s_sfw_id, s_man_id, s_prd_id, s_sfw_nm, s_sfw_code, i_sfw_id, i_mda_id, i_swi_manuals, i_swi_box) in cart_data: row = TR() if s_sfw_nm: row.append (TD(s_sfw_nm,bgcolor=LIGHT_GREY)) else: row.append (TD(" ",bgcolor=LIGHT_GREY)) if s_sfw_code: row.append (TD(s_sfw_code,bgcolor=LIGHT_GREY)) else: row.append (TD(" ",bgcolor=LIGHT_GREY)) if ManufacturerName.has_key (s_man_id): row.append (TD(ManufacturerName[s_man_id][Manufacturer_CommonName],bgcolor=LIGHT_GREY)) else: row.append (TD(" ",bgcolor=LIGHT_GREY)) if i_swi_manuals == "t": row.append (TD(Image("/kevan/collection/images/tick.gif",width=12,height=12,alt="has manual"), bgcolor=LIGHT_GREY,align="center")) else: row.append (TD(Image("/kevan/collection/images/cross.gif",width=12,height=12,alt="no manual"), bgcolor=LIGHT_GREY,align="center")) if i_swi_box == "t": row.append (TD(Image("/kevan/collection/images/tick.gif",width=12,height=12,alt="has box"), bgcolor=LIGHT_GREY,align="center")) else: row.append (TD(Image("/kevan/collection/images/cross.gif",width=12,height=12,alt="no box"), bgcolor=LIGHT_GREY,align="center")) ctbl.append (row) doc.append (ctbl) # -------------------------------------------------- # finally write what we have to the output file... doc.write (product_page_file (m_man_res,p_prd_res)) # -------------------------------------------------------------------------------- def do_index_pages (): print "Doing index pages..." # build table of transfered items... print " getting transfer information..." result = db_query ("SELECT prd_id,hwi_transfered FROM hwitem") transfered_table = {} for prd_id,hwi_transfered in result: if hwi_transfered == 'f': transfered_table[string.atoi(prd_id)] = 1 # Get the defined product types from the database... # for prod_type in ProductTypeIdList: (title,filename,resource) = ProductType[prod_type] print " ",title+"s",prod_type doc = CompColDoc () doc.menu_selection = 100 + prod_type doc.title = title doc.heading = title + 's' doc.dir_level = 0 results = db_query ("SELECT m.man_full_nm,m.man_cmn_nm,m.man_res, " " p.prd_id,p.prd_nm,p.man_id,p.ptyp_id,p.prd_desc, " " p.prd_res " "FROM manufacturer m, product p " "WHERE p.ptyp_id = %s AND "%(prod_type) + " p.man_id = m.man_id AND " " p.prd_include = 't' " "ORDER BY m.man_full_nm,p.prd_nm ") last_man_name = None prod_table = {} for (man_fullname,man_name,man_res,prod_id,name,manufacturer_id,type,description,resource) in results: resource = string.strip(resource) try: prod_table[man_name].append (man_res,prod_id,name,manufacturer_id,type,description,resource) except KeyError: prod_table[man_name] = [(man_res,prod_id,name,manufacturer_id,type,description,resource)] doc.append (P()) table = TableLite(border = 0,width="100%",cellspacing=2) table.append (TR(TD(" ", align = "left", bgcolor = GREY), TD(Strong("Manufacturer"), align = "left", bgcolor = GREY), TD(Strong("Product"), align = "left", bgcolor = GREY), TD(Strong("Picture"), align = "center", bgcolor = GREY)) ) prod_keys = prod_table.keys() prod_keys.sort() count = 0 for man_name in prod_keys: prod_list = prod_table[man_name] prod_count = 0 for man_res,prod_id,name,manufacturer_id,type,description,resource in prod_list: if transfered_table.has_key(string.atoi(prod_id)): prod_count = prod_count + 1 if prod_count == 0: continue man_res = prod_list[0][0] man_name_len = len(man_name) first = 1 for man_res,prod_id,name,manufacturer_id,type,description,resource in prod_list: man_res = string.strip(man_res) resource = string.strip(resource) if not transfered_table.has_key(string.atoi(prod_id)): continue count = count + 1 href = "manufacturer-%s/%s.html"%(man_res,resource) if not first: this_row = TR(TD("%d"%(count),valign = "top", bgcolor = LIGHT_GREY, align="right")) else: this_row = TR(TD("%d"%(count),valign = "top", bgcolor = LIGHT_GREY, align= "right"), TD(Href(manufacturer_index_href (man_res,prod_type),Strong(Emphasis(man_name))), valign = "top", rowspan = prod_count, bgcolor = LIGHT_GREY)) first = 0 if man_name == name[:man_name_len] and len(name) != man_name_len and name[man_name_len] == " ": print_name = name[man_name_len:] else: print_name = name this_row.append (TD(Href(href,print_name),bgcolor=LIGHT_GREY)) if os.path.exists (product_picture_file (man_res,resource,"large")): image = Image ("%s/tick.gif"%(base_url_images), alt="yes", width=12, height=12) else: image = Image ("%s/cross.gif"%(base_url_images), alt="no", width=12, height=12) this_row.append (TD(image,align="center",valign="middle",bgcolor=LIGHT_GREY)) table.append (this_row) print " ",count doc.append (Center(table)) doc.write (ptype_index_file(filename)) # -------------------------------------------------------------------------------- def do_static_pages (): """The static pages on my web site are partially generated from the database. I store the body HTML in a plain file that I can edit using my favourite editor on Unix. The other information like title, description, keywords etc. are stored in the database. This allows me to bolt on the standard headers and footers I use on my web site.""" print "Doing static pages..." # Get the list of static pages from the database... # result = db_query ("SELECT spg_id,spg_filename,spg_title,spg_desc,spg_keywords,spg_menu,spg_heading " + "FROM static_page " + "ORDER BY spg_filename") for (id,filename,title,description,keywords,has_menu,has_heading) in result: print " ",filename id = string.atoi(id) # Create a Document... # doc = CompColDoc() doc.dir_level = 0 doc.menu_selection = 200 + id # Set the heading and whether we have a menu bar from fields in the database... # if has_heading == "t": doc.heading = title if has_menu == "t": doc.has_menu = YES else: doc.has_menu = NO # Output my standard header... doc.title = title doc.description = description doc.keywords = string.splitfields (keywords,",") # Open the source file and dump its contents to the destination file... source_filename = os.path.join (in_html_dir,filename) infd = open (source_filename,"r") doc.append (RawText (infd.read())) infd.close() # Write the Document to the output file... doc.write (static_page_file (filename)) # -------------------------------------------------------------------------------- def do_other_collectors_page (): print "Doing other collectors page..." doc = CompColDoc() doc.dir_level = 0 doc.menu_selection = 301 doc.title = "Other Collectors" doc.heading = "Other Collectors" doc.append (Paragraph("Below are links to other collectors web pages that I have found on the Web.")) list = BulletList() result = db_query ("SELECT ocol_id,ocol_surname,ocol_fornames,ocol_title,ocol_url,ocol_desc " + "FROM other_collector " + "ORDER BY ocol_surname,ocol_fornames") for (id,surname,fornames,title,url,description) in result: print " ",fornames,surname str = MyContainer (Href(url,Emphasis(Strong("%s %s"%(fornames,surname)))), RawText(" - "), Emphasis (title)) if description: str.append (BR(),description,P()) else: str.append (BR(),P()) list.append (str) doc.append (list) # Write the document... doc.write (other_collectors_file()) # -------------------------------------------------------------------------------- def do_plain_text (): print "Doing plain text report..." fd = open (plain_text_file(),"w") # Dump some totals... types = ProductType.keys() types.sort() fd.write ("\n") fd.write ("Unique Products...\n") fd.write ("\n") for t in types: data = db_query ("SELECT count(*) FROM product WHERE ptyp_id = %s and prd_include = 't'"%(t)) fd.write (" %-15s = %3s\n"%(ProductType[t][ProductType_name],data[0][0])) # Dump all the hardware items I have... line = (107 * "-") + "\n" fd.write ("\n") fd.write ("All Items...\n") fd.write ("\n") fd.write (line) fd.write ("| %-20s "%("Manufacturer")) fd.write ("| %-30s "%("Product")) fd.write ("| %-13s "%("Class")) fd.write ("| %-9s "%("Status")) fd.write ("| %-20s "%("Serial Number")) fd.write ("|\n") data = db_query ("SELECT m.man_cmn_nm, m.man_id," + " p.prd_nm, p.prd_id, p.man_id, p.ptyp_id," + " hw.prd_id,hw.hwi_serial_num,hw.its_id " + "FROM manufacturer m, product p , hwitem hw " + "WHERE m.man_id = p.man_id AND " + " p.prd_id = hw.prd_id AND " + " hw.hwi_transfered = 'f' " + "ORDER BY m.man_cmn_nm,p.prd_nm,hw.hwi_serial_num") last_man_name = None for (man_name,man_id,prod_name,prod_id,prod_manid,ptype,item_prodid,serial,status) in data: if man_name == last_man_name: fd.write ("| %-20s "%(" ")) else: fd.write (line) fd.write ("| %-20s "%(man_name)) last_man_name = man_name fd.write ("| %-30s "%(prod_name)) fd.write ("| %-13s "%(ProductType[string.atoi(ptype)][ProductType_name])) fd.write ("| %-9s "%(ItemStatus[status][ItemStatus_name])) fd.write ("| %-20s "%(serial)) fd.write ("|\n") fd.write (line) fd.close () # ==================================================================================================== def get_image_size (path): if not os.path.isfile(path): return (None,None,None) stats = os.stat(path) size = int(stats[6] / 1024.0) cmd = "/usr/local/bin/identify %s"%(path) pd = os.popen(cmd,"r") raw_line = pd.readline() pd.close() [x,y] = string.splitfields(string.split(raw_line)[1],"x") return (string.atoi(x),string.atoi(y),size) def do_pictures (): print "Generating pictures..." comment = "-comment \"Copyright Kevan Heydon \" " thumb_size = 64.0 normal_pat = regex.compile ("\([a-z0-9_]+\)-\([a-z0-9_]+\)\.tif") extra_pat = regex.compile ("\([a-z0-9_]+\)-\([a-z0-9_]+\)-\([a-z0-9_]+\)\.tif") files = os.listdir (in_picture_dir) files.sort() fd = open (dot_image_size_file(),"w",0) for file in files: src_path = os.path.join (in_picture_dir,file) (xs,ys,sz) = get_image_size (src_path) print print "%s (%dx%d) %dKb"%(src_path,xs,ys,sz) if normal_pat.search(file) == 0: (manufacturer,product) = normal_pat.group (1,2) man_dir = "manufacturer-"+manufacturer # create the output directory if it doesn't already exist... make_manufacturer_dir (manufacturer) # Do large picture dst_path = product_picture_file (manufacturer,product,"large") cmd = ("/usr/local/bin/convert", "-quality 80", "-interlace NONE", comment, "%s jpeg:%s"%(src_path,dst_path)) os.system (string.join(cmd)) (nxs,nys,nsz) = get_image_size (dst_path) print " -> %s (%dx%d) %dKb"%(dst_path,nxs,nys,nsz) fd.write ("%s %s %s %d %d %d\n"%(manufacturer, product, "large" ,nxs, nys, nsz)) # Do small picture dst_path = product_picture_file (manufacturer,product,"small") cmd = ("/usr/local/bin/convert", "-quality 90", "-interlace NONE", comment, "-geometry 33%", "%s jpeg:%s"%(src_path,dst_path)) os.system (string.join(cmd)) (nxs,nys,nsz) = get_image_size (dst_path) print " -> %s (%dx%d) %dKb"%(dst_path,nxs,nys,nsz) fd.write ("%s %s %s %d %d %d\n"%(manufacturer, product, "small" ,nxs, nys, nsz)) continue # Do Thumbnail... dst_path = product_picture_file (manufacturer,product,"thumb") if xs > ys: geom = "64" else: geom = "x64" cmd = ("/usr/local/bin/convert", "-quality 90", "-interlace NONE", comment, "-geometry %s"%(geom), "%s jpeg:%s"%(src_path,dst_path)) os.system (string.join(cmd)) (nxs,nys,nsz) = get_image_size (dst_path) print " -> %s (%dx%d) %dKb"%(dst_path,nxs,nys,nsz) fd.write ("%s %s %s %d %d %d\n"%(manufacturer, product, "large" ,nxs, nys, nsz)) elif extra_pat.search(file) == 0: (manufacturer,product,text) = extra_pat.group (1,2,3) # create the output directory if it doesn't already exist... make_manufacturer_dir (manufacturer) dst_path = product_picture_file (manufacturer,product,text) cmd = ("/usr/local/bin/convert", "-quality 85", "-interlace NONE", comment, "%s jpeg:%s"%(src_path,dst_path)) os.system (string.join(cmd)) (nxs,nys,nsz) = get_image_size (dst_path) print " -> %s (%dx%d) %dKb"%(dst_path,nxs,nys,nsz) fd.write ("%s %s %s %d %d %d\n"%(manufacturer, product, text ,nxs, nys, nsz)) else: print "*** IGNORING:",file fd.close () # ==================================================================================================== def main(): global image_size_table global dbase_connection for arg in sys.argv[1:]: arg = string.lower(arg) if arg == "-html": # Load the image sizes... image_size_table = ImageSizes() # Connect to database and generate pages... print "Connecting to database..." dbase_connection = pgext.connect (dbname="collection") read_static_tables () do_plain_text () do_static_pages () do_other_collectors_page () do_index_pages () do_manufacturer_pages () elif arg == "-pic": do_pictures () elif arg == "-clear": print "Removing..." print " text files" os.system ("/usr/bin/rm -rf %s/*.txt"%(base_dir_collection)) print " html files" os.system ("/usr/bin/rm -rf %s/*.html"%(base_dir_collection)) print " manufacturer directories" os.system ("/usr/bin/rm -rf %s/manufacturer-*"%(base_dir_collection)) print " pictures" os.system ("/usr/bin/rm -rf %s/pictures/*"%(base_dir_collection)) print "Done.." # -------------------------------------------------------------------------------- if 0: import profile profile.run("main()") print dq_count print len(dq_diff.keys()) qs = [] for k,v in dq_queries.items(): qs.append(v,k) qs.sort () for v,k in qs: print "%4d -> %s"%(v,k) else: main ()