2011-10-10 12:54:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist 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.
|
|
|
|
#
|
|
|
|
# cdist 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 cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import collections
|
|
|
|
|
|
|
|
import cdist
|
|
|
|
|
|
|
|
|
|
|
|
class AbsolutePathRequiredError(cdist.Error):
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'Absolute path required, got: %s' % self.path
|
|
|
|
|
|
|
|
|
|
|
|
class FileList(collections.MutableSequence):
|
|
|
|
"""A list that stores it's state in a file.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def __init__(self, path, initial=None):
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
raise AbsolutePathRequiredError(path)
|
2011-10-10 14:37:56 +00:00
|
|
|
self.path = path
|
2011-10-10 12:54:26 +00:00
|
|
|
if initial:
|
|
|
|
# delete existing file
|
2011-10-10 14:37:56 +00:00
|
|
|
os.unlink(self.path)
|
2011-10-10 12:54:26 +00:00
|
|
|
for i in initial:
|
|
|
|
self.append(i)
|
|
|
|
|
|
|
|
def __read(self):
|
|
|
|
lines = []
|
|
|
|
try:
|
2011-10-10 14:37:56 +00:00
|
|
|
with open(self.path) as fd:
|
2011-10-10 12:54:26 +00:00
|
|
|
for line in fd:
|
|
|
|
lines.append(line.rstrip('\n'))
|
|
|
|
except EnvironmentError as e:
|
|
|
|
# error ignored
|
|
|
|
pass
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def __write(self, lines):
|
|
|
|
try:
|
2011-10-10 14:37:56 +00:00
|
|
|
with open(self.path, 'w') as fd:
|
2011-10-10 12:54:26 +00:00
|
|
|
for line in lines:
|
|
|
|
fd.write(str(line) + '\n')
|
|
|
|
except EnvironmentError as e:
|
|
|
|
# error ignored
|
|
|
|
raise
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(list(self))
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
return self.__read()[index]
|
|
|
|
|
|
|
|
def __setitem__(self, index, value):
|
|
|
|
lines = self.__read()
|
|
|
|
lines[index] = value
|
|
|
|
self.__write(lines)
|
|
|
|
|
|
|
|
def __delitem__(self, index):
|
|
|
|
lines = self.__read()
|
|
|
|
del lines[index]
|
|
|
|
self.__write(lines)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
lines = self.__read()
|
|
|
|
return len(lines)
|
|
|
|
|
|
|
|
def insert(self, index, value):
|
|
|
|
lines = self.__read()
|
|
|
|
lines.insert(index, value)
|
|
|
|
self.__write(lines)
|
|
|
|
|
|
|
|
def sort(self):
|
|
|
|
lines = sorted(self)
|
|
|
|
self.__write(lines)
|
|
|
|
|
|
|
|
|
|
|
|
class FileListProperty(FileList):
|
2011-10-10 14:37:56 +00:00
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
"""
|
|
|
|
:param path: string or callable
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
class Foo(object):
|
|
|
|
parameters = DirectoryDictProperty(lambda obj: os.path.join(obj.absolute_path, 'parameter'))
|
|
|
|
other_dict = DirectoryDictProperty('/tmp/folder')
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.absolute_path = '/tmp/foo'
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.path = None
|
|
|
|
self.__path = path
|
|
|
|
|
|
|
|
def _set_path(self, *args, **kwargs):
|
|
|
|
if self.path is None:
|
|
|
|
path = self.__path
|
|
|
|
if callable(path):
|
|
|
|
path = path(*args, **kwargs)
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
raise AbsolutePathRequiredError(path)
|
|
|
|
self.path = path
|
|
|
|
|
2011-10-10 12:54:26 +00:00
|
|
|
# Descriptor Protocol
|
|
|
|
def __get__(self, obj, objtype=None):
|
|
|
|
if obj is None:
|
|
|
|
return self.__class__
|
2011-10-10 14:37:56 +00:00
|
|
|
self._set_path(obj)
|
2011-10-10 12:54:26 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __set__(self, obj, value):
|
2011-10-10 14:37:56 +00:00
|
|
|
self._set_path(obj)
|
|
|
|
os.unlink(self.path)
|
2011-10-10 12:54:26 +00:00
|
|
|
for item in value:
|
|
|
|
self.append(item)
|
|
|
|
|
|
|
|
def __delete__(self, obj):
|
|
|
|
raise AttributeError("can't delete attribute")
|
|
|
|
|
|
|
|
|
|
|
|
class DirectoryDict(collections.MutableMapping):
|
2011-10-11 07:02:01 +00:00
|
|
|
"""A dict that stores it's items as files in a directory.
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
"""
|
2011-10-10 14:37:56 +00:00
|
|
|
def __init__(self, path, initial=None, **kwargs):
|
2011-10-10 12:54:26 +00:00
|
|
|
if not os.path.isabs(path):
|
|
|
|
raise AbsolutePathRequiredError(path)
|
2011-10-10 14:37:56 +00:00
|
|
|
self.path = path
|
|
|
|
if initial is not None:
|
|
|
|
self.update(initial)
|
|
|
|
if kwargs:
|
2011-10-10 12:54:26 +00:00
|
|
|
self.update(kwargs)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(dict(self))
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
try:
|
2011-10-10 14:37:56 +00:00
|
|
|
with open(os.path.join(self.path, key), "r") as fd:
|
2011-10-10 12:54:26 +00:00
|
|
|
return fd.read().rstrip('\n')
|
|
|
|
except EnvironmentError:
|
|
|
|
raise KeyError(key)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2011-10-10 14:37:56 +00:00
|
|
|
with open(os.path.join(self.path, key), "w") as fd:
|
2011-10-10 12:54:26 +00:00
|
|
|
fd.write(str(value))
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
2011-10-10 14:37:56 +00:00
|
|
|
os.remove(os.path.join(self.path, key))
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
2011-10-10 14:37:56 +00:00
|
|
|
return iter(os.listdir(self.path))
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
2011-10-10 14:37:56 +00:00
|
|
|
return len(os.listdir(self.path))
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DirectoryDictProperty(DirectoryDict):
|
2011-10-10 14:37:56 +00:00
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
"""
|
|
|
|
:param path: string or callable
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
class Foo(object):
|
|
|
|
parameters = DirectoryDictProperty(lambda obj: os.path.join(obj.absolute_path, 'parameter'))
|
|
|
|
other_dict = DirectoryDictProperty('/tmp/folder')
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.absolute_path = '/tmp/foo'
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.path = None
|
|
|
|
self.__path = path
|
|
|
|
|
|
|
|
def _set_path(self, *args, **kwargs):
|
|
|
|
if self.path is None:
|
|
|
|
path = self.__path
|
|
|
|
if callable(path):
|
|
|
|
path = path(*args, **kwargs)
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
raise AbsolutePathRequiredError(path)
|
|
|
|
self.path = path
|
|
|
|
|
2011-10-10 12:54:26 +00:00
|
|
|
# Descriptor Protocol
|
|
|
|
def __get__(self, obj, objtype=None):
|
|
|
|
if obj is None:
|
|
|
|
return self.__class__
|
2011-10-10 14:37:56 +00:00
|
|
|
self._set_path(obj)
|
2011-10-10 12:54:26 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __set__(self, obj, value):
|
2011-10-10 14:37:56 +00:00
|
|
|
self._set_path(obj)
|
2011-10-10 12:54:26 +00:00
|
|
|
if value is not None:
|
2011-10-10 14:37:56 +00:00
|
|
|
for name in self.keys():
|
|
|
|
del self[name]
|
2011-10-10 12:54:26 +00:00
|
|
|
self.update(value)
|
|
|
|
|
|
|
|
def __delete__(self, obj):
|
|
|
|
raise AttributeError("can't delete attribute")
|
|
|
|
|
|
|
|
|
|
|
|
class FileBooleanProperty(object):
|
|
|
|
def __init__(self, path):
|
|
|
|
"""
|
|
|
|
:param path: string or callable
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
class Foo(object):
|
|
|
|
changed = FileBoolean(lambda obj: os.path.join(obj.absolute_path, 'changed'))
|
|
|
|
other_boolean = FileBoolean('/tmp/other_boolean')
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.absolute_path = '/tmp/foo_boolean'
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._path = path
|
|
|
|
|
|
|
|
def _get_path(self, *args, **kwargs):
|
|
|
|
path = self._path
|
|
|
|
if callable(path):
|
|
|
|
return path(*args, **kwargs)
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
raise AbsolutePathRequiredError(path)
|
|
|
|
return path
|
|
|
|
|
|
|
|
# Descriptor Protocol
|
|
|
|
def __get__(self, obj, objtype=None):
|
|
|
|
if obj is None:
|
|
|
|
return self.__class__
|
|
|
|
path = self._get_path(obj)
|
|
|
|
return os.path.isfile(path)
|
|
|
|
|
|
|
|
def __set__(self, obj, value):
|
|
|
|
path = self._get_path(obj)
|
|
|
|
if value:
|
|
|
|
open(path, "w").close()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
os.remove(path)
|
|
|
|
except EnvironmentError:
|
|
|
|
# ignore
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __delete__(self, obj):
|
|
|
|
raise AttributeError("can't delete attribute")
|
2011-10-11 07:02:01 +00:00
|
|
|
|
|
|
|
# FIXME: should have same anchestor as FileList
|
|
|
|
class FileStringProperty(object):
|
|
|
|
"""A string property which stores its state in a file.
|
|
|
|
"""
|
|
|
|
def __init__(self, path):
|
|
|
|
"""
|
|
|
|
:param path: string or callable
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
class Foo(object):
|
|
|
|
source = FileStringProperty(lambda obj: os.path.join(obj.absolute_path, 'source'))
|
|
|
|
other = FileStringProperty('/tmp/other')
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.absolute_path = '/tmp/foo_boolean'
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._path = path
|
|
|
|
|
|
|
|
def _get_path(self, *args, **kwargs):
|
|
|
|
path = self._path
|
|
|
|
if callable(path):
|
|
|
|
return path(*args, **kwargs)
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
raise AbsolutePathRequiredError(path)
|
|
|
|
return path
|
|
|
|
|
|
|
|
# Descriptor Protocol
|
|
|
|
def __get__(self, obj, objtype=None):
|
|
|
|
if obj is None:
|
|
|
|
return self.__class__
|
|
|
|
path = self._get_path(obj)
|
|
|
|
value = ""
|
|
|
|
try:
|
|
|
|
with open(path, "r") as fd:
|
|
|
|
value = fd.read().rstrip('\n')
|
|
|
|
except EnvironmentError:
|
|
|
|
pass
|
|
|
|
return value
|
|
|
|
|
|
|
|
def __set__(self, obj, value):
|
|
|
|
path = self._get_path(obj)
|
|
|
|
with open(path, "w") as fd:
|
|
|
|
fd.write(str(value))
|
|
|
|
|
|
|
|
def __delete__(self, obj):
|
|
|
|
raise AttributeError("can't delete attribute")
|