(args=None)
| 1140 | |
| 1141 | |
| 1142 | def fill_command(args=None): |
| 1143 | import sys |
| 1144 | import optparse |
| 1145 | import pkg_resources |
| 1146 | import os |
| 1147 | if args is None: |
| 1148 | args = sys.argv[1:] |
| 1149 | dist = pkg_resources.get_distribution('Paste') |
| 1150 | parser = optparse.OptionParser( |
| 1151 | version=coerce_text(dist), |
| 1152 | usage=_fill_command_usage) |
| 1153 | parser.add_option( |
| 1154 | '-o', '--output', |
| 1155 | dest='output', |
| 1156 | metavar="FILENAME", |
| 1157 | help="File to write output to (default stdout)") |
| 1158 | parser.add_option( |
| 1159 | '--html', |
| 1160 | dest='use_html', |
| 1161 | action='store_true', |
| 1162 | help="Use HTML style filling (including automatic HTML quoting)") |
| 1163 | parser.add_option( |
| 1164 | '--env', |
| 1165 | dest='use_env', |
| 1166 | action='store_true', |
| 1167 | help="Put the environment in as top-level variables") |
| 1168 | options, args = parser.parse_args(args) |
| 1169 | if len(args) < 1: |
| 1170 | print('You must give a template filename') |
| 1171 | sys.exit(2) |
| 1172 | template_name = args[0] |
| 1173 | args = args[1:] |
| 1174 | vars = {} |
| 1175 | if options.use_env: |
| 1176 | vars.update(os.environ) |
| 1177 | for value in args: |
| 1178 | if '=' not in value: |
| 1179 | print('Bad argument: %r' % value) |
| 1180 | sys.exit(2) |
| 1181 | name, value = value.split('=', 1) |
| 1182 | if name.startswith('py:'): |
| 1183 | name = name[:3] |
| 1184 | value = eval(value) |
| 1185 | vars[name] = value |
| 1186 | if template_name == '-': |
| 1187 | template_content = sys.stdin.read() |
| 1188 | template_name = '<stdin>' |
| 1189 | else: |
| 1190 | with open(template_name, 'rb', encoding="latin-1") as f: |
| 1191 | template_content = f.read() |
| 1192 | if options.use_html: |
| 1193 | TemplateClass = HTMLTemplate |
| 1194 | else: |
| 1195 | TemplateClass = Template |
| 1196 | template = TemplateClass(template_content, name=template_name) |
| 1197 | result = template.substitute(vars) |
| 1198 | if options.output: |
| 1199 | with open(options.output, 'wb') as f: |
no test coverage detected