<commit_before>import re
import os
import sys
import warnings
from itertools import dropwhile
from optparse import make_option

from django.core.management.base import CommandError, BaseCommand

try:
    set
except NameError:
    from sets import Set as set     # For Python 2.3

# Intentionally silence DeprecationWarnings about os.popen3 in Python 2.6. It's
# still sensible for us to use it, since subprocess didn't exist in 2.3.
warnings.filterwarnings('ignore', category=DeprecationWarning, message=r'os\.popen3')

pythonize_re = re.compile(r'\n\s*//')

def handle_extensions(extensions=('html',)):
    """
    organizes multiple extensions that are separated with commas or passed by
    using --extension/-e multiple times.

    for example: running 'django-admin makemessages -e js,txt -e xhtml -a'
    would result in a extension list: ['.js', '.txt', '.xhtml']

    >>> handle_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py'])
    ['.html', '.js']
    >>> handle_extensions(['.html, txt,.tpl'])
    ['.html', '.tpl', '.txt']
    """
    ext_list = []
    for ext in extensions:
        ext_list.extend(ext.replace(' ','').split(','))
    for i, ext in enumerate(ext_list):
        if not ext.startswith('.'):
            ext_list[i] = '.%s' % ext_list[i]

    # we don't want *.py files here because of the way non-*.py files
    # are handled in make_messages() (they are copied to file.ext.py files to
    # trick xgettext to parse them as Python files)
    return set([x for x in ext_list if x != '.py'])

def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None):
    """
    Uses the locale directory from the Django SVN tree or an application/
    project to process all
    """
    # Need to ensure that the i18n framework is enabled
    from django.conf import settings
    if settings.configured:
        settings.USE_I18N = True
    else:
        settings.configure(USE_I18N = True)

    from django.utils.translation import templatize

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    elif os.path.isdir('locale'):
        localedir = os.path.abspath('locale')
    else:
        raise CommandError("This script should be run from the Django SVN tree or your project or app tree. If you did indeed run it from the SVN checkout or your project or application, maybe you are just missing the conf/locale (in the django tree) or locale (for project and application) directory? It is not created automatically, you have to create it by hand if you want to enable i18n for your project or application.")

    if domain not in ('django', 'djangojs'):
        raise CommandError("currently makemessages only supports domains 'django' and 'djangojs'")

    if (locale is None and not all) or domain is None:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        raise CommandError(message)

    # xgettext versions prior to 0.15 assumed Python source files were encoded
    # in iso-8859-1, and produce utf-8 output.  In the case where xgettext is
    # given utf-8 input (required for Django files with non-ASCII characters),
    # this results in a utf-8 re-encoding of the original utf-8 that needs to be
    # undone to restore the original utf-8.  So we check the xgettext version
    # here once and set a flag to remember if a utf-8 decoding needs to be done
    # on xgettext's output for Python files.  We default to assuming this isn't
    # necessary if we run into any trouble determining the version.
    xgettext_reencodes_utf8 = False
    (stdin, stdout, stderr) = os.popen3('xgettext --version', 't')
    match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', stdout.read())
    if match:
        xversion = (int(match.group('major')), int(match.group('minor')))
        if xversion < (0, 15):
            xgettext_reencodes_utf8 = True

    languages = []
    if locale is not None:
        languages.append(locale)
    elif all:
        languages = [el for el in os.listdir(localedir) if not el.startswith('.')]

    for locale in languages:
        if verbosity > 0:
            print "processing language", locale
        basedir = os.path.join(localedir, locale, 'LC_MESSAGES')
        if not os.path.isdir(basedir):
            os.makedirs(basedir)

        pofile = os.path.join(basedir, '%s.po' % domain)
        potfile = os.path.join(basedir, '%s.pot' % domain)

        if os.path.exists(potfile):
            os.unlink(potfile)

        all_files = []
        for (dirpath, dirnames, filenames) in os.walk("."):
            all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            file_base, file_ext = os.path.splitext(file)
            if domain == 'djangojs' and file_ext == '.js':
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                src = open(os.path.join(dirpath, file), "rU").read()
                src = pythonize_re.sub('\n#', src)
                thefile = '%s.py' % file
                open(os.path.join(dirpath, thefile), "w").write(src)
                cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                old = '#: '+os.path.join(dirpath, thefile)[2:]
                new = '#: '+os.path.join(dirpath, file)[2:]
                msgs = msgs.replace(old, new)
                if os.path.exists(potfile):
                    # Strip the header
                    msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
                else:
                    msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
                thefile = file
                if file_ext in extensions:
                    src = open(os.path.join(dirpath, file), "rU").read()
                    thefile = '%s.py' % file
                    try:
                        open(os.path.join(dirpath, thefile), "w").write(templatize(src))
                    except SyntaxError, msg:
                        msg = "%s (file: %s)" % (msg, os.path.join(dirpath, file))
                        raise SyntaxError(msg)
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                    domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

                if xgettext_reencodes_utf8:
                    msgs = msgs.decode('utf-8').encode('iso-8859-1')

                if thefile != file:
                    old = '#: '+os.path.join(dirpath, thefile)[2:]
                    new = '#: '+os.path.join(dirpath, file)[2:]
                    msgs = msgs.replace(old, new)
                if os.path.exists(potfile):
                    # Strip the header
                    msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
                else:
                    msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout, stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, 't')
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--locale', '-l', default=None, dest='locale',
            help='Creates or updates the message files only for the given locale (e.g. pt_BR).'),
        make_option('--domain', '-d', default='django', dest='domain',
            help='The domain of the message files (default: "django").'),
        make_option('--all', '-a', action='store_true', dest='all',
            default=False, help='Reexamines all source code and templates for new translation strings and updates all message files for all available languages.'),
        make_option('--extension', '-e', dest='extensions',
            help='The file extension(s) to examine (default: ".html", separate multiple extensions with commas, or use -e multiple times)',
            action='append'),
    )
    help = "Runs over the entire source tree of the current directory and pulls out all strings marked for translation. It creates (or updates) a message file in the conf/locale (in the django tree) or locale (for project and application) directory."

    requires_model_validation = False
    can_import_settings = False

    def handle(self, *args, **options):
        if len(args) != 0:
            raise CommandError("Command doesn't accept any arguments")

        locale = options.get('locale')
        domain = options.get('domain')
        verbosity = int(options.get('verbosity'))
        process_all = options.get('all')
        extensions = options.get('extensions') or ['html']

        if domain == 'djangojs':
            extensions = []
        else:
            extensions = handle_extensions(extensions)

        if '.js' in extensions:
            raise CommandError("JavaScript files should be examined by using the special 'djangojs' domain only.")

        make_messages(locale, domain, verbosity, process_all, extensions)
<commit_msg>Fixed #10867: make the makemessages command ignore files and only process directories. Thanks, diegobz.<commit_after>import re
import os
import sys
import glob
import warnings
from itertools import dropwhile
from optparse import make_option

from django.core.management.base import CommandError, BaseCommand

try:
    set
except NameError:
    from sets import Set as set     # For Python 2.3

# Intentionally silence DeprecationWarnings about os.popen3 in Python 2.6. It's
# still sensible for us to use it, since subprocess didn't exist in 2.3.
warnings.filterwarnings('ignore', category=DeprecationWarning, message=r'os\.popen3')

pythonize_re = re.compile(r'\n\s*//')

def handle_extensions(extensions=('html',)):
    """
    organizes multiple extensions that are separated with commas or passed by
    using --extension/-e multiple times.

    for example: running 'django-admin makemessages -e js,txt -e xhtml -a'
    would result in a extension list: ['.js', '.txt', '.xhtml']

    >>> handle_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py'])
    ['.html', '.js']
    >>> handle_extensions(['.html, txt,.tpl'])
    ['.html', '.tpl', '.txt']
    """
    ext_list = []
    for ext in extensions:
        ext_list.extend(ext.replace(' ','').split(','))
    for i, ext in enumerate(ext_list):
        if not ext.startswith('.'):
            ext_list[i] = '.%s' % ext_list[i]

    # we don't want *.py files here because of the way non-*.py files
    # are handled in make_messages() (they are copied to file.ext.py files to
    # trick xgettext to parse them as Python files)
    return set([x for x in ext_list if x != '.py'])

