Example #1
0
    async def __login(self) -> None:
        utc_now = utils.get_utc_now()
        if not self.__key:
            self.__key = __create_device_key()
        if not self.__checksum:
            self.__checksum = __create_device_checksum(self.__key,
                                                       self.__device_type)

        base_url = await core.get_base_url()
        url = f'{base_url}{self.__login_path}'
        utc_now = utils.get_utc_now()
        async with aiohttp.ClientSession() as session:
            async with session.post(url) as response:
                data = await response.text(encoding='utf-8')

        result = utils.convert.raw_xml_to_dict(data)
        self.__last_login = utc_now
        if 'UserService' in result.keys():
            user = result['UserService']['UserLogin']['User']

            if user.get('Name', None):
                self.__user = None
                self.__access_token = None
                raise DeviceInUseError(
                    'Cannot login. The device is already in use.')
            self.__user = user
            self.__access_token = result['UserService']['UserLogin'][
                'accessToken']
            self.__set_can_login_until(utc_now)
        else:
            self.__access_token = None
        self.__set_access_token_expiry()
Example #2
0
 async def update(self, channel: TextChannel = None, can_post: bool = None, latest_message: Message = None, change_mode: AutoDailyChangeMode = None, store_now_as_created_at: bool = False) -> bool:
     settings: Dict[str, object] = {}
     update_channel = channel is not None and channel != self.channel
     update_can_post = can_post is not None and can_post != self.can_post
     update_latest_message = (latest_message is None and store_now_as_created_at) or (latest_message is not None and latest_message.id != self.latest_message_id and (latest_message.edited_at or latest_message.created_at) != self.latest_message_modified_at)
     update_change_mode = change_mode is not None and change_mode != self.change_mode
     if update_channel:
         settings[_COLUMN_NAME_DAILY_CHANNEL_ID] = channel.id
     if update_can_post:
         settings[_COLUMN_NAME_DAILY_CAN_POST] = can_post
     if update_latest_message:
         if store_now_as_created_at:
             settings[_COLUMN_NAME_DAILY_LATEST_MESSAGE_CREATED_AT] = utils.get_utc_now()
         else:
             settings[_COLUMN_NAME_DAILY_LATEST_MESSAGE_ID] = latest_message.id
             settings[_COLUMN_NAME_DAILY_LATEST_MESSAGE_CREATED_AT] = latest_message.created_at
             settings[_COLUMN_NAME_DAILY_LATEST_MESSAGE_MODIFIED_AT] = latest_message.edited_at or latest_message.created_at
     if update_change_mode:
         settings[_COLUMN_NAME_DAILY_CHANGE_MODE] = change_mode
     success = await db_update_server_settings(self.guild_id, settings)
     if success:
         if update_channel:
             self.__channel = channel
         if update_can_post:
             self.__can_post = settings.get(_COLUMN_NAME_DAILY_CAN_POST)
         if update_latest_message:
             self.__latest_message_id = settings.get(_COLUMN_NAME_DAILY_LATEST_MESSAGE_ID)
             self.__latest_message_created_at = settings.get(_COLUMN_NAME_DAILY_LATEST_MESSAGE_CREATED_AT)
             self.__latest_message_modified_at = settings.get(_COLUMN_NAME_DAILY_LATEST_MESSAGE_MODIFIED_AT)
         if update_change_mode:
             self.__delete_on_change = settings[_COLUMN_NAME_DAILY_CHANGE_MODE]
     return success
Example #3
0
def is_tourney_running(start_date: datetime = None, utc_now: datetime = None) -> bool:
    if not utc_now:
        utc_now = utils.get_utc_now()
    if not start_date:
        start_date = get_current_tourney_start(utc_now)

    return start_date < utc_now
Example #4
0
def create_xl_from_data(data: List[Iterable[Any]],
                        file_prefix: str,
                        data_retrieved_at: datetime,
                        column_formats: List[str],
                        file_name: Optional[str] = None) -> str:
    if data_retrieved_at is None:
        data_retrieved_at = utils.get_utc_now()
    save_to = file_name or get_file_name(file_prefix, data_retrieved_at,
                                         FILE_ENDING.XL)

    wb = openpyxl.Workbook()
    ws = wb.active

    for item in data:
        ws.append(item)

    col_count = len(data[0]) + 1
    row_count = len(data) + 1
    for i, col_no in enumerate(range(1, col_count)):
        column_format = column_formats[i]
        if column_format:
            for row_no in range(2, row_count):
                ws.cell(row_no, col_no).number_format = column_format

    wb.save(save_to)
    return save_to
