예제 #1
0
class App:
    def __init__(self, CONSTANTS):
        for name in CONSTANTS:
            setattr(self, name, CONSTANTS[name])
        self.meetup = MeetupRSS(self.MEETUP_GROUP_NAME)
        self.git = GitClient(self.REPO_PATH, self.REPO_AUTHOR_NAME,
                             self.REPO_AUTHOR_EMAIL)
        self.writer = FileWriter(self.EVENT_POSTS_DIR)
        self.db = FileDB(self.EVENT_POSTS_DIR)

    def sync_event_file(self, event):
        renamed = False

        file_info = self.db.find_event(event['id'])
        if file_info:
            if file_info['title'] != event['title']:
                renamed = True
                (file_date,
                 title) = FileWriter.split_filename(file_info['filename'])
                filename = FileWriter.create_filename(file_date,
                                                      event['title'])
            else:
                filename = file_info['filename']
        else:
            filename = FileWriter.create_filename(event['time'],
                                                  event['title'])

        if renamed:
            self.writer.delete(file_info['filename'])

        self.writer.write(MeetupEvent(event), filename)

    def sync_git(self):
        if self.git.status:
            self.git.stage_all()
            self.git.commit()
            try:
                self.git.push()
                return True
            except Exception as e:
                self.git.remove_head_commit()
                self.git.pull()
                raise e
        else:
            return False

    def poll_and_update(self):
        events = self.meetup.events

        if events:
            self.git.reset_hard()
            self.git.pull()

            for event in events:
                event = self.meetup.parse_event(event)
                self.sync_event_file(event)

            self.sync_git()
예제 #2
0
class FileWriterTest(SiteBotTestCase):
    def setUp(self):
        super(FileWriterTest, self).setUp()
        self.test_dir = tempfile.mkdtemp()
        self.subject = FileWriter(self.test_dir)

    def tearDown(self):
        super(FileWriterTest, self).tearDown()
        shutil.rmtree(self.test_dir)

    def test_create_filename(self):
        file_date = date(2017, 1, 20)
        file_title = 'A Test'
        self.assertEqual(FileWriter.create_filename(file_date, file_title),
                         '2017-01-20-a-test.md')

    def test_split_filename(self):
        self.assertEqual(
            FileWriter.split_filename('2017-01-30-another-event.md'),
            (date(2017, 1, 30), 'another-event'))

    def test_base_path(self):
        self.assertEqual(self.subject.base_path, self.test_dir)

    def test_write(self):
        event = FakeEvent()
        expected_file = '2017-01-01-the-event.md'
        self.subject.write(event, expected_file)
        written = open(path.join(self.test_dir, expected_file))
        self.assertEqual(written.read(), 'event_text')
        written.close()

    def test_delete(self):
        doomed_file_name = '2017-01-01-doomed.md'
        with open(path.join(self.test_dir, doomed_file_name), 'w') as f:
            f.write('Doomed')

        self.assertTrue(path.exists(path.join(self.test_dir,
                                              doomed_file_name)))
        self.subject.delete(doomed_file_name)
        self.assertFalse(
            path.exists(path.join(self.test_dir, doomed_file_name)))

    def test_delete_nonexistant_file(self):
        doomed_file_name = '2017-01-01-doomed-ghost.md'
        self.assertFalse(
            path.exists(path.join(self.test_dir, doomed_file_name)))
        try:
            self.subject.delete(doomed_file_name)
        except:
            self.fail('attempting to delete a nonexistant file should not ' +
                      'raise an exception')