def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None):
    """
    Uses the locale directory from the Django SVN tree or an application/
    project to process all
    """
    # Need to ensure that the i18n framework is enabled
    from django.conf import settings
    if settings.configured:
        settings.USE_I18N = True
    else:
        settings.configure(USE_I18N = True)

    from django.utils.translation import templatize

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    elif os.path.isdir('locale'):
        localedir = os.path.abspath('locale')
    else:
        raise CommandError("This script should be run from the Django SVN tree or your project or app tree. If you did indeed run it from the SVN checkout or your project or application, maybe you are just missing the conf/locale (in the django tree) or locale (for project and application) directory? It is not created automatically, you have to create it by hand if you want to enable i18n for your project or application.")

    if domain not in ('django', 'djangojs'):
        raise CommandError("currently makemessages only supports domains 'django' and 'djangojs'")

    if (locale is None and not all) or domain is None:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        raise CommandError(message)

    # xgettext versions prior to 0.15 assumed Python source files were encoded
    # in iso-8859-1, and produce utf-8 output.  In the case where xgettext is
    # given utf-8 input (required for Django files with non-ASCII characters),
    # this results in a utf-8 re-encoding of the original utf-8 that needs to be
    # undone to restore the original utf-8.  So we check the xgettext version
    # here once and set a flag to remember if a utf-8 decoding needs to be done
    # on xgettext's output for Python files.  We default to assuming this isn't
    # necessary if we run into any trouble determining the version.
    xgettext_reencodes_utf8 = False
    (stdin, stdout, stderr) = os.popen3('xgettext --version', 't')
    match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', stdout.read())
    if match:
        xversion = (int(match.group('major')), int(match.group('minor')))
        if xversion < (0, 15):
            xgettext_reencodes_utf8 = True

    languages = []
    if locale is not None:
        languages.append(locale)
    elif all:
        locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % localedir))
        languages = [os.path.basename(l) for l in locale_dirs]

    for locale in languages:
        if verbosity > 0:
            print "processing language", locale
        basedir = os.path.join(localedir, locale, 'LC_MESSAGES')
        if not os.path.isdir(basedir):
            os.makedirs(basedir)

        pofile = os.path.join(basedir, '%s.po' % domain)
        potfile = os.path.join(basedir, '%s.pot' % domain)

        if os.path.exists(potfile):
            os.unlink(potfile)

        all_files = []
        for (dirpath, dirnames, filenames) in os.walk("."):
            all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            file_base, file_ext = os.path.splitext(file)
            if domain == 'djangojs' and file_ext == '.js':
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                src = open(os.path.join(dirpath, file), "rU").read()
                src = pythonize_re.sub('\n#', src)
                thefile = '%s.py' % file
                open(os.path.join(dirpath, thefile), "w").write(src)
                cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                old = '#: '+os.path.join(dirpath, thefile)[2:]
                new = '#: '+os.path.join(dirpath, file)[2:]
                msgs = msgs.replace(old, new)
                if os.path.exists(potfile):
                    # Strip the header
                    msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
                else:
                    msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
                thefile = file
                if file_ext in extensions:
                    src = open(os.path.join(dirpath, file), "rU").read()
                    thefile = '%s.py' % file
                    try:
                        open(os.path.join(dirpath, thefile), "w").write(templatize(src))
                    except SyntaxError, msg:
                        msg = "%s (file: %s)" % (msg, os.path.join(dirpath, file))
                        raise SyntaxError(msg)
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                    domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

                if xgettext_reencodes_utf8:
                    msgs = msgs.decode('utf-8').encode('iso-8859-1')

                if thefile != file:
                    old = '#: '+os.path.join(dirpath, thefile)[2:]
                    new = '#: '+os.path.join(dirpath, file)[2:]
                    msgs = msgs.replace(old, new)
                if os.path.exists(potfile):
                    # Strip the header
                    msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
                else:
                    msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout, stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, 't')
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--locale', '-l', default=None, dest='locale',
            help='Creates or updates the message files only for the given locale (e.g. pt_BR).'),
        make_option('--domain', '-d', default='django', dest='domain',
            help='The domain of the message files (default: "django").'),
        make_option('--all', '-a', action='store_true', dest='all',
            default=False, help='Reexamines all source code and templates for new translation strings and updates all message files for all available languages.'),
        make_option('--extension', '-e', dest='extensions',
            help='The file extension(s) to examine (default: ".html", separate multiple extensions with commas, or use -e multiple times)',
            action='append'),
    )
    help = "Runs over the entire source tree of the current directory and pulls out all strings marked for translation. It creates (or updates) a message file in the conf/locale (in the django tree) or locale (for project and application) directory."

    requires_model_validation = False
    can_import_settings = False

    def handle(self, *args, **options):
        if len(args) != 0:
            raise CommandError("Command doesn't accept any arguments")

        locale = options.get('locale')
        domain = options.get('domain')
        verbosity = int(options.get('verbosity'))
        process_all = options.get('all')
        extensions = options.get('extensions') or ['html']

        if domain == 'djangojs':
            extensions = []
        else:
            extensions = handle_extensions(extensions)

        if '.js' in extensions:
            raise CommandError("JavaScript files should be examined by using the special 'djangojs' domain only.")

        make_messages(locale, domain, verbosity, process_all, extensions)
<|endoftext|>


<commit_before>#!/usr/bin/env python2
import sys
import os
import argparse
import yaml
from app.expression import expr_constructor, json_constructor
from app import default

def main():
    parser = argparse.ArgumentParser(description='Endpoint tester')
    parser.add_argument("--auth", metavar='AUTH_CONFIG_FILE', type=str,
                        help='The configuration file containg authentication information')
    parser.add_argument("scenario_file", metavar='SCENARIO_FILE', type=str, nargs="?",
                        help='The path to the scenario file')
    parser.add_argument("-X", action="store_true", default=False, help='Stop at the first error')
    parser.add_argument("--version", action="store_true", default=False, help='Get version number')
    args = parser.parse_args()

    if args.version:
        print default.__version__
        return 0

    if args.scenario_file:
        scenario_root = os.path.abspath(os.path.join(os.path.abspath(args.scenario_file), os.pardir))
        scenario_file_path = os.path.abspath(args.scenario_file)

        # check that there is a scenario file
        if not os.path.isfile(scenario_file_path):
            print "{} does not exist".format(scenario_file_path)
            return -1

        with open(scenario_file_path, 'r') as scene_file:
            yaml.add_constructor('!expr', expr_constructor)
            yaml.add_constructor('!json', json_constructor)
            scene = yaml.load(scene_file)
    else:
        scenario_root = os.path.abspath(os.path.join(os.path.abspath("."), os.pardir))
        yaml.add_constructor('!expr', expr_constructor)
        yaml.add_constructor('!json', json_constructor)
        scene = yaml.load(sys.stdin.read())

    if args.auth:
        # check that there is a config file
        if not os.path.isfile(args.auth):
            print "{} does not exist".format(args.auth)
            return -1

        with open(args.auth, 'r') as conf:
            config = yaml.load(conf)
    else:
        config = {}

    command_parser = default.CommandParser(config, scene, scenario_root, exit_on_error=args.X)
    return command_parser.parse()

if __name__ == "__main__":
    sys.exit(main())
<commit_msg>fix: on certain systems python2 doesn't exist<commit_after>#!/usr/bin/env python2.7
import sys
import os
import argparse
import yaml
from app.expression import expr_constructor, json_constructor
from app import default

def main():
    parser = argparse.ArgumentParser(description='Endpoint tester')
    parser.add_argument("--auth", metavar='AUTH_CONFIG_FILE', type=str,
                        help='The configuration file containg authentication information')
    parser.add_argument("scenario_file", metavar='SCENARIO_FILE', type=str, nargs="?",
                        help='The path to the scenario file')
    parser.add_argument("-X", action="store_true", default=False, help='Stop at the first error')
    parser.add_argument("--version", action="store_true", default=False, help='Get version number')
    args = parser.parse_args()

    if args.version:
        print default.__version__
        return 0

    if args.scenario_file:
        scenario_root = os.path.abspath(os.path.join(os.path.abspath(args.scenario_file), os.pardir))
        scenario_file_path = os.path.abspath(args.scenario_file)

        # check that there is a scenario file
        if not os.path.isfile(scenario_file_path):
            print "{} does not exist".format(scenario_file_path)
            return -1

        with open(scenario_file_path, 'r') as scene_file:
            yaml.add_constructor('!expr', expr_constructor)
            yaml.add_constructor('!json', json_constructor)
            scene = yaml.load(scene_file)
    else:
        scenario_root = os.path.abspath(os.path.join(os.path.abspath("."), os.pardir))
        yaml.add_constructor('!expr', expr_constructor)
        yaml.add_constructor('!json', json_constructor)
        scene = yaml.load(sys.stdin.read())

    if args.auth:
        # check that there is a config file
        if not os.path.isfile(args.auth):
            print "{} does not exist".format(args.auth)
            return -1

        with open(args.auth, 'r') as conf:
            config = yaml.load(conf)
    else:
        config = {}

    command_parser = default.CommandParser(config, scene, scenario_root, exit_on_error=args.X)
    return command_parser.parse()

if __name__ == "__main__":
    sys.exit(main())
<|endoftext|>


<commit_before># coding: utf-8
import sublime
import sublime_plugin
import difflib
import time
import os.path

class DiffTabsCommand(sublime_plugin.WindowCommand):
    def run(self, group, index):
        window = self.window

        aview = window.active_view()
        if window.get_view_index(aview) == (group, index):
            return
        bview = window.views_in_group(group)[index]

        aname = aview.file_name() if aview.file_name() else aview.name()
        bname = bview.file_name() if bview.file_name() else bview.name()

        a = aview.substr(sublime.Region(0, aview.size())).splitlines()
        b = bview.substr(sublime.Region(0, bview.size())).splitlines()

        adate = time.ctime() if aview.is_dirty() else time.ctime(os.stat(aname).st_mtime)
        bdate = time.ctime() if bview.is_dirty() else time.ctime(os.stat(bname).st_mtime)

        diff = difflib.unified_diff(b, a, bname, aname, bdate, adate, lineterm='')
        difftxt = u"\n".join(line for line in diff)

        if difftxt == "":
            sublime.status_message("Files are identical")
        else:
            v = window.new_file()
            v.set_name(os.path.basename(bname) + " -> " + os.path.basename(aname))
            v.set_scratch(True)
            v.set_syntax_file('Packages/Diff/Diff.tmLanguage')
            edit = v.begin_edit()
            v.insert(edit, 0, difftxt)
            v.end_edit(edit)

    def is_visible(self, group, index):
        window = self.window
        view = window.active_view()
        return window.get_view_index(view) != (group, index)
<commit_msg>ST3 Restricted begin_edit() and end_edit()<commit_after># coding: utf-8
import sublime
import sublime_plugin
import difflib
import time
import os.path

class DiffTabsCommand(sublime_plugin.WindowCommand):
    def run(self, group, index):
        window = self.window

        aview = window.active_view()
        if window.get_view_index(aview) == (group, index):
            return
        bview = window.views_in_group(group)[index]

        aname = aview.file_name() if aview.file_name() else aview.name()
        bname = bview.file_name() if bview.file_name() else bview.name()

        a = aview.substr(sublime.Region(0, aview.size())).splitlines()
        b = bview.substr(sublime.Region(0, bview.size())).splitlines()

        adate = time.ctime() if aview.is_dirty() else time.ctime(os.stat(aname).st_mtime)
        bdate = time.ctime() if bview.is_dirty() else time.ctime(os.stat(bname).st_mtime)

        diff = difflib.unified_diff(b, a, bname, aname, bdate, adate, lineterm='')
        difftxt = u"\n".join(line for line in diff)

        if difftxt == "":
            sublime.status_message("Files are identical")
        else:
            v = window.new_file()
            v.set_name(os.path.basename(bname) + " -> " + os.path.basename(aname))
            v.set_scratch(True)
            v.set_syntax_file('Packages/Diff/Diff.tmLanguage')
            v.run_command('append', {'characters':difftxt})

    def is_visible(self, group, index):
        window = self.window
        view = window.active_view()
        return window.get_view_index(view) != (group, index)