Example #5
0
async def post_raw_data(ctx: Context, retriever: entity.EntityRetriever,
                        entity_name: str, entity_id: str) -> None:
    if ctx.author.id in settings.RAW_COMMAND_USERS:
        retrieved_at = utils.get_utc_now()
        mode = None
        if entity_id:
            if '--json' in entity_id:
                entity_id = entity_id.replace('--json', '').strip()
                mode = 'json'
            elif '--xml' in entity_id:
                entity_id = entity_id.replace('--xml', '').strip()
                mode = 'xml'
        if entity_id:
            try:
                entity_id = int(entity_id)
            except:
                raise ValueError(
                    f'Invalid parameter specified: `{entity_id}` is not a valid entity id!'
                )
        if entity_id:
            await __post_raw_entity(ctx, retriever, entity_name,
                                    str(entity_id), mode, retrieved_at)
        else:
            await __post_raw_file(ctx, retriever, entity_name, mode,
                                  retrieved_at)
    else:
        await ctx.send(
            'You are not allowed to use this command. If you think this is an error, join the support server and contact the bot\'s author.'
        )
Example #6
0
async def __post_raw_file(ctx: Context, retriever: entity.EntityRetriever,
                          entity_name: str, mode: str,
                          retrieved_at: datetime) -> None:
    async with ctx.typing():
        retrieved_at = retrieved_at or utils.get_utc_now()
        entity_name = entity_name.replace(' ', '_')
        file_name_prefix = f'{entity_name}_designs'
        raw_data = await retriever.get_raw_data()
        raw_data_dict = utils.convert.raw_xml_to_dict(raw_data,
                                                      fix_attributes=True,
                                                      preserve_lists=True)
        if mode == 'xml':
            file_path = __create_raw_file(raw_data, mode, file_name_prefix,
                                          retrieved_at)
        elif mode == 'json':
            data = json.dumps(raw_data_dict)
            file_path = __create_raw_file(data, mode, file_name_prefix,
                                          retrieved_at)
        else:
            start = time.perf_counter()
            flattened_data = __flatten_raw_dict_for_excel(raw_data_dict)
            time1 = time.perf_counter() - start
            print(
                f'Flattening the {entity_name} data took {time1:.2f} seconds.')

            start = time.perf_counter()
            file_path = excel.create_xl_from_raw_data_dict(
                flattened_data, file_name_prefix, retrieved_at)
            time2 = time.perf_counter() - start
            print(
                f'Creating the excel sheet took {time2:.2f} seconds ({time1+time2:.2f} seconds in total).'
            )
    await utils.discord.post_output_with_files(ctx, [], [file_path])
    os.remove(file_path)
Example #7
0
    def is_valid(self):
        '''
        Perform validation
        '''
        # Dates in future only if not a completed reminder
        if (self._get_reminder_utc_aware_datetime() <=
                get_utc_now()) and not self._():
            QtGui.QMessageBox.warning(self, "Date warning",
                                      unicode("Reminder date is in the past."))
            self.addEditRemDlgCalendarWidget.setFocus()
            return False

        # Check length of the reminder
        if len(self._get_reminder_note()) > 1000:
            QtGui.QMessageBox.warning(
                self, "Too long warning",
                "Reminder is %i characters too long!" %
                (len(self._get_reminder_note()) - 1000))
            self.addEditRemDlgTextEdit.setFocus()
            self.addEditRemDlgTextEdit.selectAll()
            return False
        if len(self._get_reminder_note()) == 0:
            QtGui.QMessageBox.warning(self, "Empty text warning",
                                      "Missing reminder note.")
            self.addEditRemDlgTextEdit.setFocus()
            self.addEditRemDlgTextEdit.selectAll()
            return False

        return True
Example #8
0
 def can_login(self) -> bool:
     if self.__can_login_until is None:
         return True
     utc_now = utils.get_utc_now()
     if self.__can_login_until <= utc_now and self.__can_login_until.date == utc_now.date:
         return False
     return True
