示例#1
0
    def test_pypandoc_compatibility(self):
        """
        This test is testing if org-mode can be converted to html via pypandoc
        """

        from_formats, to_formats = pypandoc.get_pandoc_formats()
        self.assertTrue('org' in from_formats)
        self.assertTrue('html5' in to_formats)
示例#2
0
    def test_pypandoc_compatibility(self):
        """
        This test is testing if org-mode can be converted to html via pypandoc
        """

        from_formats, to_formats = pypandoc.get_pandoc_formats()
        self.assertTrue(u'org' in from_formats)
        self.assertTrue(u'html5' in to_formats)
示例#3
0
def convert(ctx, name, destination_format, destination_file, list_docs,
            formats):
    """Convert to destination_format and print to stdout or save to file if provided."""
    # yew = ctx.obj["YEW"]
    if formats or not destination_format:
        formats = pypandoc.get_pandoc_formats()
        click.echo("Input formats:")
        for f in formats[0]:
            click.echo("\t" + f)
        click.echo("Output formats:")
        for f in formats[1]:
            click.echo("\t" + f)
        sys.exit(0)

    docs = shared.get_document_selection(ctx, name, list_docs)
    if not docs:
        sys.exit(1)
    doc = docs[0]
    click.echo(doc.name)
    click.echo(doc.kind)
    click.echo(destination_format)

    if destination_format in ["docx", "pdf", "odt"]:
        destination_file = "{}.{}".format(slugify(doc.name),
                                          destination_format)

    if destination_file:
        dest = pypandoc.convert(
            doc.get_content(),
            format=doc.kind,
            to=destination_format,
            outputfile=destination_file,
        )
        click.echo(destination_file)
    else:
        dest = pypandoc.convert_text(doc.get_content(),
                                     format=doc.kind,
                                     to=destination_format)
        click.echo(dest)
    sys.stdout.flush()
import os, datetime
from django.template import loader, Template, Context
from django.conf import settings

from django.utils import html

from asena.views import token_protect

import logging, pprint
logger = logging.getLogger(__name__)

try:
    import pypandoc
    pypandoc.get_pandoc_formats()
    PANDOC_AVAILABLE=True
except OSError as e:
    logger.warn(e)
    PANDOC_AVAILABLE=False
    
PANDOC_AVAILABLE=False

DEFAULT_DATE_FORMAT='%Y%m%d_%H%M%S'

def _get_default_filename():
    now = datetime.datetime.now()
    snow = now.strftime(DEFAULT_DATE_FORMAT)
    return 'resume_%s'%(snow)

def write_rst(request, rst_template, context, filename=None):
    if not filename:
        filename = _get_default_filename()
示例#5
0
def create_header():
    header = "<name>SOG Atrium Backup</name>\n"
    header += "<description>Migrated Atrium Data importable to XWiki</description>\n"
    header += "<licence>Not public</licence>\n"
    header += "<author>XWiki.Atrium</author>\n"

    return header

def create_page_files(pages):
    for page in pages:
        # check for folders on path
        formal_path = page.build_prefixed_path().replace(".", "/")
        physical_path = output_folder_path + "/" + formal_path
        print("Creating XWiki Page %s" % formal_path)
        if not os.path.exists(physical_path):
            os.makedirs(physical_path)

        xml_tree_string = page.build_xml_content_file()
        f = open(physical_path + "/WebHome.xml", "w")
        f.write(xml_tree_string.decode('utf-8'))
        f.close()

if __name__ == "__main__":
    if not os.path.exists(output_folder_path):
        os.makedirs(output_folder_path)

    print(pypandoc.get_pandoc_formats()[1])
    initialize()
    convert_atrium_db_to_xar()