<|endoftext|>


<commit_before>import copy
from logs import StockLog

class InvalidOrderException(Exception): pass

class Machine(object):
    """
    Coffee machine out from factory. Can do anything. Should be put in
    maintenance or usage mode
    """

    CoinsType = [200, 100, 50, 20, 10]
    StocksType = ['coffee', 'tea', 'chocolate', 'milk', 'sugar']
    DefaultPrices = {'coffee': 20, 'tea': 10, 'chocolate': 30,
                     'milk': 5, 'sugar': [0, 5, 15, 15]}  # On peut aussi faire
                                                          # coffee: [0, 20]

    def __init__(self, max_stocks=None, max_coins=None, stock_price=None):
        if not max_stocks:
            self._max_stocks = {key: 100 for key in Machine.StocksType}
        if not max_coins:
            self._max_coins = {key: 100 for key in Machine.CoinsType}
        if not stock_price:
            self._stock_price = copy.copy(Machine.DefaultPrices)
        self._stocks = {key: 0 for key in Machine.StocksType}
        self._cash = {key: 0 for key in Machine.CoinsType}
        self._coins = {key: 0 for key in Machine.CoinsType}
        self._log = []
        self._prices = Machine.DefaultPrices

    def edit_prices(self, **prices):

        for type_ in Machine.StocksType:
            try:
                new_val = prices[type_]
            except KeyError:
                pass
            else:
                if new_val > 0:
                    self.prices[type_] = new_val

    def edit_stocks(self, **stocks):
        """
        Edit the stocks in the coffee machine.

        If the value is not possible ie less than current stock or over the
        stock limit, then this value is dismissed.

        Usage:
        ```
        m.edit_stocks(coffee=80, tea=50, chocolate=42)
        ```
        ```
        new_stocks = {'coffee': 80, 'tea': 50, 'chocolate' : 42}
        m.edit_stocks(**new_stocks)
        ```
        """
        prev_stocks = self.stocks
        for type_ in Machine.StocksType:
            try:
                new_val = stocks[type_]
            except KeyError:
                pass
            else:
                if self.stocks[type_] < new_val <= self.max_stocks[type_]:
                    self.stocks[type_] = new_val
        self._log.append(StockLog(prev_stocks, self.stocks))

    def parse_order(self, command):
        drink = {}
        drink['sugar'] = command[0] * 2 + command[1]
        drink['milk'] = command[2]
        if command[3]:
            drink['tea'] = 1
            drink['coffee'] = 0
            drink['chocolate'] = 0
        else:
            drink['tea'] = 0
            drink['coffee'] = command[4]
            drink['chocolate'] = command[5]
        return drink

    def order(self, command):
        drink = self.parse_order(command)
        if not drink['beverage']:
            raise InvalidOrderException('You need to choose a beverage')
        for key in self.stocks:
            pass # Test if enough stock or raise error

    @property
    def max_stocks(self):
        return self._max_stocks

    @property
    def max_coins(self):
        return self._max_coins

    @property
    def stocks(self):
        return self._stocks

    @property
    def coins(self):
        return self._coins

    @property
    def log(self):
        return self._log

    @property
    def prices(self):
        return self._prices

    def __repr__(self):
        return 'Machine à café d\'usine'

class MachineFunc(object):

    def __init__(self, machine_factory):
        self.m = machine_factory

    def edit_stock(self, *args, **kwargs):
        raise NotImplemented()

    def order(self, *args, **kwargs):
        return self.m.order(*args, **kwargs)

    @property
    def fact(self):
        return self.m
<commit_msg>Order rewritten, need Drink and Cash classes<commit_after>import copy
from logs import StockLog
from drink import Drink
from coins import Coins

class InvalidOrderException(Exception): pass

class Machine(object):
    """
    Coffee machine out from factory. Can do anything. Should be put in
    maintenance or usage mode
    """

    MaxCashInput = 200
    CoinsType = [200, 100, 50, 20, 10]
    StocksType = ['coffee', 'tea', 'chocolate', 'milk', 'sugar']
    DefaultPrices = {'coffee': 20, 'tea': 10, 'chocolate': 30,
                     'milk': 5, 'sugar': [0, 5, 15, 15]}  # On peut aussi faire
                                                          # coffee: [0, 20]

    def __init__(self, max_stocks=None, max_coins=None, stock_prices=None):
        if not max_stocks:
            self._max_stocks = {key: 100 for key in Machine.StocksType}
        if not max_coins:
            self._max_coins = {key: 100 for key in Machine.CoinsType}
        if not stock_prices:
            self._stock_prices = copy.copy(Machine.DefaultPrices)
        self._stocks = {key: 0 for key in Machine.StocksType}
        self._cash = Coins((0 for key in Machine.CoinsType, Machine.CoinsType))
        self._coins = Coins((0 for key in Machine.CoinsType, Machine.CoinsType))
        self._log = []

    def edit_prices(self, **prices):  # TODO SUGAR NOT WORKING WITH THIS CODE

        for type_ in Machine.StocksType:
            try:
                new_val = prices[type_]
            except KeyError:
                pass
            else:
                if new_val > 0:
                    self.stock_prices[type_] = new_val

    def edit_stocks(self, **stocks):
        """
        Edit the stocks in the coffee machine.

        If the value is not possible ie less than current stock or over the
        stock limit, then this value is dismissed.

        Usage:
        ```
        m.edit_stocks(coffee=80, tea=50, chocolate=42)
        ```
        ```
        new_stocks = {'coffee': 80, 'tea': 50, 'chocolate' : 42}
        m.edit_stocks(**new_stocks)
        ```
        """
        prev_stocks = self.stocks
        for type_ in Machine.StocksType:
            try:
                new_val = stocks[type_]
            except KeyError:
                pass
            else:
                if self.stocks[type_] < new_val <= self.max_stocks[type_]:
                    self.stocks[type_] = new_val
        self._log.append(StockLog(prev_stocks, self.stocks))

    def order(self, command, coins_in):
        drink = Drink(command, self.stock_price)
        coins = Coins(coins_in, Machine.CoinsType)
        coins_out = self.give_change(coins, Machine.MaxCashInput)
        coins -= coins_out
        if coins.value == 0:
            return None, coins_out  # Give all the cash
        if not drink.has_beverage:
            raise InvalidOrderException('You need to choose a beverage')
        for item in drink.stocks:
            if self.stocks[item] - drink.stocks[item] < 0:
                raise NotEnoughStockException(
                        'Not enough {} in stock'.format(item))
        if coins.value < drink.price:
            raise InvalidOrderException('Not Enough change to pay the order')

        coins_out += self.give_change(coins, drink.price)
        self.add_to_cash(coins - coins_out)
        self.remove_stock(drink.stocks)
        return drink, coins_out

    @property
    def max_stocks(self):
        return self._max_stocks

    @property
    def max_coins(self):
        return self._max_coins

    @property
    def stocks(self):
        return self._stocks

    @property
    def coins(self):
        return self._coins

    @property
    def log(self):
        return self._log

    @property
    def stock_prices(self):
        return self._stock_prices

    def __repr__(self):
        return 'Machine à café d\'usine'

class MachineFunc(object):

    def __init__(self, machine_factory):
        self.m = machine_factory

    def edit_stock(self, *args, **kwargs):
        raise NotImplemented()

    def order(self, *args, **kwargs):
        return self.m.order(*args, **kwargs)

    @property
    def fact(self):
        return self.m
<|endoftext|>


<commit_before>import pymc as pm
import numpy

# ===========================
# = Convergence diagnostics =
# ===========================

# Simple dose-response model
n = 5*numpy.ones(4,dtype=int)
dose=numpy.array([-.86,-.3,-.05,.73])
x=numpy.array([0,1,3,5], dtype=float)

alpha = pm.Normal('alpha', mu=0.0, tau=0.01)
beta = pm.Normal('beta', mu=0.0, tau=0.01)

@pm.deterministic
def theta(a=alpha, b=beta, d=dose):
    """theta = inv_logit(a+b)"""
    return pm.invlogit(a+b*d)

"""deaths ~ binomial(n, p)"""
deaths = pm.Binomial('deaths', n=n, p=theta, value=x, observed=True)

my_model = [alpha, beta, theta, deaths]

# Instantiate and run sampler
S = pm.MCMC(my_model)
S.sample(10000, burn=5000)

# Calculate and plot Geweke scores
scores = pm.geweke(S, intervals=20)
pm.Matplot.geweke_plot(scores)

# Geweke plot for a single parameter
trace = S.trace('alpha')[:]
alpha_scores = pm.geweke(trace, intervals=20)
pm.Matplot.geweke_plot(alpha_scores, 'alpha')

# Calculate Raftery-Lewis diagnostics
pm.raftery_lewis(S, q=0.025, r=0.01)

"""
Sample output:

========================
Raftery-Lewis Diagnostic
========================

937 iterations required (assuming independence) to achieve 0.01 accuracy
with 95 percent probability.

Thinning factor of 1 required to produce a first-order Markov chain.

39 iterations to be discarded at the beginning of the simulation (burn-in).

11380 subsequent iterations required.

Thinning factor of 11 required to produce an independence chain.
"""

# =========================
# = Autocorrelation plots =
# =========================

# Autocorrelation for the entire model
pm.Matplot.autocorrelation(S)

# Autocorrelation for just the slope parameter
pm.Matplot.autocorrelation(beta)

# ===================
# = Goodness-of-fit =
# ===================

