Implemented additional files/folders adding
[biaweb_qt.git] / biaweb_exporter.py
index 7c887c6..4560181 100644 (file)
@@ -3,6 +3,7 @@
 
 import os
 import os.path
+import sys
 import time
 import sqlite3
 import string
@@ -48,6 +49,109 @@ def html_format_rating (rating):
        rating_str = "".join (items)
        return rating_str
 
+# function to copy additional files and folders to the destination path
+def copy_files_folders (conf, files_to_copy, folders_to_copy):
+       # create the cgi-bin directory and try to copy search.py into the destination directory if possible
+       # otherwise user must copy it manually
+       search_script_path = os.path.join (sys.path[0], "search.py")
+       if os.path.exists (search_script_path):
+               try:
+                       os.mkdir (os.path.join (conf[5], "cgi-bin"))
+                       shutil.copy2 (search_script_path, os.path.join(conf[5], "cgi-bin"))
+               except IOError, OSError:
+                       return False
+
+       # try to copy the star rating images  to destination directory if possible
+       # otherwise user must copy it manually
+       rating_img_star = os.path.join (sys.path[0], "star.gif")
+       rating_img_greystar = os.path.join (sys.path[0], "star-grey.gif")
+       if os.path.exists (rating_img_star):
+               try:
+                       shutil.copy2 (rating_img_star, conf[5])
+               except IOError, OSError:
+                       return False
+       if os.path.exists (rating_img_greystar):
+               try:
+                       shutil.copy2 (rating_img_greystar, conf[5])
+               except IOError, OSError:
+                       return False
+
+       # additional files to copy
+
+       # first copy files
+       # check if files to copy is not empty
+       if files_to_copy <> []:
+               for src, dest in files_to_copy:
+                       # get full path from relative path in dest
+                       full_dest = os.path.join (conf[5], dest)
+                       try:
+                               shutil.copy2 (src, full_dest)
+                       except IOError, OSError:
+                               return False
+
+       # additional folders to copy
+
+       # now copy the folders
+       if folders_to_copy <> []:
+               for src, dest in folders_to_copy:
+                       # get full path from relative path in dest
+                       full_dest = os.path.join (conf[5], dest)
+                       try:
+                               shutil.copytree (src, full_dest)
+                       except IOError, OSError:
+                               return False
+
+       # finally return true
+       return True
+
+
+
+# function to generate article pages
+def generate_article_pages (dbname, conf, templates, category_str, bestrated_str):
+       # main template
+       tpl_main = string.Template (templates[0][1])
+       # article template
+       tpl_articlebit = string.Template (templates[1][1])
+
+       # get all articles from the database
+       articles = biaweb_db.site_articles (dbname)
+       if articles == False:
+               return
+
+       # walk through each article and generate the file in the appropriate category
+       # folder
+       for art in articles:
+               art_cdate = time.ctime (art[5])
+               art_mdate = time.ctime (art[6])
+               rating_str = html_format_rating (art[9])
+               # now build the article from the article bit template
+               article_str = tpl_articlebit.safe_substitute (article_title = art[1],
+                                                                                                       article_cdate = art_cdate,
+                                                                                                       article_mdate = art_mdate,
+                                                                                                       rating = rating_str,
+                                                                                                       article_contents = art[4])
+
+               # now build the article page
+               articlepage_str = tpl_main.safe_substitute (site_title = conf[1],
+                                                                                                       site_url = "http://" + conf[0],
+                                                                                                       meta_keywords = art[3],
+                                                                                                       meta_description = art[2],
+                                                                                                       page_title = conf[1],
+                                                                                                       page_desc = conf[3],
+                                                                                                       contents_bit = article_str,
+                                                                                                       list_of_categories = category_str,
+                                                                                                       list_best_rated = bestrated_str,
+                                                                                                       copyright = conf[6])
+               # write to the article file
+               try:
+                       farticle = open (os.path.join (conf[5], art[13], art[8] + ".html"), "w+")
+                       farticle.write (articlepage_str)
+               except OSError, IOError:
+                       return False
+
+       # finally return true
+       return True
+
 # function to generate category directories and indices
 def generate_category_indices (dbname, conf, templates, category_str, bestrated_str, category_list):
        # main template
@@ -217,9 +321,20 @@ def generate_site (dbname, files_to_copy, folders_to_copy, search_type_full=True
        if ret == False:
                return False
 
+       # generate the category directories and indices
        ret = generate_category_indices (dbname, conf, tpls, cats_str, best_rated_str, cats)
        if ret == False:
                return False
 
+       # generate the article pages
+       ret = generate_article_pages (dbname, conf, tpls, cats_str, best_rated_str)
+       if ret == False:
+               return False
+
+       # copy other files/folders into the destination path
+       ret = copy_files_folders (conf, files_to_copy, folders_to_copy)
+       if ret == False:
+               return False
+
        # finally when all is successfully done return true
        return True