示例#6
0
def parse_args(sysargv):
    pandocInputFormats, pandocOutputFormats = pypandoc.get_pandoc_formats()
    parser = argparse.ArgumentParser(
        prog='easyblogger',
        description="Easily manage posts on Blogger blogs",
        fromfile_prefix_chars='@')
    parser.add_argument("-i",
                        "--clientid",
                        help="Your API Client id",
                        default="132424086208.apps.googleusercontent.com")
    parser.add_argument("-s",
                        "--secret",
                        help="Your API Client secret",
                        default="DKEk2rvDKGDAigx9q9jpkyqI")
    parser.add_argument(
        "-v",
        "--verbose",
        help="verbosity(log level) - default CRITICAL",
        choices=["INFO", "DEBUG", "WARNING", "ERROR", "CRITICAL"],
        type=str.upper,
        default="CRITICAL")
    group = parser.add_mutually_exclusive_group()
    group.add_argument("--blogid", help="Your blog id")
    group.add_argument("--url", help="Your blog url")

    subparsers = parser.add_subparsers(help="sub-command help", dest="command")

    get_parser = subparsers.add_parser("get", help="list posts")
    group = get_parser.add_mutually_exclusive_group()
    group.add_argument("-p", "--postId", help="the post id")
    group.add_argument("-l",
                       "--labels",
                       help="comma separated list of labels",
                       type=toUnicode)
    group.add_argument("-q", "--query", help="search term", type=toUnicode)
    group.add_argument("-u",
                       help="the full post url",
                       metavar="URL",
                       type=toUnicode)
    output_format = get_parser.add_mutually_exclusive_group()
    output_format.add_argument("-f",
                               "--fields",
                               help="fields to output",
                               default="id,title,url")
    output_format.add_argument(
        "-d",
        "--doc",
        help="""Output as document - use one of the output
        formats supported by pandoc: """ + ", ".join(pandocOutputFormats))
    get_parser.add_argument(
        "-w",
        "--write-files",
        dest='tofiles',
        help="write output files (only used with --doc). " +
        "True if more than one post is retrieved",
        action="store_true")
    get_parser.add_argument("--legacy-frontmatter",
                            dest='legacyFrontmatter',
                            help="use legacy front matter format (deprecated)",
                            action="store_true")
    get_parser.add_argument("-c", "--count", type=int, help="count")

    post_parser = subparsers.add_parser("post", help="create a new post")
    post_parser.add_argument("-t",
                             "--title",
                             help="Post title",
                             type=toUnicode)
    post_parser.add_argument("-l",
                             "--labels",
                             help="comma separated list of labels",
                             type=toUnicode)
    post_parser.add_argument("--publish",
                             action="store_true",
                             help="Publish to the blog [default: false]")
    post_parser.add_argument("--date",
                             dest='publishDate',
                             help="Publish date (ISO8601) of the post - ex:" +
                             "2018-01-01T10:00:00+05:30")
    post_input = post_parser.add_mutually_exclusive_group(required=True)
    post_input.add_argument("-c",
                            "--content",
                            help="Post content",
                            type=toUnicode)
    post_input.add_argument("-f",
                            "--file",
                            type=argparse.FileType('r'),
                            help="Post content - input file")
    post_parser.add_argument("--filters",
                             nargs="+",
                             default=[],
                             help="pandoc filters")
    post_parser.add_argument("--format",
                             help="Content format: " +
                             ", ".join(pandocInputFormats),
                             choices=pandocInputFormats,
                             default="html")
    delete_parser = subparsers.add_parser("delete", help="delete a post")
    delete_parser.add_argument("postIds", nargs="+", help="the post to delete")

    update_parser = subparsers.add_parser("update", help="update a post")
    update_parser.add_argument("postId", help="the post to update")
    update_parser.add_argument("-t", "--title", help="Post title")
    update_parser.add_argument(
        "--date",
        dest='publishDate',
        help="Publish date (ISO8601) of the post - ex:" +
        "2018-01-01T10:00:00+05:30")
    update_input = update_parser.add_mutually_exclusive_group()
    update_input.add_argument("-c", "--content", help="Post content")
    update_input.add_argument("-f",
                              "--file",
                              type=argparse.FileType('r'),
                              help="Post content - input file")

    update_parser.add_argument("--format",
                               help="Content format: " +
                               ", ".join(pandocInputFormats),
                               choices=pandocInputFormats,
                               default="html")

    update_parser.add_argument("-l",
                               "--labels",
                               help="comma separated list of labels",
                               type=toUnicode)

    update_parser.add_argument("--publish",
                               action="store_true",
                               help="Publish to the blog [default: false]")

    update_parser.add_argument("--filters",
                               nargs="+",
                               default=[],
                               help="pandoc filters")

    file_parser = subparsers.add_parser(
        "file", help="Figure out what to do from the input file")
    file_parser.add_argument("file",
                             nargs="+",
                             help="Post content - input file")

    config = os.path.expanduser("~/.easyblogger")
    if (os.path.exists(config)):
        sysargv = ["@" + config] + sysargv
    args = parser.parse_args(sysargv)
    verbosity = logging.getLevelName(args.verbose)
    # print(verbosity, logging.getLevelName(verbosity))
    coloredlogs.install(verbosity)
    if args.verbose != "critical":
        logger.setLevel(logging.INFO)
        logger.info("setting log level to: %s ", args.verbose)
    logger.setLevel(verbosity)

    logger.debug("Final args:")
    logger.debug(sysargv)

    return args
