~ursinha/launchpad/add-all-fl-tests

« back to all changes in this revision

Viewing changes to lib/canonical/launchpad/database/hwdb.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2009-08-20 12:36:07 UTC
  • mfrom: (9094.2.2 hwdb-device-class-api)
  • Revision ID: launchpad@pqm.canonical.com-20090820123607-w7k7ahwkclxlonh8
[r=cprov][ui=none] allow arbitrary int values for
        HWDeviceClass.main_class and HWDeviceClass.sub_class;
        expose HWDeviceClass to the webservice

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
__all__ = [
9
9
    'HWDevice',
10
10
    'HWDeviceClass',
11
 
    'HWDeviceClassSet',
12
11
    'HWDeviceSet',
13
12
    'HWDeviceDriverLink',
14
13
    'HWDeviceDriverLinkSet',
57
56
from lp.registry.model.teammembership import TeamParticipation
58
57
from lp.soyuz.interfaces.distroarchseries import IDistroArchSeries
59
58
from canonical.launchpad.interfaces.hwdb import (
60
 
    HWBus, HWMainClass, HWSubClass, HWSubmissionFormat,
61
 
    HWSubmissionKeyNotUnique, HWSubmissionProcessingStatus, IHWDevice,
62
 
    IHWDeviceClass, IHWDeviceClassSet, IHWDeviceDriverLink,
63
 
    IHWDeviceDriverLinkSet, IHWDeviceNameVariant, IHWDeviceNameVariantSet,
64
 
    IHWDeviceSet, IHWDriver, IHWDriverName, IHWDriverPackageName,
65
 
    IHWDriverSet, IHWSubmission, IHWSubmissionBug, IHWSubmissionBugSet,
66
 
    IHWSubmissionDevice, IHWSubmissionDeviceSet, IHWSubmissionSet,
67
 
    IHWSystemFingerprint, IHWSystemFingerprintSet, IHWVendorID,
68
 
    IHWVendorIDSet, IHWVendorName, IHWVendorNameSet, IllegalQuery,
69
 
    ParameterError)
 
59
    HWBus, HWSubmissionFormat, HWSubmissionKeyNotUnique,
 
60
    HWSubmissionProcessingStatus, IHWDevice, IHWDeviceClass,
 
61
    IHWDeviceClassSet, IHWDeviceDriverLink, IHWDeviceDriverLinkSet,
 
62
    IHWDeviceNameVariant, IHWDeviceNameVariantSet, IHWDeviceSet, IHWDriver,
 
63
    IHWDriverName, IHWDriverPackageName, IHWDriverSet, IHWSubmission,
 
64
    IHWSubmissionBug, IHWSubmissionBugSet, IHWSubmissionDevice,
 
65
    IHWSubmissionDeviceSet, IHWSubmissionSet, IHWSystemFingerprint,
 
66
    IHWSystemFingerprintSet, IHWVendorID, IHWVendorIDSet, IHWVendorName,
 
67
    IHWVendorNameSet, IllegalQuery, ParameterError)
70
68
from canonical.launchpad.interfaces.launchpad import ILaunchpadCelebrities
71
69
from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet
72
70
from lp.registry.interfaces.distribution import IDistribution
769
767
        result_set.order_by((HWDriver.package_name, HWDriver.name))
770
768
        return result_set
771
769
 
 
770
    @property
 
771
    def classes(self):
 
772
        """See `IHWDevice.`"""
 
773
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
 
774
        result_set = store.find(
 
775
            HWDeviceClass,
 
776
            HWDeviceClass.device == self.id)
 
777
        result_set.order_by(HWDeviceClass.main_class, HWDeviceClass.sub_class)
 
778
        return result_set
 
779
 
 
780
    def getOrCreateDeviceClass(self, main_class, sub_class=None):
 
781
        """See `IHWDevice.`"""
 
782
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
 
783
        result_set = store.find(
 
784
            HWDeviceClass,
 
785
            HWDeviceClass.device == self.id,
 
786
            HWDeviceClass.main_class == main_class,
 
787
            HWDeviceClass.sub_class == sub_class)
 
788
        existing_record = result_set.one()
 
789
        if existing_record is not None:
 
790
            return existing_record
 
791
        return HWDeviceClass(
 
792
            device=self, main_class=main_class, sub_class=sub_class)
 
793
 
 
794
    def removeDeviceClass(self, main_class, sub_class=None):
 
795
        """See `IHWDevice.`"""
 
796
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
 
797
        result_set = store.find(
 
798
            HWDeviceClass,
 
799
            HWDeviceClass.device == self.id,
 
800
            HWDeviceClass.main_class == main_class,
 
801
            HWDeviceClass.sub_class == sub_class)
 
802
        existing_record = result_set.one()
 
803
        if existing_record is not None:
 
804
            store.remove(existing_record)
 
805
 
772
806
 
773
807
class HWDeviceSet:
774
808
    """See `IHWDeviceSet`."""
1023
1057
    implements(IHWDeviceClass)
1024
1058
 
1025
1059
    device = ForeignKey(dbName='device', foreignKey='HWDevice', notNull=True)
1026
 
    main_class = EnumCol(enum=HWMainClass, notNull=True)
1027
 
    sub_class = EnumCol(enum=HWSubClass)
1028
 
 
1029
 
    def _create(self, id, **kw):
1030
 
        """Create a HWDeviceClass record.
1031
 
 
1032
 
        Ensure that main_class and sub_class have consistent values.
1033
 
        """
1034
 
        main_class = kw.get('main_class')
1035
 
        if main_class is None:
1036
 
            raise TypeError('HWDeviceClass() did not get expected keyword '
1037
 
                            'argument main_class')
1038
 
        sub_class = kw.get('sub_class')
1039
 
        if sub_class is not None:
1040
 
            if not sub_class.name.startswith(main_class.name + '_'):
1041
 
                raise TypeError(
1042
 
                    'HWDeviceClass() did not get matching argument values '
1043
 
                    'for main_class: %r and sub_class: %r.'
1044
 
                    % (main_class, sub_class))
1045
 
        SQLBase._create(self, id, **kw)
 
1060
    main_class = IntCol(notNull=True)
 
1061
    sub_class = IntCol(notNull=False)
 
1062
 
 
1063
    def delete(self):
 
1064
        """See `IHWDeviceClass`."""
 
1065
        store = Store.of(self)
 
1066
        store.remove(self)
1046
1067
 
1047
1068
 
1048
1069
class HWDeviceClassSet:
1049
1070
    """See `IHWDeviceClassSet`."""
1050
1071
    implements(IHWDeviceClassSet)
1051
1072
 
1052
 
    def create(self, device, main_class, sub_class=None):
 
1073
    def get(self, id):
1053
1074
        """See `IHWDeviceClassSet`."""
1054
 
        return HWDeviceClass(device=device, main_class=main_class,
1055
 
                             sub_class=sub_class)
 
1075
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
 
1076
        return store.find(HWDeviceClass, HWDeviceClass.id == id).one()
1056
1077
 
1057
1078
 
1058
1079
class HWSubmissionDevice(SQLBase):