Skip to content

Commit

Permalink
Improved Script | Added Exception | Removed Redundancy
Browse files Browse the repository at this point in the history
As per Jordan's comment review, I have improved the script to better suite the codebase
  • Loading branch information
KiPageFault committed Nov 8, 2024
1 parent efbaacd commit 94d771a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 76 deletions.
11 changes: 8 additions & 3 deletions odins_spear/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,14 @@ class OSInvalidPasswordType(OSError):
"""

def __str__(self) -> str:
return f"Invalid or unsupported password, please review supported passwords."

return f"Invalid or unsupported password, please review supported passwords."

class OSExtensionNotFound(OSError):
""" Raised when a searched extension is not found
"""

def __str__(self) -> str:
return f"Cannot locate extension. Please alter search criteria"

class OSServiceNotAssigned(OSError):
""" Raised a service needed is not assigned to a Broadworks entity.
Expand All @@ -123,7 +129,6 @@ class OSServiceNotAssigned(OSError):
def __str__(self) -> str:
return f"Service not assigend to target Broadworks entity. Please check services assigned."


class OSFileNotFound(OSError):
""" Raised when a file can not be found.
"""
Expand Down
12 changes: 10 additions & 2 deletions odins_spear/scripter.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,17 @@ def remove_numbers(self, service_provider_id: str, group_id: str, start_of_range
return scripts.remove_numbers.main(self.api, service_provider_id, group_id, start_of_range_number,
end_of_range_number)

def locate_free_extension(self, service_provider_id: str, group_id: str, field: list):
def locate_free_extension(self, service_provider_id: str, group_id: str, extension_range: list):
"""Locates the lowest value free extension given the provided range of extension numbers.
return scripts.locate_free_extension(self.api, service_provider_id, group_id, field)
Note: The extension range is passed as a list like such [100, 1000]
Args:
service_provider_id (str): Service Provider/ Enterprise ID where Group is located which hosts needed free extensions
group_id (str): Group ID where target extensions are located.
extension_range (list): List value representing the search range for free extensions
"""
return scripts.locate_free_extension.main(self.api, service_provider_id, group_id, extension_range)

def service_provider_trunking_capacity(self, service_provider_id: str):
"""Returns a JSON breakdown of the Trunking Call Capacity of a Service Provider/ Enterprise (SP/ENT).
Expand Down
95 changes: 24 additions & 71 deletions odins_spear/scripts/locate_free_extension.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from exceptions import OSExtensionNotFound

def retrieve_extensions(
api,
Expand All @@ -7,97 +8,49 @@ def retrieve_extensions(

extensions = []

dataset = api.get.users(
service_provider_id,
group_id
)

for data in dataset:
if not data['extension']:
continue
###

extensions.append(data['extension'])

dataset = api.get.group_hunt_groups(
service_provider_id,
group_id
dataset = (
api.get.users( service_provider_id, group_id ) +
api.get.group_hunt_groups( service_provider_id,group_id ) +
api.get.group_call_centers( service_provider_id, group_id ) +
api.get.auto_attendants( service_provider_id, group_id )
)

for data in dataset:
if not data['extension']:
continue
###

extensions.append(data['extension'])
extensions.append(int(data['extension']))

dataset = api.get.group_call_centers(
service_provider_id,
group_id
)

for data in dataset:
if not data['extension']:
continue
###

extensions.append(data['extension'])

dataset = api.get.auto_attendants(
service_provider_id,
group_id
)

for data in dataset:
if not data['extension']:
continue

extensions.append(data['extension'])
###

if not extensions:
return

return extensions
###
return extensions if extensions else None

def main(
api,
service_provider_id,
group_id,
field: list
service_provider_id: str,
group_id: str,
extension_range: list
):
'''Retrieves The Lowest Free Extension Available In The Designated Group Passed.
'''
Retrieves The Lowest Free Extension Available In The Designated Group Passed.

field is passed as such [100, 1000]
'''
### Retrieve List Of Occupied Extensions Within The Group
if extension_range[0] > extension_range[1]:
initial_range = extension_range[1]

extension_range[1] = extension_range[0]
extension_range[0] = initial_range

# Retrieve List Of Occupied Extensions Within The Group
extensions = retrieve_extensions(
api,
service_provider_id,
group_id,
)

### Cull Out Of Range Extensions
for extension in extensions:

if field[0] < extension and field[1] > extension:
continue

extensions.remove(extension)
###

### Locate Initial Free Extension
extension_set = set(extensions)

for extension in range(field[0] + 1, field[1]):
if extension not in extension_set:
return extension
####
for extension in range(extension_range[0], extension_range[1] + 1):
if extension not in extensions:
return {'extension': extension}

print("Unable To Locate A Free Extension Within The Range")
return
if not extension:
raise OSExtensionNotFound



Expand Down

0 comments on commit 94d771a

Please sign in to comment.