示例#7
0
def configure():
    """ Perform initial setup. """
    config = {
        'sync_interval': 300
    }
    generate_key = not click.confirm("Do you already have an API key for "
                                     "zotero-cli?")
    if generate_key:
        (config['api_key'],
         config['library_id']) = ZoteroBackend.create_api_key()
    else:
        config['api_key'] = click.prompt(
            "Please enter the API key for zotero-cli")
        config['library_id'] = click.prompt("Please enter your library ID")
    sync_method = select(
        [("local", "Local Zotero storage"),
            ("zotcoud", "Use Zotero file cloud"),
            ("webdav", "Use WebDAV storage")],
        default=1, required=True,
        prompt="How do you want to access files for reading?")
    if sync_method == "local":
        storage_dirs = tuple(find_storage_directories())
        if storage_dirs:
            options = [(name, "{} ({})".format(click.style(name, fg="cyan"),
                                               path))
                       for name, path in storage_dirs]
            config['storage_dir'] = select(
                options, required=False,
                prompt="Please select a storage directory (-1 to enter "
                       "manually)")
        if config.get('storage_dir') is None:
            click.echo(
                "Could not automatically locate a Zotero storage directory.")
            while True:
                storage_dir = click.prompt(
                    "Please enter the path to your Zotero storage directory",
                    default='')
                if not storage_dir:
                    storage_dir = None
                    break
                elif not os.path.exists(storage_dir):
                    click.echo("Directory does not exist!")
                elif not re.match(r'.*storage/?', storage_dir):
                    click.echo("Path must point to a `storage` directory!")
                else:
                    config['storage_dir'] = storage_dir
                    break
    elif sync_method == "webdav":
        while True:
            if not config.get('webdav_path'):
                config['webdav_path'] = click.prompt(
                    "Please enter the WebDAV URL")
            if not config.get('webdav_user'):
                config['webdav_user'] = click.prompt(
                    "Please enter the WebDAV user name")
                config['webdav_pass'] = click.prompt(
                    "Please enter the WebDAV password")
            try:
                test_resp = requests.get(
                    config['webdav_path'],
                    auth=(config['webdav_user'],
                          config['webdav_pass']))
            except requests.ConnectionError:
                click.echo("Invalid WebDAV URL, could not reach server.")
                config['webdav_path'] = None
                continue
            if test_resp.status_code == 501:
                break
            elif test_resp.status_code == 404:
                click.echo("Invalid WebDAV path, does not exist.")
                config['webdav_path'] = None
            elif test_resp.status_code == 401:
                click.echo("Bad credentials.")
                config['webdav_user'] = None
            else:
                click.echo("Unknown error, please check your settings.")
                config['webdav_path'] = None
                config['webdav_user'] = None
    config['sync_method'] = sync_method

    markup_formats = pypandoc.get_pandoc_formats()[0]
    config['note_format'] = select(zip(markup_formats, markup_formats),
                                   default=markup_formats.index('markdown'),
                                   prompt="Select markup format for notes")
    save_config(config)
    zot = ZoteroBackend(config['api_key'], config['library_id'], 'user')
    click.echo("Initializing local index...")
    num_synced = zot.synchronize()
    click.echo("Synchronized {} items.".format(num_synced))
