Esempio n. 1
0
def test_build_message_success(monkeypatch):
    def fake_request():
        return FakeResponse()

    monkeypatch.setattr('utils.make_request', fake_request)
    result = utils.build_message()
    assert result["success"] is True
Esempio n. 2
0
def test_build_message_failure(monkeypatch):
    def fake_request():
        return FakeResponse(status=400, body='not allowed here!')

    monkeypatch.setattr('utils.make_request', fake_request)
    result = utils.build_message()
    assert result["success"] is False
    assert result["error"] == "not allowed here!"
Esempio n. 3
0
    def handle_list(self, conn):
        nodes = [topic.path for topic in self.trie.find_all_nodes()]
        connection_info = self.connections[conn]
        serialization = connection_info.serialization
        if len(nodes) == 0:
            nodes = "No topics yet"
        message = build_message(serialization, OperationType.LIST.value, None,
                                nodes)
        message_bytes = general_encode(serialization, message)

        connection_info.send(message_bytes)
Esempio n. 4
0
    def get_message(self, content=None, context=None):
        """
            Renders the message with the supplied content.
            Uses self.content if no content is specified
            Uses self.example_data if not context is specified
        """
        if not content:
            content = self.content

        if not context:
            context = self.example_data

        return build_message(content, context)
Esempio n. 5
0
    def send_email(self,
                   mail_to,
                   context,
                   title=None,
                   attachments=None,
                   reply_to=None):
        """
            Sends an email based on this template to the passed list of emails
            and using the passed context dictionary
        """

        if not settings.EMAIL_REPLY_TO:
            raise NameError('Please define the EMAIL_REPLY_TO '
                            'variable in settings')

        if reply_to is not None:
            reply = reply_to
        else:
            reply = settings.EMAIL_REPLY_TO

        cc_list = []
        bcc_list = []
        for cc in self.copyemail_set.all():
            if cc.bcc:
                bcc_list.append(cc.address)
            else:
                cc_list.append(cc.address)

        if not title:
            title = self.title

        m = EmailMessage(subject=title,
                         body=build_message(self.content, context),
                         to=mail_to,
                         cc=cc_list,
                         bcc=bcc_list,
                         headers={'Reply-To': reply},
                         attachments=attachments)
        m.send()
Esempio n. 6
0
    def request_to_enter(self, topic_node, message):
        self.clock_adjustments()

        timestamp = message["timestamp"]
        content = message["content"]
        serialization = message["serialization"]
        message_to_publish = build_message(serialization,
                                           OperationType.PUBLISH.value,
                                           topic_node.topic.path, content,
                                           timestamp)

        enter_message = build_message_between_brokers(
            OperationType.ENTER.value, self.clock, message_to_publish,
            self.broker_id)
        request = Request(OperationType.ENTER.value, self.clock,
                          enter_message["message_id"], self.broker_id,
                          message_to_publish)

        self.request_queue.append(request)
        self.sort_queue()

        message_bytes = general_encode(self.serialization, enter_message)
        send_to_all_brokers(self.brokers_dict.values(), message_bytes)
        print(f"Sent enter for {enter_message['message_id']}")
Esempio n. 7
0
 def cancel(self):
     message = build_message(self.serialization_type,
                             OperationType.CANCEL.value, self.topic, "")
     message_bytes = general_encode(self.serialization_type, message)
     send_message_packed_with_size(self.queue_socket, message_bytes)
     self.subscribed = False
Esempio n. 8
0
 def list(self):
     message = build_message(self.serialization_type,
                             OperationType.LIST.value, None, "")
     message_bytes = general_encode(self.serialization_type, message)
     send_message_packed_with_size(self.queue_socket, message_bytes)
Esempio n. 9
0
 def subscribe(self):
     message = build_message(self.serialization_type,
                             OperationType.SUBSCRIBE.value, self.topic, "")
     message_bytes = general_encode(self.serialization_type, message)
     send_message_packed_with_size(self.queue_socket, message_bytes)
     self.subscribed = True
