Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWI-4279 Fix BXML Library Anti Pattern #206

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bandwidth/models/bxml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .bxml import Bxml
from .nestable_verb import NestableVerb
from .response import Response
from .root import Root
from .terminal_verb import TerminalVerb
from .verb import Verb
from .verbs import *
71 changes: 71 additions & 0 deletions bandwidth/models/bxml/nestable_verb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
nestable_verb.py

Defines the base nestable_verb class for all BXML verbs that can have nested verbs

@copyright Bandwidth INC
"""
from __future__ import annotations
import re
from typing import Union
import xml.etree.ElementTree as ET
from .verb import Verb
ckoegel marked this conversation as resolved.
Show resolved Hide resolved


class NestableVerb(Verb):
"""Base class for nestable BXML verbs
"""
ssml_regex = r"<([a-zA-Z//].*?)>"

def __init__(self, tag: str, content: str = None, nested_verbs: list[Verb] = None):
"""Initialize the verb model

Args:
tag (str): Name of the XML element
content (str, optional): XML element content. Defaults to None.
nested_verbs (list[Verb], optional): XML element children. Defaults to None.
"""
self._tag = tag
self._content = content
self._nested_verbs = nested_verbs
if not self._nested_verbs:
self._nested_verbs = []

def _to_etree_element(self) -> ET.Element:
"""Generate an ET.Element object from a NestableVerb Object

Returns:
ET.Element: ET.Element representation of NestableVerb
"""
root = ET.Element(self._tag)
if self._content:
root.text = self._content
self._set_attributes(root)
if self._nested_verbs:
for verb in self._nested_verbs:
root.append(verb._to_etree_element())
return root

def _generate_xml(self) -> ET.ElementTree:
"""Generates an XML dom

Returns:
ET.Element: The XML dom for the verb and its nested verbs
"""
root = ET.Element(self._tag)
if self._content:
root.text = self._content
self._set_attributes(root)
if self._nested_verbs:
for verb in self._nested_verbs:
root.append(verb._to_etree_element())
dom = ET.ElementTree(root)
return dom

def add_verb(self, verb) -> None:
"""Add a verb to the object's nested_verbs array

Args:
verb (Verb): BXML verb to nest within the parent. Becomes a child xml element.
"""
self._nested_verbs.append(verb)
34 changes: 0 additions & 34 deletions bandwidth/models/bxml/terminal_verb.py

This file was deleted.

19 changes: 1 addition & 18 deletions bandwidth/models/bxml/verb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Verb:
"""Base class for BXML verbs
"""

def __init__(self, tag: str, content: str = None, nested_verbs: list[Verb] = None):
def __init__(self, tag: str, content: str = None):
"""Initialize the verb model

Args:
Expand All @@ -25,9 +25,6 @@ def __init__(self, tag: str, content: str = None, nested_verbs: list[Verb] = Non
"""
self._tag = tag
self._content = content
self._nested_verbs = nested_verbs
if not self._nested_verbs:
self._nested_verbs = []

@property
def _attributes(self) -> Union[None, dict]:
Expand Down Expand Up @@ -73,9 +70,6 @@ def _to_etree_element(self) -> ET.Element:
if self._content:
root.text = self._content
self._set_attributes(root)
if self._nested_verbs:
for verb in self._nested_verbs:
root.append(verb._to_etree_element())
return root

def _generate_xml(self) -> ET.ElementTree:
Expand All @@ -88,20 +82,9 @@ def _generate_xml(self) -> ET.ElementTree:
if self._content:
root.text = self._content
self._set_attributes(root)
if self._nested_verbs:
for verb in self._nested_verbs:
root.append(verb._to_etree_element())
dom = ET.ElementTree(root)
return dom

def add_verb(self, verb) -> None:
"""Add a verb to the object's nested_verbs array

Args:
verb (BxmlVerb): BXML verb to nest within the parent. Becomes a child xml element.
"""
self._nested_verbs.append(verb)

def to_bxml(self) -> str:
"""Return the serialized BXML string

Expand Down
7 changes: 4 additions & 3 deletions bandwidth/models/bxml/verbs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from .bridge import Bridge
from .conference import Conference
from .custom_param import CustomParam
from .hangup import Hangup
from .forward import Forward
from .gather import Gather
from .pause import Pause
from .hangup import Hangup
from .pause_recording import PauseRecording
from .pause import Pause
from .phone_number import PhoneNumber
from .play_audio import PlayAudio
from .record import Record
Expand All @@ -19,8 +20,8 @@
from .start_stream import StartStream
from .start_transcription import StartTranscription
from .stop_gather import StopGather
from .stop_stream import StopStream
from .stop_recording import StopRecording
from .stop_stream import StopStream
from .stop_transcription import StopTranscription
from .stream_param import StreamParam
from .tag import Tag
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class Bridge(TerminalVerb):
class Bridge(Verb):

def __init__(
self, target_call: str, bridge_complete_url: str=None,
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/conference.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class Conference(TerminalVerb):
class Conference(Verb):

def __init__(
self, name: str, mute: str=None,
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/custom_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class CustomParam(TerminalVerb):
class CustomParam(Verb):
def __init__(self, name: str = None, value: str = None):
"""
Initialize a <CustomParam> verb
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class Forward(TerminalVerb):
class Forward(Verb):

def __init__(
self, to: str=None, _from: str=None,
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
@copyright Bandwidth INC
"""
from typing import Union, List
from ..verb import Verb
from ..nestable_verb import NestableVerb
from .play_audio import PlayAudio
from .speak_sentence import SpeakSentence


class Gather(Verb):
class Gather(NestableVerb):

def __init__(
self, audio_verbs: List[Union[PlayAudio, SpeakSentence]] = [],
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/hangup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class Hangup(TerminalVerb):
class Hangup(Verb):

def __init__(self):
"""Initialize a <Hangup> verb
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/pause.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class Pause(TerminalVerb):
class Pause(Verb):
def __init__(self, duration:int=1):
"""Initialize a <Pause> verb
Args:
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/pause_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class PauseRecording(TerminalVerb):
class PauseRecording(Verb):

def __init__(self):
"""Initialize a <PauseRecording> verb
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/phone_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class PhoneNumber(TerminalVerb):
class PhoneNumber(Verb):

def __init__(
self, number: str, transfer_answer_url: str=None, transfer_answer_method: str=None,
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/play_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class PlayAudio(TerminalVerb):
class PlayAudio(Verb):

def __init__(
self, audio_uri: str,
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class Record(TerminalVerb):
class Record(Verb):

def __init__(
self, record_complete_url: str=None,
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class Redirect(TerminalVerb):
class Redirect(Verb):

def __init__(
self, redirect_url: str, redirect_method: str = None,
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/resume_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class ResumeRecording(TerminalVerb):
class ResumeRecording(Verb):

def __init__(
self
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class Ring(TerminalVerb):
class Ring(Verb):

def __init__(
self, duration: int=None,
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/send_dtmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class SendDtmf(TerminalVerb):
class SendDtmf(Verb):

def __init__(
self, digits: str,
Expand Down
4 changes: 2 additions & 2 deletions bandwidth/models/bxml/verbs/sip_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

@copyright Bandwidth INC
"""
from ..terminal_verb import TerminalVerb
from ..verb import Verb


class SipUri(TerminalVerb):
class SipUri(Verb):

def __init__(
self, uri: str, uui: str=None, transfer_answer_url: str=None, transfer_answer_method: str=None,
Expand Down
Loading