Example #9
0
async def set_setting(setting_name: str,
                      value: Any,
                      utc_now: datetime = None) -> bool:
    __log_db_function_enter('set_setting',
                            setting_name=f'\'{setting_name}\'',
                            value=value,
                            utc_now=utc_now)

    column_name = None
    if isinstance(value, bool):
        column_name = 'settingboolean'
    elif isinstance(value, int):
        column_name = 'settingint'
    elif isinstance(value, float):
        column_name = 'settingfloat'
    elif isinstance(value, datetime):
        column_name = 'settingtimestamptz'
    else:
        column_name = 'settingtext'

    success = True
    setting, modify_date = await get_setting(setting_name)
    if utc_now is None:
        utc_now = utils.get_utc_now()
    query = ''
    if setting is None and modify_date is None:
        query = f'INSERT INTO settings ({column_name}, modifydate, settingname) VALUES ($1, $2, $3)'
    elif setting != value:
        query = f'UPDATE settings SET {column_name} = $1, modifydate = $2 WHERE settingname = $3'
    success = not query or await try_execute(query,
                                             [value, utc_now, setting_name])
    if success:
        __settings_cache[setting_name] = value
    return success
Example #10
0
def get_tornado_risks(date, hour):
    if not date:
        dt = utils.get_utc_now()
    else:
        dt = datetime.fromisoformat(date)
    outlook_lines = get_outlook_lines(dt, hour)
    in_tornado_section = False
    risks = {}
    risk = None

    for line in outlook_lines:
        line = line.replace('\n', '')
        if not line:
            continue
        if line == '... TORNADO ...':
            in_tornado_section = True
            continue
        if not in_tornado_section:
            continue
        if line == '&&':
            print('Done processing tornado section')
            break
        if line[:4].strip():
            risk = line[:4]
            risks[risk] = []
            line_pairs = line[7:].split(' ')
            parsed_coords = [parse_coords(x) for x in line_pairs]
            risks[risk] += parsed_coords
        else:
            line_pairs = line[7:].split(' ')
            parsed_coords = [parse_coords(x) for x in line_pairs]
            risks[risk] += parsed_coords

    return risks
Example #11
0
async def get_event_details(
        ctx: Context,
        situation_id: str = None,
        all_events: bool = False,
        latest_only: bool = False,
        as_embed: bool = settings.USE_EMBEDS) -> Union[List[Embed], List[str]]:
    has_situation_id = entity.entity_property_has_value(situation_id)
    if has_situation_id and all_events or has_situation_id and latest_only or all_events and latest_only:
        raise ValueError(
            f'Only one of these parameters may be True: situation_id, all_events, latest_only'
        )
    utc_now = utils.get_utc_now()
    situations_data = await situations_designs_retriever.get_data_dict3()
    situation_infos = sorted(situations_data.values(),
                             key=lambda x:
                             (utils.parse.pss_datetime(x['EndDate']),
                              int(x[SITUATION_DESIGN_KEY_NAME])),
                             reverse=True)

    if situation_id:
        situation_infos = [situations_data[situation_id]]
    elif all_events:
        situation_infos.reverse()
    elif latest_only:
        situation_infos = [situation_infos[0]]
    else:
        situation_infos = __get_current_situations_infos(
            situations_data.values(), utc_now)

    if not situation_infos:
        if all_events:
            raise NotFound(f'There\'s no event data.')
        else:
            raise NotFound(f'There\'s no event running currently.')
    else:
        chars_data = await crew.characters_designs_retriever.get_data_dict3()
        collections_data = await crew.collections_designs_retriever.get_data_dict3(
        )
        items_data = await item.items_designs_retriever.get_data_dict3()
        missions_data = await mission.missions_designs_retriever.get_data_dict3(
        )
        rooms_data = await room.rooms_designs_retriever.get_data_dict3()

        situations_details_collection = __create_situations_details_collection_from_infos(
            situation_infos,
            situations_data,
            chars_data,
            collections_data,
            items_data,
            missions_data,
            rooms_data,
            utc_now=utc_now)
        if as_embed:
            return (await situations_details_collection.
                    get_entities_details_as_embed(ctx))
        else:
            return (
                await
                situations_details_collection.get_entities_details_as_text())
Example #12
0
def mock_get_daily_info() -> EntityInfo:
    utc_now = utils.get_utc_now()
    if utc_now.hour < 1:
        if utc_now.minute < 20:
            return __mock_get_daily_info_1()
        else:
            return __mock_get_daily_info_2()
    else:
        return __mock_get_daily_info_1()
