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):
|
2021-03-30 05:56:38 +00:00
|
|
|
return 'Absolute path required, got: {}'.format(self.path)
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
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-13 16:01:46 +00:00
|
|
|
try:
|
|
|
|
os.unlink(self.path)
|
|
|
|
except EnvironmentError:
|
|
|
|
# ignored
|
|
|
|
pass
|
2011-10-10 12:54:26 +00:00
|
|
|
for i in initial:
|
|
|
|
self.append(i)
|
|
|
|
|
|
|
|
def __read(self):
|
|
|
|
lines = []
|
2011-10-13 15:59:23 +00:00
|
|
|
# if file does not exist return empty list
|
2011-10-10 12:54:26 +00:00
|
|
|
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'))
|
2018-10-12 17:08:36 +00:00
|
|
|
except EnvironmentError:
|
2011-10-10 12:54:26 +00:00
|
|
|
# error ignored
|
|
|
|
pass
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def __write(self, lines):
|
2011-10-18 11:27:44 +00:00
|
|
|
try:
|
|
|
|
with open(self.path, 'w') as fd:
|
|
|
|
for line in lines:
|
|
|
|
fd.write(str(line) + '\n')
|
|
|
|
except EnvironmentError as e:
|
|
|
|
# should never happen
|
|
|
|
raise cdist.Error(str(e))
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(list(self))
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
return self.__read()[index]
|
2011-10-18 11:29:44 +00:00
|
|
|
|
2011-10-10 12:54:26 +00:00
|
|
|
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 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
|
2011-10-18 11:27:44 +00:00
|
|
|
try:
|
|
|
|
# create directory if it doesn't exist
|
|
|
|
if not os.path.isdir(self.path):
|
|
|
|
os.mkdir(self.path)
|
|
|
|
except EnvironmentError as e:
|
|
|
|
raise cdist.Error(str(e))
|
2011-10-10 14:37:56 +00:00
|
|
|
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:
|
2018-10-12 17:08:36 +00:00
|
|
|
value = fd.read().splitlines()
|
|
|
|
# if there is no value/empty line then return ''
|
|
|
|
# if there is only one value then return that value
|
|
|
|
# if there are multiple lines in file then return list
|
|
|
|
if not value:
|
|
|
|
return ''
|
|
|
|
elif len(value) == 1:
|
|
|
|
return value[0]
|
|
|
|
else:
|
|
|
|
return value
|
2011-10-10 12:54:26 +00:00
|
|
|
except EnvironmentError:
|
|
|
|
raise KeyError(key)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2011-10-18 11:27:44 +00:00
|
|
|
try:
|
|
|
|
with open(os.path.join(self.path, key), "w") as fd:
|
2013-12-21 21:00:57 +00:00
|
|
|
if (not hasattr(value, 'strip') and
|
2013-12-21 21:49:06 +00:00
|
|
|
(hasattr(value, '__getitem__') or
|
2016-07-05 18:44:24 +00:00
|
|
|
hasattr(value, '__iter__'))):
|
2013-12-21 21:00:57 +00:00
|
|
|
# if it looks like a sequence and quacks like a sequence,
|
|
|
|
# it is a sequence
|
2012-06-04 20:01:32 +00:00
|
|
|
for v in value:
|
|
|
|
fd.write(str(v) + '\n')
|
|
|
|
else:
|
|
|
|
fd.write(str(value))
|
2014-04-22 09:04:02 +00:00
|
|
|
# ensure file ends with a single newline
|
|
|
|
if value and value[-1] != '\n':
|
|
|
|
fd.write('\n')
|
2011-10-18 11:27:44 +00:00
|
|
|
except EnvironmentError as e:
|
|
|
|
raise cdist.Error(str(e))
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
def __delitem__(self, key):
|
2011-10-13 23:15:08 +00:00
|
|
|
try:
|
|
|
|
os.remove(os.path.join(self.path, key))
|
|
|
|
except EnvironmentError:
|
|
|
|
raise KeyError(key)
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
2011-10-18 11:27:44 +00:00
|
|
|
try:
|
|
|
|
return iter(os.listdir(self.path))
|
|
|
|
except EnvironmentError as e:
|
|
|
|
raise cdist.Error(str(e))
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
2011-10-18 11:27:44 +00:00
|
|
|
try:
|
|
|
|
return len(os.listdir(self.path))
|
|
|
|
except EnvironmentError as e:
|
|
|
|
raise cdist.Error(str(e))
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
|
2020-06-20 19:16:23 +00:00
|
|
|
class FileBasedProperty:
|
2011-10-13 23:15:08 +00:00
|
|
|
attribute_class = None
|
2011-10-10 14:37:56 +00:00
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
"""
|
|
|
|
:param path: string or callable
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
Abstract super class. Subclass and set the class member
|
|
|
|
attribute_class accordingly.
|
2011-10-13 23:15:08 +00:00
|
|
|
|
|
|
|
Usage with a sublcass:
|
2011-10-10 14:37:56 +00:00
|
|
|
|
2020-06-20 19:16:23 +00:00
|
|
|
class Foo:
|
2016-07-05 18:44:24 +00:00
|
|
|
# note that the actual DirectoryDict is stored as __parameters
|
|
|
|
# on the instance
|
|
|
|
parameters = DirectoryDictProperty(
|
|
|
|
lambda instance: os.path.join(instance.absolute_path,
|
|
|
|
'parameter'))
|
|
|
|
# note that the actual DirectoryDict is stored as __other_dict
|
|
|
|
# on the instance
|
2011-10-13 23:15:08 +00:00
|
|
|
other_dict = DirectoryDictProperty('/tmp/other_dict')
|
2011-10-10 14:37:56 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.absolute_path = '/tmp/foo'
|
|
|
|
|
|
|
|
"""
|
2011-10-13 23:15:08 +00:00
|
|
|
self.path = path
|
2011-10-10 14:37:56 +00:00
|
|
|
|
2011-10-13 23:15:08 +00:00
|
|
|
def _get_path(self, instance):
|
|
|
|
path = self.path
|
|
|
|
if callable(path):
|
|
|
|
path = path(instance)
|
|
|
|
return path
|
|
|
|
|
|
|
|
def _get_property_name(self, owner):
|
|
|
|
for name, prop in owner.__dict__.items():
|
|
|
|
if self == prop:
|
|
|
|
return name
|
|
|
|
|
|
|
|
def _get_attribute(self, instance, owner):
|
|
|
|
name = self._get_property_name(owner)
|
2021-03-30 05:56:38 +00:00
|
|
|
attribute_name = '__{}'.format(name)
|
2011-10-13 23:15:08 +00:00
|
|
|
if not hasattr(instance, attribute_name):
|
|
|
|
path = self._get_path(instance)
|
|
|
|
attribute_instance = self.attribute_class(path)
|
|
|
|
setattr(instance, attribute_name, attribute_instance)
|
|
|
|
return getattr(instance, attribute_name)
|
|
|
|
|
|
|
|
def __get__(self, instance, owner):
|
|
|
|
if instance is None:
|
|
|
|
return self
|
|
|
|
return self._get_attribute(instance, owner)
|
|
|
|
|
|
|
|
def __delete__(self, instance):
|
2011-10-10 12:54:26 +00:00
|
|
|
raise AttributeError("can't delete attribute")
|
|
|
|
|
|
|
|
|
2011-10-13 23:15:08 +00:00
|
|
|
class DirectoryDictProperty(FileBasedProperty):
|
|
|
|
attribute_class = DirectoryDict
|
2011-10-10 12:54:26 +00:00
|
|
|
|
2011-10-13 23:15:08 +00:00
|
|
|
def __set__(self, instance, value):
|
|
|
|
attribute_instance = self._get_attribute(instance, instance.__class__)
|
|
|
|
for name in attribute_instance.keys():
|
|
|
|
del attribute_instance[name]
|
|
|
|
attribute_instance.update(value)
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
|
2011-10-13 23:15:08 +00:00
|
|
|
class FileListProperty(FileBasedProperty):
|
|
|
|
attribute_class = FileList
|
2011-10-10 12:54:26 +00:00
|
|
|
|
2011-10-13 23:15:08 +00:00
|
|
|
def __set__(self, instance, value):
|
|
|
|
path = self._get_path(instance)
|
|
|
|
try:
|
|
|
|
os.unlink(path)
|
|
|
|
except EnvironmentError:
|
|
|
|
# ignored
|
|
|
|
pass
|
|
|
|
attribute_instance = self._get_attribute(instance, instance.__class__)
|
|
|
|
for item in value:
|
|
|
|
attribute_instance.append(item)
|
2011-10-10 12:54:26 +00:00
|
|
|
|
|
|
|
|
2011-10-13 23:15:08 +00:00
|
|
|
class FileBooleanProperty(FileBasedProperty):
|
|
|
|
"""A boolean property which uses a file to represent its value.
|
|
|
|
|
|
|
|
File exists -> True
|
|
|
|
File does not exists -> False
|
|
|
|
"""
|
2011-10-10 12:54:26 +00:00
|
|
|
# Descriptor Protocol
|
2011-10-13 23:15:08 +00:00
|
|
|
def __get__(self, instance, owner):
|
|
|
|
if instance is None:
|
|
|
|
return self
|
|
|
|
path = self._get_path(instance)
|
2011-10-10 12:54:26 +00:00
|
|
|
return os.path.isfile(path)
|
|
|
|
|
2011-10-13 23:15:08 +00:00
|
|
|
def __set__(self, instance, value):
|
|
|
|
path = self._get_path(instance)
|
2011-10-10 12:54:26 +00:00
|
|
|
if value:
|
2011-10-18 11:27:44 +00:00
|
|
|
try:
|
|
|
|
open(path, "w").close()
|
|
|
|
except EnvironmentError as e:
|
|
|
|
raise cdist.Error(str(e))
|
2011-10-10 12:54:26 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
os.remove(path)
|
|
|
|
except EnvironmentError:
|
|
|
|
# ignore
|
|
|
|
pass
|
|
|
|
|
2011-10-11 07:38:03 +00:00
|
|
|
|
2011-10-13 23:15:08 +00:00
|
|
|
class FileStringProperty(FileBasedProperty):
|
2011-10-11 07:38:03 +00:00
|
|
|
"""A string property which stores its value in a file.
|
2011-10-11 07:02:01 +00:00
|
|
|
"""
|
|
|
|
# Descriptor Protocol
|
2011-10-13 23:15:08 +00:00
|
|
|
def __get__(self, instance, owner):
|
|
|
|
if instance is None:
|
|
|
|
return self
|
|
|
|
path = self._get_path(instance)
|
2011-10-11 07:02:01 +00:00
|
|
|
value = ""
|
|
|
|
try:
|
|
|
|
with open(path, "r") as fd:
|
2014-04-22 09:04:02 +00:00
|
|
|
value = fd.read().rstrip('\n')
|
2011-10-11 07:02:01 +00:00
|
|
|
except EnvironmentError:
|
|
|
|
pass
|
|
|
|
return value
|
|
|
|
|
2011-10-13 23:15:08 +00:00
|
|
|
def __set__(self, instance, value):
|
|
|
|
path = self._get_path(instance)
|
2011-10-11 07:38:03 +00:00
|
|
|
if value:
|
2011-10-18 11:27:44 +00:00
|
|
|
try:
|
|
|
|
with open(path, "w") as fd:
|
|
|
|
fd.write(str(value))
|
2014-04-22 09:04:02 +00:00
|
|
|
# ensure file ends with a single newline
|
|
|
|
if value[-1] != '\n':
|
|
|
|
fd.write('\n')
|
2011-10-18 11:27:44 +00:00
|
|
|
except EnvironmentError as e:
|
|
|
|
raise cdist.Error(str(e))
|
2011-10-11 07:38:03 +00:00
|
|
|
else:
|
|
|
|
try:
|
2011-10-13 23:15:08 +00:00
|
|
|
os.remove(path)
|
2011-10-11 07:38:03 +00:00
|
|
|
except EnvironmentError:
|
|
|
|
pass
|