aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/parso/py2/parso/file_io.py
blob: 85b7283ecca87f9b34873bb59039c4123ae678f1 (plain) (blame)
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
import os 
from parso._compatibility import FileNotFoundError 
 
import __res as res 
 
 
class FileIO(object): 
    def __init__(self, path): 
        self.path = path 
 
    def read(self):  # Returns bytes/str 
        # We would like to read unicode here, but we cannot, because we are not 
        # sure if it is a valid unicode file. Therefore just read whatever is 
        # here. 
        data = res.resfs_read(self.path) 
        if data: 
            return data 
        with open(self.path, 'rb') as f: 
            return f.read() 
 
    def get_last_modified(self): 
        """ 
        Returns float - timestamp or None, if path doesn't exist. 
        """ 
        try: 
            return os.path.getmtime(self.path) 
        except OSError: 
            # Might raise FileNotFoundError, OSError for Python 2 
            return None 
 
    def _touch(self): 
        try: 
            os.utime(self.path, None) 
        except FileNotFoundError: 
            try: 
                file = open(self.path, 'a') 
                file.close() 
            except (OSError, IOError):  # TODO Maybe log this? 
                return False 
        return True 
 
    def __repr__(self): 
        return '%s(%s)' % (self.__class__.__name__, self.path) 
 
 
class KnownContentFileIO(FileIO): 
    def __init__(self, path, content): 
        super(KnownContentFileIO, self).__init__(path) 
        self._content = content 
 
    def read(self): 
        return self._content