Example #13
0
    async def __login(self) -> None:
        base_url = await core.get_base_url()
        url = f'{base_url}{DEVICE_LOGIN_PATH}'
        utc_now = utils.get_utc_now()
        client_datetime = utils.format.pss_datetime(utc_now)
        query_params = {
            'advertisingKey':
            '""',
            'checksum':
            _create_device_checksum(self.__key, self.__device_type,
                                    client_datetime),
            'clientDateTime':
            client_datetime,
            'deviceKey':
            self.__key,
            'deviceType':
            self.__device_type,
            'isJailBroken':
            'false',
            'languageKey':
            'en',
        }
        if settings.PRINT_DEBUG_WEB_REQUESTS:
            print(f'[WebRequest] Attempting to get data from url: {url}')
            print(
                f'[WebRequest]   with parameters: {json.dumps(query_params, separators=(",", ":"))}'
            )
        async with aiohttp.ClientSession() as session:
            async with session.post(url, params=query_params) as response:
                data = await response.text(encoding='utf-8')
                if settings.PRINT_DEBUG_WEB_REQUESTS:
                    log_data = data or ''
                    if log_data and len(log_data) > 100:
                        log_data = log_data[:100]
                    print(f'[WebRequest] Returned data: {log_data}')

        result = utils.convert.raw_xml_to_dict(data)
        self.__last_login = utc_now
        if 'UserService' in result.keys():
            user = result['UserService']['UserLogin']['User']

            if user.get('Name', None):
                self.__user = None
                self.__access_token = None
                raise DeviceInUseError(
                    'Cannot login. The device is already in use.')
            self.__user = user
            self.__access_token = result['UserService']['UserLogin'][
                'accessToken']
            self.__set_can_login_until(utc_now)
        else:
            error_message = result.get('UserLogin', {}).get('errorMessage')
            if error_message:
                raise LoginError(error_message)
            self.__access_token = None
        self.__set_access_token_expiry()
Example #14
0
async def create_data_lua_file(entity_retriever: _EntityRetriever, entity_name: str) -> str:
    """
    Returns the file_path to the created file.
    """
    timestamp = _utils.get_utc_now().strftime('%Y%m%d-%H%M%S')
    data = await get_data_lua(entity_retriever)
    file_path = f'wiki_{entity_name}_data_{timestamp}.lua'
    with open(file_path, 'w') as fp:
        fp.write(data)
    return file_path
Example #15
0
    def __get_is_data_outdated(self) -> bool:
        if self.__UPDATE_INTERVAL_ORIG == 0:
            return True

        utc_now = utils.get_utc_now()
        self.__WRITE_LOCK.acquire()
        modify_date = self.__modify_date
        self.__WRITE_LOCK.release()
        result = modify_date is None or utc_now - modify_date > self.__UPDATE_INTERVAL
        return result
Example #16
0
async def get_full_fleet_info_as_text(
    ctx: Context,
    fleet_info: EntityInfo,
    max_tourney_battle_attempts: int = None,
    past_fleets_data: EntitiesData = None,
    past_users_data: EntitiesData = None,
    past_retrieved_at: datetime = None,
    as_embed: bool = settings.USE_EMBEDS
) -> Tuple[Union[List[Embed], List[str]], List[str]]:
    """Returns a list of lines for the post, as well as the paths to the spreadsheet created"""
    fleet_id = fleet_info[FLEET_KEY_NAME]
    fleet_name = fleet_info[FLEET_DESCRIPTION_PROPERTY_NAME]
    is_past_data = past_fleets_data and past_users_data and past_retrieved_at

    if is_past_data:
        retrieved_at = past_retrieved_at
        if fleet_info.get('CurrentAllianceName') is None:
            current_fleet_info = await get_fleets_data_by_id(fleet_id)
            if current_fleet_info[
                    FLEET_DESCRIPTION_PROPERTY_NAME] != fleet_info[
                        FLEET_DESCRIPTION_PROPERTY_NAME]:
                fleet_info['CurrentAllianceName'] = current_fleet_info[
                    FLEET_DESCRIPTION_PROPERTY_NAME]
        fleet_users_data = {
            user_id: user_info
            for user_id, user_info in past_users_data.items()
            if user_info.get(FLEET_KEY_NAME) == fleet_id
        }
    else:
        retrieved_at = utils.get_utc_now()
        fleet_info = await get_fleets_data_by_id(fleet_id)
        fleet_users_data = await __get_fleet_users_data_by_fleet_id(fleet_id)

    post_content = await __get_fleet_details_by_info(
        ctx,
        fleet_info,
        fleet_users_data,
        max_tourney_battle_attempts=max_tourney_battle_attempts,
        retrieved_at=retrieved_at,
        is_past_data=is_past_data,
        as_embed=as_embed)
    fleet_sheet_file_name = excel.get_file_name(fleet_name,
                                                retrieved_at,
                                                excel.FILE_ENDING.XL,
                                                consider_tourney=False)
    file_path = __create_fleet_sheet_xl(
        fleet_users_data,
        retrieved_at,
        fleet_sheet_file_name,
        max_tourney_battle_attempts=max_tourney_battle_attempts)
    file_paths = []
    if file_path:
        file_paths.append(file_path)

    return post_content, file_paths