# Simulate deaths, using posterior predictive distribution
deaths_sim = pm.Binomial('deaths_sim', n=n, p=theta, value=x)

# Expected number of deaths, based on theta
expected_deaths = pm.Lambda('expected_deaths', lambda theta=theta: theta*n)

my_model = [alpha, beta, theta, deaths, deaths_sim, expected_deaths]

# Create MCMC sampler
S = pm.MCMC(my_model)
S.sample(1000)

x_sim = deaths_sim.trace()

# Create GOF plot
pm.Matplot.gof_plot(x_sim, x, name='x')

# Calculate and plot discrepancies
D = pm.diagnostics.discrepancy(x, x_sim, expected_deaths)
pm.Matplot.discrepancy_plot(D, name='D', report_p=True)
<commit_msg>Clarified GOF example.<commit_after>import pymc as pm
import numpy

# ===========================
# = Convergence diagnostics =
# ===========================

# Simple dose-response model
n = [5]*4
dose = [-.86,-.3,-.05,.73]
x = [0,1,3,5]

alpha = pm.Normal('alpha', mu=0.0, tau=0.01)
beta = pm.Normal('beta', mu=0.0, tau=0.01)

@pm.deterministic
def theta(a=alpha, b=beta, d=dose):
    """theta = inv_logit(a+b)"""
    return pm.invlogit(a+b*d)

"""deaths ~ binomial(n, p)"""
deaths = pm.Binomial('deaths', n=n, p=theta, value=x, observed=True)

my_model = [alpha, beta, theta, deaths]

# Instantiate and run sampler
S = pm.MCMC(my_model)
S.sample(10000, burn=5000)

# Calculate and plot Geweke scores
scores = pm.geweke(S, intervals=20)
pm.Matplot.geweke_plot(scores)

# Geweke plot for a single parameter
trace = S.trace('alpha')[:]
alpha_scores = pm.geweke(trace, intervals=20)
pm.Matplot.geweke_plot(alpha_scores, 'alpha')

# Calculate Raftery-Lewis diagnostics
pm.raftery_lewis(S, q=0.025, r=0.01)

"""
Sample output:

========================
Raftery-Lewis Diagnostic
========================

937 iterations required (assuming independence) to achieve 0.01 accuracy
with 95 percent probability.

Thinning factor of 1 required to produce a first-order Markov chain.

39 iterations to be discarded at the beginning of the simulation (burn-in).

11380 subsequent iterations required.

Thinning factor of 11 required to produce an independence chain.
"""

# =========================
# = Autocorrelation plots =
# =========================

# Autocorrelation for the entire model
pm.Matplot.autocorrelation(S)

# Autocorrelation for just the slope parameter
pm.Matplot.autocorrelation(beta)

# ===================
# = Goodness-of-fit =
# ===================

# Simulate deaths, using posterior predictive distribution
@pm.deterministic
def deaths_sim(n=n, p=theta):
    """deaths_sim = rbinomial(n, p)"""
    return pm.rbinomial(n, p)

# Expected number of deaths, based on theta
expected_deaths = pm.Lambda('expected_deaths', lambda theta=theta: theta*n)

my_model = [alpha, beta, theta, deaths, deaths_sim, expected_deaths]

# Create MCMC sampler
S = pm.MCMC(my_model)
S.sample(1000)

x_sim = deaths_sim
x_exp = expected_deaths

# Create GOF plot
pm.Matplot.gof_plot(x_sim, x, name='x')

# Calculate and plot discrepancies
D = pm.diagnostics.discrepancy(x, x_sim, x_exp)
pm.Matplot.discrepancy_plot(D, name='D', report_p=True)

<|endoftext|>


<commit_before>import datetime
import hashlib
import os
import sys
import requests
import shutil
import bs4
from more_itertools import unique_everseen
from urlparse import urlparse

from utilkit import datetimeutil

from flask import Flask, abort, redirect, render_template, request, url_for
from flask_peewee.db import Database
#from flask_peewee.utils import get_object_or_404
from peewee import *

try:
    import settings
except ImportError:
    print('Copy settings_example.py to settings.py and set the configuration to your own preferences')
    sys.exit(1)

# app configuration
APP_ROOT = os.path.dirname(os.path.realpath(__file__))
MEDIA_ROOT = os.path.join(APP_ROOT, 'static')
MEDIA_URL = '/static/'
DATABASE = {
    'name': os.path.join(APP_ROOT, 'bookmarks.db'),
    'engine': 'peewee.SqliteDatabase',
}
#PHANTOM = '/usr/local/bin/phantomjs'
#SCRIPT = os.path.join(APP_ROOT, 'screenshot.js')

# create our flask app and a database wrapper
app = Flask(__name__)
app.config.from_object(__name__)
db = Database(app)

# set custom url for the app, for example '/bookmarks'
try:
    app.config['APPLICATION_ROOT'] = settings.APPLICATION_ROOT
except AttributeError:
    pass

# Cache the tags
all_tags = {}

def clean_tags(tags_list):
    tags_res = [x.strip() for x in tags_list]
    tags_res = list(unique_everseen(tags_res))
    tags_res.sort()
    if tags_res and tags_res[0] == '':
        del tags_res[0]
    return tags_res

class User(db.Model):
    """ User account """
    username = CharField()
    key = CharField()
    created_date = DateTimeField(default=datetime.datetime.now)

    def generate_key(self):
        """ Generate userkey """
        self.key = os.urandom(24).encode('hex')
        return self.key

class Bookmark(db.Model):
    """ Bookmark instance, connected to User """
    # Foreign key to User
    userkey = CharField()

    title = CharField(default='')
    url = CharField()
    created_date = DateTimeField(default=datetime.datetime.now)
    modified_date = DateTimeField(null=True)
    #image = CharField(default='')
    url_hash = CharField(default='')
    tags = CharField(default='')
    starred = BooleanField(default=False)

    # Website (domain) favicon
    favicon = CharField(null=True)

    # Status code: 200 is OK, 404 is not found, for example (showing an error)
    http_status = IntegerField(default=200)

    class Meta:
        ordering = (('created_date', 'desc'),)

    #def fetch_image(self):
    #    url_hash = hashlib.md5(self.url).hexdigest()
    #    filename = 'bookmark-%s.png' % url_hash

    #    outfile = os.path.join(MEDIA_ROOT, filename)
    #    params = [PHANTOM, SCRIPT, self.url, outfile]

    #    exitcode = subprocess.call(params)
    #    if exitcode == 0:
    #        self.image = os.path.join(MEDIA_URL, filename)

    def set_hash(self):
        """ Generate hash """
        self.url_hash = hashlib.md5(self.url).hexdigest()

    def set_title_from_source(self):
        """ Request the title by requesting the source url """
        try:
            result = requests.get(self.url)
            self.http_status = result.status_code
        except:
            # For example 'MissingSchema: Invalid URL 'abc': No schema supplied. Perhaps you meant http://abc?'
            self.http_status = 404
        if self.http_status == 200:
            html = bs4.BeautifulSoup(result.text, 'html.parser')
            try:
                self.title = html.title.text.strip()
            except AttributeError:
                self.title = ''
        return self.title

    def set_status_code(self):
        """ Check the HTTP status of the url, as it might not exist for example """
        result = requests.head(self.url)
        self.http_status = result.status_code
        return self.http_status

    def set_favicon(self):
        """ Fetch favicon for the domain """
        # http://codingclues.eu/2009/retrieve-the-favicon-for-any-url-thanks-to-google/
        u = urlparse(self.url)
        domain = u.netloc
        filename = os.path.join(MEDIA_ROOT, 'favicons/' + domain + '.png')
        # if file exists, don't re-download it
        response = requests.get('http://www.google.com/s2/favicons?domain=' + domain, stream=True)
        with open(filename, 'wb') as out_file:
            shutil.copyfileobj(response.raw, out_file)
        del response
        self.favicon = domain + '.png'

    def set_tags(self, tags):
        """ Set tags from `tags`, strip and sort them """
        tags_split = tags.split(',')
        tags_clean = clean_tags(tags_split)
        self.tags = ','.join(tags_clean)

    @property
    def tags_list(self):
        """ Get the tags as a list, iterable in template """
        if self.tags:
            return self.tags.split(',')
        else:
            return []

    def to_dict(self):
        result = {
                'title': self.title,
                'url': self.url,
                'created':  datetimeutil.datetime_to_string(self.created_date),
                'url_hash': self.url_hash,
                'tags': self.tags,
                }
        return result

class PublicTag(db.Model):
    """ Publicly shared tag """
    tagkey = CharField()
    userkey = CharField()
    tag = CharField()
    created_date = DateTimeField(default=datetime.datetime.now)

    def generate_key(self):
        """ Generate hash-based key for publicly shared tag """
        self.tagkey = os.urandom(16).encode('hex')

def get_tags_for_user(userkey):
    """ Extract all tags from the bookmarks """
    bookmarks = Bookmark.select().filter(Bookmark.userkey == userkey)
    tags = []
    for bookmark in bookmarks:
        tags += bookmark.tags_list
    return clean_tags(tags)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.route('/')
def index():
    """ Homepage, point visitors to project page """
    return render_template('index.html')

@app.route('/<userkey>', methods=['GET', 'POST'])
def bookmarks(userkey):
    """ User homepage, list their (unfiltered) bookmarks """
    #return object_list('bookmarks.html', Bookmark.select())
    #user = User.select(key=userkey)
    #if user:
    #    bookmarks = Bookmark.select(User=user)
    #    return render_template('bookmarks.html', bookmarks)
    #else:
    #    abort(404)
    message = request.args.get('message')
    if request.method == 'POST':
        filter_on = request.form['filter']
        bookmarks = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.title.contains(filter_on)).order_by(Bookmark.created_date.desc())
        return render_template('bookmarks.html', bookmarks=bookmarks, userkey=userkey, tags=all_tags[userkey], filter=filter_on, message=message)
    else:
        bookmarks = Bookmark.select().where(Bookmark.userkey == userkey).order_by(Bookmark.created_date.desc())
        return render_template('bookmarks.html', bookmarks=bookmarks, userkey=userkey, tags=all_tags[userkey], message=message)

