# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - Data associated with a single Request

    @copyright: 2001-2003 by Jürgen Hermann <jh@web.de>
    @copyright: 2003-2004 by Thomas Waldmann
    @license: GNU GPL, see COPYING for details.
    ORS specific request classes    
"""
import sys,os, mimetypes, time, urllib,shutil
from MoinMoin import config, search, wikiutil
from MoinMoin.Page import Page
from MoinMoin.request import RequestCGI,RequestCLI
from MoinMoin.util import MoinMoinNoFooter, filesys, web

from MoinMoin.action import AttachFile



class RequestORSCGI(RequestCGI):
    """
    specialized on CGI requests
    if self.ors_mod_active is 0, behaves just like RequestCGI but allows extended call syntax
    """

    def __init__(self, properties={}):
        RequestCGI.__init__(self, properties)
        self.keywords={}
        self.export_mode=0
        self.pagevars=[]
        self.ors_mod_active=1

    def exportAttachments(self,pagename):
        """
        export all attachments for page
        """
        return
    
        files=AttachFile._get_files(self, pagename)
        attach_dir = AttachFile.getAttachDir(self, pagename, create=1)
        pname = wikiutil.quoteWikinameFS(pagename)
        attach_dir = os.path.join(self.export_dir, pname, "attachments")
        if len(files)==0:
            return
        if not os.path.isdir(attach_dir): 
            filesys.makeDirs(attach_dir)
        for filename in files:
            if filename.endswith('.chm'):
                continue
            fpath = AttachFile.getFilename(self, pagename, filename)
            tpath=os.path.join(attach_dir,filename)
            shutil.copyfile( fpath, tpath) 

class RequestExportCLI(RequestCLI):
    """ specialized on export requests """

    def __init__(self, properties={}):
        RequestCLI.__init__(self, properties)
        self.export_mode=1
        self.export_dir="."
        self.keywords={}
        self.pagevars=[]
        self.loadTheme('modern')

        d = {'title_text': None, 'title_link': None, 'page': None,}
        self.themedict = d
        self.setPragma('section-numbers', '0')

    def getPragma(self, key, defval=None):
        """ Query a pragma value (#pragma processing instruction)

            Keys are not case-sensitive.
        """
        if key=='section-numbers':
            return '0'
        return self.pragma.get(key.lower(), defval)

    def http_redirect(self, url):
        """ Redirect to a fully qualified, or server-rooted URL """
        raise Exception("Redirect to #%s# not handled by exporter!" % url)

    def setup_args(self, form=None):
        args = {
            'action':['print'],
            'media':['projection'],
            }
        return args

    def getAttachUrl(self,pagename, filename,addts=0, escaped=0):

        attach_dir = AttachFile.getAttachDir(self, pagename, create=0)
        pname = wikiutil.quoteWikinameFS(pagename)
        attach_dir = os.path.join(".", pname, "attachments")

        url = "%s/%s" % (attach_dir,
            urllib.quote(filename.encode(config.charset)))
        return url


    def exportAttachments(self,pagename):
        """
        export all attachments for page
        """
        files=AttachFile._get_files(self, pagename)
        attach_dir = AttachFile.getAttachDir(self, pagename, create=1)
        pname = wikiutil.quoteWikinameFS(pagename)
        attach_dir = os.path.join(self.export_dir, pname, "attachments")
        if len(files)==0:
            return
        if not os.path.isdir(attach_dir): 
            filesys.makeDirs(attach_dir)
        for filename in files:
            if filename.endswith('.chm'):
                continue
            fpath = AttachFile.getFilename(self, pagename, filename)
            tpath=os.path.join(attach_dir,filename)
            shutil.copyfile( fpath, tpath) 


