Example #1
0
 def _check_updates(self):
     response = get("https://api.ratesapi.io/api/latest")
     if response.status_code == 200:
         storage = Storage(storage_name)
         latest_api_date = json.loads(response.text)["date"]
         logging.debug(f"API date: {latest_api_date}")
         rates = storage.get_dict()
         latest_storage_date = rates.get("last_update")
         logging.debug(f"Storage date: {latest_storage_date}")
         if latest_storage_date:
             if (
                 latest_storage_date < latest_api_date
                 or [
                     currency
                     for currency, rates in rates["currencies"].items()
                     if rates["rates"].keys() != supported_currencies.keys()
                 ]
                 or rates["currencies"].keys() != supported_currencies.keys()
             ):
                 return "update"
             else:
                 return "ok"
         else:
             return "empty"
     else:
         return "failed"
Example #2
0
    def initGameCallBack(self):

        if isinstance(self.interFrame, Frame):
            self.interFrame.destroy()
            self.timer.destroy()
            self.counter.destroy()

        if isinstance(self.gameFrame, Frame):
            self.gameFrame.destroy()

        storage = Storage()
        self.mines = storage.get("mines")
        h = storage.get("height")
        w = storage.get("width")
        self.isGameStarted = False

        self.tiles = [[j for j in range(w)] for i in range(h)]
        self.bombs = set()
        self.bombsPrediction = set()
        self.tilesAmount = w * h

        self.board = Board(w, h, self.mines)

        self.root.bind_class('Label', '<Button-1>', self.openTileEvent)
        self.root.bind_class('Label', '<Button-2>', self.flagTileEvent)
        self.root.bind('<KeyPress>', self.keyboardEvent)

        self.code = ""
        self.__createFrame()
        self.__initInterface()
Example #3
0
 def test_keys_failure(self):
     """
     Test Storage.keys command for failure
     :return:
     """
     storage = Storage()
     storage._keys_dict = {'1': 'one', 'abc': '1'}
     self.assertRaises(StoragePatternError, storage.keys, 'ab[cd')
Example #4
0
    def setUp(self) -> None:
        self.storage = Storage()
        self.gc = StorageGarbageCollector(self.storage)
        self.now = time.time()

        def fake_time():
            return self.now

        self.fake_time = fake_time
Example #5
0
 def __init__(self, config: Config):
     self.config = config
     self.storage = Storage(config)
     self.health_code_map = {
         1: "success",
         0: "failure",
         -1: "unresponsive",
         -2: "unreachable",
         -3: "unknown errs",
     }
Example #6
0
    def __init__(self, name):
        Device.__init__(self, name)
        CheckCyclicTask.__init__(self)

        # default config values
        self._eep = self.DEFAULT_EEP.clone()

        self._storage_max_age = None  # type: Optional[int]  # age in seconds

        self._storage = Storage()
Example #7
0
    def __init__(self, config=None):
        configurable.Configurable.__init__(self, config)

        self._event_handlers = {
            'key.detach': KeyDetach(self),
            'key.invalidate': KeyInvalidate(self)
        }
        self._society = Society(self, self.config.g('society.members', []),
                                self.config.g('society.announceInterval', 60))
        self._storage = Storage(self)
        self._time_start = time.time()
Example #8
0
def main(args):
    """Coordinate the skill. Main function called in IBM Cloud Functions"""

    ### Helper Functions ###

    def do_cleanup(paths: list) -> None:
        """Cleanup the leftover paths

           Args:
                paths: a list of paths to clean up
        """
        for path in paths:
            os.remove(path)

    def get_config(config_file_path: str) -> dict:
        """Load the skill config

           Args:
                config_file_path: location of config file
        """
        config = {}
        with open(config_file_path) as config_file:
            config = json.load(config_file)
        return config

    ### Main Function ###

    # Get args
    request_body = args
    config_file = args.get('config', './config.json')
    config = get_config(config_file)

    # # Build pieces
    storage = Storage(config, request_body)
    action = Action(config)

    # # Download --> ACTION --> Upload
    file_data = storage.file_data  # {name: "", id: 0000}
    file_path = storage.download_file()

    # Run Action
    action.do_action(file_path, file_data['id'])

    # Upload/Update new file
    results = action.push2storage(storage)

    # Clean up local files
    do_cleanup([file_path])

    # Return status
    return {"status": "success"}
