本文整理汇总了Python中string.Template.read方法的典型用法代码示例。如果您正苦于以下问题:Python Template.read方法的具体用法?Python Template.read怎么用?Python Template.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类string.Template
的用法示例。
在下文中一共展示了Template.read方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: upload_table
# 需要导入模块: from string import Template [as 别名]
# 或者: from string.Template import read [as 别名]
def upload_table(file_input, filename):
if filename == '':
with open("templates/index_error.template", 'r') as index_error:
index_err_message = Template(index_error.read())
return index_err_message.substitute(error_message="Please enter a file!")
if filename[len(filename)-4:] != ".xls" and filename[len(filename)-5:] != ".xlsx":
with open("templates/index_error.template", 'r') as index_error:
index_err_message = Template(index_error.read())
return index_err_message.substitute(error_message="Wrong file extension!")
if filename[len(filename)-4:] == ".xls":
workbook = xlrd.open_workbook(file_contents=file_input.read(), encoding_override='cp1252', formatting_info=True)
else:
workbook = xlrd.open_workbook(file_contents=file_input.read(), encoding_override='cp1252')
list_of_sheets_name = workbook.sheet_names()
id_tabs_list = []
for sheet_name in list_of_sheets_name:
sheet_name = unicodedata.normalize('NFKD', sheet_name).encode('ASCII', 'ignore')
sheet_name = sheet_name.lower()
sheet_name = sheet_name.replace(' ', '-')
id_tabs_list.append(sheet_name)
with open("templates/index.template", 'r') as index_file:
index = Template(index_file.read())
with open("templates/table.template", 'r') as table_file:
table = Template(table_file.read())
with open("templates/table_rows.template", 'r') as table_rows_file:
table_rows = Template(table_rows_file.read())
with open("templates/table_row_col.template", 'r') as table_row_col:
table_row_col = Template(table_row_col.read())
with open("templates/table_row_col_head.template", 'r') as table_row_col_head:
table_row_col_head = Template(table_row_col_head.read())
with open("templates/tabs.template", 'r') as tabs:
tabs = Template(tabs.read())
num_sheet = 0
render_table = ""
render_tab = ""
active = " in active"
for sheet in workbook.sheets():
if num_sheet != 0:
render_tab += tabs.substitute(tab=list_of_sheets_name[num_sheet],
tab_id=id_tabs_list[num_sheet]) + "\t\t\t\t"
active = ""
render_table_rows, render_table_head = process_sheet(sheet, table_rows,
table_row_col, table_row_col_head)
render_table += table.substitute(tab_id=id_tabs_list[num_sheet],
active=active,
table_head=render_table_head,
table_rows=render_table_rows) + "\t\t\t\t"
num_sheet += 1
#print render_table
return index.substitute(tab_id=id_tabs_list[0],
first_tab=list_of_sheets_name[0],
tab=render_tab,
table=render_table)
示例2: generate_cpp
# 需要导入模块: from string import Template [as 别名]
# 或者: from string.Template import read [as 别名]
def generate_cpp(tmpl, tests):
"""Behelyettesiti a template-be az osszegyujtott testeseteket.
"""
tmpl = Template(tmpl.read())
return tmpl.safe_substitute(TESTS=tests)
示例3: get_template
# 需要导入模块: from string import Template [as 别名]
# 或者: from string.Template import read [as 别名]
def get_template (file_name):
template = None
with open (file_name, 'r') as template:
template = Template (template.read ())
return template
示例4: _send_alert
# 需要导入模块: from string import Template [as 别名]
# 或者: from string.Template import read [as 别名]
def _send_alert(self, mail_type, user, region_ids, name_dict, uptime_dict, warning_threshold, critical_threshold,
stop=False, dept=None, rg_dict=None):
user_name = user.split('.')[0].capitalize()
profiles = dept if dept else [self._profile_name.upper()]
number = 0
if self._cloud_provider == 'Azure':
table = prettytable.PrettyTable(['Region', 'RG', 'Name', 'Uptime'])
for region, ids in region_ids.items():
number += len(ids)
for iid in ids:
table.add_row([
region,
rg_dict[iid],
name_dict[iid],
uptime_dict[iid]
])
else:
table = prettytable.PrettyTable(['Region', 'Instance ID', 'Name', 'Uptime'])
for region, ids in region_ids.items():
number += len(ids)
for iid in ids:
table.add_row([
region,
iid,
name_dict[iid],
uptime_dict[iid]
])
table.align = 'l'
if number > 1:
s = 's'
have = 'have'
else:
s = ''
have = 'has'
if len(profiles) > 1:
ss = 's'
else:
ss = ''
if number > 1:
some_of_them = 'and either all or some of them'
else:
some_of_them = 'that'
sender = CONFIG.EMAIL_FROM
recipient = user + '@' + CONFIG.EMAIL_DOMAIN
cc_recipient = []
if mail_type in ['warning', 'critical']:
for profile in profiles:
if profile.lower() in CONFIG.HEADS:
cc_recipient += CONFIG.HEADS[profile.lower()]
if recipient in cc_recipient:
cc_recipient.remove(recipient)
if cc_recipient:
cc = ' (cc: %s)' % ', '.join(cc_recipient)
else:
cc = ''
subject = '%s %s %s: running instances' % (mail_type.upper(), self._cloud_provider, '/'.join(profiles))
if stop:
stop_msg = '\nANY INSTANCES RUNNING FOR LONGER THAN %s HOURS WILL BE STOPPED IMMEDIATELY!\
\n\nPlease check your %s account and make sure there are no more offending instances.\n' % \
((critical_threshold / 3600), self._cloud_provider)
else:
stop_msg = '\nPLEASE IMMEDIATELY STOP OR TERMINATE ANY INSTANCES THAT ARE NO LONGER IN USE!\n'
template = open('%s/templates/%s.txt' % (os.path.dirname(os.path.realpath(__file__)), mail_type))
template = Template(template.read())
message = template.substitute({
'user_name': user_name,
'number': number,
's': s,
'cloud': self._cloud_provider,
'profile': '/'.join(profiles),
'ss': ss,
'table': table,
'warning_threshold': warning_threshold / 3600,
'critical_threshold': critical_threshold / 3600,
'bp_url': self._bp_url[self._cloud_provider],
'some_of_them': some_of_them,
'stop_msg': stop_msg,
'have': have,
})
print('Sending %s notification to %s%s... ' % (mail_type, recipient, cc), end='')
response = self._mailer.send(
sender=sender,
recipients=recipient,
subject=subject,
message=message,
code=True,
cc=cc_recipient
)
#.........这里部分代码省略.........