Exemplo n.º 1
0
def load_items(fixture_path: str):
    """Load default items and assets to database.

    Args:
        fixture_path - Path relative to fixtures. i.e 'data/items.json'
    """
    items = json_parser(resource_string(__name__, fixture_path))

    with db.session.begin_nested():
        for item in items:
            assets = item.pop('assets')

            c_item = CollectionItem(**item)
            c_item.save(commit=False)

            c_tile = CollectionTile()
            c_tile.collection_id = c_item.collection_id
            c_tile.grs_schema_id = c_item.grs_schema_id
            c_tile.tile_id = c_item.tile_id
            c_tile.save(commit=False)

            for asset in assets:
                b = Band.query().filter(
                    Band.collection_id == c_item.collection_id,
                    Band.name == asset.pop('band')).one()

                a = Asset(**asset)
                a.band = b

                a.save(commit=False)
Exemplo n.º 2
0
def get_credentials():
    """Retrieve global secrets with credentials."""
    file = resource_path.join(resource_path.dirname(CURRENT_DIR),
                              'secrets.json')

    with open(file) as f:
        return json_parser(f.read())
Exemplo n.º 3
0
def load_model(fixture_path: str, model_class):
    """Load fixture to database.

    Args:
        fixture_path - Path relative to fixtures. i.e 'data/tiles.json'
        model_class - SQLAlchemy Model Class
    """
    schemas = json_parser(resource_string(__name__, fixture_path))

    with db.session.begin_nested():
        for schema in schemas:
            model = model_class(**schema)

            model.save(commit=False)
Exemplo n.º 4
0
def load_collections(fixture_path: str):
    """Load default collections to database.

    Args:
        fixture_path - Path relative to fixtures. i.e 'data/tiles.json'
    """
    collections = json_parser(resource_string(__name__, fixture_path))

    with db.session.begin_nested():
        for collection in collections:
            bands = collection.pop('bands')

            c = Collection(**collection)
            c.save(commit=False)

            for band in bands:
                b = Band(**band)
                b.collection = c

                b.save(commit=False)
Exemplo n.º 5
0
def correction_sen2cor280(scene):
    """Dispatch sen2cor 2.8.0 execution."""
    safeL2Afull = scene['file'].replace('MSIL1C', 'MSIL2A')

    fragments = Path(safeL2Afull).name.split('_')
    fragments[3] = 'N9999'

    processing_date = datetime.strptime(fragments[-1].replace('.SAFE', ''),
                                        '%Y%m%dT%H%M%S')
    today = datetime.utcnow().timetuple()

    processing_date = processing_date.replace(year=today.tm_year,
                                              month=today.tm_mon,
                                              day=today.tm_mday)

    output_dir = '_'.join(fragments[:-1])
    sensing_date = datetime.strptime(fragments[2], '%Y%m%dT%H%M%S')
    output_dir = Config.DATA_DIR / Path(
        'Repository/Archive/S2_MSI/{}/{}_{}.SAFE'.format(
            sensing_date.strftime('%Y-%m'), output_dir,
            processing_date.strftime('%Y%m%dT%H%M%S')))

    os.makedirs(str(output_dir), exist_ok=True)
    output_dir.mkdir(parents=True, exist_ok=True)

    logging.info('Using outputdir {}'.format(str(output_dir)))
    scene['output_dir'] = str(output_dir)

    # Send scene to the sen2cor service
    req = resource_get('{}/sen2cor'.format(Config.SEN2COR_URL), params=scene)

    result = json_parser(req.content)

    if req.status_code != 200 and result and result.get('status') == 'ERROR':
        rmtree(str(output_dir))
        raise RuntimeError('Error in sen2cor execution')

    return str(output_dir)
Exemplo n.º 6
0
def correction_sen2cor255(scene):
    """Dispatch sen2cor 2.5.5 execution."""
    safeL2Afull = scene['file'].replace('MSIL1C', 'MSIL2A')
    # TODO: check if file exists and validate SAFE
    valid = False
    if os.path.exists(safeL2Afull) and valid == True:
        return safeL2Afull
    if not os.path.exists(safeL2Afull) or valid == False:
        # Send scene to the sen2cor service

        req = resource_get('{}/sen2cor'.format(Config.SEN2COR_URL),
                           params=scene)
        # Ensure the request has been successfully
        assert req.status_code == 200

        result = json_parser(req.content)

        if result and result.get('status') == 'ERROR':
            if os.path.exists(safeL2Afull):
                shutil.rmtree(safeL2Afull, ignore_errors=True)
            raise RuntimeError('Error in sen2cor execution')

    return safeL2Afull