#@app.route('/<userkey>/<urlhash>')
#def viewbookmark(userkey, urlhash):
#    """ Bookmark detail view """
#    bookmark = Bookmark.select(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey)
#    return render_template('viewbookmark.html', userkey=userkey, bookmark=bookmark)

@app.route('/<userkey>/<urlhash>/json')
def viewbookmarkjson(userkey, urlhash):
    """ Serialise bookmark to json """
    bookmark = Bookmark.select(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey)
    return bookmark.to_dict()

@app.route('/<userkey>/<urlhash>')
@app.route('/<userkey>/<urlhash>/edit')
def editbookmark(userkey, urlhash):
    """ Bookmark edit form """
    # bookmark = getbyurlhash()
    bookmark = Bookmark.get(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey)
    message = request.args.get('message')
    return render_template('edit.html', action='Edit bookmark', userkey=userkey, bookmark=bookmark, message=message, formaction='edit')

@app.route('/<userkey>/add')
def addbookmark(userkey):
    """ Bookmark add form """
    bookmark = Bookmark(title='', url='', tags='')
    return render_template('edit.html', action='Add bookmark', userkey=userkey, bookmark=bookmark)

def updatebookmark(userkey, request, urlhash = None):
    """ Add (no urlhash) or edit (urlhash is set) a bookmark """
    title = request.form.get('title')
    url = request.form.get('url')
    tags = request.form.get('tags')
    starred = False
    if request.form.get('starred'):
        starred = True

    if url and not urlhash:
        # New bookmark
        bookmark, created = Bookmark.get_or_create(url=url, userkey=userkey)
        if not created:
            message = 'Existing bookmark, did not overwrite with new values'
            return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash, message=message))
    elif url:
        # Existing bookmark, get from DB
        bookmark = Bookmark.get(userkey == userkey, Bookmark.url_hash == urlhash)
        # Editing this bookmark, set modified_date to now
        bookmark.modified_date = datetime.datetime.now()

    bookmark.title = title
    bookmark.url = url
    bookmark.starred = starred
    bookmark.set_tags(tags)
    bookmark.set_hash()
    #bookmark.fetch_image()
    if not title:
        # Title was empty, automatically fetch it from the url, will also update the status code
        bookmark.set_title_from_source()
    else:
        bookmark.set_status_code()

    if bookmark.http_status == 200:
        bookmark.set_favicon()

    bookmark.save()
    return bookmark

@app.route('/<userkey>/adding', methods=['GET', 'POST'])
#@app.route('/<userkey>/adding')
def addingbookmark(userkey):
    """ Add the bookmark from form submit by /add """

    if request.method == 'POST':
        bookmark = updatebookmark(userkey, request)
        if type(bookmark).__name__ == 'Response':
            return bookmark
        all_tags[userkey] = get_tags_for_user(userkey)
        return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash))
    return redirect(url_for('add'))

@app.route('/<userkey>/<urlhash>/editing', methods=['GET', 'POST'])
def editingbookmark(userkey, urlhash):
    """ Edit the bookmark from form submit """

    if request.method == 'POST':
        bookmark = updatebookmark(userkey, request, urlhash=urlhash)
        all_tags[userkey] = get_tags_for_user(userkey)
        return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash))
    return redirect(url_for('add'))

@app.route('/<userkey>/<urlhash>/delete', methods=['GET', 'POST'])
def deletingbookmark(userkey, urlhash):
    """ Delete the bookmark from form submit by <urlhash>/delete """
    Bookmark.delete().where(Bookmark.userkey==userkey, Bookmark.url_hash==urlhash)
    message = 'Bookmark deleted'
    all_tags[userkey] = get_tags_for_user(userkey)
    return redirect(url_for('bookmarks', userkey=userkey, message=message))

@app.route('/<userkey>/tags')
def tags(userkey):
    """ Overview of all tags used by user """
    tags = get_tags_for_user(userkey)
    print tags
    return render_template('tags.html', tags=tags, userkey=userkey)

@app.route('/<userkey>/tag/<tag>')
def tag(userkey, tag):
    """ Overview of all bookmarks with a certain tag """
    bookmarks = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.tags.contains(tag))
    tags = get_tags_for_user(userkey)
    pageheader = 'tag: ' + tag
    message = request.args.get('message')

    try:
        publictag = PublicTag.get(PublicTag.userkey == userkey, PublicTag.tag == tag)
    except PublicTag.DoesNotExist:
        publictag = None

    return render_template('bookmarks.html', bookmarks=bookmarks, userkey=userkey, tags=tags, tag=tag, publictag=publictag, action=pageheader, message=message)

@app.route('/pub/<tagkey>')
def publictag(tagkey):
    """ Read-only overview of the bookmarks in the userkey/tag of this PublicTag """
    #this_tag = get_object_or_404(PublicTag.select().where(PublicTag.tagkey == tagkey))
    try:
        this_tag = PublicTag.get(PublicTag.tagkey == tagkey)
        bookmarks = Bookmark.select().where(Bookmark.userkey == this_tag.userkey, Bookmark.tags.contains(this_tag.tag))
        return render_template('publicbookmarks.html', bookmarks=bookmarks, tag=tag, action=this_tag.tag)
    except PublicTag.DoesNotExist:
        abort(404)

@app.route('/<userkey>/<tag>/makepublic', methods=['GET', 'POST'])
def addpublictag(userkey, tag):
    #user = get_object_or_404(User.get(User.key == userkey))
    try:
        User.get(User.key == userkey)
    except User.DoesNotExist:
        abort(404)
    try:
        publictag = PublicTag.get(PublicTag.userkey == userkey, PublicTag.tag == tag)
    except PublicTag.DoesNotExist:
        publictag = None
    if not publictag:
        newpublictag = PublicTag()
        newpublictag.generate_key()
        newpublictag.userkey = userkey
        newpublictag.tag = tag
        newpublictag.save()

        message = 'Public link to this tag created'
        return redirect(url_for('tag', userkey=userkey, tag=tag, message=message))
    else:
        message = 'Public link already existed'
        return redirect(url_for('tag', userkey=userkey, tag=tag, message=message))

@app.route('/<systemkey>/adduser')
def adduser(systemkey):
    """ Add user endpoint, convenience """
    if systemkey == settings.SYSTEMKEY:
        newuser = User()
        newuser.generate_key()
        newuser.username = 'Nomen Nescio'
        newuser.save()
        all_tags[newuser.key] = []
        return redirect('/' + newuser.key, code=302)
    else:
        abort(404)

# Initialise
# create the bookmark, user and public tag tables if they do not exist
Bookmark.create_table(True)
User.create_table(True)
PublicTag.create_table(True)

users = User.select()
print 'Current user keys:'
for user in users:
    all_tags[user.key] = get_tags_for_user(user.key)
    print user.key

if __name__ == '__main__':
    # run the application
    app.run(port=9999, debug=True)
<commit_msg>Stub for sort method<commit_after>import datetime
import hashlib
import os
import sys
import requests
import shutil
import bs4
from more_itertools import unique_everseen
from urlparse import urlparse

from utilkit import datetimeutil

from flask import Flask, abort, redirect, render_template, request, url_for
from flask_peewee.db import Database
#from flask_peewee.utils import get_object_or_404
from peewee import *

try:
    import settings
except ImportError:
    print('Copy settings_example.py to settings.py and set the configuration to your own preferences')
    sys.exit(1)

# app configuration
APP_ROOT = os.path.dirname(os.path.realpath(__file__))
MEDIA_ROOT = os.path.join(APP_ROOT, 'static')
MEDIA_URL = '/static/'
DATABASE = {
    'name': os.path.join(APP_ROOT, 'bookmarks.db'),
    'engine': 'peewee.SqliteDatabase',
}
#PHANTOM = '/usr/local/bin/phantomjs'
#SCRIPT = os.path.join(APP_ROOT, 'screenshot.js')

# create our flask app and a database wrapper
app = Flask(__name__)
app.config.from_object(__name__)
db = Database(app)

# set custom url for the app, for example '/bookmarks'
try:
    app.config['APPLICATION_ROOT'] = settings.APPLICATION_ROOT
except AttributeError:
    pass

# Cache the tags
all_tags = {}

def clean_tags(tags_list):
    tags_res = [x.strip() for x in tags_list]
    tags_res = list(unique_everseen(tags_res))
    tags_res.sort()
    if tags_res and tags_res[0] == '':
        del tags_res[0]
    return tags_res

class User(db.Model):
    """ User account """
    username = CharField()
    key = CharField()
    created_date = DateTimeField(default=datetime.datetime.now)

    def generate_key(self):
        """ Generate userkey """
        self.key = os.urandom(24).encode('hex')
        return self.key