Example #17
0
async def try_set_schema_version(version: str) -> bool:
    __log_db_function_enter('try_set_schema_version', version=f'\'{version}\'')

    prior_version = await get_schema_version()
    utc_now = utils.get_utc_now()
    if not prior_version:
        query = f'INSERT INTO settings (modifydate, settingtext, settingname) VALUES ($1, $2, $3)'
    else:
        query = f'UPDATE settings SET modifydate = $1, settingtext = $2 WHERE settingname = $3'
    success = await try_execute(query, [utc_now, version, 'schema_version'])
    return success
Example #18
0
 def get_latest_data(self, initializing: bool = False) -> TourneyData:
     utc_now = utils.get_utc_now()
     year, month = TourneyDataClient.__get_tourney_year_and_month(utc_now)
     result = None
     while year > self.from_year or month >= self.from_month:
         result = self.get_data(year, month, initializing=initializing)
         if result:
             break
         month -= 1
         if month == 0:
             year -= 1
             month = 12
     return result
Example #19
0
async def update_schema_v_1_2_4_0() -> bool:
    column_definitions = [('dailydeleteonchange', 'BOOLEAN', False, False,
                           None)]

    schema_version = await get_schema_version()
    if schema_version:
        compare_1240 = utils.compare_versions(schema_version, '1.2.4.0')
        compare_1220 = utils.compare_versions(schema_version, '1.2.2.0')
        if compare_1240 <= 0:
            return True
        elif compare_1220 > 0:
            return False

    print(
        f'[update_schema_v_1_2_4_0] Updating database schema from v1.2.2.0 to v1.2.4.0'
    )

    query_lines = []
    for (column_name, column_type, column_is_primary, column_not_null,
         column_default) in column_definitions:
        column_definition = utils.database.get_column_definition(
            column_name,
            column_type,
            is_primary=column_is_primary,
            not_null=column_not_null,
            default=column_default)
        query_lines.append(
            f'ALTER TABLE serversettings ADD COLUMN IF NOT EXISTS {column_definition}'
        )

    query = '\n'.join(query_lines)
    success = await try_execute(query)
    if success:
        utc_now = utils.get_utc_now()
        daily_info = await daily.get_daily_info()

        __settings = {
            daily.__get_daily_info_setting_name(key): (value, utc_now)
            for key, value in daily_info.items()
        }
        success = await set_settings(__settings)
        if success:
            await daily.__update_db_daily_info_cache()

        #success = await daily.db_set_daily_info(daily_info, utc_now) Function has been updated must use old methods
        if success:
            success = await try_set_schema_version('1.2.4.0')
    return success
Example #20
0
async def get_sales_history_for_rooms(
        ctx: Context,
        room_type: str,
        room_type_pretty: str,
        reverse: bool = False,
        as_embed: bool = settings.USE_EMBEDS) -> Union[List[Embed], List[str]]:
    if not room_type:
        raise ValueError('The room type must neither be None nor empty.')
    utc_now = utils.get_utc_now()

    db_sales_infos = await __db_get_sales_infos(utc_now=utc_now,
                                                category_type='Room')
    sales_infos = await __process_db_sales_infos(db_sales_infos,
                                                 utc_now,
                                                 filter_old=False)
    sales_infos = [
        sales_info for sales_info in sales_infos
        if sales_info['entity_details'].entity_info['RoomType'] == room_type
    ]

    if sales_infos:
        title = f'{room_type_pretty.capitalize()}s have been sold on'
        sales_details = []
        for sales_info in sales_infos:
            name = sales_info['name']
            sold_on_date = sales_info['expiry_date'] - utils.datetime.ONE_DAY
            sold_on = utils.format.datetime(sold_on_date,
                                            include_time=False,
                                            include_tz=False)
            star_date = utils.datetime.get_star_date(sold_on_date)
            sold_ago = (utc_now - sold_on_date).days
            price = sales_info['original_price']
            currency = sales_info['currency']
            day = 'day' + 's' if sold_ago != 1 else ''
            sales_details.append(
                f'{sold_on} (Star date {star_date}, {sold_ago} {day} ago): **{name}** for {price} {currency}'
            )

        if as_embed:
            colour = utils.discord.get_bot_member_colour(ctx.bot, ctx.guild)
            result = utils.discord.create_basic_embeds_from_description(
                title, description=sales_details, colour=colour)
        else:
            result = [f'**{title}**']
            result.extend(sales_details)
        return result
    raise Error(
        f'There is no past sales data available for {room_type_pretty}s.')
