Changeset 943
- Timestamp:
- 08/04/06 12:51:40 (7 years ago)
- File:
-
- 1 edited
-
ccsd/trunk/crcnetd/_utils/ccsd_cfengine.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ccsd/trunk/crcnetd/_utils/ccsd_cfengine.py
r942 r943 536 536 start = time.time() 537 537 log_debug("Started host generation thread for %s" % host) 538 session = getSessionE(networkData["session_id"]) 538 539 539 540 # Retrieve the list of templates to generate and flag the start … … 566 567 files = t.writeTemplate(filename, template.multiFile) 567 568 tend = time.time() 569 # Set properties 570 format = getattr(t, "highlightFormat", "") 571 if format != "": 572 for file in files: 573 session.revision.propset(file, "ccs:format", format) 568 574 # Record how long the file took to generate 569 575 _statsLock.acquire() … … 631 637 log_debug("Started template generation thread for %s" % template_id) 632 638 tstart = time.time() 639 session = getSessionE(networkData["session_id"]) 633 640 # Instantiate a template 634 641 template = _networkTemplates[template_id] … … 640 647 files = t.writeTemplate(filename, template.multiFile) 641 648 tend = time.time() 649 # Set properties 650 format = getattr(template, "highlightFormat", "") 651 if format != "": 652 for file in files: 653 session.revision.propset(file, "ccs:format", format) 642 654 # Record how long the file took to generate 643 655 _statsLock.acquire() … … 1047 1059 entries[0]["props"] = props 1048 1060 if doMarkup: 1049 entries[0]["contents"] = markup(file) 1050 1061 if "ccs:format" in props.keys(): 1062 entries[0]["contents"] = markup(file, props["ccs:format"]) 1063 else: 1064 entries[0]["contents"] = markup(file) 1065 1051 1066 return entries 1052 1067 1053 def markup(file): 1054 return file.split("\n") 1068 def markup(file, format="sh"): 1069 """Passes the file through enscript for syntax highlighting""" 1070 1071 # Generate the command line to pass to enscript 1072 cmdline = config_get("cfengine", "enscript_path", "enscript") 1073 cmdline += ' --color -h -q --language=html -p - -E' + format 1074 log_debug("Enscript command line: %s" % cmdline) 1075 1076 # Run enscript 1077 (fdi, fdo) = os.popen2(cmdline) 1078 fdi.write(file) 1079 fdi.close() 1080 odata = fdo.read() 1081 rv = fdo.close() 1082 if rv is not None: 1083 log_warn("Could not run enscript to markup file!") 1084 log_debug(odata) 1085 return file.split("\n") 1086 1087 # Strip header and footer 1088 i = odata.find('<PRE>') 1089 beg = i > 0 and i + 6 1090 i = odata.rfind('</PRE>') 1091 end = i > 0 and i or len(odata) 1092 odata = EnscriptDeuglifier().format(odata[beg:end]) 1093 return odata.splitlines() 1094 1095 # Enscript Deuglifier code from Trac 1096 # 1097 # Copyright (C) 2003-2006 Edgewall Software 1098 # All rights reserved. 1099 # 1100 # Licensed using the Modified BSD license 1101 class EnscriptDeuglifier(object): 1102 def __new__(cls): 1103 self = object.__new__(cls) 1104 if not hasattr(cls, '_compiled_rules'): 1105 cls._compiled_rules = re.compile('(?:'+'|'.join(cls.rules())+')') 1106 self._compiled_rules = cls._compiled_rules 1107 return self 1108 1109 def format(self, indata): 1110 return re.sub(self._compiled_rules, self.replace, indata) 1111 1112 def replace(self, fullmatch): 1113 for mtype, match in fullmatch.groupdict().items(): 1114 if match: 1115 if mtype == 'font': 1116 return '<span>' 1117 elif mtype == 'endfont': 1118 return '</span>' 1119 return '<span class="code-%s">' % mtype 1120 1121 def rules(cls): 1122 return [ 1123 r'(?P<comment><FONT COLOR="#B22222">)', 1124 r'(?P<keyword><FONT COLOR="#5F9EA0">)', 1125 r'(?P<type><FONT COLOR="#228B22">)', 1126 r'(?P<string><FONT COLOR="#BC8F8F">)', 1127 r'(?P<func><FONT COLOR="#0000FF">)', 1128 r'(?P<prep><FONT COLOR="#B8860B">)', 1129 r'(?P<lang><FONT COLOR="#A020F0">)', 1130 r'(?P<var><FONT COLOR="#DA70D6">)', 1131 r'(?P<font><FONT.*?>)', 1132 r'(?P<endfont></FONT>)' 1133 ] 1134 rules = classmethod(rules) 1055 1135 1056 1136 #####################################################################
Note: See TracChangeset
for help on using the changeset viewer.