class Bookmark(db.Model):
    """ Bookmark instance, connected to User """
    # Foreign key to User
    userkey = CharField()

    title = CharField(default='')
    url = CharField()
    created_date = DateTimeField(default=datetime.datetime.now)
    modified_date = DateTimeField(null=True)
    #image = CharField(default='')
    url_hash = CharField(default='')
    tags = CharField(default='')
    starred = BooleanField(default=False)

    # Website (domain) favicon
    favicon = CharField(null=True)

    # Status code: 200 is OK, 404 is not found, for example (showing an error)
    http_status = IntegerField(default=200)

    class Meta:
        ordering = (('created_date', 'desc'),)

    #def fetch_image(self):
    #    url_hash = hashlib.md5(self.url).hexdigest()
    #    filename = 'bookmark-%s.png' % url_hash

    #    outfile = os.path.join(MEDIA_ROOT, filename)
    #    params = [PHANTOM, SCRIPT, self.url, outfile]

    #    exitcode = subprocess.call(params)
    #    if exitcode == 0:
    #        self.image = os.path.join(MEDIA_URL, filename)

    def set_hash(self):
        """ Generate hash """
        self.url_hash = hashlib.md5(self.url).hexdigest()

    def set_title_from_source(self):
        """ Request the title by requesting the source url """
        try:
            result = requests.get(self.url)
            self.http_status = result.status_code
        except:
            # For example 'MissingSchema: Invalid URL 'abc': No schema supplied. Perhaps you meant http://abc?'
            self.http_status = 404
        if self.http_status == 200:
            html = bs4.BeautifulSoup(result.text, 'html.parser')
            try:
                self.title = html.title.text.strip()
            except AttributeError:
                self.title = ''
        return self.title

    def set_status_code(self):
        """ Check the HTTP status of the url, as it might not exist for example """
        result = requests.head(self.url)
        self.http_status = result.status_code
        return self.http_status

    def set_favicon(self):
        """ Fetch favicon for the domain """
        # http://codingclues.eu/2009/retrieve-the-favicon-for-any-url-thanks-to-google/
        u = urlparse(self.url)
        domain = u.netloc
        filename = os.path.join(MEDIA_ROOT, 'favicons/' + domain + '.png')
        # if file exists, don't re-download it
        response = requests.get('http://www.google.com/s2/favicons?domain=' + domain, stream=True)
        with open(filename, 'wb') as out_file:
            shutil.copyfileobj(response.raw, out_file)
        del response
        self.favicon = domain + '.png'

    def set_tags(self, tags):
        """ Set tags from `tags`, strip and sort them """
        tags_split = tags.split(',')
        tags_clean = clean_tags(tags_split)
        self.tags = ','.join(tags_clean)

    @property
    def tags_list(self):
        """ Get the tags as a list, iterable in template """
        if self.tags:
            return self.tags.split(',')
        else:
            return []

    def to_dict(self):
        result = {
                'title': self.title,
                'url': self.url,
                'created':  datetimeutil.datetime_to_string(self.created_date),
                'url_hash': self.url_hash,
                'tags': self.tags,
                }
        return result

class PublicTag(db.Model):
    """ Publicly shared tag """
    tagkey = CharField()
    userkey = CharField()
    tag = CharField()
    created_date = DateTimeField(default=datetime.datetime.now)

    def generate_key(self):
        """ Generate hash-based key for publicly shared tag """
        self.tagkey = os.urandom(16).encode('hex')

def get_tags_for_user(userkey):
    """ Extract all tags from the bookmarks """
    bookmarks = Bookmark.select().filter(Bookmark.userkey == userkey)
    tags = []
    for bookmark in bookmarks:
        tags += bookmark.tags_list
    return clean_tags(tags)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.route('/')
def index():
    """ Homepage, point visitors to project page """
    return render_template('index.html')

@app.route('/<userkey>', methods=['GET', 'POST'])
@app.route('/<userkey>/sort/<sortmethod>', methods=['GET', 'POST'])
def bookmarks(userkey, sortmethod = None):
    """ User homepage, list their bookmarks, optionally filtered and/or sorted """
    #return object_list('bookmarks.html', Bookmark.select())
    #user = User.select(key=userkey)
    #if user:
    #    bookmarks = Bookmark.select(User=user)
    #    return render_template('bookmarks.html', bookmarks)
    #else:
    #    abort(404)
    message = request.args.get('message')
    if request.method == 'POST':
        filter_on = request.form['filter']
        bookmarks = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.title.contains(filter_on)).order_by(Bookmark.created_date.desc())
        return render_template('bookmarks.html', bookmarks=bookmarks, userkey=userkey, tags=all_tags[userkey], filter=filter_on, message=message)
    else:
        bookmarks = Bookmark.select().where(Bookmark.userkey == userkey).order_by(Bookmark.created_date.desc())
        return render_template('bookmarks.html', bookmarks=bookmarks, userkey=userkey, tags=all_tags[userkey], message=message)

#@app.route('/<userkey>/<urlhash>')
#def viewbookmark(userkey, urlhash):
#    """ Bookmark detail view """
#    bookmark = Bookmark.select(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey)
#    return render_template('viewbookmark.html', userkey=userkey, bookmark=bookmark)

@app.route('/<userkey>/<urlhash>/json')
def viewbookmarkjson(userkey, urlhash):
    """ Serialise bookmark to json """
    bookmark = Bookmark.select(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey)
    return bookmark.to_dict()

@app.route('/<userkey>/<urlhash>')
@app.route('/<userkey>/<urlhash>/edit')
def editbookmark(userkey, urlhash):
    """ Bookmark edit form """
    # bookmark = getbyurlhash()
    bookmark = Bookmark.get(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey)
    message = request.args.get('message')
    return render_template('edit.html', action='Edit bookmark', userkey=userkey, bookmark=bookmark, message=message, formaction='edit')

@app.route('/<userkey>/add')
def addbookmark(userkey):
    """ Bookmark add form """
    bookmark = Bookmark(title='', url='', tags='')
    return render_template('edit.html', action='Add bookmark', userkey=userkey, bookmark=bookmark)

def updatebookmark(userkey, request, urlhash = None):
    """ Add (no urlhash) or edit (urlhash is set) a bookmark """
    title = request.form.get('title')
    url = request.form.get('url')
    tags = request.form.get('tags')
    starred = False
    if request.form.get('starred'):
        starred = True

    if url and not urlhash:
        # New bookmark
        bookmark, created = Bookmark.get_or_create(url=url, userkey=userkey)
        if not created:
            message = 'Existing bookmark, did not overwrite with new values'
            return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash, message=message))
    elif url:
        # Existing bookmark, get from DB
        bookmark = Bookmark.get(userkey == userkey, Bookmark.url_hash == urlhash)
        # Editing this bookmark, set modified_date to now
        bookmark.modified_date = datetime.datetime.now()

    bookmark.title = title
    bookmark.url = url
    bookmark.starred = starred
    bookmark.set_tags(tags)
    bookmark.set_hash()
    #bookmark.fetch_image()
    if not title:
        # Title was empty, automatically fetch it from the url, will also update the status code
        bookmark.set_title_from_source()
    else:
        bookmark.set_status_code()

    if bookmark.http_status == 200:
        bookmark.set_favicon()

    bookmark.save()
    return bookmark

@app.route('/<userkey>/adding', methods=['GET', 'POST'])
#@app.route('/<userkey>/adding')
def addingbookmark(userkey):
    """ Add the bookmark from form submit by /add """

    if request.method == 'POST':
        bookmark = updatebookmark(userkey, request)
        if type(bookmark).__name__ == 'Response':
            return bookmark
        all_tags[userkey] = get_tags_for_user(userkey)
        return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash))
    return redirect(url_for('add'))

@app.route('/<userkey>/<urlhash>/editing', methods=['GET', 'POST'])
def editingbookmark(userkey, urlhash):
    """ Edit the bookmark from form submit """

    if request.method == 'POST':
        bookmark = updatebookmark(userkey, request, urlhash=urlhash)
        all_tags[userkey] = get_tags_for_user(userkey)
        return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash))
    return redirect(url_for('add'))

@app.route('/<userkey>/<urlhash>/delete', methods=['GET', 'POST'])
def deletingbookmark(userkey, urlhash):
    """ Delete the bookmark from form submit by <urlhash>/delete """
    Bookmark.delete().where(Bookmark.userkey==userkey, Bookmark.url_hash==urlhash)
    message = 'Bookmark deleted'
    all_tags[userkey] = get_tags_for_user(userkey)
    return redirect(url_for('bookmarks', userkey=userkey, message=message))

@app.route('/<userkey>/tags')
def tags(userkey):
    """ Overview of all tags used by user """
    tags = get_tags_for_user(userkey)
    print tags
    return render_template('tags.html', tags=tags, userkey=userkey)

@app.route('/<userkey>/tag/<tag>')
def tag(userkey, tag):
    """ Overview of all bookmarks with a certain tag """
    bookmarks = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.tags.contains(tag))
    tags = get_tags_for_user(userkey)
    pageheader = 'tag: ' + tag
    message = request.args.get('message')

    try:
        publictag = PublicTag.get(PublicTag.userkey == userkey, PublicTag.tag == tag)
    except PublicTag.DoesNotExist:
        publictag = None

    return render_template('bookmarks.html', bookmarks=bookmarks, userkey=userkey, tags=tags, tag=tag, publictag=publictag, action=pageheader, message=message)

@app.route('/pub/<tagkey>')
def publictag(tagkey):
    """ Read-only overview of the bookmarks in the userkey/tag of this PublicTag """
    #this_tag = get_object_or_404(PublicTag.select().where(PublicTag.tagkey == tagkey))
    try:
        this_tag = PublicTag.get(PublicTag.tagkey == tagkey)
        bookmarks = Bookmark.select().where(Bookmark.userkey == this_tag.userkey, Bookmark.tags.contains(this_tag.tag))
        return render_template('publicbookmarks.html', bookmarks=bookmarks, tag=tag, action=this_tag.tag)
    except PublicTag.DoesNotExist:
        abort(404)

@app.route('/<userkey>/<tag>/makepublic', methods=['GET', 'POST'])
def addpublictag(userkey, tag):
    #user = get_object_or_404(User.get(User.key == userkey))
    try:
        User.get(User.key == userkey)
    except User.DoesNotExist:
        abort(404)
    try:
        publictag = PublicTag.get(PublicTag.userkey == userkey, PublicTag.tag == tag)
    except PublicTag.DoesNotExist:
        publictag = None
    if not publictag:
        newpublictag = PublicTag()
        newpublictag.generate_key()
        newpublictag.userkey = userkey
        newpublictag.tag = tag
        newpublictag.save()

        message = 'Public link to this tag created'
        return redirect(url_for('tag', userkey=userkey, tag=tag, message=message))
    else:
        message = 'Public link already existed'
        return redirect(url_for('tag', userkey=userkey, tag=tag, message=message))

