Skip to content

Commit

Permalink
Merge pull request #307 from robbrad/306_collectdata_class
Browse files Browse the repository at this point in the history
#306 collect data class
  • Loading branch information
OliverCullimore authored Jul 22, 2023
2 parents 99a5928 + de8c685 commit 85a6512
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 75 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "uk_bin_collection"
version = "0.5.0"
version = "0.6.0"
description = "Python Lib to collect UK Bin Data"
readme = "README.md"
authors = ["Robert Bradley <[email protected]>"]
Expand Down
4 changes: 3 additions & 1 deletion uk_bin_collection/tests/step_defs/test_validate_council.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def scrape_step(context, council):
args.append(f"-s")

try:
context.parse_result = collect_data.main(args)
CollectData = collect_data.UKBinCollectionApp()
CollectData.set_args(args)
context.parse_result = CollectData.run()
except Exception as err:
logging.error(traceback.format_exc())
logging.info(f"Schema: {err}")
Expand Down
140 changes: 67 additions & 73 deletions uk_bin_collection/uk_bin_collection/collect_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,86 @@
import os
import sys

from uk_bin_collection.uk_bin_collection.get_bin_data import \
AbstractGetBinDataClass

# We use this method to dynamically import the council processor
SRC_PATH = os.path.join("councils")
module_path = os.path.realpath(os.path.join(os.path.dirname(__file__), SRC_PATH))
sys.path.append(module_path)


def client_code(
get_bin_data_class: AbstractGetBinDataClass, address_url, **kwargs
) -> None:
"""
The client code calls the template method to execute the algorithm. Client
code does not have to know the concrete class of an object it works with,
as long as it works with objects through the interface of their base class.
"""

return get_bin_data_class.template_method(address_url, **kwargs)


def get_council_module(council_module_str):
return importlib.import_module(council_module_str)
class UKBinCollectionApp:
def __init__(self):
self.parser = argparse.ArgumentParser(description="")
self.parser.add_argument("module", type=str, help="Name of council module to use")
self.parser.add_argument(
"URL", type=str, help="URL to parse - should be wrapped in double quotes"
)
self.parser.add_argument(
"-p",
"--postcode",
type=str,
help="Postcode to parse - should include a space and be wrapped in "
"double-quotes",
required=False,
)
self.parser.add_argument(
"-n", "--number", type=str, help="House number to parse", required=False
)
self.parser.add_argument(
"-s",
"--skip_get_url",
action="store_true",
help="Skips the generic get_url - uses one in council class",
required=False,
)
self.parser.add_argument("-u", "--uprn", type=str, help="UPRN to parse", required=False)
self.parser.add_argument(
"-d",
"--dev_mode",
action="store_true",
help="Enables development mode - creates/updates outputs .json file for the council on each run",
required=False,
)
self.parsed_args = None

def set_args(self, args):
self.parsed_args = self.parser.parse_args(args)

def main(args):
parser = argparse.ArgumentParser(description="")
parser.add_argument("module", type=str, help="Name of council module to use")
parser.add_argument(
"URL", type=str, help="URL to parse - should be wrapped in double quotes"
)
parser.add_argument(
"-p",
"--postcode",
type=str,
help="Postcode to parse - should include a space and be wrapped in "
"double-quotes",
required=False,
)
parser.add_argument(
"-n", "--number", type=str, help="House number to parse", required=False
)
parser.add_argument(
"-s",
"--skip_get_url",
action="store_true",
help="Skips the generic get_url - uses one in council class",
required=False,
)
parser.add_argument("-u", "--uprn", type=str, help="UPRN to parse", required=False)
parser.add_argument("-us", "--usrn", type=str, help="USRN to parse", required=False)
parser.add_argument(
"-d",
"--dev_mode",
action="store_true",
help="Enables development mode - creates/updates outputs .json file for the council on each run",
required=False,
)
parsed_args = parser.parse_args(args)
def get_council_module(self, council_module_str):
return importlib.import_module(council_module_str)

council_module_str = parsed_args.module
address_url = parsed_args.URL
council_module = get_council_module(council_module_str)
postcode = parsed_args.postcode
paon = parsed_args.number
uprn = parsed_args.uprn
usrn = parsed_args.usrn
skip_get_url = parsed_args.skip_get_url
dev_mode = parsed_args.dev_mode
def client_code(self, get_bin_data_class, address_url, **kwargs) -> None:
"""
The client code calls the template method to execute the algorithm. Client
code does not have to know the concrete class of an object it works with,
as long as it works with objects through the interface of their base class.
"""
return get_bin_data_class.template_method(address_url, **kwargs)

return client_code(
council_module.CouncilClass(),
address_url,
postcode=postcode,
paon=paon,
uprn=uprn,
usrn=usrn,
skip_get_url=skip_get_url,
dev_mode=dev_mode,
council_module_str=council_module_str,
)
def run(self):
council_module_str = self.parsed_args.module
address_url = self.parsed_args.URL
council_module = self.get_council_module(council_module_str)
postcode = self.parsed_args.postcode
paon = self.parsed_args.number
uprn = self.parsed_args.uprn
skip_get_url = self.parsed_args.skip_get_url
dev_mode = self.parsed_args.dev_mode

# parse arguments using optparse or argparse or what have you
self.client_code(
council_module.CouncilClass(),
address_url,
postcode=postcode,
paon=paon,
uprn=uprn,
skip_get_url=skip_get_url,
dev_mode=dev_mode,
council_module_str=council_module_str,
)


if __name__ == "__main__":
import sys

main(sys.argv[1:])
app = UKBinCollectionApp()
app.set_args(sys.argv[1:])
app.run()

0 comments on commit 85a6512

Please sign in to comment.