Example #21
0
async def get_sales_history(
        ctx: Context,
        entity_info: EntityInfo,
        reverse: bool = False,
        as_embed: bool = settings.USE_EMBEDS) -> Union[List[Embed], List[str]]:
    utc_now = utils.get_utc_now()

    category_type = entity_info.get('entity_type')
    entity_id = entity_info.get('entity_id')
    entity_id = int(entity_id) if entity_id else None
    entity_name = entity_info.get('entity_name')

    db_sales_infos = await __db_get_sales_infos(utc_now=utc_now,
                                                category_type=category_type,
                                                entity_id=entity_id)
    sales_infos = await __process_db_sales_infos(
        db_sales_infos, utc_now, filter_old=(entity_id is None))
    if reverse:
        sales_infos = reversed(sales_infos)

    if sales_infos:
        title = f'{entity_name} has been sold on'
        sales_details = []
        for sales_info in sales_infos:
            sold_on_date = sales_info['expiry_date'] - utils.datetime.ONE_DAY
            sold_on = utils.format.datetime(sold_on_date,
                                            include_time=False,
                                            include_tz=False)
            star_date = utils.datetime.get_star_date(sold_on_date)
            sold_ago = (utc_now - sold_on_date).days
            price = sales_info['original_price']
            currency = sales_info['currency']
            day = 'day' + 's' if sold_ago != 1 else ''
            sales_details.append(
                f'{sold_on} (Star date {star_date}, {sold_ago} {day} ago) for {price} {currency}'
            )

        if as_embed:
            colour = utils.discord.get_bot_member_colour(ctx.bot, ctx.guild)
            result = utils.discord.create_basic_embeds_from_description(
                title, description=sales_details, colour=colour)
        else:
            result = [f'**{title}**']
            result.extend(sales_details)
        return result
    raise Error(f'There is no past sales data available for {entity_name}.')
Example #22
0
 def get_latest_monthly_data(self, initializing: bool = False) -> TourneyData:
     utc_now = utils.get_utc_now()
     year, month = TourneyDataClient.__get_last_tourney_year_and_month(utc_now)
     if settings.MOST_RECENT_TOURNAMENT_DATA:
         month += 1
         if month == 13:
             month = 1
             year += 1
     result = None
     while year > self.from_year or month >= self.from_month:
         result = self.get_data(year, month, initializing=initializing)
         if result:
             break
         month -= 1
         if month == 0:
             year -= 1
             month = 12
     return result
Example #23
0
    def _init_widgets(self):
        '''
        Init the widgets according to self.reminder_dict
        '''
        # Defaults
        due = dt2str(get_utc_now())
        note = None
        complete = False
        if self.edit_reminder_id is not None:
            # For edit, load from existing, set title and enable 'Complete' chckbx
            self.setWindowTitle('Edit reminder')
            self.addEditRemDlgCompletecheckBox.setEnabled(True)
            with session_scope() as session:
                reminder = session.query(Reminder).get(
                    int(self.edit_reminder_id))
                due = reminder.due
                note = reminder.note
                complete = reminder.complete

        # Set date
        local_due = utcstr2local(due, self.time_zone)
        self.addEditRemDlgCalendarWidget.setSelectedDate(
            QtCore.QDate(local_due.year, local_due.month, local_due.day))
        # Set time
        self.addEditRemDlgTimeEdit.setTime(
            QtCore.QTime(local_due.time().hour,
                         local_due.time().minute))
        # Set note
        if note:
            self.addEditRemDlgTextEdit.document().setPlainText(unicode(note))
        # Set completed
        if complete:
            self.addEditRemDlgCompletecheckBox.setCheckState(QtCore.Qt.Checked)

        # If this edit is a Reschedule
        if self.reschedule is True:
            # This is in reaction to 'Reschedule' from notification dlg
            # Deselect 'complete' and also highlight time edit
            logger.debug(
                'Reschedule is True so overriding the complete check status')
            self.addEditRemDlgCompletecheckBox.setCheckState(
                QtCore.Qt.Unchecked)
            self.addEditRemDlgTimeEdit.selectAll()
            self.addEditRemDlgTimeEdit.setFocus()
