has added Co-Op chain. Extendability to other chain has significantly improved as well.

This commit is contained in:
KorenLazar
2020-12-17 22:48:10 +02:00
parent 4c0eba1771
commit be47a5ad81
10 changed files with 320 additions and 139 deletions

53
main.py
View File

@@ -1,11 +1,22 @@
from argparse import ArgumentParser
import logging
from promotion import main_latest_promos, get_promos_by_name
from store import get_store_id, store_id_type
from utils import get_products_prices
from store_utils import get_store_id
from utils import LOGS_DIRNAME, XMLS_DIRNAME, get_products_prices
from supermarket_chain import SupermarketChain
from shufersal import ShuferSal
from co_op import CoOp
from pathlib import Path
# TODO: improve extendability: support addition of different supermarket chains
# TODO: fix problem of left-to-right printing in Windows' cmd
# TODO: fix problem of left-to-right printing
Path(LOGS_DIRNAME).mkdir(exist_ok=True)
Path(XMLS_DIRNAME).mkdir(exist_ok=True)
chain_dict = {
'Shufersal': ShuferSal(),
'Co-Op': CoOp(),
}
if __name__ == '__main__':
parser = ArgumentParser()
@@ -13,7 +24,7 @@ if __name__ == '__main__':
help="generates a promos_{store_id}.log file with all the promotions in the requested store",
metavar='store_id',
nargs=1,
type=store_id_type,
type=SupermarketChain.store_id_type,
)
parser.add_argument('--find_promos_by_name',
help="prints all promos containing the given promo_name in the given store",
@@ -31,30 +42,44 @@ if __name__ == '__main__':
metavar='city',
nargs=1,
)
parser.add_argument('--load_xml',
help='boolean flag representing whether to load an existing xml',
parser.add_argument('--load_prices',
help='boolean flag representing whether to load an existing price XML file',
action='store_true',
)
parser.add_argument('--load_promos',
help='boolean flag representing whether to load an existing promo XML file',
action='store_true',
)
parser.add_argument('--load_stores',
help='boolean flag representing whether to load an existing stores XML file',
action='store_true',
)
parser.add_argument('--chain',
required=True,
help='The name of the requested chain',
choices=['Shufersal', 'Co-Op'],
)
args = parser.parse_args()
chain: SupermarketChain = chain_dict[args.chain]
if args.promos:
arg_store_id = int(args.promos[0])
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename=f'promos_{arg_store_id}.log', mode='w', encoding='utf-8')
handler = logging.FileHandler(filename=f'logs/{args.chain}_promos_{arg_store_id}.log', mode='w',
encoding='utf-8')
logger.addHandler(handler)
main_latest_promos(store_id=arg_store_id,
load_xml=args.load_xml,
logger=logger)
main_latest_promos(store_id=arg_store_id, load_xml=args.load_prices, logger=logger, chain=chain)
elif args.price:
get_products_prices(store_id=args.price[0], product_name=args.price[1], load_xml=args.load_xml)
get_products_prices(chain, store_id=args.price[0], load_xml=args.load_prices, product_name=args.price[1])
elif args.find_store_id:
arg_city = args.find_store_id[0]
get_store_id(city=arg_city, load_xml=args.load_xml)
get_store_id(city=arg_city, load_xml=args.load_stores, chain=chain)
elif args.find_promos_by_name:
arg_store_id = int(args.find_promos_by_name[0])
get_promos_by_name(store_id=arg_store_id, load_xml=args.load_xml, promo_name=args.find_promos_by_name[1])
get_promos_by_name(store_id=arg_store_id, chain=chain, promo_name=args.find_promos_by_name[1],
load_prices=args.load_prices, load_promos=args.load_promos)