Example #1
0
    def copy_story(self, story, library, new_name):
        if "{}: {}".format(library, new_name) in self.stories:
            Snackbar("Cannot create story. '{}' already exists in {}.".format(new_name, library)).show()
            return

        if self.stories[story]['library'] == library:
            source_story_file = Path(self.stories[story]['story'].story_config_file)
            dest_story_file = source_story_file.parent.joinpath("{}.ini".format(new_name))
            dest_story_file.write_bytes(source_story_file.read_bytes())
            new_story = self.app.libraries[library].add_new_story(new_name)
            if new_story is None:
                return None

            self.add_story(library, new_story)
            self.set_story = new_story
            self.set_library = library
            self.setup_settings_panel()
        else:
            source_story_file = Path(self.stories[story]['story'].story_config_file)
            dest_story_file = self.app.library_dir.joinpath(library).joinpath("{}.ini".format(new_name))
            dest_story_file.write_bytes(source_story_file.read_bytes())
            new_story = self.app.libraries[library].add_new_story(new_name)
            if new_story is None:
                return None

            self.add_story(library, new_story)
            self.set_story = new_story
            self.set_library = library
            self.setup_settings_panel()

        self.setup_settings_panel()
        self.app.story_title_screen()
Example #2
0
def save_mail(index, destdir, custom_filters=None):
    destdir = Path(destdir)
    custom_filters = custom_filters or []

    mail_file = Path(str(index))
    if mail_file.exists():
        os.unlink(str(mail_file))
    if not mail_command([
            "save {index}".format(index=index),
            'delete {index}'.format(index=index)
    ]):
        logging.error("Cannot save first message.")
        return False
    if not mail_file.exists():
        logging.error("Cannot save first message.")
        return False
    try:
        try:
            eml = email.parser.BytesHeaderParser().parsebytes(
                mail_file.read_bytes())
        except AttributeError:  # pragma: no cover -- py2 compatibility
            eml = email.parser.Parser().parsestr(mail_file.read_bytes().decode(
                'utf-8', 'replace'),
                                                 headersonly=True)
        for command in custom_filters:
            if not run_custom_filter(command, mail_file):
                os.unlink(str(mail_file))
                break
        else:
            name = eml['Subject'] or 'mail.{0}.{1}'.format(
                os.getpid(),
                datetime.datetime.now().isoformat())
            name = clckwrkbdgr.fs.make_valid_filename(name)
            destfile = clckwrkbdgr.fs.make_unique_filename(destdir / name)
            destdir.mkdir(parents=True, exist_ok=True)
            os.rename(str(mail_file), str(destfile))
    except:
        logging.exception(
            'Failed to process saved mail file {0}. Backup of original mbox is stored at {1}'
            .format(mail_file, MAILBOX_BAK))
        return False
    return True
site_module_path = Path(
    subprocess.check_output(
        ['python3', '-c',
         'import site; print(site.__file__)']).decode().strip())

UNPATCHED_PYTHON_HISTORY_CODE = b"""\
            history = os.path.join(os.path.expanduser('~'),
                                   '.python_history')
"""

PATCHED_PYTHON_HISTORY_CODE = b"""\
            history = os.path.join(os.path.expanduser('~/.state'),
                                   '.python_history')
"""

if UNPATCHED_PYTHON_HISTORY_CODE not in site_module_path.read_bytes():
    context.done()

if not commands.has_sudo_rights():
    trace.info('Have no sudo rights, skipping.')
    context.done()

diff = subprocess.Popen(
    ['diff', '-u', str(site_module_path), '-'],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE)
patch, _ = diff.communicate(site_module_path.read_bytes().replace(
    UNPATCHED_PYTHON_HISTORY_CODE, PATCHED_PYTHON_HISTORY_CODE))
diff.wait()

trace.error(