"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/extras/highlight_pipe.py" (12 May 2020, 1290 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 from subprocess import *
    2 
    3 class HighlightPipe:
    4     """ This Python package serves as interface to the highlight utility.
    5     Input and output streams are handled with pipes.
    6     Command line parameter length is validated before use."""
    7 
    8     def __init__(self):
    9         self.cmd = 'highlight'
   10         self.src=''
   11         self.options=dict()
   12         self.success=False
   13 
   14     def getResult(self):
   15         cmd = self.cmd
   16         for k, v in self.options.iteritems():
   17             option=" --%s" % k
   18             if ( v != '1'): option += "=%s" % v
   19             if (len(option)<50): cmd += option
   20         p = Popen(cmd, shell=True, bufsize=512, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
   21         (child_stdin, child_stdout, child_stderr) = (p.stdin, p.stdout, p.stderr)
   22 
   23         child_stdin.write(self.src)
   24         child_stdin.close()
   25         err_msg = child_stderr.readlines()
   26         if (len(err_msg)>0): return err_msg
   27         self.success=True
   28         return child_stdout.readlines()
   29 
   30 
   31 ###############################################################################
   32 
   33 def main():
   34     gen = HighlightPipe();
   35     gen.options['syntax'] = 'c'
   36     gen.options['style'] = 'edit-vim'
   37     gen.options['enclose-pre'] = '1'
   38     gen.options['fragment'] = '1'
   39     gen.options['inline-css'] = '1'
   40     gen.src = 'int main ()\n{ return 0; }'
   41 
   42     print gen.getResult()
   43     if not gen.success: print "Execution failed."
   44 
   45 if __name__=="__main__":
   46     main()