def expose_api_key(): """*********** |EXPOSES API KEY|*********** """ API_KEY = get_config_value('keys', 'API_KEY') #decrypt hidden text - function will fail without 'private_key.key' file! api_key = decrypt(API_KEY) print(api_key)
from util.custom_exceptions import RouteIdException from util.config_parser import get_config_value BASELINE_URL = get_config_value("url", "BASELINE_URL") #TODO: to reduce text redundancy below, #remove keys and list them above or in config as enums #Then, instead of appending "-x", we can create a function #to compress tuple keys and attach as trailing string value to baseline url! FEED_URLS = { ('1', '2', '3', '4', '5', '6'): BASELINE_URL, ('A', 'C', 'E'): BASELINE_URL + "-ace", ('G', ): BASELINE_URL + "-g", ('N', 'Q', 'R', 'W'): BASELINE_URL + "-nqrw", ('B', 'D', 'F', 'M'): BASELINE_URL + "-bdfm", ('J', 'Z'): BASELINE_URL + "-jz", ('L', ): BASELINE_URL + "-l", ('7', ): BASELINE_URL + "-7" } def get_url(route_id): if isinstance(route_id, int): raise TypeError(str(route_id) + " must be of type char or string!") for key, value in FEED_URLS.items(): for route in key: if route_id is route: return FEED_URLS.get(key) raise RouteIdException(
# NOTE: This Codebase version 3.7 maybe backwards compatible with [3.0 >= x <= 3.6.12] but needs to be confirmed! version_info = sys.version_info assert (3, 7) <= version_info <= (3, 9), \ "Python Version " + str(version_info[0]) + "." + str(version_info[1]) + " is not compatible with this application!" import requests from queue import PriorityQueue from mta.tokenauth import TokenAuth from config.stops_dict import STOPS from realtime_feed_urls import get_url from util.cryptographic_func import decrypt from config.gtfs_class_parsers import FEED_MESSAGE from util import timestamp_operators, config_parser from google.protobuf.json_format import MessageToDict gtfs_parser = FEED_MESSAGE.get("class") cipher_key = config_parser.get_config_value('keys', 'API_KEY') API_KEY = decrypt(cipher_key).decode("utf-8") class MTARealTimeFeedParser: ''' class that pulls in real time data feeds from the Metropolitan Transportation Authority based in New York City. The feed that request.get() returns is serialized thus, Google's gtfs_realtime_pb2 in essential for conversion of this binary into readable format This class populates a queue for all available trains relevant to a given stop, sorted by departure time. ''' def __init__(self, route_id, stop_id): if stop_id not in STOPS.keys(): raise KeyError(stop_id + " is not a valid stop_id!") else: self.__stop_name = STOPS.get(stop_id)
import math import datetime from util.config_parser import get_config_value SEC_TO_MINUTE = 60 ROUNDING_THRESHOLD = float(get_config_value("timing", "ROUNDING_THRESHOLD")) def convert_timestamp_to_datetime(timestamp): return datetime.datetime.fromtimestamp( int(timestamp)).strftime('%m-%d-%Y %H:%M:%S') #TODO: assure timestamp is within the domain of EST! def compute_relative_time(timestamp): """subtracts current timestamp from param timestamp and converts value into minutes. performs necessary minute rounding (i.e. rounds up if remainder >= 0.5 else rounds down)""" #NOTE: uncomment code to perform minute analysis to assure rounding accuracy #if timestamp > datetime.datetime.now().timestamp(): #print((timestamp - datetime.datetime.now().timestamp())/SEC_TO_MINUTE) #TODO: cache datetime.now() reference for better runtime (at the cost of slightly less accurate time) # or just compute at each line (at the cost of slower runtime but accurate timing) if timestamp > datetime.datetime.now().timestamp(): if ((timestamp - datetime.datetime.now().timestamp())/SEC_TO_MINUTE) - \ int(((timestamp - datetime.datetime.now().timestamp())/SEC_TO_MINUTE)) < ROUNDING_THRESHOLD: return math.floor( ((timestamp - datetime.datetime.now().timestamp()) / SEC_TO_MINUTE))
from cryptography.fernet import Fernet from util.os_func import read_file, write_file from util.config_parser import get_config_value KEY_FILE = get_config_value("key_file", "KEY_FILE") def encrypt(plaintext): """ :param plaintext: sensitive text needed to be concealed :return: cipher text that can be exposed publically """ fernet = Fernet( read_file(relative_path='..', file_name=KEY_FILE, mode="rb")[0]) return fernet.encrypt(plaintext) def decrypt(ciphertext): """ :param ciphertext: encrypted text as string type :return: plaintext format needed to be executed by app logic """ fernet = Fernet( read_file(relative_path='..', file_name=KEY_FILE, mode="rb")[0]) return fernet.decrypt(str.encode(ciphertext)) def generate_keyfile(): """ :return: saves key to 'private_key.key' file """
""" metaprogramming script to transform certain comma-separated values in txt_files/*.txt into simple key value pairs located in config/*_dict.py """ from util.os_func import os, read_file, write_file from util.custom_exceptions import FileExtensionException from util.config_parser import get_config_value SAVE_TO_DIRECTORY = get_config_value("Paths", "SAVE_DICT_DIRECTORY") def generate_dictionary(relative_path, filename, key_index, value_index): """ :return: generates a new .py file containing a dictionary variable whose key->values pairing is created from parametrized values which corresponds to list indices after string split occurs """ if key_index < 0 or value_index < 0: raise ValueError( "Both index values must be greater than or equal to 0") if filename[-4:] != ".txt": raise FileExtensionException(filename +\ " is not a txt file!") else: data = read_file(relative_path=relative_path, file_name=filename, mode='r')[1:]