Example #24
0
async def set_settings(settings: Dict[str, Tuple[object, datetime]]) -> bool:
    __log_db_function_enter('set_settings', settings=settings)

    utc_now = utils.get_utc_now()
    if settings:
        query_lines = []
        args = []
        success = True
        current_settings = await get_settings(settings.keys())
        for setting_name, (value, modified_at) in settings.items():
            query = ''
            column_name = None
            if isinstance(value, bool):
                column_name = 'settingboolean'
                value = utils.database.convert_boolean(value)
            elif isinstance(value, int):
                column_name = 'settingint'
            elif isinstance(value, float):
                column_name = 'settingfloat'
            elif isinstance(value, datetime):
                column_name = 'settingtimestamptz'
                value = utils.database.convert_timestamp(value)
            else:
                column_name = 'settingtext'
                value = utils.database.convert_text(value)
            current_value, db_modify_date = current_settings[setting_name]
            modify_date = db_modify_date or utc_now

            setting_name = utils.database.convert_text(setting_name)
            if current_value is None and db_modify_date is None:
                query = f'INSERT INTO settings ({column_name}, modifydate, settingname) VALUES ({value}, \'{modified_at}\', {setting_name});'
            elif current_value != value:
                query = f'UPDATE settings SET {column_name} = {value}, modifydate = \'{modified_at}\' WHERE settingname = {setting_name};'

            if query:
                query_lines.append(query)
                args.extend([value, modified_at, setting_name])
        success = not query_lines or await try_execute('\n'.join(query_lines))
        if success:
            __settings_cache.update(settings)
        return success
    else:
        return True
Example #25
0
    def retrieve_past_month_year(month: str, year: str, utc_now: datetime) -> Tuple[int, int]:
        if not utc_now:
            utc_now = utils.get_utc_now()

        if month is None:
            temp_month = (utc_now.month - 2) % 12 + 1
        else:
            temp_month = utils.datetime.MONTH_NAME_TO_NUMBER.get(month.lower(), None)
            temp_month = temp_month or utils.datetime.MONTH_SHORT_NAME_TO_NUMBER.get(month.lower(), None)
            if temp_month is None:
                try:
                    temp_month = int(month)
                except:
                    raise ValueError(f'Parameter month got an invalid value: {month}')
        if year is None:
            year = utc_now.year
            if utc_now.month <= temp_month:
                year -= 1
        return temp_month, int(year)
Example #26
0
 def thermal_data(self, start=None, end=None, days_back=60, evaluate_preprocess=False):
     """
     :param start: In UTC time.
     :param end: In UTC time.
     :param days_back: if start is None, then we set start to end - timedelta(days=days_back). 
     :param evaluate_preprocess: Whether to add the fields t_max, t_min, t_mean, t_std to the preprocessed data.
     :return: pd.df {zone: pd.df columns: t_in', 't_next', 'dt','t_out', 'action', 'a1', 'a2', [other mean zone temperatures]}
              where t_out and zone temperatures are the mean values over the intervals. 
              a1 is whether heating and a2 whether cooling.
     """
     if end is None:
         end = utils.get_utc_now()
     if start is None:
         start = end - timedelta(days=days_back)
     z, o = self._get_inside_data(start, end), self._get_outside_data(start, end)
     # Take mean of all weather stations.
     o = self._preprocess_outside_data(o.values())
     print("Received Thermal Data from MDAL.")
     return self._preprocess_thermal_data(z, o, evaluate_preprocess)