Example #9
0
 def convert(self, pretiffy=False):
     if self.input_currency and self.output_currency:
         if len(self.input_currency) > 1:
             return (
                 "Too many input currency options! Please insert a currency code/symbol matching the unique currency.",
                 400,
             )
         else:
             input_dict = {
                 "amount": self.amount,
                 "currency": self.input_currency[0]
             }
             storage = Storage(storage_name)
             rates = storage.get_dict()
             try:
                 rates = rates["currencies"][
                     self.input_currency[0]]["rates"]
                 logging.info(rates)
             except KeyError:
                 rates = {}
             if rates:
                 output_dict = {
                     currency:
                     round(rates[currency] *
                           self.amount, 2) if currency in rates else None
                     for currency in self.output_currency
                 }
                 return (
                     json.dumps(
                         {
                             "input": input_dict,
                             "output": output_dict
                         },
                         indent=4,
                         sort_keys=True,
                     ) if pretiffy else {
                         "input": input_dict,
                         "output": output_dict
                     },
                     200,
                 )
             else:
                 return (
                     "Missing currency exchange rates. Please try to update the storage.",
                     400,
                 )
     else:
         return (
             "Unsupported currency symbol/code! Please check inserted values with supported values help and try again!",
             400,
         )
def test_create_new_entry(tmpdir, keyword, emotion):
    db_path = str(tmpdir.mkdir("database"))
    db_name = "tmp_db"
    stor = Storage(db_name, db_path, "tmp_table")

    kw = "'"+keyword+"'"
    emo = "'"+emotion+"'"
    stor.create_new_entry("tmp_table", kw, emo)

    conn = sqlite3.connect(os_path.join(db_path, db_name))
    cur = conn.cursor()
    # where keyword = ? and emotion = ? , (keyword, emotion)
    entriesMade = cur.execute("select * from tmp_table").fetchall()
    conn.close()
    assert entriesMade == [(keyword, emotion)]
Example #11
0
    def test_error_write_permission(self):
        path = "/usr/bin/not_allowed_to_write_into"
        self.assertFalse(os.path.exists(path))

        p = Storage()
        p.set_file(path)
        p.load()

        data1 = 123
        p.set("data1", data1)

        with self.assertRaises(StorageException):
            p.save()

        self.assertFalse(os.path.exists(path))
Example #12
0
 def __init__(self):
     bot = commands.Bot(command_prefix='.')
     self.storage = Storage()
     self.channel_controller = ChannelConstroller(DiscordMessageBus(bot),
                                                  self.storage.unique_tag)
     self.bot = attach_commands(bot, self.storage, self.channel_controller)
     self.looper = Looper(bot, self.storage, self.channel_controller)
def getScheduleStorage(load, pv, RATEOPTION):
    st = Storage(df=load, RATE=RATEOPTION)
    load_t = preproc_df(load)
    pv_t = preproc_df(pv)

    r_nbc = st.RATE["NBC"]
    r_on_m = preproc_demand_rate_m(st, "ONPEAK")
    r_mid_m = preproc_demand_rate_m(st, "MIDPEAK")
    r_off_m = preproc_demand_rate_m(st, "OFFPEAK")

    c_whole = 0.05
    PCS_max = 500.0
    CAP_ESS = 1000.0
    SOC_min = 0.0
    SOC_max = 1.0
    CC = st.RATE["CC"]

    ef_c = 0.96
    ef_d = (1.0 / 0.96)

    solver = build_solve_milp(st,
                              load_t=load_t, pv_t=pv_t, \
                              cost_t=preproc_cost(st), \
                              r_nbc=r_nbc, r_on_m=r_on_m, r_mid_m=r_mid_m, r_off_m=r_off_m, \
                              c_whole=c_whole, PCS_max=PCS_max, \
                              ef_c=ef_c, ef_d=ef_d, CAP_ess=CAP_ESS, SOC_min=SOC_min, SOC_max=SOC_max, CC=CC)
