summaryrefslogtreecommitdiff
path: root/yarnstep.py
blob: 30ccae1cbec29c434a0b4a1934998bd18afbbc01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# yarnstep.py -- Python code for making yarn IMPLEMENTS sections nicer
#
# This is meant to be imported, not used with yarn --shell-library.
#
# Copyright 2016  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import os

import cliapp


_next_match = 1
def get_next_match():
    global _next_match
    name = 'MATCH_{}'.format(_next_match)
    _next_match += 1
    return os.environ[name]


def get_next_match_as_int():
    return int(get_next_match())


def get_next_match_as_datadir_path():
    return datadir(get_next_match())


def datadir(relative):
    return os.path.join(os.environ['DATADIR'], relative)


def srcdir(relative):
    return os.path.join(os.environ['SRCDIR'], relative)


def iter_over_files(root):
    for dirname, _, filenames in os.walk(root):
        for filename in filenames:
            yield os.path.join(dirname, filename)


def cat(filename):
    with open(filename) as f:
        return f.read()


def write_file(filename, data):
    dirname = os.path.dirname(filename)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    with open(filename, 'w') as f:
        f.write(data)


def unescape_backslashes(s):
    result = ''
    while s:
        if s.startswith('\\') and len(s) > 1:
            result += unescape_char(s[1])
            s = s[2:]
        else:
            result += s[0]
            s = s[1:]
    return result


def unescape_char(c):
    table = {
        'n': '\n',
    }
    return table.get(c, c)