def test_safe_open(tmpdir):
    path = "data/file.txt"
    absolute_path = os.path.join(str(tmpdir), path)
    with safe_open(absolute_path, "a+") as file:
        content = "test"
        file.write(content)
        file.seek(0)
        assert file.read() == content
Exemplo n.º 2
0
    def __init__(self, history_file_path: str = absolute_path):
        """Initialize the History class.

        :param history_file_path: The file path
        """
        self.history_file = safe_open(history_file_path, "a+")
        content = self.history_file.read()
        self.history_fileeeds = content.split("\n")
Exemplo n.º 3
0
    def __init__(self,
                 include_old: Optional[bool] = False,
                 thread_count: Optional[int] = None):
        """Initialize the Agent class.

        :param include_old: Whether to include cached articles, defaults to False
        :param thread_count: An argument that enforces a certain thread count for this instance
        """
        self.include_old = bool(include_old)
        assert thread_count is None or thread_count >= 1
        self.thread_count = thread_count

        feeds_file = safe_open(absolute_path, "r")
        content = feeds_file.read()
        feeds_file.close()
        self.feeds = content.split("\n")
Exemplo n.º 4
0
    def __init__(self):
        """Initialize the class by configuring and connecting to the email client."""
        self.config = configparser.ConfigParser()

        # Initialize the configuration if it has not been set
        self.config.read(absolute_config_path)
        if len(self.config["DEFAULT"]) == 0:
            print("An email configuration has not been set. \
In order to deliver articles, we must connect to an email server. You \
will now be guided through configuring the email details:")
            # Set email sender information
            props = [
                ("host", "What is the email host", None, None),
                ("port", "What is the port", 465, None),
                ("email", "What is the email address", None, None),
                (
                    "password",
                    "What is the password (typing will be hidden)",
                    None,
                    True,
                ),
            ]
            for label, question, default, should_hide in props:
                value = click.prompt(question, default, should_hide)
                self.config["DEFAULT"][label] = str(value)

            # Get kindle email address
            click.confirm(
                f"""We will need your kindle email address to send articles. \
Please:
\n1. Copy the kindle email address for delivery purposes.
2. Navigate to Preferences > Personal Document Settings and add `{self.config["DEFAULT"]["email"]}`
\nLearn more at https://amzn.to/3nVmuBy. Please confirm that we may open Amazon.com \
so you can retrieve your kindle email.""",
                abort=True,
            )
            click.launch("https://amazon.com/mycd")

            kindle_email = click.prompt("Please enter the kindle email below")
            self.config["DEFAULT"]["kindle_email"] = kindle_email

            with safe_open(absolute_config_path, "w") as configfile:
                self.config.write(configfile)
Exemplo n.º 5
0
    def create_book_attachment(book_path: str) -> MIMEBase:
        """Create a MIMEBase attachment containing the book file.

        :param book_path: The path to the epub version of the book.
        :return: The MIMEBase instance to be attached using
            the `message.attach` method.
        """
        mobi_path = KindleDelivery.convert_issue_to_mobi(book_path)

        with safe_open(mobi_path, "rb") as attachment:
            # The content type "application/octet-stream" means that it is a binary file
            part = MIMEBase("application", "octet-stream")
            part.set_payload(attachment.read())

        # Encode to base64
        encoders.encode_base64(part)

        # Add header
        part.add_header("Content-Disposition",
                        "attachment; filename= issue.mobi")
        return part