<commit_before>"""High-level Object Oriented LVM API"""

import os
import signal
from holland.lib.lvm.raw import pvs, vgs, lvs, lvsnapshot, lvremove, \
                                mount, umount, blkid
from holland.lib.lvm.util import getdevice, SignalManager
from holland.lib.lvm.errors import LVMCommandError

class Volume(object):
    """Abstract Volume object for LVM Volume implementations

    This class should not directly be instantiated, but rather one
    of its subclasses such as PhysicalVolume, VolumeGroup or LogicalVolume
    """

    def __new__(cls, *args, **kwargs):
        if cls is Volume:
            raise NotImplementedError('Volume is an abstract base class and '
                                      'should not be directly instantiated')
        return super(Volume, cls).__new__(cls, *args, **kwargs)

    def __init__(self, attributes=()):
        self.attributes = dict(attributes)


    def __getattr__(self, key):
        try:
            return self.attributes[key]
        except KeyError:
            return super(Volume, self).__getattribute__(key)
 
    def reload(self):
        """Reload a Volume with underlying data, which may have changed"""

        raise NotImplementedError()

    def lookup(cls, pathspec):
        """Lookup a volume for the pathspec given

        This will always return the first volume and raise an error
        if multiple volumes are found.

        :returns: Volume instance
        """
        raise NotImplementedError()
    lookup = classmethod(lookup)

    def search(cls, pathspec=None):
        """Search for volumes for the pathspec given

        This will search for any volumes matching the pathspec and return
        an iterable to walk across the volumes found.

        :returns: iterable of Volume instances
        """
        raise NotImplementedError()
    search = classmethod(search)

    def __repr__(self):
        return '%s()' % (self.__class__.__name__,) 

class PhysicalVolume(Volume):
    """LVM Physical Volume representation"""

    def reload(self):
        """Reload this PhysicalVolume"""
        self.attributes, = pvs(self.pv_name)

    def lookup(cls, pathspec):
        """Lookup a physical volume for the pathspec given

        This will always return the first volume found and raise an error
        if multiple volumes match ``pathspec``

        :returns: PhysicalVolume instance
        """
        try:
            volume, = pvs(pathspec)
            return cls(volume)
        except ValueError:
            #XX: Perhaps we should be more specific :)
            raise LookupError("No PhysicalVolume could be found for "
                              "pathspec %r" %
                              pathspec)
    lookup = classmethod(lookup)

    def search(cls, pathspec=None):
        """Search for volumes matching ``pathspec``

        This will search for any physical volumes matching ``pathspec`` and
        return an iterable that provides instance of PhysicalVolume

        :returns: iterable of PhysicalVolume instances
        """

        for volume in pvs(pathspec):
            yield volume
    search = classmethod(search)

    def __repr__(self):
        return "%s(device=%r)" % (self.__class__.__name__, self.pv_name)

class VolumeGroup(Volume):
    """LVM VolumeGroup representation"""

    def reload(self):
        """Reload this VolumeGroup"""
        self.attributes, = vgs(self.vg_name)
<commit_msg>PhysicalVolume.search now yields PhysicalVolumes rather than dicts<commit_after>"""High-level Object Oriented LVM API"""

import os
import signal
from holland.lib.lvm.raw import pvs, vgs, lvs, lvsnapshot, lvremove, \
                                mount, umount, blkid
from holland.lib.lvm.util import getdevice, SignalManager
from holland.lib.lvm.errors import LVMCommandError

class Volume(object):
    """Abstract Volume object for LVM Volume implementations

    This class should not directly be instantiated, but rather one
    of its subclasses such as PhysicalVolume, VolumeGroup or LogicalVolume
    """

    def __new__(cls, *args, **kwargs):
        if cls is Volume:
            raise NotImplementedError('Volume is an abstract base class and '
                                      'should not be directly instantiated')
        return super(Volume, cls).__new__(cls, *args, **kwargs)

    def __init__(self, attributes=()):
        self.attributes = dict(attributes)


    def __getattr__(self, key):
        try:
            return self.attributes[key]
        except KeyError:
            return super(Volume, self).__getattribute__(key)
 
    def reload(self):
        """Reload a Volume with underlying data, which may have changed"""

        raise NotImplementedError()

    def lookup(cls, pathspec):
        """Lookup a volume for the pathspec given

        This will always return the first volume and raise an error
        if multiple volumes are found.

        :returns: Volume instance
        """
        raise NotImplementedError()
    lookup = classmethod(lookup)

    def search(cls, pathspec=None):
        """Search for volumes for the pathspec given

        This will search for any volumes matching the pathspec and return
        an iterable to walk across the volumes found.

        :returns: iterable of Volume instances
        """
        raise NotImplementedError()
    search = classmethod(search)

    def __repr__(self):
        return '%s()' % (self.__class__.__name__,) 

class PhysicalVolume(Volume):
    """LVM Physical Volume representation"""

    def reload(self):
        """Reload this PhysicalVolume"""
        self.attributes, = pvs(self.pv_name)

    def lookup(cls, pathspec):
        """Lookup a physical volume for the pathspec given

        This will always return the first volume found and raise an error
        if multiple volumes match ``pathspec``

        :returns: PhysicalVolume instance
        """
        try:
            volume, = pvs(pathspec)
            return cls(volume)
        except ValueError:
            #XX: Perhaps we should be more specific :)
            raise LookupError("No PhysicalVolume could be found for "
                              "pathspec %r" %
                              pathspec)
    lookup = classmethod(lookup)

    def search(cls, pathspec=None):
        """Search for volumes matching ``pathspec``

        This will search for any physical volumes matching ``pathspec`` and
        return an iterable that provides instance of PhysicalVolume

        :returns: iterable of PhysicalVolume instances
        """

        for volume in pvs(pathspec):
            yield cls(volume)
    search = classmethod(search)

    def __repr__(self):
        return "%s(device=%r)" % (self.__class__.__name__, self.pv_name)

class VolumeGroup(Volume):
    """LVM VolumeGroup representation"""

    def reload(self):
        """Reload this VolumeGroup"""
        self.attributes, = vgs(self.vg_name)

{'fraction_non_alphanumeric': 0.08249352145119493, 'fraction_numerical': 0.0, 'mean_word_length': 2.425542406311637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3426536', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1724, 'n_words': 638}