Example #14
0
    def test_extract_subs(self):
        tmp_dir = tempfile.mkdtemp()

        file = os.path.join(tmp_dir, 'fragment.mkv')
        shutil.copyfile('fragment.mkv', file)
        with Storage(':memory:') as storage:
            app_run_config = AppRunConfig(tmp_dir, [languages.get(part1=x) for x in ['ru', 'en', 'fr']],
                                          [(languages.get(part1='ru'), languages.get(part1='en'))], ".*", {}, False,
                                          False)

            extract_subs = ExtractSubs(app_run_config, storage)
            extract_subs.scan_files()

            video_files = storage.get_all_video_files()
            self.assertEqual(1, len(video_files))
            fragment_file = video_files[0]
            subtitles = storage.get_all_subtitles_by_video_file_id(fragment_file['id'])
            self.assertEqual(5, len(subtitles))
            subtitles_file_names = set(os.path.basename(subtitle['full_path']) for subtitle in subtitles)
            self.assertEqual({'fragment_eng_SDH.srt', 'fragment_fra.srt', 'fragment.rus_eng.ass',
                              'fragment_rus_Forced.srt', 'fragment_rus.srt'}, subtitles_file_names)
            # 1 - cause forced subtitles is empty, file is empty, can't merge
            self.assertEqual(1, len(storage.get_all_merged_subtitles_by_video_file_id(fragment_file['id'])))

        shutil.rmtree(tmp_dir, ignore_errors=True)
Example #15
0
 def test_keys_ttl(self):
     """
     Test Storage.keys with ttl
     :return:
     """
     self.now = time.time()
     with patch('time.time', self.fake_time):
         storage = Storage()
         storage.set('1', 'one', self.now + 5)
         storage.set('2', 'two')
         storage.set('3', 'three', self.now + 10)
         self.now += 6
         self.assertEqual(['2', '3'], storage.keys('*'))
         self.assertEqual(['2', '3'], list(storage._keys_dict.keys()))
Example #16
0
def main():
    data = Data([1, 2, 3, 4, 5], "lol")
    print(data)
    storage = Storage()
    storage.add(data)
    storage.add(Data([1, 2, 3, 4, 5], "kek"))
    print(storage.getAll())
Example #17
0
 def update_prefixes_in_storage(storage: Storage, prefix: str) -> bool:
     """
     Add a new prefix into database
     return whether the prefix has been registered before
     """
     prefixes_msg = PrefixesInStorage()
     ret = storage.get("prefixes")
     if ret:
         prefixes_msg.ParseFromString(ret)
     for prefix_item in prefixes_msg.prefixes:
         if prefix_item.name == prefix or prefix.startswith(
                 prefix_item.name):
             return True
     new_prefix = prefixes_msg.prefixes.add()
     new_prefix.name = prefix
     storage.put("prefixes", prefixes_msg.SerializeToString())
     logging.info("add a new prefix into the database")
     return False
Example #18
0
 def test_set_moe(self):
     """
     Test Storage.set_moe method
     :return:
     """
     self.now = time.time()
     with patch('time.time', self.fake_time):
         storage = Storage()
         self.assertRaises(StorageKeyError, storage.set_moe, 1, {'moe': 2})
         storage.set(1, 'one')
         storage.set(2, 'two', 2)
         self.assertEqual(None, storage.set_moe(1, 2))
         self.assertEqual(None, storage.set_moe(2, None))
         self.now += 3
         self.assertRaises(StorageKeyError, storage.get, 1)
         self.assertEqual('two', storage.get(2))
Example #19
0
    def test_set(self):
        """
        Test Storage.set basic functionality.
        :return:
        """
        storage = Storage()
        items_to_add = {
            'test_s': 'hello',
            'test_list': [1, 2, 'three'],
            'test_dict': {
                1: 'one',
                2: 'two'
            }
        }
        moes = {'test_list': time.time() + 5}
        for key in items_to_add.keys():
            storage.set(key, items_to_add[key], moes.get(key))

        self.assertEqual(items_to_add, storage._keys_dict,
                         'Added keys are wrong.')
        self.assertEqual(moes, storage._moe_dict, 'Added moes are wrong.')
