Skip to content

Commit

Permalink
Chore/abstract choices method (#355)
Browse files Browse the repository at this point in the history
* abstracting choices() method into base class

* refactored 'email' typo

* created Choices class for choices() method inheritance

* abstract choices method from all classes, not just file events

* moved Choices class to sdk.queries.__init__.py

* moved Choices into choices.py

* modified Choices docstr to read 'values' not 'names'

* rename get_attribute_keys to get_attribute_values
  • Loading branch information
tora-kozic committed Aug 11, 2021
1 parent 3e28450 commit 96a186a
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 150 deletions.
14 changes: 14 additions & 0 deletions src/py42/choices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from py42.util import get_attribute_values_from_class


class Choices:
"""Helper class to provide the choices() method to a class."""

@classmethod
def choices(cls):
"""Returns attribute values for the given class.
Returns:
(list): A list containing the attribute values of the given class.
"""
return get_attribute_values_from_class(cls)
8 changes: 2 additions & 6 deletions src/py42/clients/cases.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
from datetime import datetime

from py42.util import get_attribute_keys_from_class
from py42.choices import Choices
from py42.util import parse_timestamp_to_milliseconds_precision


class CaseStatus:
class CaseStatus(Choices):
OPEN = "OPEN"
CLOSED = "CLOSED"

@staticmethod
def choices():
return get_attribute_keys_from_class(CaseStatus)


class CasesClient:
"""A client to expose cases API.
Expand Down
8 changes: 2 additions & 6 deletions src/py42/clients/detectionlists.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from py42.choices import Choices
from py42.exceptions import Py42BadRequestError
from py42.exceptions import Py42UnableToCreateProfileError
from py42.util import get_attribute_keys_from_class


class RiskTags:
class RiskTags(Choices):
"""The available RiskTags for :meth:`~py42.clients.detectionlists.DetectionListsClient.add_user_risk_tags()`
and :meth:`~py42.clients.detectionlists.DetectionListsClient.remove_user_risk_tags()`"""

Expand All @@ -15,10 +15,6 @@ class RiskTags:
POOR_SECURITY_PRACTICES = "POOR_SECURITY_PRACTICES"
CONTRACT_EMPLOYEE = "CONTRACT_EMPLOYEE"

@staticmethod
def choices():
return get_attribute_keys_from_class(RiskTags)


class DetectionListsClient:
"""`Rest documentation <https://developer.code42.com/api/#tag/Detection-Lists>`__"""
Expand Down
8 changes: 2 additions & 6 deletions src/py42/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from py42.util import get_attribute_keys_from_class
from py42.choices import Choices


class SortDirection:
class SortDirection(Choices):
"""Code42 request `sort_direction` constants for sorting returned lists in responses."""

DESC = "DESC"
ASC = "ASC"

@staticmethod
def choices():
return get_attribute_keys_from_class(SortDirection)
26 changes: 5 additions & 21 deletions src/py42/sdk/queries/alerts/filters/alert_filter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from py42.choices import Choices
from py42.sdk.queries.query_filter import create_filter_group
from py42.sdk.queries.query_filter import create_query_filter
from py42.sdk.queries.query_filter import QueryFilterStringField
from py42.sdk.queries.query_filter import QueryFilterTimestampField
from py42.util import get_attribute_keys_from_class
from py42.util import MICROSECOND_FORMAT
from py42.util import parse_timestamp_to_microseconds_precision

Expand Down Expand Up @@ -113,7 +113,7 @@ class RuleId(QueryFilterStringField):
_term = "ruleId"


class RuleSource(QueryFilterStringField):
class RuleSource(QueryFilterStringField, Choices):
"""Class that filters alerts based on rule source.
Available options are:
Expand All @@ -128,12 +128,8 @@ class RuleSource(QueryFilterStringField):
DEPARTING_EMPLOYEE = "Departing Employee"
HIGH_RISK_EMPLOYEE = "High Risk Employee"

@staticmethod
def choices():
return get_attribute_keys_from_class(RuleSource)


class RuleType(QueryFilterStringField):
class RuleType(QueryFilterStringField, Choices):
"""Class that filters alerts based on rule type.
Available options are:
Expand All @@ -148,18 +144,14 @@ class RuleType(QueryFilterStringField):
CLOUD_SHARE_PERMISSIONS = "FedCloudSharePermissions"
FILE_TYPE_MISMATCH = "FedFileTypeMismatch"

@staticmethod
def choices():
return get_attribute_keys_from_class(RuleType)


class Description(AlertQueryFilterStringField):
"""Class that filters alerts based on rule description text."""

_term = "description"


class Severity(QueryFilterStringField):
class Severity(QueryFilterStringField, Choices):
"""Class that filters alerts based on severity.
Available options are:
Expand All @@ -174,12 +166,8 @@ class Severity(QueryFilterStringField):
MEDIUM = "MEDIUM"
LOW = "LOW"

@staticmethod
def choices():
return get_attribute_keys_from_class(Severity)


class AlertState(QueryFilterStringField):
class AlertState(QueryFilterStringField, Choices):
"""Class that filters alerts based on alert state.
Available options are:
Expand All @@ -195,7 +183,3 @@ class AlertState(QueryFilterStringField):
DISMISSED = "RESOLVED"
PENDING = "PENDING"
IN_PROGRESS = "IN_PROGRESS"

@staticmethod
def choices():
return get_attribute_keys_from_class(AlertState)
8 changes: 2 additions & 6 deletions src/py42/sdk/queries/fileevents/filters/cloud_filter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from py42.choices import Choices
from py42.sdk.queries.fileevents.file_event_query import FileEventFilterStringField
from py42.sdk.queries.query_filter import QueryFilterBooleanField
from py42.util import get_attribute_keys_from_class


class Actor(FileEventFilterStringField):
Expand Down Expand Up @@ -35,7 +35,7 @@ class SharedWith(FileEventFilterStringField):
_term = "sharedWith"


class SharingTypeAdded(FileEventFilterStringField):
class SharingTypeAdded(FileEventFilterStringField, Choices):
"""Class that filters results to include events where a file's sharing permissions were
changed to a value that increases exposure (applies to cloud data source events only).
Expand All @@ -50,7 +50,3 @@ class SharingTypeAdded(FileEventFilterStringField):
SHARED_VIA_LINK = "SharedViaLink"
IS_PUBLIC = "IsPublic"
OUTSIDE_TRUSTED_DOMAIN = "OutsideTrustedDomains"

@staticmethod
def choices():
return get_attribute_keys_from_class(SharingTypeAdded)
20 changes: 4 additions & 16 deletions src/py42/sdk/queries/fileevents/filters/event_filter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from py42.choices import Choices
from py42.sdk.queries.fileevents.file_event_query import FileEventFilterStringField
from py42.sdk.queries.fileevents.file_event_query import FileEventFilterTimestampField
from py42.sdk.queries.query_filter import QueryFilterBooleanField
from py42.util import get_attribute_keys_from_class


class EventTimestamp(FileEventFilterTimestampField):
class EventTimestamp(FileEventFilterTimestampField, Choices):
"""Class that filters events based on the timestamp of the event that occurred.
Available event timestamp constants are provided as class attributes, These
Expand Down Expand Up @@ -36,12 +36,8 @@ class EventTimestamp(FileEventFilterTimestampField):
FOURTEEN_DAYS = "P14D"
THIRTY_DAYS = "P30D"

@staticmethod
def choices():
return get_attribute_keys_from_class(EventTimestamp)


class EventType(FileEventFilterStringField):
class EventType(FileEventFilterStringField, Choices):
"""Class that filters file events based on event type.
Available event types are provided as class attributes:
Expand All @@ -68,10 +64,6 @@ class EventType(FileEventFilterStringField):
EMAILED = "EMAILED"
PRINTED = "PRINTED"

@staticmethod
def choices():
return get_attribute_keys_from_class(EventType)


class InsertionTimestamp(FileEventFilterTimestampField):
"""Class that filters events based on the timestamp of when the event was actually added to the
Expand All @@ -84,7 +76,7 @@ class InsertionTimestamp(FileEventFilterTimestampField):
_term = "insertionTimestamp"


class Source(FileEventFilterStringField):
class Source(FileEventFilterStringField, Choices):
"""Class that filters events by event source.
Available source types are provided as class attributes:
Expand All @@ -110,10 +102,6 @@ class Source(FileEventFilterStringField):
GMAIL = "Gmail"
OFFICE_365 = "Office365"

@staticmethod
def choices():
return get_attribute_keys_from_class(Source)


class MimeTypeMismatch(QueryFilterBooleanField):
"""Class that filters events by whether or not a file's mime type matches its extension type."""
Expand Down
14 changes: 3 additions & 11 deletions src/py42/sdk/queries/fileevents/filters/exposure_filter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from py42.choices import Choices
from py42.sdk.queries.fileevents.file_event_query import FileEventFilterStringField
from py42.util import get_attribute_keys_from_class


class ExposureType(FileEventFilterStringField):
class ExposureType(FileEventFilterStringField, Choices):
"""Class that filters events based on exposure type.
Available options are provided as class attributes:
Expand All @@ -24,10 +24,6 @@ class ExposureType(FileEventFilterStringField):
IS_PUBLIC = "IsPublic"
OUTSIDE_TRUSTED_DOMAINS = "OutsideTrustedDomains"

@staticmethod
def choices():
return get_attribute_keys_from_class(ExposureType)


class ProcessName(FileEventFilterStringField):
"""Class that filters events based on the process name involved in the exposure (applies to
Expand Down Expand Up @@ -95,7 +91,7 @@ class RemovableMediaSerialNumber(FileEventFilterStringField):
_term = "removableMediaSerialNumber"


class SyncDestination(FileEventFilterStringField):
class SyncDestination(FileEventFilterStringField, Choices):
"""Class that filters events based on the name of the cloud service the file is synced with
(applies to ``synced to cloud service`` events only).
Expand All @@ -119,10 +115,6 @@ class SyncDestination(FileEventFilterStringField):
DROPBOX = "Dropbox"
ONEDRIVE = "OneDrive"

@staticmethod
def choices():
return get_attribute_keys_from_class(SyncDestination)


class SyncDestinationUsername(FileEventFilterStringField):
"""Class that filters events based on the username associated with the cloud service
Expand Down
8 changes: 2 additions & 6 deletions src/py42/sdk/queries/fileevents/filters/file_filter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from py42.choices import Choices
from py42.sdk.queries.fileevents.file_event_query import FileEventFilterComparableField
from py42.sdk.queries.fileevents.file_event_query import FileEventFilterStringField
from py42.util import get_attribute_keys_from_class


class FileCategory(FileEventFilterStringField):
class FileCategory(FileEventFilterStringField, Choices):
"""Class that filters events by category of the file observed.
Available file categories are provided as class attributes:
Expand Down Expand Up @@ -37,10 +37,6 @@ class FileCategory(FileEventFilterStringField):
VIRTUAL_DISK_IMAGE = "VirtualDiskImage"
ZIP = "Archive"

@staticmethod
def choices():
return get_attribute_keys_from_class(FileCategory)


class FileName(FileEventFilterStringField):
"""Class that filters events by the name of the file observed."""
Expand Down

0 comments on commit 96a186a

Please sign in to comment.