def test_read_config_valids(self):
     # Default config path
     self.assertIsNotNone(config_parser.read_config())
     # Overridden config path
     self.assertIsNotNone(
         config_parser.read_config(
             config_path=constants.DEFAULT_CONFIG_FILE_PATH))
 def test_get_username_invalids(self):
     config = config_parser.read_config()
     # None config
     self.assertIsNone(config_parser.get_username(config=None))
     # Empty username
     config['credentials']['username'] = ''
     self.assertIsNone(config_parser.get_username(config=config))
Example #3
0
def run(argv):
    """This function is the core-function using all the other components to
       do the expected work.

       This includes parsing the commandline, reading "config.ini", parsing
       the input-file and actual downloading of all the files.

       Called by ```run.py``` in base-dir.

    """
    # Parse arguments
    arg_parser = CLI()
    input_path, verbose = arg_parser.parse(argv[1:])  # argv[0] = script-name

    # Read config-file
    output_path = read_config()

    # Create and use input-parser
    parser = InputParser(input_path, output_path, verbose)

    # Create downloader
    downloader = Downloader(output_path, verbose)

    # Use downloader
    for url, filename in parser.get_url_targetname_pairs():
        downloader.download(url, filename)
Example #4
0
def sync_drive():
    while True:
        config = config_parser.read_config()
        verbose = config_parser.get_verbose(config=config)
        username = config_parser.get_username(config=config)
        destination_path = config_parser.prepare_destination(config=config)
        if username and destination_path:
            try:
                api = PyiCloudService(apple_id=username,
                                      password=utils.get_password_from_keyring(
                                          username=username))
                if not api.requires_2sa:
                    sync_directory(drive=api.drive,
                                   destination_path=destination_path,
                                   root=destination_path,
                                   items=api.drive.dir(),
                                   top=True,
                                   filters=config['filters'],
                                   remove=config_parser.get_remove_obsolete(
                                       config=config),
                                   verbose=verbose)
                else:
                    print('Error: 2FA is required. Please log in.')
            except exceptions.PyiCloudNoStoredPasswordAvailableException:
                print(
                    'password is not stored in keyring. Please save the password in keyring.'
                )
        sleep_for = config_parser.get_sync_interval(config=config)
        next_sync = (datetime.datetime.now() + datetime.timedelta(
            minutes=sleep_for)).strftime('%l:%M%p %Z on %b %d, %Y')
        print(f'Resyncing at {next_sync} ...')
        if sleep_for < 0:
            break
        time.sleep(sleep_for)
 def test_get_verbose_invalids(self):
     config = config_parser.read_config()
     config['settings']['verbose'] = None
     self.assertFalse(config_parser.get_verbose(config=config))
     del config['settings']['verbose']
     self.assertFalse(config_parser.get_verbose(config=config))
     del config['settings']
     self.assertFalse(config_parser.get_verbose(config=config))
    def test_non_existing_file(self):
        """File given does not exist. Nothing to parse from.

           This "non-existence" in this test is true with high-probability.

        """
        with self.assertRaises(UtilsFileDoesNotExistError):
            config = read_config(self.config_file_valid_format.name + '1')
 def test_get_sync_interval_valids(self):
     # Given sync interval
     config = config_parser.read_config()
     self.assertEqual(config['settings']['sync_interval'],
                      config_parser.get_sync_interval(config=config))
     # Default sync interval
     del config['settings']['sync_interval']
     self.assertEqual(constants.DEFAULT_SYNC_INTERVAL_SEC,
                      config_parser.get_sync_interval(config=config))
Example #8
0
 def setUp(self) -> None:
     self.config = config_parser.read_config(config_path=tests.CONFIG_PATH)
     self.filters = self.config['filters']
     self.root = tests.DRIVE_DIR
     self.destination_path = self.root
     os.makedirs(self.destination_path, exist_ok=True)
     self.service = data.PyiCloudServiceMock(data.AUTHENTICATED_USER, data.VALID_PASSWORD)
     self.drive = self.service.drive
     self.items = self.drive.dir()
     self.file_item = self.drive[self.items[4]]['Test']['Scanned document 1.pdf']
     self.file_name = 'Scanned document 1.pdf'
     self.local_file_path = os.path.join(self.destination_path, self.file_name)
 def test_prepare_destination_valids(self):
     config = config_parser.read_config()
     # Given destination
     actual = config_parser.prepare_destination(config=config)
     self.assertEqual(os.path.abspath(config['settings']['destination']),
                      actual)
     self.assertTrue(os.path.exists(actual))
     self.assertTrue(os.path.isdir(actual))
     os.rmdir(actual)
     # Default destination
     del config['settings']['destination']
     actual = config_parser.prepare_destination(config=config)
     self.assertEqual(os.path.abspath(constants.DEFAULT_DRIVE_DESTINATION),
                      actual)
     self.assertTrue(os.path.exists(actual))
     self.assertTrue(os.path.isdir(actual))
     os.rmdir(actual)
 def test_get_remove_obsolete_valids(self):
     config = config_parser.read_config()
     config['settings']['remove_obsolete'] = True
     self.assertTrue(config_parser.get_remove_obsolete(config=config))
     del config['settings']['remove_obsolete']
     self.assertFalse(config_parser.get_remove_obsolete(config=config))
 def test_get_username_valids(self):
     config = config_parser.read_config()
     # Given username
     self.assertEqual(config['credentials']['username'],
                      config_parser.get_username(config=config))
 def test_read_config_invalids(self):
     # Invalid config path
     self.assertIsNone(
         config_parser.read_config(config_path='invalid/path'))
     # None config path
     self.assertIsNone(config_parser.read_config(config_path=None))
    def test_existing_invalid_file_a(self):
        """File given exists and is an invalid config (section output missing).

        """
        with self.assertRaises(ConfigParserParseErrorSection):
            config = read_config(self.config_file_invalid_format_a.name)
    def test_existing_invalid_file_c(self):
        """File given exists and is an invalid config (parser fails).

        """
        with self.assertRaises(ConfigParserParseError):
            config = read_config(self.config_file_invalid_format_c.name)
    def test_existing_invalid_file_b(self):
        """File given exists and is an invalid config (key TARGET_DIR missing).

        """
        with self.assertRaises(ConfigParserParseErrorKey):
            config = read_config(self.config_file_invalid_format_b.name)
 def test_get_verbose_valids(self):
     config = config_parser.read_config()
     self.assertEqual(config['settings']['verbose'],
                      config_parser.get_verbose(config=config))
     config['settings']['verbose'] = True
     self.assertTrue(config_parser.get_verbose(config=config))
 def test_smtp_password_invalids(self):
     config = config_parser.read_config()
     self.assertIsNone(config_parser.get_smtp_password(config=None))
    def test_existing_valid_file(self):
        """File given exists and is a valid config.

        """
        config = read_config(self.config_file_valid_format.name)
        self.assertEqual(config, 'example_out')