示例#8
0
def store_file(filename,
               store_root=STORE_ROOT,
               store_dirfmt=STORE_DIRFMT,
               pandoc_fnfmt="{store_dir}/{stem}.md",
               add_lstat=True,
               add_hash='md5',
               verbose=2):

    inputfn_attrs = get_filename_attrs(filename)

    store_dir = os.path.join(store_root,
                             store_dirfmt).format(store_root=store_root,
                                                  **inputfn_attrs)

    if verbose and verbose > 0:
        print("Creating store %r for file %r" % (store_dir, filename))

    config = DEFAULT_METADATA.copy()
    archive_dir = os.path.join(store_dir, config['archive'])
    os.makedirs(archive_dir)
    config['inputfn'] = filename

    if add_hash:
        if add_hash is True:
            add_hash = HASH_METHOD
        config['hash_method'] = add_hash
        config['hash_hexdigest'] = hash_file(filename)

    if add_lstat:
        # (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)
        # Do not include atime, it is frequently updated (e.g. by search indexing).
        lstat = os.lstat(filename)
        stat_attrs = (
            'st_size',  # size, in bytes
            'st_ctime',
            'st_ctime_ns',  # time of creation (Windows) or change (Unix)
            'st_mtime',
            'st_mtime_ns',  # time of modificaton.
            'st_nlink',
            'st_dev',
            'st_ino',  # device, inode
            'st_mode',
            'st_uid',
            'st_gid',  # filemode, user id, group id,
            # 'st_flags', 'st_gen',  # user-defined flags, generation,
        )
        config['lstat'] = {a: getattr(lstat, a, 0)
                           for a in stat_attrs}  # list(lstat)

    try:
        with zipfile.ZipFile(filename, 'r') as zipfd:
            zipfd.extractall(archive_dir)
    except zipfile.BadZipfile:
        import tempfile
        with tempfile.TemporaryDirectory() as tempdir:
            tempfn = os.path.join(tempdir, inputfn_attrs['name'])
            print("Copying %r -> %r" % (filename, tempfn))
            shutil.copyfile(filename, tempfn)
            with zipfile.ZipFile(tempfn, 'r') as zipfd:
                zipfd.extractall(archive_dir)

    if pandoc_fnfmt:
        pandoc_supported_formats = pypandoc.get_pandoc_formats()  # from, to
        if isinstance(pandoc_fnfmt, str):
            pandoc_fnfmt = [pandoc_fnfmt]
        for output_fnfmt in pandoc_fnfmt:
            pandoc_fn = output_fnfmt.format(store_root=store_root,
                                            store_dir=store_dir,
                                            **inputfn_attrs)
            assert '.' in pandoc_fn
            output_format = pandoc_fn.rsplit('.')[-1]
            # if output_format not in pandoc_supported_formats[0]:
            #     print(" - Output format %r not supported by Pandoc!" % output_format)
            # elif inputfn_attrs['filetype'] not in pandoc_supported_formats[1]:
            #     print(" - Input  format %r not supported by Pandoc!" % inputfn_attrs['filetype'])
            # else:
            try:
                if verbose and verbose > 1:
                    print(" - Making %s file: %r -> %r" %
                          (output_format, filename, pandoc_fn))
                pypandoc.convert_file(filename,
                                      output_format,
                                      outputfile=pandoc_fn)
            except RuntimeError as exc:
                print(" - Could not convert with pandoc: %s" % (exc, ))
    metadata_fn = os.path.join(store_dir, FILE_METADATA_FN)

    with open(metadata_fn, 'w') as fp:
        yaml.dump(config, fp, default_flow_style=False)
示例#9
0
文件: utils.py 项目: ellisonbg/podoc
def get_pandoc_formats():
    import pypandoc
    return pypandoc.get_pandoc_formats()
示例#10
0
 def output_format(self, output_format):
     if not (output_format in pypandoc.get_pandoc_formats()[1]):
         raise ValueError(
             '{} is not supported like output format'.format(output_format))
     self._output_format = output_format
