scrapy / scrapy
1
"""Helper functions for working with templates"""
2

3 7
import os
4 7
import re
5 7
import string
6

7

8 7
def render_templatefile(path, **kwargs):
9 7
    with open(path, 'rb') as fp:
10 7
        raw = fp.read().decode('utf8')
11

12 7
    content = string.Template(raw).substitute(**kwargs)
13

14 7
    render_path = path[:-len('.tmpl')] if path.endswith('.tmpl') else path
15

16 7
    if path.endswith('.tmpl'):
17 7
        os.rename(path, render_path)
18

19 7
    with open(render_path, 'wb') as fp:
20 7
        fp.write(content.encode('utf8'))
21

22

23 7
CAMELCASE_INVALID_CHARS = re.compile(r'[^a-zA-Z\d]')
24

25

26 7
def string_camelcase(string):
27
    """ Convert a word  to its CamelCase version and remove invalid chars
28

29
    >>> string_camelcase('lost-pound')
30
    'LostPound'
31

32
    >>> string_camelcase('missing_images')
33
    'MissingImages'
34

35
    """
36 7
    return CAMELCASE_INVALID_CHARS.sub('', string.title())

Read our documentation on viewing source code .

Loading