Example #1
0
    def check_media(self):
        """Check media (will rebuild missing LaTeX files)"""
        with cd(self.col.media.dir()):
            click.echo('Checking media DB ... ', nl=False)
            output = self.col.media.check()
            click.echo('done!')

            if len(output.missing) + len(output.unused) == 0:
                click.secho('No unused or missing files found.', fg='white')
                return

            for file in output.missing:
                click.secho(f'Missing: {file}', fg='red')

            if len(output.missing) > 0 \
                    and click.confirm('Render missing LaTeX?'):
                out = self.col.media.render_all_latex()
                if out is not None:
                    nid, _ = out
                    click.secho(f'Error prosessing node: {nid}', fg='red')

                    if click.confirm('Review note?'):
                        note = Note(self, self.col.getNote(nid))
                        note.review()

            for file in output.unused:
                click.secho(f'Unused: {file}', fg='red')

            if len(output.unused) > 0 \
                    and click.confirm('Delete unused media?'):
                for file in output.unused:
                    if os.path.isfile(file):
                        os.remove(file)
Example #2
0
    def sync(self):
        """Sync collection to AnkiWeb"""
        if self.pm is None:
            return

        hkey = self.pm.sync_key()
        hostNum = self.pm.sync_shard()
        if not hkey:
            click.echo('No sync auth registered in profile')
            return

        # Initialize servers and sync clients
        server = RemoteServer(hkey, hostNum=hostNum)
        main_client = Syncer(self.col, server)

        # Perform main sync
        try:
            click.echo('Syncing deck ... ', nl=False)
            ret = main_client.sync()
        except Exception as e:
            if 'sync cancelled' in str(e):
                server.abort()
            click.secho('Error during sync!', fg='red')
            click.echo(e)
            raise click.Abort()

        # Parse return value
        if ret == "noChanges":
            click.echo('done (no changes)!')
        elif ret == "success":
            click.echo('done!')
        elif ret == "serverAbort":
            click.echo('aborted!')
            return
        elif ret == "fullSync":
            click.echo('aborted!')
            click.secho('Full sync required!', fg='red')
            return
        else:
            click.echo('failed!')
            click.echo(f'Message: {ret}')
            return

        # Perform media sync
        try:
            debug_output = 'media=debug' in os.environ.get('RUST_LOG', '')

            with cd(self.col.media.dir()):
                if debug_output:
                    click.echo('Syncing media:')
                else:
                    click.echo('Syncing media ... ', nl=False)
                self.col.backend.sync_media(
                    hkey, f"https://sync{hostNum}.ankiweb.net/msync/")
                if not debug_output:
                    click.echo('done!')
        except Exception as e:
            if "sync cancelled" in str(e):
                return
            raise
Example #3
0
    def check_media(self):
        """Check media (will rebuild missing LaTeX files)"""
        import click
        from apy.utilities import cd

        with cd(self.col.media.dir()):
            click.echo('Checking media DB ... ', nl=False)
            nohave, unused, warnings = self.col.media.check()
            click.echo('done!')

            if not warnings + unused + nohave:
                click.secho('No unused or missing files found.', fg='white')
                return

            for warning in warnings:
                click.secho(warning, fg='red')

            for file in nohave:
                click.secho(f'Missing: {file}', fg='red')

            if unused:
                for file in unused:
                    click.secho(f'Unused: {file}', fg='red')

                if not click.confirm('Delete unused media?'):
                    return

                for file in unused:
                    if os.path.isfile(file):
                        os.remove(file)
Example #4
0
    def show_images(self):
        """Show in the fields"""
        images = []
        for html in self.n.values():
            images += _get_imgs_from_html_latex(html, self.a, self.n.model())
            images += _get_imgs_from_html(html)

        with cd(self.a.col.media.dir()):
            for file in images:
                view_cmd = cfg['img_viewers'].get(file.suffix[1:],
                                                  cfg['img_viewers_default'])
                subprocess.Popen(view_cmd + [file],
                                 stdout=subprocess.DEVNULL,
                                 stderr=subprocess.DEVNULL)
Example #5
0
    def sync(self):
        """Sync collection to AnkiWeb"""
        if self.pm is None:
            return

        auth = self.pm.sync_auth()
        if auth is None:
            return

        # Make sure database is saved first
        self.col.save(trx=False)

        # Perform main sync
        try:
            debug_output = 'anki::sync=debug' in os.environ.get('RUST_LOG', '')

            if debug_output:
                click.secho('Syncing deck:', fg='blue')
            else:
                click.echo('Syncing deck ... ', nl=False)

            self.col.sync_collection(auth)

            if not debug_output:
                click.echo('done!')
            else:
                click.echo('')
        except Exception as e:
            click.secho('Error during sync!', fg='red')
            click.echo(e)
            raise click.Abort()

        # Perform media sync
        try:
            debug_output = 'media=debug' in os.environ.get('RUST_LOG', '')

            with cd(self.col.media.dir()):
                if debug_output:
                    click.secho('Syncing media:', fg='blue')
                else:
                    click.echo('Syncing media ... ', nl=False)
                self.col.sync_media(auth)
                if not debug_output:
                    click.echo('done!')
        except Exception as e:
            if "sync cancelled" in str(e):
                return
            raise
Example #6
0
File: note.py Project: ckp95/apy
    def show_images(self):
        """Show in the fields"""
        images = []
        for html in self.n.values():
            images += self.get_lateximg_from_field(html)

        with cd(self.a.col.media.dir()):
            for file in images:
                if file.suffix == '.svg':
                    subprocess.Popen(['display', '-density', '300', file],
                                     stdout=subprocess.DEVNULL,
                                     stderr=subprocess.DEVNULL)
                else:
                    subprocess.Popen(['feh', file],
                                     stdout=subprocess.DEVNULL,
                                     stderr=subprocess.DEVNULL)
Example #7
0
File: note.py Project: Aylexx/apy
    def show_images(self):
        """Show in the fields"""
        images = []
        for val in self.n.values():
            source = val + "\n".join(self.get_lateximg_from_field(val))
            images += [
                Path(x['src'])
                for x in BeautifulSoup(source, 'html.parser').find_all('img')
            ]

        with cd(self.a.col.media.dir()):
            for file in images:
                if file.suffix == '.svg':
                    subprocess.Popen(['display', '-density', '300', file],
                                     stdout=subprocess.DEVNULL,
                                     stderr=subprocess.DEVNULL)
                else:
                    subprocess.Popen(['feh', file],
                                     stdout=subprocess.DEVNULL,
                                     stderr=subprocess.DEVNULL)