File: pathparse.py

package info (click to toggle)
theano 1.0.3+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 30,752 kB
  • sloc: python: 141,182; ansic: 9,505; makefile: 259; sh: 214; pascal: 81
file content (57 lines) | stat: -rw-r--r-- 1,396 bytes parent folder | download
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
from __future__ import absolute_import, print_function, division
import os
import sys


class PathParser(object):
    """
    Class that allows to modify system's PATH environment variable
    at runtime. Currently used in ``theano.gpuarray.dnn`` module
    on Windows only.

    **Examples**:

    ..code-block:: python

        theano.pathparse.PathParser(pathToAdd1, pathToAdd2, ...)
        # PATH is then automatically updated for this execution.


    ..code-block:: python

        paths = theano.pathparse.PathParser()
        paths.add(path1)
        paths.add(path2)
        # PATH is updated after each call to ``add()``.

    """
    paths = set()

    def _add(self, path):
        path = path.strip()
        if path:
            if sys.platform == 'win32':
                # Windows is case-insensitive.
                path = path.lower()
            self.paths.add(os.path.abspath(path))

    def _update(self):
        os.environ['PATH'] = os.pathsep.join(sorted(self.paths))

    def _parse(self):
        for path in os.environ['PATH'].split(os.pathsep):
            self._add(path)

    def __init__(self, *paths):
        self._parse()
        for path in paths:
            self._add(path)
        self._update()

    def add(self, path):
        self._add(path)
        self._update()

    def _debug(self):
        for path in sorted(self.paths):
            print(path)