@app.route('/<systemkey>/adduser')
def adduser(systemkey):
    """ Add user endpoint, convenience """
    if systemkey == settings.SYSTEMKEY:
        newuser = User()
        newuser.generate_key()
        newuser.username = 'Nomen Nescio'
        newuser.save()
        all_tags[newuser.key] = []
        return redirect('/' + newuser.key, code=302)
    else:
        abort(404)

# Initialise
# create the bookmark, user and public tag tables if they do not exist
Bookmark.create_table(True)
User.create_table(True)
PublicTag.create_table(True)

users = User.select()
print 'Current user keys:'
for user in users:
    all_tags[user.key] = get_tags_for_user(user.key)
    print user.key

if __name__ == '__main__':
    # run the application
    app.run(port=9999, debug=True)
<|endoftext|>


<commit_before>#!/usr/bin/env python

import subprocess
import sys
import shutil

XPKG = sys.argv[1]
OUTPUT = sys.argv[2]

#
# BUILD
#
def mdbuild (solution_path):
	xbuild_args = [
		"/Applications/MonoDevelop.app/Contents/MacOS/mdtool",
		"build",
		"--configuration:Release",
		solution_path,
	]
	err = subprocess.call (xbuild_args)
	if err:
		raise Exception ("MDTOOL RETURNED %s" % err)

# mdbuild ("MonoDroid/Xamarin.Mobile Android.sln")
# mdbuild ("MonoTouch/Xamarin.Mobile iOS.sln")

# Assemble docs
err = subprocess.call (["make", "-C", "docs", "publish"])
if err:
	raise Exception ("MAKE RETURNED %s" % err)

# Move refs
IOSDLL="MonoTouch/Xamarin.Mobile/bin/iPhoneSimulator/Release/Xamarin.Mobile.dll"
ANDDLL="MonoDroid/Xamarin.Mobile/bin/Release/Xamarin.Mobile.dll"
WPDLL ="WindowsPhone/Xamarin.Mobile/bin/Release/Xamarin.Mobile.dll"
WP8DLL ="WindowsPhone8/Xamarin.Mobile/bin/Release/Xamarin.Mobile.dll"
W8DLL = "WindowsRT/Xamarin.Mobile/bin/Release/Xamarin.Mobile.dll"

shutil.copy (IOSDLL, "MonoTouch/Samples/Xamarin.Mobile.dll")
shutil.copy (ANDDLL, "MonoDroid/Samples/Xamarin.Mobile.dll")
shutil.copy (WPDLL, "WindowsPhone/Samples/Xamarin.Mobile.dll")
shutil.copy (WP8DLL, "WindowsPhone8/Samples/Xamarin.Mobile.dll")
shutil.copy (W8DLL, "WindowsRT/Samples/Xamarin.Mobile.dll")

#
# PACKAGE
#
xpkg_args = [
	"mono",
	XPKG,
	"create",
	OUTPUT,
	"--name", "Xamarin.Mobile",
	"--details", "Details.md",
	"--license", "LICENSE.md",
	"--getting-started", "GettingStarted.md",
	"--publisher", "Xamarin",
	"--publisher-url", "http://xamarin.com",
	"--docs", "http://xamarin.com/mobileapi",
	"--monodoc", "docs",
	"--summary", "Xamarin.Mobile is a library that exposes a single set of APIs for accessing common mobile device functionality across iOS, Android and Windows platforms.",
	"--icon", "component/icon_128x128.png",
	"--icon", "component/icon_512x512.png",
	"--library", "ios:" + IOSDLL,
	"--library", "android:" + ANDDLL,
	"--library", "winphone-7.1:" + WPDLL,
	"--library", "winphone-8:" + WP8DLL,
	"--library", "winrt:" + W8DLL,
	"--sample", "Xamarin.Mobile Android Samples. Android samples for the AddressBook, MediaPicker and Geolocator APIs.:MonoDroid/Samples/Xamarin.Mobile.Android.Samples.sln",
	"--sample", "Xamarin.Mobile iOS Samples. iOS samples for the AddressBook, MediaPicker and Geolocator APIs.:MonoTouch/Samples/Xamarin.Mobile.iOS.Samples.sln",
	"--sample", "Xamarin.Mobile Windows Phone 7.5 Samples. Windows Phone 7 samples for the AddressBook, MediaPicker and Geolocator APIs.:WindowsPhone/Samples/Xamarin.Mobile.WP.Samples.sln",
	"--sample", "Xamarin.Mobile Windows Phone 8 Samples. Windows Phone 8 Samples for the AddressBook, MediaPicker and Geolocator APIs.:WindowsPhone8/Samples/Xamarin.Mobile.WP8.Samples.sln",
	"--sample", "Xamarin.Mobile Windows 8 Samples. Windows 8 Samples for the MediaPicker and Geolocator APIs.:WindowsRT/Samples/Xamarin.Mobile.WinRT.Samples.sln"
]
subprocess.call (xpkg_args)
<commit_msg>--sample-no-manifest the windows samples<commit_after>#!/usr/bin/env python

import subprocess
import sys
import shutil

XPKG = sys.argv[1]
OUTPUT = sys.argv[2]

#
# BUILD
#
def mdbuild (solution_path):
	xbuild_args = [
		"/Applications/MonoDevelop.app/Contents/MacOS/mdtool",
		"build",
		"--configuration:Release",
		solution_path,
	]
	err = subprocess.call (xbuild_args)
	if err:
		raise Exception ("MDTOOL RETURNED %s" % err)

# mdbuild ("MonoDroid/Xamarin.Mobile Android.sln")
# mdbuild ("MonoTouch/Xamarin.Mobile iOS.sln")

# Assemble docs
err = subprocess.call (["make", "-C", "docs", "publish"])
if err:
	raise Exception ("MAKE RETURNED %s" % err)

# Move refs
IOSDLL="MonoTouch/Xamarin.Mobile/bin/iPhoneSimulator/Release/Xamarin.Mobile.dll"
ANDDLL="MonoDroid/Xamarin.Mobile/bin/Release/Xamarin.Mobile.dll"
WPDLL ="WindowsPhone/Xamarin.Mobile/bin/Release/Xamarin.Mobile.dll"
WP8DLL ="WindowsPhone8/Xamarin.Mobile/bin/Release/Xamarin.Mobile.dll"
W8DLL = "WindowsRT/Xamarin.Mobile/bin/Release/Xamarin.Mobile.dll"

shutil.copy (IOSDLL, "MonoTouch/Samples/Xamarin.Mobile.dll")
shutil.copy (ANDDLL, "MonoDroid/Samples/Xamarin.Mobile.dll")
shutil.copy (WPDLL, "WindowsPhone/Samples/Xamarin.Mobile.dll")
shutil.copy (WP8DLL, "WindowsPhone8/Samples/Xamarin.Mobile.dll")
shutil.copy (W8DLL, "WindowsRT/Samples/Xamarin.Mobile.dll")

#
# PACKAGE
#
xpkg_args = [
	"mono",
	XPKG,
	"create",
	OUTPUT,
	"--name", "Xamarin.Mobile",
	"--details", "Details.md",
	"--license", "LICENSE.md",
	"--getting-started", "GettingStarted.md",
	"--publisher", "Xamarin",
	"--publisher-url", "http://xamarin.com",
	"--docs", "http://xamarin.com/mobileapi",
	"--monodoc", "docs",
	"--summary", "Xamarin.Mobile is a library that exposes a single set of APIs for accessing common mobile device functionality across iOS, Android and Windows platforms.",
	"--icon", "component/icon_128x128.png",
	"--icon", "component/icon_512x512.png",
	"--library", "ios:" + IOSDLL,
	"--library", "android:" + ANDDLL,
	"--library", "winphone-7.1:" + WPDLL,
	"--library", "winphone-8:" + WP8DLL,
	"--library", "winrt:" + W8DLL,
	"--sample", "Xamarin.Mobile Android Samples. Android samples for the AddressBook, MediaPicker and Geolocator APIs.:MonoDroid/Samples/Xamarin.Mobile.Android.Samples.sln",
	"--sample", "Xamarin.Mobile iOS Samples. iOS samples for the AddressBook, MediaPicker and Geolocator APIs.:MonoTouch/Samples/Xamarin.Mobile.iOS.Samples.sln",
	"--sample-no-manifest", "Xamarin.Mobile Windows Phone 7.5 Samples. Windows Phone 7 samples for the AddressBook, MediaPicker and Geolocator APIs.:WindowsPhone/Samples/Xamarin.Mobile.WP.Samples.sln",
	"--sample-no-manifest", "Xamarin.Mobile Windows Phone 8 Samples. Windows Phone 8 Samples for the AddressBook, MediaPicker and Geolocator APIs.:WindowsPhone8/Samples/Xamarin.Mobile.WP8.Samples.sln",
	"--sample-no-manifest", "Xamarin.Mobile Windows 8 Samples. Windows 8 Samples for the MediaPicker and Geolocator APIs.:WindowsRT/Samples/Xamarin.Mobile.WinRT.Samples.sln"
]
subprocess.call (xpkg_args)
<|endoftext|>


<commit_before>
#  IATI Data Quality, tools for Data QA on IATI-formatted  publications
#  by Mark Brough, Martin Keegan, Ben Webb and Jennifer Smith
#
#  Copyright (C) 2013  Publish What You Fund
#
#  This programme is free software; you may redistribute and/or modify
#  it under the terms of the GNU Affero General Public License v3.0

from flask import render_template, flash, request, Markup, \
    session, redirect, url_for, escape, Response, abort, send_file
import StringIO
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import (LoginManager, current_user, login_required,
                            login_user, logout_user, UserMixin, AnonymousUser,
                            confirm_login, fresh_login_required)
from sqlalchemy import func
from datetime import datetime

from iatidataquality import app
from iatidataquality import db

import os
import sys
import json

current = os.path.dirname(os.path.abspath(__file__))
parent = os.path.dirname(current)
sys.path.append(parent)

from iatidq import dqdownload, dqregistry, dqindicators, dqorganisations, dqpackages
import aggregation

from iatidq.models import *