示例#11
0
 def input_format(self, input_format):
     if not (input_format in pypandoc.get_pandoc_formats()[0]):
         raise ValueError(
             '{} is not supported like input formats'.format(input_format))
     self._input_format = input_format
示例#12
0
def index():
    _, output_formats = pypandoc.get_pandoc_formats()
    return render_template("index.html", output_formats=output_formats)
class ConvertTextParams(BaseModel):
    source: str = Field(None, description= 'Unicode string or bytes (see encoding)')
    to: str = Field(..., description= 'format into which the input should be convertedcan be one of {}'.format(pypandoc.get_pandoc_formats()[1]))
    format: str= Field(..., description='the format of the inputs; can be one of {}'.format(pypandoc.get_pandoc_formats()[1]))
    extra_args: List[str] = Field((), description='extra arguments (list of strings) to be passed to pandoc (Default value = ())')
    encoding: str = Field('utf-8', description="the encoding of the input bytes (Default value = 'utf-8')")
    filters: List[str] = Field(None, description="pandoc filters e.g. filters=['pandoc-citeproc']")
from pydantic import BaseModel, Field
from typing import List
import pypandoc
from enum import Enum

available_format = pypandoc.get_pandoc_formats()[1]

class ConvertTextParams(BaseModel):
    source: str = Field(None, description= 'Unicode string or bytes (see encoding)')
    to: str = Field(..., description= 'format into which the input should be convertedcan be one of {}'.format(pypandoc.get_pandoc_formats()[1]))
    format: str= Field(..., description='the format of the inputs; can be one of {}'.format(pypandoc.get_pandoc_formats()[1]))
    extra_args: List[str] = Field((), description='extra arguments (list of strings) to be passed to pandoc (Default value = ())')
    encoding: str = Field('utf-8', description="the encoding of the input bytes (Default value = 'utf-8')")
    filters: List[str] = Field(None, description="pandoc filters e.g. filters=['pandoc-citeproc']")

示例#15
0
#!/usr/bin/env python

from distutils.core import setup

# Read README for long description
readme = open('README').read()
try:
    import pypandoc
    pypandoc.get_pandoc_formats()
    readme = pypandoc.convert(readme, to='rst', format='markdown')
except:
    pass

with open('pylibscrypt/__init__.py') as f:
    for l in f.readlines():
        if l.startswith('__version__'):
            version = l.split('=')[1].strip().strip("'")

