/usr/lib/python2.7/dist-packages/clint/resources.py is in python-clint 0.3.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | # -*- coding: utf-8 -*-
"""
clint.resources
~~~~~~~~~~~~~~~
This module contains all the application resource features of clint.
"""
from __future__ import absolute_import
from __future__ import with_statement
import errno
from os import remove, removedirs
from os.path import isfile, join as path_join
from .packages.appdirs import AppDirs, AppDirsError
from .utils import mkdir_p, is_collection
__all__ = (
'init', 'user', 'site', 'cache',
'log', 'NotConfigured'
)
class AppDir(object):
"""Application Directory object."""
def __init__(self, path=None):
self.path = path
self._exists = False
if path:
self._create()
def __repr__(self):
return '<app-dir: %s>' % (self.path)
def __getattribute__(self, name):
if not name in ('_exists', 'path', '_create', '_raise_if_none'):
if not self._exists:
self._create()
return object.__getattribute__(self, name)
def _raise_if_none(self):
"""Raises if operations are carried out on an unconfigured AppDir."""
if not self.path:
raise NotConfigured()
def _create(self):
"""Creates current AppDir at AppDir.path."""
self._raise_if_none()
if not self._exists:
mkdir_p(self.path)
self._exists = True
def open(self, filename, mode='r'):
"""Returns file object from given filename."""
self._raise_if_none()
fn = path_join(self.path, filename)
return open(fn, mode)
def write(self, filename, content, binary=False):
"""Writes given content to given filename."""
self._raise_if_none()
fn = path_join(self.path, filename)
if binary:
flags = 'wb'
else:
flags = 'w'
with open(fn, flags) as f:
f.write(content)
def append(self, filename, content, binary=False):
"""Appends given content to given filename."""
self._raise_if_none()
fn = path_join(self.path, filename)
if binary:
flags = 'ab'
else:
flags = 'a'
with open(fn, 'a') as f:
f.write(content)
return True
def delete(self, filename=''):
"""Deletes given file or directory. If no filename is passed, current
directory is removed.
"""
self._raise_if_none()
fn = path_join(self.path, filename)
try:
if isfile(fn):
remove(fn)
else:
removedirs(fn)
except OSError as why:
if why.errno == errno.ENOENT:
pass
else:
raise why
def read(self, filename, binary=False):
"""Returns contents of given file with AppDir.
If file doesn't exist, returns None."""
self._raise_if_none()
fn = path_join(self.path, filename)
if binary:
flags = 'br'
else:
flags = 'r'
try:
with open(fn, flags) as f:
return f.read()
except IOError:
return None
def sub(self, path):
"""Returns AppDir instance for given subdirectory name."""
if is_collection(path):
path = path_join(path)
return AppDir(path_join(self.path, path))
# Module locals
user = AppDir()
site = AppDir()
cache = AppDir()
log = AppDir()
def init(vendor, name):
global user, site, cache, log
ad = AppDirs(name, vendor)
user.path = ad.user_data_dir
site.path = ad.site_data_dir
cache.path = ad.user_cache_dir
log.path = ad.user_log_dir
class NotConfigured(IOError):
"""Application configuration required. Please run resources.init() first."""
|