aboutsummaryrefslogtreecommitdiffstats
path: root/roles/library/filepath.py
blob: 356f5fb9b8ede4ebf1483a5a34ccf680c0ccecb9 (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
#!/usr/bin/env python3

import os
from urllib.parse import quote

class FilterModule(object):
    def filters(self):
        return {
            'filepath': self.filepath
        }

    def filepath(self, path, *filename):
        #
        # path: a string or a list of string that contains successive parts
        #       of the path. Nul or empty parts are removed
        # filename: the optionnal filename to be used after the path. It may
        #       be specified using multiple args to be concatenate (useful
        #       when building dynamic names in ansible/jinja templates)
        #
        '''build a gitlab filepath given `path' and `filename'.'''

        if path is not None:
            if not isinstance(path, list):
                path = [path]
            path = list(filter(None, path))
            if path:
                path = os.path.normpath(os.path.join(path[0], *path[1:]))

        if filename:
            filename = ''.join(list(filter(None, filename)))

        if path and filename:
            path = os.path.join(path, filename)
        elif filename:
            path = filename

        if path:
            return quote(path, safe='')

        return None