setup(
    name='pylibscrypt',
    version=version,
    description='Scrypt for Python',
    long_description=readme,
    author='Jan Varho',
    author_email='*****@*****.**',
    url='https://github.com/jvarho/pylibscrypt',
    license='ISC License',
    packages=['pylibscrypt'],
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
示例#16
0
    def query(self,
              todays=None,
              bdays=None,
              otype=None,
              ext=None,
              att_get=None,
              att_max=None,
              mail=None,
              first=None,
              sort_case=None,
              sort_rules=None):
        """Query Gmail e-mail for specified date

        Kwargs:
            todays: Date to query (e.g. 2016-01-01; default today)
            bdays: Days back to look for e-mail.
            mail: Give yourself e-mail notification with query results
            ext: Email file extension (default blank)

        Returns:
            Output todays email to specified output folder and prints or
            e-mails message with query results.
        """

        if bdays is None:
            bdays = self.cfg_args.bdays

        if otype is None:
            otype = self.cfg_args.otype

        if ext is None:
            ext = self.cfg_args.ext

        if att_get is None:
            att_get = self.cfg_args.att_get

        if att_max is None:
            att_max = self.cfg_args.att_max

        if mail is None:
            mail = self.cfg_args.mail

        if first is None:
            first = self.cfg_args.first

        if sort_case is None:
            sort_case = self.cfg_args.sort_case

        if sort_rules is None:
            sort_rules = self.cfg_args.sort_file

        ptypes = pandoc.get_pandoc_formats()[1]
        if otype not in ptypes and not otype == 'eml':
            raise Warning("Output type must be: {}".format(', '.join(ptypes)))

        max_size = parse_string(att_max) if att_get else None
        sort = sort_rules != ''

        # Get date to query, recursively create output dir
        # ------------------------------------------------

        todays = todays if todays else str(datetime.date.today())
        outdir = os.path.join(self.outdir, todays)
        self.finaldir = outdir
        mkdir_recursive(outdir)

        # Query Gmail
        # -----------

        try:
            df = self.query_todays(todays, bdays, first, otype, max_size)
        except:
            df = None
            res = "Gmail query FAILED"

        ext = ext_dict[otype] if ext == '' else ext
        if df is not None:
            print_df_query(df, outdir, self.tzstr, otype, ext)
            if sort:
                if os.path.isfile(sort_rules):
                    try:
                        self.sort_query(sort_rules, sort_case)
                    except:
                        print("Sorting failed. Check '{}'".format(sort_rules))
                else:
                    print("'{}' not found. Can't sort.".format(sort_rules))
            res = "Success! See output folder:" + os.linesep + outdir
        else:
            res = 'No e-mail %s' % todays

        # Report success/fail
        # -------------------

        if mail:
            msg = [
                "Content-Type: text/plain; charset=\"UTF-8\"",
                "MIME-Version: 1.0",
                "Content-Transfer-Encoding: message/rfc2822",
                "To: <%s>" % self.outmail,
                "From: <%s>" % self.outmail,
                "Subject: Mail Dump for %s" % todays, "", res
            ]

            b = base64.b64encode(os.linesep.join(msg))
            b = b.replace('+', '-').replace('/', '_')
            self.messages.insert(userId='me', body={'raw': b}).execute()
        else:
            print(res)
示例#17
0
 def test_get_pandoc_formats(self):
     inputs, outputs = pypandoc.get_pandoc_formats()
     self.assertTrue("markdown" in inputs)
     self.assertTrue("json" in inputs)
     self.assertTrue("twiki" in inputs)
     self.assertTrue("markdown" in outputs)
示例#18
0
import logging
import pathlib
import re
import shutil
import tarfile
import zipfile
from pathlib import Path
from typing import Union, IO, Any

import pypandoc

logger = logging.getLogger('asyncio')

__FORMATS = pypandoc.get_pandoc_formats()

PANDOC_SERVICE_INPUT_FORMATS = frozenset(__FORMATS[0])

PANDOC_SERVICE_OUTPUT_FORMATS = frozenset(['pdf'] + __FORMATS[1])

PANDOC_SERVICE_FORMATS = frozenset(PANDOC_SERVICE_INPUT_FORMATS
                                   | PANDOC_SERVICE_OUTPUT_FORMATS)


class ExtractArchiveError(Exception):
    pass


class NotAnArchiveError(ExtractArchiveError):
    pass

示例#19
0
 def test_get_pandoc_formats(self):
     inputs, outputs = pypandoc.get_pandoc_formats()
     self.assertTrue("markdown" in inputs)
     self.assertTrue("json" in inputs)
     self.assertTrue("twiki" in inputs)
     self.assertTrue("markdown" in outputs)
示例#20
0
import pypandoc
from .mkcException import mkcException

(from_formats,to_formats) = pypandoc.get_pandoc_formats()

class StrLang(object):
	"""Class defining a string, PLUS mention of its lang (ie LaTeX, Markdown, etc.)"""
	def __init__(self, string, lang=None):
		if lang is not None  and  lang not in from_formats:
			raise mkcException( "The lang "+ lang+" cannot be converted by Pandoc.")
		self.string=string
		self.lang=lang
		
	def convertTo(self, lang=None):
		if lang is not None  and lang not in to_formats:
			raise mkcException( "The string '"+self.string+"' cannot be converted to "+lang+" by Pandoc ('"+lang+"' is not supported).")
		
		if lang is not None and self.lang != lang and self.lang is not None:
			return pypandoc.convert( self.string, format=self.lang, to=lang)
		else:
			return self.string
		
	def __str__(self):
		return self.string
	
示例#21
0
def configure():
    """ Perform initial setup. """
    config = {'sync_interval': 300}
    generate_key = not click.confirm("Do you already have an API key for "
                                     "zotero-cli?")
    if generate_key:
        (config['api_key'],
         config['library_id']) = ZoteroBackend.create_api_key()
    else:
        config['api_key'] = click.prompt(
            "Please enter the API key for zotero-cli")
        config['library_id'] = click.prompt("Please enter your library ID")
    sync_method = select([("local", "Local Zotero storage"),
                          ("zotcoud", "Use Zotero file cloud"),
                          ("webdav", "Use WebDAV storage")],
                         default=1,
                         required=True,
                         prompt="How do you want to access files for reading?")
    if sync_method == "local":
        storage_dirs = tuple(find_storage_directories())
        if storage_dirs:
            options = [(name, "{} ({})".format(click.style(name, fg="cyan"),
                                               path))
                       for name, path in storage_dirs]
            config['storage_dir'] = select(
                options,
                required=False,
                prompt="Please select a storage directory (-1 to enter "
                "manually)")
        if config.get('storage_dir') is None:
            click.echo(
                "Could not automatically locate a Zotero storage directory.")
            while True:
                storage_dir = click.prompt(
                    "Please enter the path to your Zotero storage directory",
                    default='')
                if not storage_dir:
                    storage_dir = None
                    break
                elif not os.path.exists(storage_dir):
                    click.echo("Directory does not exist!")
                elif not re.match(r'.*storage/?', storage_dir):
                    click.echo("Path must point to a `storage` directory!")
                else:
                    config['storage_dir'] = storage_dir
                    break
    elif sync_method == "webdav":
        while True:
            if not config.get('webdav_path'):
                config['webdav_path'] = click.prompt(
                    "Please enter the WebDAV URL (without '/zotero'!)")
            if not config.get('webdav_user'):
                config['webdav_user'] = click.prompt(
                    "Please enter the WebDAV user name")
                config['webdav_pass'] = click.prompt(
                    "Please enter the WebDAV password")
            try:
                test_resp = requests.options(config['webdav_path'],
                                             auth=(config['webdav_user'],
                                                   config['webdav_pass']))
            except requests.ConnectionError:
                click.echo("Invalid WebDAV URL, could not reach server.")
                config['webdav_path'] = None
                continue
            if test_resp.status_code == 200:
                break
            elif test_resp.status_code == 404:
                click.echo("Invalid WebDAV path, does not exist.")
                config['webdav_path'] = None
            elif test_resp.status_code == 401:
                click.echo("Bad credentials.")
                config['webdav_user'] = None
            else:
                click.echo("Unknown error, please check your settings.")
                click.echo("Server response code was: {}".format(
                    test_resp.status_code))
                config['webdav_path'] = None
                config['webdav_user'] = None
    config['sync_method'] = sync_method

    markup_formats = pypandoc.get_pandoc_formats()[0]
    config['note_format'] = select(zip(markup_formats, markup_formats),
                                   default=markup_formats.index('markdown'),
                                   prompt="Select markup format for notes")
    save_config(config)
    zot = ZoteroBackend(config['api_key'], config['library_id'], 'user')
    click.echo("Initializing local index...")
    num_synced = zot.synchronize()
    click.echo("Synchronized {} items.".format(num_synced))
示例#22
0
import pypandoc
from .mkcException import mkcException

(from_formats, to_formats) = pypandoc.get_pandoc_formats()


class StrLang(object):
    """Class defining a string, PLUS mention of its lang (ie LaTeX, Markdown, etc.)"""
    def __init__(self, string, lang=None):
        if lang is not None and lang not in from_formats:
            raise mkcException("The lang " + lang +
                               " cannot be converted by Pandoc.")
        self.string = string
        self.lang = lang

    def convertTo(self, lang=None):
        if lang is not None and lang not in to_formats:
            raise mkcException("The string '" + self.string +
                               "' cannot be converted to " + lang +
                               " by Pandoc ('" + lang + "' is not supported).")

        if lang is not None and self.lang != lang and self.lang is not None:
            return pypandoc.convert(self.string, format=self.lang, to=lang)
        else:
            return self.string

    def __str__(self):
        return self.string