/scrapy/utils/template.py
Python | 34 lines | 22 code | 9 blank | 3 comment | 5 complexity | b6152c99f1628211e47a69a143c56046 MD5 | raw file
Possible License(s): BSD-3-Clause
- """Helper functions for working with templates"""
- import os
- import re
- import string
- def render_templatefile(path, **kwargs):
- with open(path, 'rb') as fp:
- raw = fp.read().decode('utf8')
- content = string.Template(raw).substitute(**kwargs)
- render_path = path[:-len('.tmpl')] if path.endswith('.tmpl') else path
- with open(render_path, 'wb') as fp:
- fp.write(content.encode('utf8'))
- if path.endswith('.tmpl'):
- os.remove(path)
- CAMELCASE_INVALID_CHARS = re.compile(r'[^a-zA-Z\d]')
- def string_camelcase(string):
- """ Convert a word to its CamelCase version and remove invalid chars
- >>> string_camelcase('lost-pound')
- 'LostPound'
- >>> string_camelcase('missing_images')
- 'MissingImages'
- """
- return CAMELCASE_INVALID_CHARS.sub('', string.title())