Example #27
0
async def get_user_details_by_info(
        ctx: Context,
        user_info: EntityInfo,
        max_tourney_battle_attempts: int = None,
        retrieved_at: datetime = None,
        past_fleet_infos: EntitiesData = None,
        as_embed: bool = settings.USE_EMBEDS) -> Union[List[Embed], List[str]]:
    is_past_data = past_fleet_infos is not None and past_fleet_infos

    user_id = user_info[USER_KEY_NAME]
    retrieved_at = retrieved_at or utils.get_utc_now()
    tourney_running = tourney.is_tourney_running(utc_now=retrieved_at)
    if past_fleet_infos:
        ship_info = {}
        fleet_info = past_fleet_infos.get(user_info.get(fleet.FLEET_KEY_NAME))
        current_user_info = await __get_user_info_by_id(user_id) or {}
        current_user_name = current_user_info.get(
            USER_DESCRIPTION_PROPERTY_NAME)
        if current_user_name and current_user_name != user_info.get(
                USER_DESCRIPTION_PROPERTY_NAME):
            user_info['CurrentName'] = current_user_name
    else:
        _, ship_info = await ship.get_inspect_ship_for_user(user_id)
        fleet_info = await __get_fleet_info_by_user_info(user_info)

    is_in_tourney_fleet = fleet.is_tournament_fleet(
        fleet_info) and tourney_running
    user_details = __create_user_details_from_info(
        user_info,
        fleet_info,
        ship_info,
        max_tourney_battle_attempts=max_tourney_battle_attempts,
        retrieved_at=retrieved_at,
        is_past_data=is_past_data,
        is_in_tourney_fleet=is_in_tourney_fleet)

    if as_embed:
        return [(await
                 user_details.get_details_as_embed(ctx, display_inline=False))]
    else:
        return (await
                user_details.get_details_as_text(entity.EntityDetailsType.LONG
                                                 ))
Example #28
0
    def get_data(self, year: int, month: int, initializing: bool = False) -> TourneyData:
        if year < self.from_year:
            raise ValueError(f'There\'s no data from {year}. Earliest data available is from {calendar.month_name[self.from_month]} {self.from_year}.')
        if year == self.from_year:
            if month < self.from_month:
                raise ValueError(f'There\'s no data from {calendar.month_name[month]} {year}. Earliest data available is from {calendar.month_name[self.from_month]} {self.from_year}.')
        if not initializing:
            if year > self.to_year or (year == self.to_year and month > self.to_month):
                utc_now = utils.get_utc_now()
                if utc_now.year == year and utc_now.month == month:
                    raise ValueError(f'There\'s no data from {calendar.month_name[month]} {year}. Most recent data available is from {calendar.month_name[self.to_month]} {self.to_year}.')

        result = self.__read_data(year, month)

        if result is None:
            result = self.__retrieve_data(year, month, initializing=initializing)
            self.__cache_data(result)

        return result
Example #29
0
def create_csv_from_data(data: List[Iterable[Any]],
                         file_prefix: str,
                         data_retrieved_at: datetime,
                         file_name: Optional[str] = None,
                         delimiter: Optional[str] = '\t') -> str:
    if data_retrieved_at is None:
        data_retrieved_at = utils.get_utc_now()
    if file_name:
        save_to = file_name
    else:
        save_to = get_file_name(file_prefix, data_retrieved_at,
                                FILE_ENDING.CSV)

    if not delimiter:
        delimiter = '\t'

    lines = [delimiter.join([str(field) for field in line]) for line in data]

    with open(save_to, mode='w') as fp:
        fp.write('\n'.join(lines))
    return save_to
Example #30
0
async def get_sales_details(
        ctx: Context,
        reverse: bool = False,
        as_embed: bool = settings.USE_EMBEDS) -> Union[List[str], List[Embed]]:
    utc_now = utils.get_utc_now()
    db_sales_infos = await __db_get_sales_infos(utc_now=utc_now)
    sales_infos = await __process_db_sales_infos(db_sales_infos, utc_now)
    if reverse:
        sales_infos = reversed(sales_infos)

    title = 'Expired sales'
    description = f'The things listed below have been sold in the {emojis.pss_shop} shop over the past 30 days. You\'ve got the chance to buy them on the Pixel Starships website for an additional 25% on the original price.'

    sales_details = []
    for sales_info in sales_infos:
        expires_in = sales_info['expires_in']
        day = 'day' + ('' if expires_in == 1 else 's')
        details = f'**{expires_in}** {day}: **{sales_info["name"]}** ({sales_info["type"]}) {sales_info["currency"]} {sales_info["price"]}'
        sales_details.append(details)
    if as_embed:
        body_lines = [description, utils.discord.ZERO_WIDTH_SPACE]
        body_lines.extend(sales_details)
        colour = utils.discord.get_bot_member_colour(ctx.bot, ctx.guild)
        footer = 'Click on the title to get redirected to the Late Sales portal, where you can purchase these offers.'
        result = utils.discord.create_basic_embeds_from_description(
            title,
            description=body_lines,
            colour=colour,
            footer=footer,
            author_url=LATE_SALES_PORTAL_HYPERLINK)
    else:
        result = [f'**{title}**', f'_{description}_']
        result.extend(sales_details)
        result.append(
            f'_Visit <{LATE_SALES_PORTAL_HYPERLINK}> to purchase these offers._'
        )
    return result