Exemplo n.º 7
0
def main():
    first_time_running = 1  # acts slightly differently on first run-through
    numTimes_already_set = 0  # to set default value if not set in ini file
    pullOnce = 0  # pulls all appointment information only once, doesn't do the automatic refresh thing
    calendarIDisSet = 0

    # ---- check OS (since some Linux distros has a different audio handling method) ---- #
    print("INFO: OS Detected as {0} {1}".format(os_id()[0], os_id()[2]))

    # The following enables the beep sound for linux
    # is running non-Windows OS/non-Windows Linux
    if os_id()[0].find("Linux") != -1 and os_id()[2].find(
            "Microsoft") == -1 or os_id()[0].find("Darwin") != -1:
        print(
            "INFO: Non-Windows/Ubuntu-Windows OS detected. Sudo access may be requested for the audio alerts to work [hopefully] properly."
        )
        from os import system
        # Beep drivers are removed by default in some linux distros (eg. Ubuntu, but not Bash on Ubuntu on Windows)
        # To temporarily enable for current session, (until reboot)
        if os_id()[0].find("Darwin") == -1:
            system("sudo modprobe pcspkr")
            system("xset b 100")  # Perhaps not necessary, mine was set to 40.
            # default bing sound. You may choose another (see to-do)
            system(
                "pactl upload-sample /usr/share/sounds/ubuntu/notifications/Mallet.ogg bell.ogg"
            )
        # now to make beep, simply run:
        # system("printf \a")
    # ---- end check OS ---- #

    # ---- read and parse through file ---- #
    # the following gathers information from a user-supplied INI file
    config_file = open("acuitybotconfig.ini")
    config_file_contents = config_file.read()
    config_file.close()

    # first remove the preceding part and first single quotation mark
    # the 0th index gets the stuff preceding the split
    userID = config_file_contents.split("userID='")[1]
    # meanwhile, the 1st index gets stuff after split
    keyAPI = config_file_contents.split("keyAPI='")[1]
    # then remove the quotation mark on the other end for userID and API key
    userID = userID.split("'")[0]
    keyAPI = keyAPI.split("'")[0]

    if (userID == "USER_ID_GOES_HERE" or keyAPI == "API_KEY_GOES_HERE"):
        print(
            "ERROR: User ID and/or API Key not set. Please set those in the acuitybotconfig.ini file. Exiting..."
        )
        exit()

    # -- check if optional ini values exist in file -- #
    calendarID_info = doesExist("calendarID='", config_file_contents)
    calendarID = calendarID_info[0]
    # even if calendarID_info[0] is value "" (default not found value), calendarIDisSet will still be false
    calendarIDisSet = calendarID_info[1]
    del calendarID_info

    numTimes_info = doesExist("numberOfTimes='", config_file_contents)
    numberOfTimes = numTimes_info[0]
    # even if numTimes_info[0] is value "" (default not found value), numTimes_already_set will still be false
    numTimes_already_set = numTimes_info[1]
    del numTimes_info  # no need for it anymore, just taking up space

    pullOnce_info = doesExist("pullOnce='", config_file_contents)
    if pullOnce_info[1]:  # if 1, we know that it's not blank
        # checks first return value for Y/y
        if pullOnce_info[0] == 'Y' or pullOnce_info[0] == 'y':
            pullOnce = 1
        else:
            print("INFO: Pull once disabled/not set.")
    del pullOnce_info  # no need for it anymore, just taking up space
    # no need for an else statement because pullOnce is by default set to false
    # -- end optional ini value checking -- #

    del config_file_contents  # no need for it anymore, just taking up space

    if not numTimes_already_set or numberOfTimes == "A_NUMBER_GOES_HERE":  # if optional line isn't in ini file
        numberOfTimes = 5  # just make 5 the default number of times if not set

    print("INFO: Set to play a beep {0} times.".format(numberOfTimes))

    if not calendarIDisSet or calendarID == "CALENDAR_ID_GOES_HERE":
        print(
            "\nCalendar ID is not set. Please select an ID from the following list: "
        )
        calendarID_list = web_get(
            'https://acuityscheduling.com/api/v1/calendars',
            auth=(userID, keyAPI))

        if calendarID_list.status_code != 200:  # < 200 OK >
            print("ERROR: Non-OK return status ({0}). Exiting...".format(
                response))
            exit()

        calendarID_list = json_parser(calendarID_list.text)

        # below you'll see that some things are offset by 1. I wanted to start at 1 as simple as possible (but it involved offsetting everything else)
        for i in range(len(calendarID_list)):
            # gets the name of the calendar
            print("{0}: {1}".format(i + 1, calendarID_list[i]["name"]))

        calendarID = input("Enter choice: ")
        if calendarID == "" or int(calendarID) > len(calendarID_list) or int(
                calendarID) < 1:
            print("ERROR: Did not enter a valid choice. Exiting...")
            exit()
        # gets the ID of the calendar
        calendarID = int(calendarID_list[int(calendarID) - 1]["id"])

    # ---- end file reading and parsing ---- #

    params = (
        ('minDate', 'TODAY'),
        ('maxDate', 'TODAY'),
        ('calendarID', calendarID),
    )  # for the get request that repeats

    print("\nINFO: Calendar ID: {0}\n".format(calendarID))

    if pullOnce:
        print(
            "INFO: Pull once enabled. Will only pull current appointments once, auto-refresh disabled."
        )

        print("\nGetting data from Acuity...")
        # needed to print text first while waiting for "- done" in Linux, doesn't really do anything in Windows
        sysout.write('')
        sysout.flush()  # same as above

        response = web_get('https://acuityscheduling.com/api/v1/appointments',
                           params=params,
                           auth=(userID, keyAPI))
        #print("- done.")
        if response.status_code != 200:  # <200 OK>
            print("ERROR: Non-OK return status ({0}). Exiting...".format(
                response))
            exit()

        # must be response.text because reponse just prints the code (i.e., 200 OK or 403 FORBIDDEN, etc.), # type dictionary
        parsed_json_result = json_parser(response.text)
        # technically is the first time running
        printJSON(parsed_json_result, 1)

        print("Exiting...")
        exit()
    else:
        old_len = 0  # used to keep track of amount of appointments
        while 1:
            something_new = 0
            appt_cancelled = 0

            print("Getting data from Acuity...")  # commas keep text inline
            # needed to print text first while waiting for "- done" in Linux, doesn't really do anything in Windows
            sysout.write('')
            sysout.flush()  # same as above

            response = web_get(
                'https://acuityscheduling.com/api/v1/appointments',
                params=params,
                auth=(userID, keyAPI))
            #print ("- done.")
            if response.status_code != 200:  # <200 OK>
                print("ERROR: Non-OK return status ({0}). Exiting...".format(
                    response))
                exit()

            # must be response.text because reponse just prints the code (i.e., 200 OK or 403 FORBIDDEN, etc.), # type dictionary
            parsed_json_result = json_parser(response.text)

            # something_new doesn't trip on first iterance
            if len(parsed_json_result) > old_len and not first_time_running:
                something_new = 1

            if old_len > len(parsed_json_result) and not first_time_running:
                appt_cancelled = 1

            if not first_time_running:
                if something_new:
                    sleep(0.5)  # just a slight delay
                    print("\nThere's a new appointment.")
                    printJSON(parsed_json_result)
                    print0("Playing beep {0} times".format(numberOfTimes),
                           numberOfTimes, 1)
                    print("\n=================\n")
                # need to account for whether an appointment has been cancelled (but it only plays half the amount of specified beeps)
                elif appt_cancelled:
                    sleep(0.5)  # just a slight delay
                    print("\nSomeone cancelled their appointment.")
                    printJSON(parsed_json_result)
                    print0("Playing beep {0} times".format(numberOfTimes / 2),
                           numberOfTimes / 2, 1)
                    print("\n================\n")
                else:
                    sleep(0.5)  # just a slight delay
                    print("\nNothing new.")
                    print("\n=================\n")

            else:
                first_time_running = 0
                printJSON(parsed_json_result, 1)

            # this plus all the other delays means a refresh happens about every 5-10 seconds
            print0("Sleeping for 5 seconds", 5)
            old_len = len(parsed_json_result)