import StringIO
import unicodecsv
import tempfile
import spreadsheet

@app.route("/publishers/")
def publishers():
    p_groups = PackageGroup.query.order_by(
        PackageGroup.name).all()

    pkgs = Package.query.order_by(Package.package_name).all()
    return render_template("packagegroups.html", p_groups=p_groups, pkgs=pkgs)

def _publisher_detail_ungrouped(p_group):
    return db.session.query(Indicator,
                                     Test,
                                     AggregateResult.results_data,
                                     AggregateResult.results_num,
                                     AggregateResult.result_hierarchy,
                                     AggregateResult.package_id,
                                     func.max(AggregateResult.runtime_id)
        ).filter(PackageGroup.id==p_group.id)

def _publisher_detail(p_group):
    aggregate_results = _publisher_detail_ungrouped(p_group)\
        .group_by(Indicator,
                   AggregateResult.result_hierarchy,
                   Test,
                   AggregateResult.package_id,
                   AggregateResult.results_data,
                   AggregateResult.results_num
        ).join(IndicatorTest
        ).join(Test
        ).join(AggregateResult
        ).join(Package
        ).join(PackageGroup
        ).all()

    """pconditions = PublisherCondition.query.filter_by(
        publisher_id=p_group.id).all()"""
    # Publisherconditions have been removed in favour
    #  of organisation conditions
    pconditions = {}

    db.session.commit()
    return aggregation.agr_results(aggregate_results,
                                   conditions=pconditions,
                                   mode="publisher")

@app.route("/publishers/<id>/detail")
def publisher_detail(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    aggregate_results = _publisher_detail(p_group)
    latest_runtime=1

    txt = render_template("publisher.html", p_group=p_group, pkgs=pkgs,
                           results=aggregate_results, runtime=latest_runtime)
    return txt

@app.route("/publishers/<id>/detail.json")
def publisher_detail_json(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    aggregate_results = _publisher_detail(p_group)
    latest_runtime=1
    """except Exception, e:
        latest_runtime = None
        aggregate_results = None"""

    return json.dumps(aggregate_results, indent=2)

@app.route("/publishers/<id>/detail.csv")
def publisher_detail_csv(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    aggregate_results = _publisher_detail(p_group)
    latest_runtime=1
    """except Exception, e:
        latest_runtime = None
        aggregate_results = None"""

    print >>sys.stderr, aggregate_results.keys()
    print "---"

    def gen_csv():
        s = StringIO.StringIO()
        w = unicodecsv.writer(s)
        for k, v in aggregate_results[1].iteritems():
            s.seek(0)
            w.writerow((k, v["test"]["description"]))
            s.seek(0)
            yield s.read()

    return Response(gen_csv(), mimetype="text/csv")

@app.route("/publishers/<id>/detail.xls")
def publisher_detail_xls(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    aggregate_results = _publisher_detail(p_group)
    latest_runtime=1
    """except Exception, e:
        latest_runtime = None
        aggregate_results = None"""

    print >>sys.stderr, aggregate_results.keys()
    print "---"

    filename = tempfile.mktemp()
    try:
        spreadsheet.workbook_from_aggregation(filename, aggregate_results)
        with file(filename) as f:
            data = f.read()
            return Response(data, mimetype='application/vnd.ms-excel')
    finally:
        if os.path.exists(filename):
            os.unlink(filename)

@app.route("/publishers/<id>/")
def publisher(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    """try:"""
    aggregate_results = _publisher_detail_ungrouped(p_group)\
        .group_by(AggregateResult.result_hierarchy,
                   Test,
                   AggregateResult.package_id,
                   Indicator,
                   AggregateResult.results_data,
                   AggregateResult.results_num,
                   AggregateResult.package_id
        ).join(IndicatorTest
        ).join(Test
        ).join(AggregateResult
        ).join(Package
        ).join(PackageGroup
        ).all()

    """pconditions = PublisherCondition.query.filter_by(
        publisher_id=p_group.id).all()"""
    # Publisher Conditions have been removed in favour of
    #  organisation conditions.
    pconditions = {}

    aggregate_results = aggregation.agr_results(aggregate_results,
                                                conditions=pconditions,
                                                mode="publisher_indicators")
    latest_runtime=1
    """except Exception, e:
        latest_runtime = None
        aggregate_results = None"""

    return render_template("publisher_indicators.html", p_group=p_group, pkgs=pkgs,
                           results=aggregate_results, runtime=latest_runtime)

<commit_msg>Remove latest runtime<commit_after>
#  IATI Data Quality, tools for Data QA on IATI-formatted  publications
#  by Mark Brough, Martin Keegan, Ben Webb and Jennifer Smith
#
#  Copyright (C) 2013  Publish What You Fund
#
#  This programme is free software; you may redistribute and/or modify
#  it under the terms of the GNU Affero General Public License v3.0

from flask import render_template, flash, request, Markup, \
    session, redirect, url_for, escape, Response, abort, send_file
import StringIO
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import (LoginManager, current_user, login_required,
                            login_user, logout_user, UserMixin, AnonymousUser,
                            confirm_login, fresh_login_required)
from sqlalchemy import func
from datetime import datetime

from iatidataquality import app
from iatidataquality import db

import os
import sys
import json

current = os.path.dirname(os.path.abspath(__file__))
parent = os.path.dirname(current)
sys.path.append(parent)

from iatidq import dqdownload, dqregistry, dqindicators, dqorganisations, dqpackages
import aggregation

from iatidq.models import *

import StringIO
import unicodecsv
import tempfile
import spreadsheet

@app.route("/publishers/")
def publishers():
    p_groups = PackageGroup.query.order_by(
        PackageGroup.name).all()

    pkgs = Package.query.order_by(Package.package_name).all()
    return render_template("packagegroups.html", p_groups=p_groups, pkgs=pkgs)

def _publisher_detail_ungrouped(p_group):
    return db.session.query(Indicator,
                                     Test,
                                     AggregateResult.results_data,
                                     AggregateResult.results_num,
                                     AggregateResult.result_hierarchy,
                                     AggregateResult.package_id,
                                     func.max(AggregateResult.runtime_id)
        ).filter(PackageGroup.id==p_group.id)

def _publisher_detail(p_group):
    aggregate_results = _publisher_detail_ungrouped(p_group)\
        .group_by(Indicator,
                   AggregateResult.result_hierarchy,
                   Test,
                   AggregateResult.package_id,
                   AggregateResult.results_data,
                   AggregateResult.results_num
        ).join(IndicatorTest
        ).join(Test
        ).join(AggregateResult
        ).join(Package
        ).join(PackageGroup
        ).all()

    """pconditions = PublisherCondition.query.filter_by(
        publisher_id=p_group.id).all()"""
    # Publisherconditions have been removed in favour
    #  of organisation conditions
    pconditions = {}

    db.session.commit()
    return aggregation.agr_results(aggregate_results,
                                   conditions=pconditions,
                                   mode="publisher")

@app.route("/publishers/<id>/detail")
def publisher_detail(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    aggregate_results = _publisher_detail(p_group)

    txt = render_template("publisher.html", p_group=p_group, pkgs=pkgs,
                           results=aggregate_results)
    return txt

@app.route("/publishers/<id>/detail.json")
def publisher_detail_json(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    aggregate_results = _publisher_detail(p_group)
    latest_runtime=1
    """except Exception, e:
        latest_runtime = None
        aggregate_results = None"""

    return json.dumps(aggregate_results, indent=2)

@app.route("/publishers/<id>/detail.csv")
def publisher_detail_csv(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    aggregate_results = _publisher_detail(p_group)
    latest_runtime=1
    """except Exception, e:
        latest_runtime = None
        aggregate_results = None"""

    print >>sys.stderr, aggregate_results.keys()
    print "---"

    def gen_csv():
        s = StringIO.StringIO()
        w = unicodecsv.writer(s)
        for k, v in aggregate_results[1].iteritems():
            s.seek(0)
            w.writerow((k, v["test"]["description"]))
            s.seek(0)
            yield s.read()

    return Response(gen_csv(), mimetype="text/csv")

@app.route("/publishers/<id>/detail.xls")
def publisher_detail_xls(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    aggregate_results = _publisher_detail(p_group)
    latest_runtime=1
    """except Exception, e:
        latest_runtime = None
        aggregate_results = None"""

    print >>sys.stderr, aggregate_results.keys()
    print "---"

    filename = tempfile.mktemp()
    try:
        spreadsheet.workbook_from_aggregation(filename, aggregate_results)
        with file(filename) as f:
            data = f.read()
            return Response(data, mimetype='application/vnd.ms-excel')
    finally:
        if os.path.exists(filename):
            os.unlink(filename)

@app.route("/publishers/<id>/")
def publisher(id=None):
    p_group = PackageGroup.query.filter_by(name=id).first_or_404()

    pkgs = db.session.query(Package
            ).filter(Package.package_group == p_group.id
            ).order_by(Package.package_name).all()

    """try:"""
    aggregate_results = _publisher_detail_ungrouped(p_group)\
        .group_by(AggregateResult.result_hierarchy,
                   Test,
                   AggregateResult.package_id,
                   Indicator,
                   AggregateResult.results_data,
                   AggregateResult.results_num,
                   AggregateResult.package_id
        ).join(IndicatorTest
        ).join(Test
        ).join(AggregateResult
        ).join(Package
        ).join(PackageGroup
        ).all()

    """pconditions = PublisherCondition.query.filter_by(
        publisher_id=p_group.id).all()"""
    # Publisher Conditions have been removed in favour of
    #  organisation conditions.
    pconditions = {}

    aggregate_results = aggregation.agr_results(aggregate_results,
                                                conditions=pconditions,
                                                mode="publisher_indicators")
    latest_runtime=1
    """except Exception, e:
        latest_runtime = None
        aggregate_results = None"""

    return render_template("publisher_indicators.html", p_group=p_group, pkgs=pkgs,
                           results=aggregate_results, runtime=latest_runtime)

<|endoftext|>