Example #20
0
    def __init__(self, storage=None):
        """
        :param storage: Storage object or None for creating it automatically
        :param gc: enable garbage collector in createt Storage
        :param file_prefix: prefix for file names for storage saving/loading,
            set None to disable saving/loading
        """
        if storage is None:
            storage = Storage()

        self.storage = storage
        self.astonished = False
Example #21
0
    def test_delete(self):
        '''
        Test Storage.delete basic functionality (without ttl).
        :return:
        '''
        storage = Storage()
        self.assertEqual(0, storage.delete([]))
        keys_to_set = {
            '1': 'hello',
            '2': 'bye',
            '3': [1, 2, 'three'],
            '4': {
                1: 'one',
                2: 'two'
            }
        }
        keys_to_delete = ['2', '4', '60']
        for key in keys_to_set:
            storage.set(key, keys_to_set[key])
        num_deleted = storage.delete(keys_to_delete)

        true_num_deleted = 0
        for key in keys_to_delete:
            if key in keys_to_set:
                keys_to_set.pop(key)
                true_num_deleted += 1
        self.assertEqual(true_num_deleted, num_deleted,
                         'Num of deleted keys is wrong.')
        self.assertEqual(keys_to_set, storage._keys_dict,
                         'Remaining keys are wrong')
Example #22
0
    def test_get(self):
        """
        Test Storage.get basic functionality (without ttl).
        :return:
        """
        storage = Storage()
        keys_to_set = {
            '1': 'hello',
            '2': 'bye',
            '3': [1, 2, 'three'],
            '4': {
                1: 'one',
                2: 'two'
            }
        }
        for key in keys_to_set.keys():
            storage.set(key, keys_to_set[key])

        values = [storage.get(key) for key in keys_to_set.keys()]
        true_values = [keys_to_set[key] for key in keys_to_set.keys()]
        self.assertEqual(true_values, values)
        self.assertRaises(StorageKeyError, storage.get, '0')
Example #23
0
 def test_set_with_get(self):
     """
     Test Storage.set with 'get' option
     :return:
     """
     storage = Storage()
     storage.set('1', 1)
     self.assertEqual(1, storage.set('1', 2, get=True),
                      "Should return previous value")
     self.assertEqual(2, storage.get('1'), 'Should get new value')
     self.assertEqual(None, storage.set('2', 1, get=True),
                      "Should return None as there was no key '2'")
def test_search_for_key(tmpdir, keyword, emotion):
    db_path = str(tmpdir.mkdir("database"))
    db_name = "tmp_db"
    stor = Storage(db_name, db_path, "tmp_table")

    stor.create_new_entry("tmp_table", "'dissapointing'", "'sad'")
    stor.create_new_entry("tmp_table", "'provoked'", "'angry'")
    stor.create_new_entry("tmp_table", "'inspired'", "'happy'")

    res = stor.search_for_keyword(db_path, db_name, "tmp_table", keyword)

    assert res == [(keyword, emotion)]
Example #25
0
 def update(self):
     status = self._check_updates()
     logging.debug(f"Status: {status}")
     if status == "ok":
         return "Already up to date."
     elif status == "failed":
         return "API call failed. Can't check for updates. Please try later"
     else:
         logging.debug("Waiting for asyncio loop")
         loop = asyncio.new_event_loop()
         asyncio.set_event_loop(loop)
         results = loop.run_until_complete(self._fetch_data())
         updated_dict = {"currencies": {}}
         min_date = ""
         for result in results:
             currency_code, response = result
             updated_dict["currencies"][currency_code] = {}
             updated_dict["currencies"][currency_code]["rates"] = {}
             if response:
                 logging.debug(json.loads(response).get("rates"))
                 content = json.loads(response)
                 updated_dict["currencies"][currency_code]["rates"] = content[
                     "rates"
                 ]
                 if (
                     currency_code
                     not in updated_dict["currencies"][currency_code]["rates"]
                 ):  # API workaround - each currency returns rates including base currency itsefl, except EUR
                     updated_dict["currencies"][currency_code]["rates"][
                         currency_code
                     ] = 1.0
                 if min_date == "" or min_date > content["date"]:
                     min_date = content["date"]
         updated_dict["last_update"] = min_date
         logging.debug(updated_dict)
         storage = Storage(storage_name)
         storage.update_storage(updated_dict)
         return "Updated successfully"
