# BAREOS - Backup Archiving REcovery Open Sourced## Copyright (C) 2015-2024 Bareos GmbH & Co. KG## This program is Free Software; you can redistribute it and/or# modify it under the terms of version three of the GNU Affero General Public# License as published by the Free Software Foundation and included# in the file LICENSE.## This program 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# Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA# 02110-1301, USA."""Handle file paths."""
[docs]classPath(object):""" Class to handle file paths. """def__init__(self,path=None):"""\ Args: path (str, optional): string representation of the file system path. """self.__set_defaults()self.set_path(path)def__str__(self):result=""ifself.is_root():result+="/"result+="/".join(self.path)if(notself.is_root())orself.len()>0:ifself.is_directory():result+="/"returnresultdef__set_defaults(self):self.path_orig=Noneself.root=Falseself.directory=Falseself.path=Nonedefset_path(self,path):ifpathisNone:self.__set_defaults()elifisinstance(path,str):self.path_orig=pathcomponents=self.path_orig.split("/")self.path=[iforiincomponentsifi!=""]ifpath=="":self.root=Falseself.directory=Trueelse:self.root=Falseifself.path_orig[0]=="/":self.root=Trueself.directory=Falseifcomponents[-1]=="":self.directory=Trueelse:# exceptionpassdefget(self,index=None):ifindexisNone:returnself.pathreturnself.path[index]
[docs]defshift(self):""" Removes the first component of the path. Example: .. code:: python >>> path = Path("/usr/bin/python") >>> path.shift() 'usr' >>> print(path) /bin/python Returns: str: First component of the path. Raises: IndexError: if path can't be shifted (path is empty). """result=self.get(0)self.remove(0)returnresult