Esempio n. 10
0
 def push(self, value):
     message = build_message(self.serialization_type,
                             OperationType.PUBLISH.value, self.topic, value)
     message_bytes = general_encode(self.serialization_type, message)
     send_message_packed_with_size(self.queue_socket, message_bytes)
    def report_changes(self, data, data_html, colnames):
        """
        Determine if any jobs have been added or removed since the last check. Report additions or deletions
        by print report or email as specified.

        :param data: jobs data returned from self.scrape()
        :param data_html: jobs HTML data returned from self.scrape()
        :param colnames: column names of jobs data
        """
        adds, adds_html, deletes = utils.find_adds_deletes(
            filepath=self.filepath,
            data=data,
            data_html=data_html,
            colnames=colnames)

        # set these to None in case they don't get defined later
        adds_table, adds_table_html, deletes_table, deletes_table_html = None, None, None, None

        # print the added and deleted jobs if print_report == True
        if adds:
            adds_table = utils.create_text_table(adds, colnames)
            adds_table_html = utils.create_html_table(adds_html, colnames)
            if self.print_report and self.report_adds:
                utils.print_table(adds_table,
                                  company_name=self.company_name,
                                  message_start="New jobs posted")

        if deletes:
            deletes_table = utils.create_text_table(deletes, colnames)
            deletes_table_html = utils.create_html_table(
                deletes, colnames)  # don't need links deletes table
            if self.print_report and self.report_deletes:
                utils.print_table(deletes_table,
                                  company_name=self.company_name,
                                  message_start="Jobs removed")

        # if there were changes that need reporting and email_report == True, send an email
        needs_report = ((self.report_adds and adds)
                        or (self.report_deletes and deletes))
        if self.email_report and needs_report:
            service = utils.establish_service()

            # build the message text
            email_adds = False
            email_deletes = False

            if self.report_adds and adds:
                email_adds = True

            if self.report_deletes and deletes:
                email_deletes = True

            text_msg, html_msg = utils.build_message(
                adds_table, adds_table_html, deletes_table, deletes_table_html,
                self.company_name, email_adds, email_deletes)

            # include link to careers page at bottom of email if available
            if (self.careers_link is not None) and (self.careers_link != ""):
                html_msg += '\n<p></p>\n<p>Careers page: <a href="' + self.careers_link + '">' + self.careers_link + \
                            '</a></p>\n'
                text_msg += '\n\nCareers page: ' + self.careers_link + '\n'

            # create and send message using Gmail API
            msg = utils.create_message(self.sender, self.to, self.subject,
                                       text_msg, html_msg)
            utils.send_message(service, user_id="me", message=msg)
Esempio n. 12
0
def test_build_message_failure(response):
    result = build_message(response(400, "not allowed here!"))
    assert result["success"] is False
    assert result["error"] == "not allowed here!"
Esempio n. 13
0
def test_build_message_success(response):
    result = build_message(response())
    assert result["success"] is True
def main():
    """Main program loop."""

    global output_dir
    global log_level

    parser = argparse.ArgumentParser(description="DocWriter Usage information")
    parser.add_argument(
        "files",
        nargs="+",
        help="list of source files to parse, wildcards are allowed",
    )
    parser.add_argument(
        "-t",
        "--title",
        metavar="T",
        help="set project title, as in '-t \"My Project\"'",
    )
    parser.add_argument(
        "-o",
        "--output",
        metavar="DIR",
        required=True,
        help="set output directory, as in '-o mydir'",
    )
    parser.add_argument(
        "-p",
        "--prefix",
        metavar="PRE",
        help="set documentation prefix, as in '-p ft2'",
    )
    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        "-q",
        "--quiet",
        help="run quietly, show only errors",
        action="store_true",
    )
    group.add_argument("-v",
                       "--verbose",
                       help="increase output verbosity",
                       action="store_true")
    args = parser.parse_args()

    # process options
    project_title = "Project"
    project_prefix = None
    output_dir = None

    if args.title:
        project_title = args.title

    if args.output:
        utils.output_dir = args.output

    if args.prefix:
        project_prefix = args.prefix

    if args.quiet:
        log_level = logging.ERROR

    if args.verbose:
        log_level = logging.DEBUG

    # set up the logger
    setup_logger(level=log_level)
    log = logging.getLogger("docwriter")

    # check all packages
    status = check.check()
    if status != 0:
        sys.exit(3)

    utils.check_output()

    # create context and processor
    source_processor = sources.SourceProcessor()
    content_processor = content.ContentProcessor()

    # retrieve the list of files to process
    file_list = utils.make_file_list(args.files)
    for filename in file_list:
        source_processor.parse_file(filename)
        content_processor.parse_sources(source_processor)

    # process sections
    content_processor.finish()

    # clean up directory
    log.info("Cleaning output directory")
    utils.clean_markdown_dir()

    formatter = tomarkdown.MdFormatter(content_processor, project_title,
                                       project_prefix)

    # build the docs
    utils.build_message()

    formatter.toc_dump()
    formatter.index_dump()
    formatter.section_dump_all()