Example #26
0
def storage(request) -> Storage:
    storage_type: Literal['sql', 'json'] = request.param

    # We can conveniently sneak in a test case right here
    with pytest.raises(RuntimeError):
        # This should fail as we have not previously
        # initialized the storage
        # for any of the supported storage types
        Storage()

    store = Storage(storage_type)
    store.db.create()

    yield store

    store.db.destroy()

    # We also need to reset the __interned cache after each run
    # Double underscore variables get namespace shifted
    # so we need to do a force lookup to find their new name
    attr_name = "__interned"
    cache_attr = [attr for attr in dir(Storage) if attr_name in attr][0]
    setattr(Storage, cache_attr, {})
Example #27
0
    def test_read_not_exists(self):
        work_dir = SetupTest.ensure_clean_work_dir()
        storage_path = os.path.join(work_dir, 'file_must_not_exists.yaml')
        self.assertFalse(os.path.exists(storage_path))

        p = Storage()
        p.set_file(storage_path)

        p.load()
Example #28
0
class TestGarbageCollector(unittest.TestCase):
    def setUp(self) -> None:
        self.storage = Storage()
        self.gc = StorageGarbageCollector(self.storage)
        self.now = time.time()

        def fake_time():
            return self.now

        self.fake_time = fake_time

    def test_one_key(self):
        """
        Test expiration works properly
        for storage with one key
        :return:
        """
        self.now = time.time()
        with patch('time.time', self.fake_time):
            self.storage.set('1', 'one', moe=self.now + 1)
            self.now += 2
            self.gc.expire_random()
            self.assertRaises(StorageKeyError, self.storage.get, '1')

    def test_many_expired_keys(self):
        """
        Test expiration of many keys simultaneously
        :return:
        """
        self.now = time.time()
        with patch('time.time', self.fake_time):
            for i in range(20):
                self.storage.set(i, i, moe=self.now + 1)
            self.now += 2
            self.gc.expire_random()
            for i in range(20):
                self.assertRaises(StorageKeyError, self.storage.get, i)
Example #29
0
    def test_keys(self):
        """
        Test Storage.keys basic functionality (without ttl)
        with redis-style pattern matching.
        :return:
        """
        storage = Storage()
        keys_to_set = {
            '1': 'hello',
            '2': 'bye',
            '3': [1, 2, 'three'],
            '4': {
                1: 'one',
                2: 'two'
            },
            'a': 1,
            'aa': 2,
            'abc': 3,
            'hello': 4
        }
        for key in keys_to_set:
            storage.set(key, keys_to_set[key])

        pattern_answers = {
            '?': ['1', '2', '3', '4', 'a'],
            '*': list(keys_to_set.keys()),
            '[13]': ['1', '3'],
            '[^a]': ['1', '2', '3', '4'],
            '[1-3]': ['1', '2', '3'],
            '?[ae]*': ['aa', 'hello']
        }
        for pattern in pattern_answers:
            self.assertEqual(
                pattern_answers[pattern], storage.keys(pattern),
                f'For pattern "{pattern}" expected {pattern_answers[pattern]}.'
            )
    def push2storage(self, storage: Storage) -> list:
        """
          Input:
            * Storage Object
            * File Data Dict of original file in the format: {name: <FILENAME>, id: <FILE ID>}
          Output:
            * File name (updated file)

          This function updates a file to storage using the insights
          gained from the Watson API.
        """

        # Update the metadata of the file for each Box Skillcard, return the results for
        # debugging

        return storage.update_file({'cards': self.cards})