summaryrefslogtreecommitdiff
path: root/wiki-to-help/hhc.py
blob: dc7a54daaaab616604368c619e48a8b235c05e9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import platform, os
from executor import Executor

class HHC(object):
    """ Class for execution of Html Help Compiler """
    hhcexe="c:\\htmlhelp\\hhc.exe"

    def __init__(self,**args):
        """
        @args Arguments for Executor.__init__()
        """
        self.args=args

    def exWindows(self,source):
        """ Private. Compile @source calling HHC natively under Windows """
        cmd=[self.hhcexe,os.path.join(source,"htmlhelp.hhp")]
        return Executor(**self.args).executor(*tuple(cmd))

    def exWine(self,source):
        """ Private. Compile @source calling HHC via Wine """
        #dirname = os.path.dirname(source)
        wine = Wine(source,"j:",self.args)
        r = wine(self.hhcexe,"j:\\htmlhelp.hhp")
        del wine
        return r

    def __call__(self,source):
        """
        Converts @source with HHC
        @source path to input directory that contains htmlhelp.hhp
        """
        windows=(platform.system()=="Windows")
        if windows is False:
            self.exWine(source)
        else:
            self.exWindows(source)

class Wine(object):
    # TODO: this should be a singleton
    def __init__(self,workingDir,driveletter,args={}):
        """ 
        Setup the wine environment. Granting access so that wine is able 
        @workingDir will be accessable via @driveletter
        @args Arguments for Executor as dict (**args)
        E.g. Wine("/tmp/dir","j:") 
        """
        homedir = os.path.expanduser('~')
        wineprefix=os.path.join(homedir,".wine")
        drive=os.path.join(wineprefix,"dosdevices",driveletter)
        if os.path.lexists(drive):
            self.driveBak = drive+".bak"
            shutil.move(drive,self.driveBak)
        os.symlink(workingDir,drive)
        self.drive = drive
        #self.driveBak = driveBak
        self.executor = Executor(**args)

    def ex(self,*cmd):
        """ execute something with wine """
        cmd = [elem for elem in cmd]
        cmd = ["/usr/bin/wine"]+cmd
        return self.executor(*tuple(cmd))

    def __call__(self,*cmd):
        return self.ex(*cmd)

    def __del__(self):
        os.remove(self.drive)
        if hasattr(self,'driveBak'):
            shutil.move(self.driveBak,self.drive)