<commit_before>'''
Compare local fonts against fonts available on fonts.google.com
'''
from __future__ import print_function
from flask import Flask, render_template
from fontTools.ttLib import TTFont
import glob
import ntpath
import requests
import re
import os

FONT_WEIGHTS = {
    # Roman
    'Thin': '100',
    'ExtraLight': '200',
    'Light': '300',
    'Regular': '400',
    'Medium': '500',
    'SemiBold': '600',
    'Bold': '700',
    'ExtraBold': '800',
    'Black': '900',
    # Italics
    'ThinItalic': '250i',
    'ExtraLightItalic': '275i',
    'LightItalic': '300i',
    'Italic': '400i',
    'MediumItalic': '500i',
    'SemiBoldItalic': '600i',
    'BoldItalic': '700i',
    'ExtraBoldItalic': '800i',
    'BlackItalic': '900i',
}

FONT_EXCEPTIONS = [
    'VT323'
]

FONT_FALLBACK = 'Inconsolata'

app = Flask(__name__)

dummy_text = open('./dummy_text.txt', 'r').read()

def _font_exists(url):
    '''Check if the url mataches the Google fonts api url'''
    if requests.get(url).status_code == 200:
        return True
    return False

def _convert_camelcase(fam_name, seperator=' '):
    '''RubikMonoOne > Rubik+Mono+One'''
    if fam_name not in FONT_EXCEPTIONS:
        return re.sub('(?!^)([A-Z]+)', r'%s\1' % seperator, fam_name)
    else:
        return fam_name

def fonts_on_google(local_fonts):
    '''Find the local font on fonts.google.com, if not found
    return a fallback font'''
    fonts = []
    url_prefix = 'https://fonts.googleapis.com/css?family='
    fallback_font = url_prefix + FONT_FALLBACK

    for path, font, span_name in local_fonts:
        fam_name, style = font.split('-')

        api_fam_name = _convert_camelcase(fam_name, '+')
        css_fam_name = _convert_camelcase(fam_name)

        url = '%s%s:%s' % (url_prefix, api_fam_name, FONT_WEIGHTS[style])

        if _font_exists(url):
            if 'i' in FONT_WEIGHTS[style]:
                span_name = '%s-%s-italic-online' % (fam_name, FONT_WEIGHTS[style][:-1])

                fonts.append((fam_name,
                              css_fam_name,
                              span_name,
                              FONT_WEIGHTS[style][:-1],
                              'italic',
                              url))
            else:
                span_name = '%s-%s-normal-online' % (fam_name, FONT_WEIGHTS[style])
                fonts.append((fam_name,
                              css_fam_name,
                              span_name,
                              FONT_WEIGHTS[style],
                              'normal',
                              url))
        else:
            fonts.append(('Inconsolata',
                          'Inconsolata',
                          'Inconsolata-400-normal-online',
                          '400',
                          'normal',
                          fallback_font))
    return fonts

def font_glyphs(local_fonts):
    """return encoded glyphs for each font"""
    glyphs = {}

    for path, font, span_name in local_fonts:
        ttfont = TTFont(path)
        glyphs[font] = []

        cmap_tbl = ttfont['cmap'].getcmap(3, 1)
        glyphs[font] = cmap_tbl.cmap.items()
    return glyphs

@app.route("/")
def test_fonts():
    sep = os.path.sep
    # web browsers use unix slashes, instead of windows
    local_fonts = []
    ttfs_in_static = glob.glob("." + sep + "static" + sep + "*.ttf")
    for path in ttfs_in_static:
        path = path.replace('\\', '/')
        font_name = ntpath.basename(path)[:-4]
        span_name = '%s-new' % ntpath.basename(path)[:-4]
        local_fonts.append((path, font_name, span_name))

    local_fonts = sorted(local_fonts, key=lambda x: x[1])
    google_fonts = fonts_on_google(local_fonts)
    char_maps = font_glyphs(local_fonts)
    to_local_fonts = ','.join([i[2] for i in local_fonts])
    to_google_fonts = ','.join([i[2] for i in google_fonts])

    return render_template('index.html',
                           dummy_text=dummy_text,
                           local_fonts=local_fonts,
                           google_fonts=google_fonts,
                           char_maps=char_maps,
                           to_local_fonts=to_local_fonts,
                           to_google_fonts=to_google_fonts)

if __name__ == "__main__":
    app.config['STATIC_FOLDER'] = 'static'
    app.run(debug=True)
<commit_msg>main: close dummy_test file<commit_after>'''
Compare local fonts against fonts available on fonts.google.com
'''
from __future__ import print_function
from flask import Flask, render_template
from fontTools.ttLib import TTFont
import glob
import ntpath
import requests
import re
import os

FONT_WEIGHTS = {
    # Roman
    'Thin': '100',
    'ExtraLight': '200',
    'Light': '300',
    'Regular': '400',
    'Medium': '500',
    'SemiBold': '600',
    'Bold': '700',
    'ExtraBold': '800',
    'Black': '900',
    # Italics
    'ThinItalic': '250i',
    'ExtraLightItalic': '275i',
    'LightItalic': '300i',
    'Italic': '400i',
    'MediumItalic': '500i',
    'SemiBoldItalic': '600i',
    'BoldItalic': '700i',
    'ExtraBoldItalic': '800i',
    'BlackItalic': '900i',
}

FONT_EXCEPTIONS = [
    'VT323'
]

FONT_FALLBACK = 'Inconsolata'

app = Flask(__name__)

with open('./dummy_text.txt', 'r') as dummy_text_file:
    dummy_text = dummy_text_file.read()

def _font_exists(url):
    '''Check if the url mataches the Google fonts api url'''
    if requests.get(url).status_code == 200:
        return True
    return False

def _convert_camelcase(fam_name, seperator=' '):
    '''RubikMonoOne > Rubik+Mono+One'''
    if fam_name not in FONT_EXCEPTIONS:
        return re.sub('(?!^)([A-Z]+)', r'%s\1' % seperator, fam_name)
    else:
        return fam_name

def fonts_on_google(local_fonts):
    '''Find the local font on fonts.google.com, if not found
    return a fallback font'''
    fonts = []
    url_prefix = 'https://fonts.googleapis.com/css?family='
    fallback_font = url_prefix + FONT_FALLBACK

    for path, font, span_name in local_fonts:
        fam_name, style = font.split('-')

        api_fam_name = _convert_camelcase(fam_name, '+')
        css_fam_name = _convert_camelcase(fam_name)

        url = '%s%s:%s' % (url_prefix, api_fam_name, FONT_WEIGHTS[style])

        if _font_exists(url):
            if 'i' in FONT_WEIGHTS[style]:
                span_name = '%s-%s-italic-online' % (fam_name, FONT_WEIGHTS[style][:-1])

                fonts.append((fam_name,
                              css_fam_name,
                              span_name,
                              FONT_WEIGHTS[style][:-1],
                              'italic',
                              url))
            else:
                span_name = '%s-%s-normal-online' % (fam_name, FONT_WEIGHTS[style])
                fonts.append((fam_name,
                              css_fam_name,
                              span_name,
                              FONT_WEIGHTS[style],
                              'normal',
                              url))
        else:
            fonts.append(('Inconsolata',
                          'Inconsolata',
                          'Inconsolata-400-normal-online',
                          '400',
                          'normal',
                          fallback_font))
    return fonts

def font_glyphs(local_fonts):
    """return encoded glyphs for each font"""
    glyphs = {}

    for path, font, span_name in local_fonts:
        ttfont = TTFont(path)
        glyphs[font] = []

        cmap_tbl = ttfont['cmap'].getcmap(3, 1)
        glyphs[font] = cmap_tbl.cmap.items()
    return glyphs

@app.route("/")
def test_fonts():
    sep = os.path.sep
    # web browsers use unix slashes, instead of windows
    local_fonts = []
    ttfs_in_static = glob.glob("." + sep + "static" + sep + "*.ttf")
    for path in ttfs_in_static:
        path = path.replace('\\', '/')
        font_name = ntpath.basename(path)[:-4]
        span_name = '%s-new' % ntpath.basename(path)[:-4]
        local_fonts.append((path, font_name, span_name))

    local_fonts = sorted(local_fonts, key=lambda x: x[1])
    google_fonts = fonts_on_google(local_fonts)
    char_maps = font_glyphs(local_fonts)
    to_local_fonts = ','.join([i[2] for i in local_fonts])
    to_google_fonts = ','.join([i[2] for i in google_fonts])

    return render_template('index.html',
                           dummy_text=dummy_text,
                           local_fonts=local_fonts,
                           google_fonts=google_fonts,
                           char_maps=char_maps,
                           to_local_fonts=to_local_fonts,
                           to_google_fonts=to_google_fonts)

if __name__ == "__main__":
    app.config['STATIC_FOLDER'] = 'static'
    app.run(debug=True)
<|endoftext|>


<commit_before>'''
Harvester for the Addis Ababa University Institutional Repository for the SHARE project

Example API call: http://etd.aau.edu.et/oai/request?verb=ListRecords&metadataPrefix=oai_dc
'''
from __future__ import unicode_literals

from scrapi.base import OAIHarvester

class AauHarvester(OAIHarvester):
    short_name = 'addis_ababa'
    long_name = 'Addis Ababa University Institutional Repository'
    url = 'http://etd.aau.edu.et'

    base_url = 'http://etd.aau.edu.et/oai/request'
    property_list = ['date', 'type', 'identifier', 'setSpec']
    timezone_granularity = True
<commit_msg>update canonical and provider uris<commit_after>'''
Harvester for the Addis Ababa University Institutional Repository for the SHARE project

Example API call: http://etd.aau.edu.et/oai/request?verb=ListRecords&metadataPrefix=oai_dc
'''
from __future__ import unicode_literals

from scrapi.base import OAIHarvester
from scrapi.base import helpers

def format_uris_addis_ababa(*args):
    identifiers = helpers.gather_identifiers(args)
    provider_uris, object_uris = helpers.seperate_provider_object_uris(identifiers)

    for arg in args:
        if arg and 'http://hdl.handle.net/123456789/' in arg[0]:
            DOC_ID = arg[0].replace('http://hdl.handle.net/123456789/', '')
            canonical_uri = 'http://etd.aau.edu.et/handle/123456789/' + DOC_ID
            provider_uris = 'http://etd.aau.edu.et/handle/123456789/' + DOC_ID

    if not canonical_uri:
        raise ValueError('No Canonical URI was returned for this record.')

    return {
        'canonicalUri': canonical_uri,
        'objectUris': object_uris,
        'providerUris': provider_uris
    }

class AauHarvester(OAIHarvester):
    short_name = 'addis_ababa'
    long_name = 'Addis Ababa University Institutional Repository'
    url = 'http://etd.aau.edu.et'

    @property
    def schema(self):
        return helpers.updated_schema(self._schema, {
            "uris": ('//ns0:header/ns0:identifier/node()', '//dc:identifier/node()', format_uris_addis_ababa)
        })

    base_url = 'http://etd.aau.edu.et/oai/request'
    property_list = ['date', 'type', 'identifier', 'setSpec']
    timezone_granularity = True
<|endoftext|>


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

# Need to ensure that the i18n framework is enabled
from django.conf import settings
settings.configure(USE_I18N = True)

from django.utils.translation import templatize
import re
import os
import sys
import getopt
from itertools import dropwhile

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

exclude_svn_re = re.compile(r'/\.svn/?')
exclude_site_re = re.compile(r'^\./site-packages')

template_extensions = re.compile('.+\.(html|txt|rst|ical|csv)$')

def make_messages():
    localedir = None

    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:
        print "This script should be run from the django svn tree or your project or app tree."
        print "If you did indeed run it from the svn checkout or your project or application,"
        print "maybe you are just missing the conf/locale (in the django tree) or locale (for project"
        print "and application) directory?"
        print "make-messages.py doesn't create it automatically, you have to create it by hand if"
        print "you want to enable i18n for your project or application."
        sys.exit(1)

    (opts, args) = getopt.getopt(sys.argv[1:], 'l:d:va')

    lang = None
    domain = 'django'
    verbose = False
    all = False

    for o, v in opts:
        if o == '-l':
            lang = v
        elif o == '-d':
            domain = v
        elif o == '-v':
            verbose = True
        elif o == '-a':
            all = True

    if domain not in ('django', 'djangojs'):
        print "currently make-messages.py only supports domains 'django' and 'djangojs'"
        sys.exit(1)
    if (lang is None and not all) or domain is None:
        print "usage: make-messages.py -l <language>"
        print "   or: make-messages.py -a"
        sys.exit(1)

    languages = []

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

    for lang in languages:

        print "processing language", lang
        basedir = os.path.join(localedir, lang, '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("."):
            if not (exclude_svn_re.search(dirpath) or
                    exclude_site_re.match(dirpath)):
                all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            if domain == 'djangojs' and file.endswith('.js'):
                if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                src = open(os.path.join(dirpath, file), "rb").read()
                src = pythonize_re.sub('\n#', src)
                open(os.path.join(dirpath, '%s.py' % file), "wb").write(src)
                thefile = '%s.py' % file
                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:
                    print "errors happened while running xgettext on %s" % file
                    print errors
                    sys.exit(8)
                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.endswith('.py') or template_extensions.match(file)):
                thefile = file
                if template_extensions.match(file):
                    src = open(os.path.join(dirpath, file), "rb").read()
                    thefile = '%s.py' % file
                    open(os.path.join(dirpath, thefile), "wb").write(templatize(src))
                if verbose:
                    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:
                    print "errors happened while running xgettext on %s" % file
                    print errors
                    sys.exit(8)
                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, 'b')
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                print "errors happened while running msguniq"
                print errors
                sys.exit(8)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 'b')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    print "errors happened while running msgmerge"
                    print errors
                    sys.exit(8)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)

if __name__ == "__main__":
    make_messages()
<commit_msg>removing specialized make-messages as I don't think we need it anymore<commit_after><|endoftext|>


<commit_before>#!/usr/bin/python -u
#
# Copyright 2015 Janos Czentye <czentye@tmit.bme.hu>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Top starter script of ESCAPEv2 for convenient purposes
"""
import argparse
import os

def main ():
  # Implement parser options
  parser = argparse.ArgumentParser(
    description="ESCAPEv2: Extensible Service ChAin Prototyping Environment "
                "using Mininet, Click, NETCONF and POX",
    add_help=True,
    version="2.0.0")
  # Add optional arguments
  escape = parser.add_argument_group("ESCAPEv2 arguments")
  escape.add_argument("-a", "--agent", action="store_true", default=False,
                      help="run in AGENT mode: start the infrastructure layer "
                           "with the ROS REST-API (without the Service "
                           "sublayer (SAS))")
  escape.add_argument("-c", "--config", metavar="path", type=str,
                      # default="pox/escape.config",
                      help="override default config filename")
  escape.add_argument("-d", "--debug", action="count", default=0,
                      help="run the ESCAPE in debug mode (can use multiple "
                           "times for more verbose logging)")
  escape.add_argument("-e", "--environment", action="store_true", default=False,
                      help="run ESCAPEv2 in the pre-defined virtualenv")
  escape.add_argument("-f", "--full", action="store_true", default=False,
                      help="run the infrastructure layer also")
  escape.add_argument("-g", "--gui", action="store_true", default=False,
                      help="initiate the graph-viewer GUI app which "
                           "automatically connects to the ROS REST-API")
  escape.add_argument("-i", "--interactive", action="store_true", default=False,
                      help="run an interactive shell for observing internal "
                           "states")
  escape.add_argument("-p", "--POXlike", action="store_true", default=False,
                      help="start ESCAPEv2 in the actual interpreter using "
                           "./pox as working directory instead of using a "
                           "separate shell process with POX's own PYTHON env")
  escape.add_argument("-r", "--rosapi", action="store_true", default=False,
                      help="start the REST-API for the Resource Orchestration "
                           "sublayer (ROS)")
  escape.add_argument("-s", "--service", metavar="file", type=str,
                      help="skip the SAS REST-API initiation and read the "
                           "service request from the given file")
  escape.add_argument("-t", "--topo", metavar="file", type=str,
                      help="read the topology from the given file explicitly")
  escape.add_argument("-x", "--clean", action="store_true", default=False,
                      help="run the cleanup task standalone and kill remained "
                           "programs, interfaces, veth parts and junk files")
  escape.add_argument("-V", "--visualization", action="store_true",
                      default=False,
                      help="run the visualization module to send data to a "
                           "remote server")
  escape.add_argument("-4", "--cfor", action="store_true", default=False,
                      help="start the REST-API for the Cf-Or interface")
  # Add remaining POX modules
  escape.add_argument("modules", metavar="...", nargs=argparse.REMAINDER,
                      help="optional POX modules")
  # Parsing arguments
  args = parser.parse_args()

  if args.clean:
    # Tailor Python path for importing mics functions without initialize
    # escape or util packages.
    import sys
    mn = os.path.abspath(os.path.dirname(__file__) + "/mininet")
    misc = os.path.abspath(os.path.dirname(__file__) + "/pox/ext/escape/util")
    sys.path.append(mn)
    sys.path.append(misc)
    if os.geteuid() != 0:
      print "Cleanup process requires root privilege!"
      return
    else:
      print "Run cleaning process..."
      from misc import remove_junks
      remove_junks()
      print "Cleaned."
      return

  # Get base dir of this script
  base_dir = os.path.abspath(os.path.dirname(__file__))

  # Create absolute path for the pox.py initial script
  # Construct POX init command according to argument
  # basic command
  cmd = [os.path.join(base_dir, "pox/pox.py"), "unify"]

  # Run ESCAPE in VERBOSE logging level if it is needed
  if args.debug == 1:
    # Set logging level to DEBUG
    cmd.append("--loglevel=DEBUG")
  elif args.debug > 1:
    # Setup logging level to specific VERBOSE
    cmd.append("--loglevel=VERBOSE")
  else:
    # Use default loglevel: INFO
    pass

  # Run the Infrastructure Layer with the required root privilege
  if args.full:
    cmd.insert(0, "sudo")
    cmd.append("--full")

  # Initiate the rudimentary GUI
  if args.gui:
    cmd.append("--gui")

  # Read the service request NFFG from a file and start the mapping process
  if args.service:
    cmd.append("--sg_file=%s" % os.path.abspath(args.service))

  # Override optional external config file
  if args.config:
    cmd.append("--config=%s" % os.path.abspath(args.config))

  # Skip the Service Layer initiation and start the ROS agent REST-API
  if args.agent:
    cmd.append("--agent")
    # AGENT mode is only useful with --full -> initiate infrastructure if needed
    if not args.full:
      cmd.insert(0, "sudo")
      cmd.append("--full")

  # Start the REST-API for the ROS layer
  if args.rosapi or args.gui:
    cmd.append("--rosapi")

  # Start an REST-API for the Cf-Or interface
  if args.cfor:
    cmd.append("--cfor")

  # Enable Visualization
  if args.visualization:
    cmd.append("--visualization")

  # Add topology file if --full is set
  if args.topo:
    if args.full or args.agent:
      cmd.append("--topo=%s" % os.path.abspath(args.topo))
    else:
      parser.error(
        message="-t/--topo can be used only with infrastructure layer! "
                "(with -f/--full or -a/--agent flag)")

  # Add the interactive shell if needed
  if args.interactive:
    cmd.append("py")
    cmd.append("--completion")

  # Add optional POX modules
  cmd.extend(args.modules)

  def __activate_virtualenv ():
    """
    Activate virtualenv based on activate script under bin/

    :return: None
    """
    try:
      activate_this = os.path.join(base_dir, 'bin/activate_this.py')
      execfile(activate_this, dict(__file__=activate_this))
    except IOError as e:
      print "Virtualenv is not set properly:\n%s" % e
      print "Remove the '.set_virtualenv' file or configure the virtualenv!"
      os._exit(1)

  # Activate virtual environment if necessary
  if args.environment:
    __activate_virtualenv()
  else:
    for entry in os.listdir(base_dir):
      if entry.upper().startswith(".USE_VIRTUALENV"):
        __activate_virtualenv()
        break

  # Starting ESCAPEv2 (as a POX module)
  print "Starting %s..." % parser.description

  if args.POXlike:
    pox_base = os.path.abspath(os.path.join(os.path.dirname(__file__), "pox"))
    # Change working directory
    os.chdir(pox_base)
    # Override first path element
    # POX use the first element of path to add pox directories to PYTHONPATH
    import sys
    sys.path[0] = pox_base
    pox_params = cmd[2:] if cmd[0] == "sudo" else cmd[1:]
    if args.debug:
      print "POX parameters: %s" % pox_params
    from pox.boot import boot
    boot(argv=pox_params)
  else:
    # Create command
    cmd = " ".join(cmd)
    if args.debug:
      print "Command: %s" % cmd
    os.system(cmd)

if __name__ == '__main__':
  main()
<commit_msg>Fix cleanup task according to new paths<commit_after>#!/usr/bin/python -u
#
# Copyright 2015 Janos Czentye <czentye@tmit.bme.hu>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Top starter script of ESCAPEv2 for convenient purposes
"""
import argparse
import os

def main ():
  # Implement parser options
  parser = argparse.ArgumentParser(
    description="ESCAPEv2: Extensible Service ChAin Prototyping Environment "
                "using Mininet, Click, NETCONF and POX",
    add_help=True,
    version="2.0.0")
  # Add optional arguments
  escape = parser.add_argument_group("ESCAPEv2 arguments")
  escape.add_argument("-a", "--agent", action="store_true", default=False,
                      help="run in AGENT mode: start the infrastructure layer "
                           "with the ROS REST-API (without the Service "
                           "sublayer (SAS))")
  escape.add_argument("-c", "--config", metavar="path", type=str,
                      # default="pox/escape.config",
                      help="override default config filename")
  escape.add_argument("-d", "--debug", action="count", default=0,
                      help="run the ESCAPE in debug mode (can use multiple "
                           "times for more verbose logging)")
  escape.add_argument("-e", "--environment", action="store_true", default=False,
                      help="run ESCAPEv2 in the pre-defined virtualenv")
  escape.add_argument("-f", "--full", action="store_true", default=False,
                      help="run the infrastructure layer also")
  escape.add_argument("-g", "--gui", action="store_true", default=False,
                      help="initiate the graph-viewer GUI app which "
                           "automatically connects to the ROS REST-API")
  escape.add_argument("-i", "--interactive", action="store_true", default=False,
                      help="run an interactive shell for observing internal "
                           "states")
  escape.add_argument("-p", "--POXlike", action="store_true", default=False,
                      help="start ESCAPEv2 in the actual interpreter using "
                           "./pox as working directory instead of using a "
                           "separate shell process with POX's own PYTHON env")
  escape.add_argument("-r", "--rosapi", action="store_true", default=False,
                      help="start the REST-API for the Resource Orchestration "
                           "sublayer (ROS)")
  escape.add_argument("-s", "--service", metavar="file", type=str,
                      help="skip the SAS REST-API initiation and read the "
                           "service request from the given file")
  escape.add_argument("-t", "--topo", metavar="file", type=str,
                      help="read the topology from the given file explicitly")
  escape.add_argument("-x", "--clean", action="store_true", default=False,
                      help="run the cleanup task standalone and kill remained "
                           "programs, interfaces, veth parts and junk files")
  escape.add_argument("-V", "--visualization", action="store_true",
                      default=False,
                      help="run the visualization module to send data to a "
                           "remote server")
  escape.add_argument("-4", "--cfor", action="store_true", default=False,
                      help="start the REST-API for the Cf-Or interface")
  # Add remaining POX modules
  escape.add_argument("modules", metavar="...", nargs=argparse.REMAINDER,
                      help="optional POX modules")
  # Parsing arguments
  args = parser.parse_args()

  if args.clean:
    # Tailor Python path for importing mics functions without initialize
    # escape or util packages.
    import sys
    mn = os.path.abspath(os.path.dirname(__file__) + "/mininet")
    # Import misc directly from util/ to avoid standard ESCAPE init steps
    misc = os.path.abspath(os.path.dirname(__file__) + "/escape/escape/util")
    sys.path.insert(0, mn)
    sys.path.insert(0, misc)
    print sys.path
    if os.geteuid() != 0:
      print "Cleanup process requires root privilege!"
      return
    else:
      print "Run cleaning process..."
      from misc import remove_junks
      remove_junks()
      print "Cleaned."
      return

  # Get base dir of this script
  base_dir = os.path.abspath(os.path.dirname(__file__))

  # Create absolute path for the pox.py initial script
  # Construct POX init command according to argument
  # basic command
  cmd = [os.path.join(base_dir, "pox/pox.py"), "unify"]

  # Run ESCAPE in VERBOSE logging level if it is needed
  if args.debug == 1:
    # Set logging level to DEBUG
    cmd.append("--loglevel=DEBUG")
  elif args.debug > 1:
    # Setup logging level to specific VERBOSE
    cmd.append("--loglevel=VERBOSE")
  else:
    # Use default loglevel: INFO
    pass

  # Run the Infrastructure Layer with the required root privilege
  if args.full:
    cmd.insert(0, "sudo")
    cmd.append("--full")

  # Initiate the rudimentary GUI
  if args.gui:
    cmd.append("--gui")

  # Read the service request NFFG from a file and start the mapping process
  if args.service:
    cmd.append("--sg_file=%s" % os.path.abspath(args.service))

  # Override optional external config file
  if args.config:
    cmd.append("--config=%s" % os.path.abspath(args.config))

  # Skip the Service Layer initiation and start the ROS agent REST-API
  if args.agent:
    cmd.append("--agent")
    # AGENT mode is only useful with --full -> initiate infrastructure if needed
    if not args.full:
      cmd.insert(0, "sudo")
      cmd.append("--full")

  # Start the REST-API for the ROS layer
  if args.rosapi or args.gui:
    cmd.append("--rosapi")

  # Start an REST-API for the Cf-Or interface
  if args.cfor:
    cmd.append("--cfor")

  # Enable Visualization
  if args.visualization:
    cmd.append("--visualization")

  # Add topology file if --full is set
  if args.topo:
    if args.full or args.agent:
      cmd.append("--topo=%s" % os.path.abspath(args.topo))
    else:
      parser.error(
        message="-t/--topo can be used only with infrastructure layer! "
                "(with -f/--full or -a/--agent flag)")

  # Add the interactive shell if needed
  if args.interactive:
    cmd.append("py")
    cmd.append("--completion")

  # Add optional POX modules
  cmd.extend(args.modules)

  def __activate_virtualenv ():
    """
    Activate virtualenv based on activate script under bin/

    :return: None
    """
    try:
      activate_this = os.path.join(base_dir, 'bin/activate_this.py')
      execfile(activate_this, dict(__file__=activate_this))
    except IOError as e:
      print "Virtualenv is not set properly:\n%s" % e
      print "Remove the '.set_virtualenv' file or configure the virtualenv!"
      os._exit(1)

  # Activate virtual environment if necessary
  if args.environment:
    __activate_virtualenv()
  else:
    for entry in os.listdir(base_dir):
      if entry.upper().startswith(".USE_VIRTUALENV"):
        __activate_virtualenv()
        break

  # Starting ESCAPEv2 (as a POX module)
  print "Starting %s..." % parser.description

  if args.POXlike:
    pox_base = os.path.abspath(os.path.join(os.path.dirname(__file__), "pox"))
    # Change working directory
    os.chdir(pox_base)
    # Override first path element
    # POX use the first element of path to add pox directories to PYTHONPATH
    import sys
    sys.path[0] = pox_base
    pox_params = cmd[2:] if cmd[0] == "sudo" else cmd[1:]
    if args.debug:
      print "POX parameters: %s" % pox_params
    from pox.boot import boot
    boot(argv=pox_params)
  else:
    # Create command
    cmd = " ".join(cmd)
    if args.debug:
      print "Command: %s" % cmd
    os.system(cmd)

if __name__ == '__main__':
  main()
<|endoftext|>


<commit_before># Licensed under a 3-clause BSD style license - see PYFITS.rst

import os
import shutil
import tempfile

class FitsTestCase(object):
    def setup(self):
        self.data_dir = os.path.join(os.path.dirname(__file__), 'data')
        self.temp_dir = tempfile.mkdtemp(prefix='fits-test-')

    def teardown(self):
        shutil.rmtree(self.temp_dir)

    def data(self, filename):
        return os.path.join(self.data_dir, filename)

    def temp(self, filename):
        return os.path.join(self.temp_dir, filename)
<commit_msg>py.test always calls the teardown even if setup didn't run, such as when a test is skipped.  This was causing an error when the ignore_sigint test is skipped on Windows.<commit_after># Licensed under a 3-clause BSD style license - see PYFITS.rst

import os
import shutil
import tempfile

class FitsTestCase(object):
    def setup(self):
        self.data_dir = os.path.join(os.path.dirname(__file__), 'data')
        self.temp_dir = tempfile.mkdtemp(prefix='fits-test-')

    def teardown(self):
        if hasattr(self, 'temp_dir') and os.path.exists(self.temp_dir):
            shutil.rmtree(self.temp_dir)

    def data(self, filename):
        return os.path.join(self.data_dir, filename)

    def temp(self, filename):
        return os.path.join(self.temp_dir, filename)
<|endoftext|>


<commit_before>from sqlalchemy import text
from sqlalchemy import orm
from sqlalchemy.dialects.postgresql import JSONB

from app import db
import app
from jobs import update_registry
from jobs import Update

from models.product import Product
from models.non_doi_product import NonDoiProduct
from models.person import Person
from models import person

q = db.session.query(Person.id)
q = q.filter(Person.orcid_id != None)
update_registry.register(Update(
    job=Person.refresh,
    query=q,
    shortcut_fn=person.shortcut_all_percentile_refsets
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_first_name,
    query=q
))
q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.calculate,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_from_orcid,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_impressions,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_api_raw != None)
q = q.filter(Product.altmetric_api_raw != {})
q = q.filter(Product.event_dates == None)
update_registry.register(Update(
    job=Product.set_event_dates,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_api_raw != None)
q = q.filter(Product.altmetric_score == None)
update_registry.register(Update(
    job=Product.set_altmetric_score,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_api_raw != None)
update_registry.register(Update(
    job=Product.calculate_metrics,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_api_raw != None)
update_registry.register(Update(
    job=Product.set_altmetric_id,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.post_counts != None)
q = q.filter(Product.post_counts != {})
update_registry.register(Update(
    job=Product.set_post_details,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_post_details,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_tweeter_details,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_coauthors,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_subscore_percentiles,
    query=q,
    shortcut_fn=person.shortcut_score_percentile_refsets
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_subscores,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_is_open_temp,
    query=q,
    queue_id=1
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_is_open,
    query=q,
    queue_id=0
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_score != None)
q = q.filter(Product.altmetric_score > 0)
update_registry.register(Update(
    job=Product.set_data_from_crossref,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_publisher,
    query=q
))

q = db.session.query(Person.id)
# q = q.filter(Person.campaign.in_(["2015_with_urls"]))
# q = q.filter(Person.campaign != "2015_with_urls")
# q = q.filter(Person.campaign.in_(["impactstory_nos", "impactstory_subscribers"]))  #@todo remove this
# q = q.filter(Person.orcid_id.in_([
#             "0000-0002-6133-2581",
#             "0000-0002-0159-2197",
#             "0000-0003-1613-5981",
#             "0000-0003-1419-2405",
#             "0000-0001-6187-6610",
#             "0000-0001-6728-7745"]))
update_registry.register(Update(
    job=Person.set_badge_percentiles,
    query=q,
    shortcut_fn=person.shortcut_badge_percentile_refsets
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.assign_badges,
    query=q,
    shortcut_fn=lambda: ["star_wars"]
))

q = db.session.query(Person.id)
# q = q.filter(Person.updated < '2016-04-10 10:00:51.972209')
update_registry.register(Update(
    job=Person.refresh_from_db,
    query=q,
    shortcut_fn=person.shortcut_all_percentile_refsets
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_non_doi_products_biblio_from_orcid,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_event_counts,
    query=q
))<commit_msg>refresh with queue<commit_after>from sqlalchemy import text
from sqlalchemy import orm
from sqlalchemy.dialects.postgresql import JSONB

from app import db
import app
from jobs import update_registry
from jobs import Update

from models.product import Product
from models.non_doi_product import NonDoiProduct
from models.person import Person
from models import person

q = db.session.query(Person.id)
q = q.filter(Person.orcid_id != None)
update_registry.register(Update(
    job=Person.refresh,
    query=q,
    shortcut_fn=person.shortcut_all_percentile_refsets,
    queue_id=0
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_first_name,
    query=q
))
q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.calculate,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_from_orcid,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_impressions,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_api_raw != None)
q = q.filter(Product.altmetric_api_raw != {})
q = q.filter(Product.event_dates == None)
update_registry.register(Update(
    job=Product.set_event_dates,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_api_raw != None)
q = q.filter(Product.altmetric_score == None)
update_registry.register(Update(
    job=Product.set_altmetric_score,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_api_raw != None)
update_registry.register(Update(
    job=Product.calculate_metrics,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_api_raw != None)
update_registry.register(Update(
    job=Product.set_altmetric_id,
    query=q
))

q = db.session.query(Product.id)
q = q.filter(Product.post_counts != None)
q = q.filter(Product.post_counts != {})
update_registry.register(Update(
    job=Product.set_post_details,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_post_details,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_tweeter_details,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_coauthors,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_subscore_percentiles,
    query=q,
    shortcut_fn=person.shortcut_score_percentile_refsets
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_subscores,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_is_open_temp,
    query=q,
    queue_id=1
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_is_open,
    query=q,
    queue_id=0
))

q = db.session.query(Product.id)
q = q.filter(Product.altmetric_score != None)
q = q.filter(Product.altmetric_score > 0)
update_registry.register(Update(
    job=Product.set_data_from_crossref,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_publisher,
    query=q
))

q = db.session.query(Person.id)
# q = q.filter(Person.campaign.in_(["2015_with_urls"]))
# q = q.filter(Person.campaign != "2015_with_urls")
# q = q.filter(Person.campaign.in_(["impactstory_nos", "impactstory_subscribers"]))  #@todo remove this
# q = q.filter(Person.orcid_id.in_([
#             "0000-0002-6133-2581",
#             "0000-0002-0159-2197",
#             "0000-0003-1613-5981",
#             "0000-0003-1419-2405",
#             "0000-0001-6187-6610",
#             "0000-0001-6728-7745"]))
update_registry.register(Update(
    job=Person.set_badge_percentiles,
    query=q,
    shortcut_fn=person.shortcut_badge_percentile_refsets
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.assign_badges,
    query=q,
    shortcut_fn=lambda: ["star_wars"]
))

q = db.session.query(Person.id)
# q = q.filter(Person.updated < '2016-04-10 10:00:51.972209')
update_registry.register(Update(
    job=Person.refresh_from_db,
    query=q,
    shortcut_fn=person.shortcut_all_percentile_refsets
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_non_doi_products_biblio_from_orcid,
    query=q
))

q = db.session.query(Person.id)
update_registry.register(Update(
    job=Person.set_event_counts,
    query=q
))<|endoftext|>


<commit_before>import argparse

def problem1():
    """
    If we list all the natural numbers below 10 that are multiples of 3 or 5,
    we get 3, 5, 6 and 9. The sum of these multiples is 23.

    Find the sum of all the multiples of 3 or 5 below 1000.
    """
    total = 0
    for n in xrange(1000):
        if (n % 3 == 0) or (n % 5 == 0):
            total += n
    return total

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--problem', '-p', type=int,
        help='problem number to evaluate, if completed')

    args = parser.parse_args()
    if args.problem:
        try:
            result = globals()['problem' + str(args.problem)]()
            print 'Problem ' + str(args.problem) + ': ' + str(result)
        except KeyError:
            print 'Problem ' + str(args.problem) + ': Function not found'
        except:
            print 'Problem ' + str(args.problem) + ': Execution failed'
<commit_msg>problem 2 solution<commit_after>import argparse

def problem1():
    """
    If we list all the natural numbers below 10 that are multiples of 3 or 5,
    we get 3, 5, 6 and 9. The sum of these multiples is 23.

    Find the sum of all the multiples of 3 or 5 below 1000.
    """
    total = 0
    for n in xrange(1000):
        if (n % 3 == 0) or (n % 5 == 0):
            total += n
    return total

def problem2():
    """
    Each new term in the Fibonacci sequence is generated by adding the
    previous two terms.

    By considering the terms in the Fibonacci sequence whose values do not
    exceed four million, find the sum of the even-valued terms.
    """
    total = 0
    t0, t1, t2 = 0, 1, 0
    while t0 <= 4000000:
        if t0 % 2 == 0:
            total += t0
        t2, t1 = t1, t0
        t0 = t2 + t1
    return total

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--problem', '-p', type=int,
        help='problem number to evaluate, if completed')

    args = parser.parse_args()
    if args.problem:
        try:
            result = globals()['problem' + str(args.problem)]()
            print 'Problem ' + str(args.problem) + ': ' + str(result)
        except KeyError:
            print 'Problem ' + str(args.problem) + ': Function not found'
        except:
            print 'Problem ' + str(args.problem) + ': Execution failed'
<|endoftext|>


<commit_before>'''
Created on Jan 8, 2012

@author: cryan

A simple GUI for plotting pulse sequences for visual inspection
'''

AWGFreq = 1e9

import sys

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.figure import Figure

from PySide import QtCore, QtGui

import numpy as np

import PulseSequencer

from APSPattern import read_APS_file
from TekPattern import read_Tek_file

import argparse
import os.path

class PulseSeqPlotWindow(QtGui.QWidget):

    def __init__(self, AWGWFs=None):
        super(PulseSeqPlotWindow, self).__init__()

        #See if we have files passed in
        if isinstance(AWGWFs, list):
            self.AWGWFs = {}
            AWGFileNames = AWGWFs
            for tmpFile in AWGFileNames:
                #Ugly hack to see if we're loading a Tek or APS files
                #Assume a naming convenction path/to/file/SequenceName-AWGName.h5
                AWGName = (os.path.split(os.path.splitext(tmpFile)[0])[1]).split('-')[1]
                if AWGName[:6] == 'TekAWG':
                    self.AWGWFs[AWGName] = read_Tek_file(tmpFile)
                elif AWGName[:6] == 'BBNAPS':
                    self.AWGWFs[AWGName] = read_APS_file(tmpFile)
                else:
                    raise NameError('Unknown AWG Type for {0}: we currently only handle TekAWG and BBNAPS.'.format(tmpFile))

        else:
            self.AWGWFs = AWGWFs
            #Convert the BBNAPS LLs into pulse sequence waveforms
            for tmpAWGName, tmpAWG in AWGWFs.items():
                if tmpAWGName[:6] == 'BBNAPS':
                    tmpWFs = {}
                    for chanStr in tmpAWG.keys():
                        tmpWFs[chanStr] = []
                        for miniLL in tmpAWG[chanStr]['LLs']:
                            tmpWFs[chanStr].append(PulseSequencer.LL2sequence(miniLL, tmpAWG[chanStr]['WFLibrary']))
                    self.AWGWFs[tmpAWGName] = tmpWFs

        numSeqs = np.max([len(tmpAWG.values()[0]) for tmpAWG in self.AWGWFs.values()])

        #Create the GUI
        self.resize(1000,700)
        self.center()
        self.setWindowTitle('Pulse Sequence Plotter')

        # generate the plot
        self.fig = Figure(figsize=(12,6), dpi=72)
        self.ax = self.fig.add_subplot(111)

        # generate the canvas to display the plot
        self.canvas = FigureCanvas(self.fig)
        self.mpl_toolbar = NavigationToolbar(self.canvas, self)

        #Create a slider to move through the different sequences
        slider = QtGui.QSlider(QtCore.Qt.Horizontal)
        slider.setRange(0, numSeqs-1)
        slider.setTickInterval(1)
        slider.setSingleStep(1)
        sliderLabel = QtGui.QLabel('Sequence Num.')
        if numSeqs > 1:
            numDigits = int(np.ceil(np.log10(numSeqs-1)))
        else:
            numDigits = 1
        sliderLCD = QtGui.QLCDNumber(numDigits)
        slider.valueChanged.connect(sliderLCD.display)
        slider.valueChanged.connect(self.update_plot)
        self.slider = slider

        #A tree view to decide what to plot
        plotCheckBoxes = QtGui.QTreeWidget()
        plotCheckBoxes.setMinimumWidth(150)
        plotCheckBoxes.setHeaderLabel('Channel Plot')
        plotCheckBoxes.itemClicked.connect(self.update_plot)
        for tmpAWGName, tmpAWG in self.AWGWFs.items():
            tmpItem = QtGui.QTreeWidgetItem([tmpAWGName])
            for tmpChannelName in sorted(tmpAWG.keys()):
                tmpChildItem = QtGui.QTreeWidgetItem([tmpChannelName])
                #Default to not checked if there is nothing going on
                if np.all(self.AWGWFs[tmpAWGName][tmpChannelName][0] == 0):
                    tmpChildItem.setCheckState(0, QtCore.Qt.Unchecked)
                else:
                    tmpChildItem.setCheckState(0,QtCore.Qt.Checked)
                tmpItem.addChild(tmpChildItem)
            plotCheckBoxes.addTopLevelItem(tmpItem)
            tmpItem.setExpanded(True)
        self.plotCheckBoxes = plotCheckBoxes

        delayShiftCheckBox = QtGui.QCheckBox('Undo Delays')
        delayShiftCheckBox.stateChanged.connect(self.update_plot)
        delayShiftCheckBox.setEnabled(False)

        #Lay everything out
        hboxSlider = QtGui.QHBoxLayout()
        hboxSlider.addStretch(1)
        hboxSlider.addWidget(sliderLabel)
        hboxSlider.addWidget(sliderLCD)
        hboxSlider.addWidget(slider)

        vboxOptions = QtGui.QVBoxLayout()
        vboxOptions.addWidget(plotCheckBoxes)
        vboxOptions.addWidget(delayShiftCheckBox)
        vboxOptions.addStretch(1)

        hboxTop = QtGui.QHBoxLayout()
        hboxTop.addWidget(self.canvas)
        hboxTop.addLayout(vboxOptions)

        hboxBottom = QtGui.QHBoxLayout()
        hboxBottom.addWidget(self.mpl_toolbar)
        hboxBottom.addStretch(1)
        hboxBottom.addLayout(hboxSlider)

        vboxTot = QtGui.QVBoxLayout()
        vboxTot.addLayout(hboxTop)
        vboxTot.addLayout(hboxBottom)

        self.setLayout(vboxTot)

        self.update_plot()

    def update_plot(self):

        self.ax.clear()

        #Get the current segment number
        curSegNum = self.slider.sliderPosition()

        vertShift = 0
        for itemct in range(self.plotCheckBoxes.topLevelItemCount()):
            tmpItem = self.plotCheckBoxes.topLevelItem(itemct)
            tmpAWGName = str(tmpItem.text(0))
            for childct in range(tmpItem.childCount()):
                tmpChild = tmpItem.child(childct)
                if tmpChild.checkState(0) == QtCore.Qt.Checked:
                    if curSegNum < len(self.AWGWFs[tmpAWGName][str(tmpChild.text(0))]):
                        self.ax.plot(self.AWGWFs[tmpAWGName][str(tmpChild.text(0))][curSegNum] + vertShift)
                    self.ax.text(0, vertShift,tmpAWGName+'-'+str(tmpChild.text(0)), fontsize=8)
                    vertShift += 2
        self.ax.set_ylim((-1, vertShift))
        self.canvas.draw()

    def center(self):
        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

def plot_pulse_seqs(AWGWFs):

    #Look to see if iPython's event loop is running
    app = QtCore.QCoreApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)

    plotterWindow = PulseSeqPlotWindow(AWGWFs)
    plotterWindow.show()

    try:
        from IPython.lib.guisupport import start_event_loop_qt4
        start_event_loop_qt4(app)
    except ImportError:
        sys.exit(app.exec_())

    #Need to a keep a reference to the window alive.
    return plotterWindow

if __name__ == '__main__':

    #See if we have been passed AWG files
    parser = argparse.ArgumentParser()
    parser.add_argument('--AWGFiles', action='store', dest='AWGFiles',  nargs='*', default=None)
    options =  parser.parse_args(sys.argv[1:])

    if options.AWGFiles:
        plot_pulse_seqs(options.AWGFiles)

    else:
        AWGWFs = {}
        AWGWFs['TekAWG1'] = {}
        AWGWFs['TekAWG1']['ch1'] = []
        AWGWFs['TekAWG1']['ch2'] = []
        AWGWFs['TekAWG1']['ch1m1'] = []

        for ct in range(1,10):
            AWGWFs['TekAWG1']['ch1'].append(np.sin(np.linspace(0,ct*np.pi,10000)))
            AWGWFs['TekAWG1']['ch2'].append(np.cos(np.linspace(0,ct*np.pi,10000)))
            AWGWFs['TekAWG1']['ch1m1'].append(np.abs(AWGWFs['TekAWG1']['ch1'][-1]) < 0.5)

        plot_pulse_seqs(AWGWFs)<commit_msg>Speed up pulse sequence plotting.<commit_after>'''
Created on Jan 8, 2012

@author: cryan

A simple GUI for plotting pulse sequences for visual inspection
'''

AWGFreq = 1e9

import sys

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.figure import Figure

from PySide import QtCore, QtGui

import numpy as np

import PulseSequencer

from APSPattern import read_APS_file
from TekPattern import read_Tek_file

import argparse
import os.path

class PulseSeqPlotWindow(QtGui.QWidget):

    def __init__(self, AWGWFs=None):
        super(PulseSeqPlotWindow, self).__init__()

        #See if we have files passed in
        if isinstance(AWGWFs, list):
            self.AWGWFs = {}
            AWGFileNames = AWGWFs
            for tmpFile in AWGFileNames:
                #Ugly hack to see if we're loading a Tek or APS files
                #Assume a naming convenction path/to/file/SequenceName-AWGName.h5
                AWGName = (os.path.split(os.path.splitext(tmpFile)[0])[1]).split('-')[1]
                if AWGName[:6] == 'TekAWG':
                    self.AWGWFs[AWGName] = read_Tek_file(tmpFile)
                elif AWGName[:6] == 'BBNAPS':
                    self.AWGWFs[AWGName] = read_APS_file(tmpFile)
                else:
                    raise NameError('Unknown AWG Type for {0}: we currently only handle TekAWG and BBNAPS.'.format(tmpFile))

        else:
            self.AWGWFs = AWGWFs
            #Convert the BBNAPS LLs into pulse sequence waveforms
            for tmpAWGName, tmpAWG in AWGWFs.items():
                if tmpAWGName[:6] == 'BBNAPS':
                    tmpWFs = {}
                    for chanStr in tmpAWG.keys():
                        tmpWFs[chanStr] = []
                        for miniLL in tmpAWG[chanStr]['LLs']:
                            tmpWFs[chanStr].append(PulseSequencer.LL2sequence(miniLL, tmpAWG[chanStr]['WFLibrary']))
                    self.AWGWFs[tmpAWGName] = tmpWFs

        numSeqs = np.max([len(tmpAWG.values()[0]) for tmpAWG in self.AWGWFs.values()])

        #Create the GUI
        self.resize(1000,700)
        self.center()
        self.setWindowTitle('Pulse Sequence Plotter')

        # generate the plot
        self.fig = Figure(figsize=(12,6), dpi=72)
        self.ax = self.fig.add_subplot(111)

        # generate the canvas to display the plot
        self.canvas = FigureCanvas(self.fig)
        self.mpl_toolbar = NavigationToolbar(self.canvas, self)

        #Create a slider to move through the different sequences
        slider = QtGui.QSlider(QtCore.Qt.Horizontal)
        slider.setRange(0, numSeqs-1)
        slider.setTickInterval(1)
        slider.setSingleStep(1)
        sliderLabel = QtGui.QLabel('Sequence Num.')
        if numSeqs > 1:
            numDigits = int(np.ceil(np.log10(numSeqs-1)))
        else:
            numDigits = 1
        sliderLCD = QtGui.QLCDNumber(numDigits)
        slider.valueChanged.connect(sliderLCD.display)
        slider.valueChanged.connect(self.update_plot)
        self.slider = slider

        #A tree view to decide what to plot
        plotCheckBoxes = QtGui.QTreeWidget()
        plotCheckBoxes.setMinimumWidth(150)
        plotCheckBoxes.setHeaderLabel('Channel Plot')
        plotCheckBoxes.itemClicked.connect(self.new_plot)
        for tmpAWGName, tmpAWG in self.AWGWFs.items():
            tmpItem = QtGui.QTreeWidgetItem([tmpAWGName])
            for tmpChannelName in sorted(tmpAWG.keys()):
                tmpChildItem = QtGui.QTreeWidgetItem([tmpChannelName])
                #Default to not checked if there is nothing going on
                if np.all(self.AWGWFs[tmpAWGName][tmpChannelName][0] == 0):
                    tmpChildItem.setCheckState(0, QtCore.Qt.Unchecked)
                else:
                    tmpChildItem.setCheckState(0,QtCore.Qt.Checked)
                tmpItem.addChild(tmpChildItem)
            plotCheckBoxes.addTopLevelItem(tmpItem)
            tmpItem.setExpanded(True)
        self.plotCheckBoxes = plotCheckBoxes

        delayShiftCheckBox = QtGui.QCheckBox('Undo Delays')
        delayShiftCheckBox.stateChanged.connect(self.update_plot)
        delayShiftCheckBox.setEnabled(False)

        #Lay everything out
        hboxSlider = QtGui.QHBoxLayout()
        hboxSlider.addStretch(1)
        hboxSlider.addWidget(sliderLabel)
        hboxSlider.addWidget(sliderLCD)
        hboxSlider.addWidget(slider)

        vboxOptions = QtGui.QVBoxLayout()
        vboxOptions.addWidget(plotCheckBoxes)
        vboxOptions.addWidget(delayShiftCheckBox)
        vboxOptions.addStretch(1)

        hboxTop = QtGui.QHBoxLayout()
        hboxTop.addWidget(self.canvas)
        hboxTop.addLayout(vboxOptions)

        hboxBottom = QtGui.QHBoxLayout()
        hboxBottom.addWidget(self.mpl_toolbar)
        hboxBottom.addStretch(1)
        hboxBottom.addLayout(hboxSlider)

        vboxTot = QtGui.QVBoxLayout()
        vboxTot.addLayout(hboxTop)
        vboxTot.addLayout(hboxBottom)

        self.setLayout(vboxTot)
        self.lines = {}
        self.new_plot()

    def new_plot(self):
        '''
        Starts a new plot if the channels have changed
        '''
        self.ax.clear()

        #Get the current segment number
        curSegNum = self.slider.sliderPosition()

        self.lines = {}
        vertShift = 0
        for itemct in range(self.plotCheckBoxes.topLevelItemCount()):
            tmpItem = self.plotCheckBoxes.topLevelItem(itemct)
            tmpAWGName = str(tmpItem.text(0))
            for childct in range(tmpItem.childCount()):
                tmpChild = tmpItem.child(childct)
                if tmpChild.checkState(0) == QtCore.Qt.Checked:
                    if curSegNum < len(self.AWGWFs[tmpAWGName][str(tmpChild.text(0))]):
                        tmpChanStr = ''.join([tmpAWGName,str(tmpChild.text(0))])
                        self.lines[tmpChanStr] = {}
                        self.lines[tmpChanStr]['handle'], = self.ax.plot(self.AWGWFs[tmpAWGName][str(tmpChild.text(0))][curSegNum] + vertShift)
                        self.lines[tmpChanStr]['vertShift'] = vertShift
                        self.lines[tmpChanStr]['awgData'] = self.AWGWFs[tmpAWGName][str(tmpChild.text(0))]
                    self.ax.text(0, vertShift,tmpAWGName+'-'+str(tmpChild.text(0)), fontsize=8)
                    vertShift += 2
        self.ax.set_ylim((-1, vertShift))
        self.canvas.draw()

    def update_plot(self):
        '''
        Just updates the y-date for the sequence number changes
        '''
        #Get the current segment number
        curSegNum = self.slider.sliderPosition()

        for tmpChanStr, tmpLine in self.lines.items():
            if curSegNum < len(tmpLine['awgData']):
                tmpLine['handle'].set_ydata(tmpLine['awgData'][curSegNum] + tmpLine['vertShift'])

        self.canvas.draw()

    def center(self):
        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

def plot_pulse_seqs(AWGWFs):

    #Look to see if iPython's event loop is running
    app = QtCore.QCoreApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)

    plotterWindow = PulseSeqPlotWindow(AWGWFs)
    plotterWindow.show()

    try:
        from IPython.lib.guisupport import start_event_loop_qt4
        start_event_loop_qt4(app)
    except ImportError:
        sys.exit(app.exec_())

    #Need to a keep a reference to the window alive.
    return plotterWindow

if __name__ == '__main__':

    #See if we have been passed AWG files
    parser = argparse.ArgumentParser()
    parser.add_argument('--AWGFiles', action='store', dest='AWGFiles',  nargs='*', default=None)
    options =  parser.parse_args(sys.argv[1:])

    if options.AWGFiles:
        plot_pulse_seqs(options.AWGFiles)

    else:
        AWGWFs = {}
        AWGWFs['TekAWG1'] = {}
        AWGWFs['TekAWG1']['ch1'] = []
        AWGWFs['TekAWG1']['ch2'] = []
        AWGWFs['TekAWG1']['ch1m1'] = []

        for ct in range(1,10):
            AWGWFs['TekAWG1']['ch1'].append(np.sin(np.linspace(0,ct*np.pi,10000)))
            AWGWFs['TekAWG1']['ch2'].append(np.cos(np.linspace(0,ct*np.pi,10000)))
            AWGWFs['TekAWG1']['ch1m1'].append(np.abs(AWGWFs['TekAWG1']['ch1'][-1]) < 0.5)

        plot_pulse_seqs(AWGWFs)<|endoftext|>


<commit_before># Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Backend.SignalSocket import SignalSocket
from UM.Preferences import Preferences
from UM.Logger import Logger
from UM.Signal import Signal, SignalEmitter
from UM.Application import Application
from UM.PluginObject import PluginObject

import Arcus

import struct
import subprocess
import threading
import platform
import sys
from time import sleep

##      Base class for any backend communication (seperate piece of software).
#       It makes use of the Socket class from libArcus for the actual communication bits.
#       The message_handler dict should be filled with message class, function pairs.
class Backend(PluginObject, SignalEmitter):
    def __init__(self):
        super().__init__() # Call super to make multiple inheritence work.
        self._supported_commands = {}

        self._message_handlers = {}

        self._socket = None
        self._port = 49674
        self._process = None
        self._backend_log = []

        Application.getInstance().callLater(self._createSocket)

    processingProgress = Signal()
    backendConnected = Signal()

    ##   \brief Start the backend / engine.
    #   Runs the engine, this is only called when the socket is fully opend & ready to accept connections
    def startEngine(self):
        try:
            command = self.getEngineCommand()
            if not command:
                self._createSocket()
                return

            self._backend_log = []
            self._process = self._runEngineProcess(command)
            Logger.log("i", "Started engine process: %s" % (self.getEngineCommand()[0]))
            self._backend_log.append(bytes("Calling engine with: %s\n" % self.getEngineCommand(), 'utf-8'))
            t = threading.Thread(target=self._storeOutputToLogThread, args=(self._process.stdout,))
            t.daemon = True
            t.start()
            t = threading.Thread(target=self._storeOutputToLogThread, args=(self._process.stderr,))
            t.daemon = True
            t.start()
        except FileNotFoundError as e:
            Logger.log("e", "Unable to find backend executable: %s" % (self.getEngineCommand()[0]))

    def close(self):
        if self._socket:
            self._socket.close()

    ##  Get the logging messages of the backend connection.
    #   \returns
    def getLog(self):
        return self._backend_log

    ##  \brief Convert byte array containing 3 floats per vertex
    def convertBytesToVerticeList(self, data):
        result = []
        if not (len(data) % 12):
            if data is not None:
                for index in range(0,int(len(data)/12)): #For each 12 bits (3 floats)
                    result.append(struct.unpack("fff",data[index*12:index*12+12]))
                return result
        else:
            Logger.log("e", "Data length was incorrect for requested type")
            return None

    ##  \brief Convert byte array containing 6 floats per vertex
    def convertBytesToVerticeWithNormalsList(self,data):
        result = []
        if not (len(data) % 24):
            if data is not None:
                for index in range(0,int(len(data)/24)): #For each 24 bits (6 floats)
                    result.append(struct.unpack("ffffff",data[index*24:index*24+24]))
                return result
        else:
            Logger.log("e", "Data length was incorrect for requested type")
            return None

    ##  Get the command used to start the backend executable
    def getEngineCommand(self):
        return [Preferences.getInstance().getValue("backend/location"), "--port", str(self._socket.getPort())]

    ##  Start the (external) backend process.
    def _runEngineProcess(self, command_list):
        kwargs = {}
        if sys.platform == "win32":
            su = subprocess.STARTUPINFO()
            su.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            su.wShowWindow = subprocess.SW_HIDE
            kwargs["startupinfo"] = su
            kwargs["creationflags"] = 0x00004000 #BELOW_NORMAL_PRIORITY_CLASS
        return subprocess.Popen(command_list, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)

    def _storeOutputToLogThread(self, handle):
        while True:
            line = handle.readline()
            if line == b"":
                break
            self._backend_log.append(line)

    ##  Private socket state changed handler.
    def _onSocketStateChanged(self, state):
        if state == Arcus.SocketState.Listening:
            if not Application.getInstance().getCommandLineOption("external-backend", False):
                self.startEngine()
        elif state == Arcus.SocketState.Connected:
            Logger.log("d", "Backend connected on port %s", self._port)
            self.backendConnected.emit()

    ##  Private message handler
    def _onMessageReceived(self):
        message = self._socket.takeNextMessage()

        if type(message) not in self._message_handlers:
            Logger.log("e", "No handler defined for message of type %s", type(message))
            return

        self._message_handlers[type(message)](message)

    ##  Private socket error handler
    def _onSocketError(self, error):
        try:
            if error.errno == 98 or error.errno == 48:# Socked in use error
                self._port += 1
            elif error.errno == 104 or error.errno == 32 or error.errno == 54 or error.errno == 41:
                # 104 is connection reset by peer. 32 is broken pipe. 54 is also connection reset by peer.
                # 41 is specific for MacOSX and happens when closing a socket.
                # All these imply the connection to the backend was broken and we need to restart it.
                Logger.log("i", "Backend crashed or closed. Restarting...")
            elif platform.system() == "Windows":
                if error.winerror == 10048:# Socked in use error
                    self._port += 1
                elif error.winerror == 10054:
                    Logger.log("i", "Backend crashed or closed. Restarting...")
            else:
                Logger.log("e", str(error))
        except Exception as e:
            Logger.log("e", "Failed to parse socket error: %s" , str(e))

        self._createSocket()

    ##  Creates a socket and attaches listeners.
    def _createSocket(self, protocol_file):
        if self._socket:
            self._socket.stateChanged.disconnect(self._onSocketStateChanged)
            self._socket.messageReceived.disconnect(self._onMessageReceived)
            self._socket.error.disconnect(self._onSocketError)

        self._socket = SignalSocket()
        self._socket.stateChanged.connect(self._onSocketStateChanged)
        self._socket.messageReceived.connect(self._onMessageReceived)
        self._socket.error.connect(self._onSocketError)

        if not self._socket.registerAllMessageTypes(protocol_file):
            Logger.log("e", "Could not register Cura protocol messages: %s", self._socket.getLastError())

        if Application.getInstance().getCommandLineOption("external-backend", False):
            Logger.log("i", "Listening for backend connections on %s", self._port)

        self._socket.listen("127.0.0.1", self._port)

<commit_msg>Use message type name as key for message handlers rather than message type<commit_after># Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Backend.SignalSocket import SignalSocket
from UM.Preferences import Preferences
from UM.Logger import Logger
from UM.Signal import Signal, SignalEmitter
from UM.Application import Application
from UM.PluginObject import PluginObject

import Arcus

import struct
import subprocess
import threading
import platform
import sys
from time import sleep

##      Base class for any backend communication (seperate piece of software).
#       It makes use of the Socket class from libArcus for the actual communication bits.
#       The message_handler dict should be filled with message class, function pairs.
class Backend(PluginObject, SignalEmitter):
    def __init__(self):
        super().__init__() # Call super to make multiple inheritence work.
        self._supported_commands = {}

        self._message_handlers = {}

        self._socket = None
        self._port = 49674
        self._process = None
        self._backend_log = []

        Application.getInstance().callLater(self._createSocket)

    processingProgress = Signal()
    backendConnected = Signal()

    ##   \brief Start the backend / engine.
    #   Runs the engine, this is only called when the socket is fully opend & ready to accept connections
    def startEngine(self):
        try:
            command = self.getEngineCommand()
            if not command:
                self._createSocket()
                return

            self._backend_log = []
            self._process = self._runEngineProcess(command)
            Logger.log("i", "Started engine process: %s" % (self.getEngineCommand()[0]))
            self._backend_log.append(bytes("Calling engine with: %s\n" % self.getEngineCommand(), 'utf-8'))
            t = threading.Thread(target=self._storeOutputToLogThread, args=(self._process.stdout,))
            t.daemon = True
            t.start()
            t = threading.Thread(target=self._storeOutputToLogThread, args=(self._process.stderr,))
            t.daemon = True
            t.start()
        except FileNotFoundError as e:
            Logger.log("e", "Unable to find backend executable: %s" % (self.getEngineCommand()[0]))

    def close(self):
        if self._socket:
            self._socket.close()

    ##  Get the logging messages of the backend connection.
    #   \returns
    def getLog(self):
        return self._backend_log

    ##  \brief Convert byte array containing 3 floats per vertex
    def convertBytesToVerticeList(self, data):
        result = []
        if not (len(data) % 12):
            if data is not None:
                for index in range(0,int(len(data)/12)): #For each 12 bits (3 floats)
                    result.append(struct.unpack("fff",data[index*12:index*12+12]))
                return result
        else:
            Logger.log("e", "Data length was incorrect for requested type")
            return None

    ##  \brief Convert byte array containing 6 floats per vertex
    def convertBytesToVerticeWithNormalsList(self,data):
        result = []
        if not (len(data) % 24):
            if data is not None:
                for index in range(0,int(len(data)/24)): #For each 24 bits (6 floats)
                    result.append(struct.unpack("ffffff",data[index*24:index*24+24]))
                return result
        else:
            Logger.log("e", "Data length was incorrect for requested type")
            return None

    ##  Get the command used to start the backend executable
    def getEngineCommand(self):
        return [Preferences.getInstance().getValue("backend/location"), "--port", str(self._socket.getPort())]

    ##  Start the (external) backend process.
    def _runEngineProcess(self, command_list):
        kwargs = {}
        if sys.platform == "win32":
            su = subprocess.STARTUPINFO()
            su.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            su.wShowWindow = subprocess.SW_HIDE
            kwargs["startupinfo"] = su
            kwargs["creationflags"] = 0x00004000 #BELOW_NORMAL_PRIORITY_CLASS
        return subprocess.Popen(command_list, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)

    def _storeOutputToLogThread(self, handle):
        while True:
            line = handle.readline()
            if line == b"":
                break
            self._backend_log.append(line)

    ##  Private socket state changed handler.
    def _onSocketStateChanged(self, state):
        if state == Arcus.SocketState.Listening:
            if not Application.getInstance().getCommandLineOption("external-backend", False):
                self.startEngine()
        elif state == Arcus.SocketState.Connected:
            Logger.log("d", "Backend connected on port %s", self._port)
            self.backendConnected.emit()

    ##  Private message handler
    def _onMessageReceived(self):
        message = self._socket.takeNextMessage()

        if message.getTypeName() not in self._message_handlers:
            Logger.log("e", "No handler defined for message of type %s", type(message))
            return

        self._message_handlers[message.getTypeName()](message)

    ##  Private socket error handler
    def _onSocketError(self, error):
        try:
            if error.errno == 98 or error.errno == 48:# Socked in use error
                self._port += 1
            elif error.errno == 104 or error.errno == 32 or error.errno == 54 or error.errno == 41:
                # 104 is connection reset by peer. 32 is broken pipe. 54 is also connection reset by peer.
                # 41 is specific for MacOSX and happens when closing a socket.
                # All these imply the connection to the backend was broken and we need to restart it.
                Logger.log("i", "Backend crashed or closed. Restarting...")
            elif platform.system() == "Windows":
                if error.winerror == 10048:# Socked in use error
                    self._port += 1
                elif error.winerror == 10054:
                    Logger.log("i", "Backend crashed or closed. Restarting...")
            else:
                Logger.log("e", str(error))
        except Exception as e:
            Logger.log("e", "Failed to parse socket error: %s" , str(e))

        self._createSocket()

    ##  Creates a socket and attaches listeners.
    def _createSocket(self, protocol_file):
        if self._socket:
            self._socket.stateChanged.disconnect(self._onSocketStateChanged)
            self._socket.messageReceived.disconnect(self._onMessageReceived)
            self._socket.error.disconnect(self._onSocketError)

        self._socket = SignalSocket()
        self._socket.stateChanged.connect(self._onSocketStateChanged)
        self._socket.messageReceived.connect(self._onMessageReceived)
        self._socket.error.connect(self._onSocketError)

        if not self._socket.registerAllMessageTypes(protocol_file):
            Logger.log("e", "Could not register Cura protocol messages: %s", self._socket.getLastError())

        if Application.getInstance().getCommandLineOption("external-backend", False):
            Logger.log("i", "Listening for backend connections on %s", self._port)

        self._socket.listen("127.0.0.1", self._port)

<|endoftext|>


<commit_before># -*- coding: iso8859-1 -*-
#
# Copyright (C) 2005 Edgewall Software
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.com/license.html.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://projects.edgewall.com/trac/.
#
# Author: Christopher Lenz <cmlenz@gmx.de>

from __future__ import generators
from trac.perm import PermissionError

class Repository(object):
    """
    Base class for a repository provided by a version control system.
    """

    def __init__(self, name, authz, log):
        self.name = name
        self.authz = authz or Authorizer()
        self.log = log

    def close(self):
        """
        Close the connection to the repository.
        """
        raise NotImplementedError

    def get_changeset(self, rev):
        """
        Retrieve a Changeset object that describes the changes made in
        revision 'rev'.
        """
        raise NotImplementedError

    def has_node(self, path, rev):
        """
        Tell if there's a node at the specified (path,rev) combination.
        """
        raise NotImplementedError

    def get_node(self, path, rev=None):
        """
        Retrieve a Node (directory or file) from the repository at the
        given path. If the rev parameter is specified, the version of the
        node at that revision is returned, otherwise the latest version
        of the node is returned.
        """
        raise NotImplementedError

    def get_oldest_rev(self):
        """
        Return the oldest revision stored in the repository.
        """
        raise NotImplementedError
    oldest_rev = property(lambda x: x.get_oldest_rev())

    def get_youngest_rev(self):
        """
        Return the youngest revision in the repository.
        """
        raise NotImplementedError
    youngest_rev = property(lambda x: x.get_youngest_rev())

    def previous_rev(self, rev):
        """
        Return the revision immediately preceding the specified revision.
        """
        raise NotImplementedError

    def next_rev(self, rev):
        """
        Return the revision immediately following the specified revision.
        """
        raise NotImplementedError

    def rev_older_than(self, rev1, rev2):
        """
        Return True if rev1 is older than rev2, i.e. if rev1 comes before rev2
        in the revision sequence.
        """
        raise NotImplementedError

    def get_youngest_rev_in_cache(self, db):
        """
        Return the youngest revision currently cached.
        The way revisions are sequenced is version control specific.
        By default, one assumes that the revisions are sequenced in time.
        """
        cursor = db.cursor()
        cursor.execute("SELECT rev FROM revision ORDER BY time DESC LIMIT 1")
        row = cursor.fetchone()
        return row and row[0] or None

    def get_path_history(self, path, rev=None, limit=None):
        """
        Retrieve all the revisions containing this path (no newer than 'rev').
        The result format should be the same as the one of Node.get_history()
        """
        raise NotImplementedError

    def normalize_path(self, path):
        """
        Return a canonical representation of path in the repos.
        """
        return NotImplementedError

    def normalize_rev(self, rev):
        """
        Return a canonical representation of a revision in the repos.
        'None' is a valid revision value and represents the youngest revision.
        """
        return NotImplementedError

class Node(object):
    """
    Represents a directory or file in the repository.
    """

    DIRECTORY = "dir"
    FILE = "file"

    def __init__(self, path, rev, kind):
        assert kind in (Node.DIRECTORY, Node.FILE), "Unknown node kind %s" % kind
        self.path = str(path)
        self.rev = rev
        self.kind = kind

    def get_content(self):
        """
        Return a stream for reading the content of the node. This method
        will return None for directories. The returned object should provide
        a read([len]) function.
        """
        raise NotImplementedError

    def get_entries(self):
        """
        Generator that yields the immediate child entries of a directory, in no
        particular order. If the node is a file, this method returns None.
        """
        raise NotImplementedError

    def get_history(self, limit=None):
        """
        Generator that yields (path, rev, chg) tuples, one for each revision in which
        the node was changed. This generator will follow copies and moves of a
        node (if the underlying version control system supports that), which
        will be indicated by the first element of the tuple (i.e. the path)
        changing.
        Starts with an entry for the current revision.
        """
        raise NotImplementedError

    def get_properties(self):
        """
        Returns a dictionary containing the properties (meta-data) of the node.
        The set of properties depends on the version control system.
        """
        raise NotImplementedError

    def get_content_length(self):
        raise NotImplementedError
    content_length = property(lambda x: x.get_content_length())

    def get_content_type(self):
        raise NotImplementedError
    content_type = property(lambda x: x.get_content_type())

    def get_name(self):
        return self.path.split('/')[-1]
    name = property(lambda x: x.get_name())

    def get_last_modified(self):
        raise NotImplementedError
    last_modified = property(lambda x: x.get_last_modified())

    isdir = property(lambda x: x.kind == Node.DIRECTORY)
    isfile = property(lambda x: x.kind == Node.FILE)

class Changeset(object):
    """
    Represents a set of changes of a repository.
    """

    ADD = 'add'
    COPY = 'copy'
    DELETE = 'delete'
    EDIT = 'edit'
    MOVE = 'move'

    def __init__(self, rev, message, author, date):
        self.rev = rev
        self.message = message
        self.author = author
        self.date = date

    def get_changes(self):
        """
        Generator that produces a (path, kind, change, base_rev, base_path)
        tuple for every change in the changeset, where change can be one of
        Changeset.ADD, Changeset.COPY, Changeset.DELETE, Changeset.EDIT or
        Changeset.MOVE, and kind is one of Node.FILE or Node.DIRECTORY.
        """
        raise NotImplementedError

class PermissionDenied(PermissionError):
    """
    Exception raised by an authorizer if the user has insufficient permissions
    to view a specific part of the repository.
    """
    def __str__(self):
        return self.action

class Authorizer(object):
    """
    Base class for authorizers that are responsible to granting or denying
    access to view certain parts of a repository.
    """

    def assert_permission(self, path):
        if not self.has_permission(path):
            raise PermissionDenied, \
                  'Insufficient permissions to access %s' % path

    def assert_permission_for_changeset(self, rev):
        if not self.has_permission_for_changeset(rev):
            raise PermissionDenied, \
                  'Insufficient permissions to access changeset %s' % rev

    def has_permission(self, path):
        return 1

    def has_permission_for_changeset(self, rev):
        return 1

<commit_msg>Add a generic implementation for the recently introduced `Repository.has_node(path,rev)`<commit_after># -*- coding: iso8859-1 -*-
#
# Copyright (C) 2005 Edgewall Software
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.com/license.html.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://projects.edgewall.com/trac/.
#
# Author: Christopher Lenz <cmlenz@gmx.de>

from __future__ import generators
from trac.perm import PermissionError

class Repository(object):
    """
    Base class for a repository provided by a version control system.
    """

    def __init__(self, name, authz, log):
        self.name = name
        self.authz = authz or Authorizer()
        self.log = log

    def close(self):
        """
        Close the connection to the repository.
        """
        raise NotImplementedError

    def get_changeset(self, rev):
        """
        Retrieve a Changeset object that describes the changes made in
        revision 'rev'.
        """
        raise NotImplementedError

    def has_node(self, path, rev):
        """
        Tell if there's a node at the specified (path,rev) combination.
        """
        try:
            self.get_node()
            return True
        except TracError:
            return False

    def get_node(self, path, rev=None):
        """
        Retrieve a Node (directory or file) from the repository at the
        given path. If the rev parameter is specified, the version of the
        node at that revision is returned, otherwise the latest version
        of the node is returned.
        """
        raise NotImplementedError

    def get_oldest_rev(self):
        """
        Return the oldest revision stored in the repository.
        """
        raise NotImplementedError
    oldest_rev = property(lambda x: x.get_oldest_rev())

    def get_youngest_rev(self):
        """
        Return the youngest revision in the repository.
        """
        raise NotImplementedError
    youngest_rev = property(lambda x: x.get_youngest_rev())

    def previous_rev(self, rev):
        """
        Return the revision immediately preceding the specified revision.
        """
        raise NotImplementedError

    def next_rev(self, rev):
        """
        Return the revision immediately following the specified revision.
        """
        raise NotImplementedError

    def rev_older_than(self, rev1, rev2):
        """
        Return True if rev1 is older than rev2, i.e. if rev1 comes before rev2
        in the revision sequence.
        """
        raise NotImplementedError

    def get_youngest_rev_in_cache(self, db):
        """
        Return the youngest revision currently cached.
        The way revisions are sequenced is version control specific.
        By default, one assumes that the revisions are sequenced in time.
        """
        cursor = db.cursor()
        cursor.execute("SELECT rev FROM revision ORDER BY time DESC LIMIT 1")
        row = cursor.fetchone()
        return row and row[0] or None

    def get_path_history(self, path, rev=None, limit=None):
        """
        Retrieve all the revisions containing this path (no newer than 'rev').
        The result format should be the same as the one of Node.get_history()
        """
        raise NotImplementedError

    def normalize_path(self, path):
        """
        Return a canonical representation of path in the repos.
        """
        return NotImplementedError

    def normalize_rev(self, rev):
        """
        Return a canonical representation of a revision in the repos.
        'None' is a valid revision value and represents the youngest revision.
        """
        return NotImplementedError

class Node(object):
    """
    Represents a directory or file in the repository.
    """

    DIRECTORY = "dir"
    FILE = "file"

    def __init__(self, path, rev, kind):
        assert kind in (Node.DIRECTORY, Node.FILE), "Unknown node kind %s" % kind
        self.path = str(path)
        self.rev = rev
        self.kind = kind

    def get_content(self):
        """
        Return a stream for reading the content of the node. This method
        will return None for directories. The returned object should provide
        a read([len]) function.
        """
        raise NotImplementedError

    def get_entries(self):
        """
        Generator that yields the immediate child entries of a directory, in no
        particular order. If the node is a file, this method returns None.
        """
        raise NotImplementedError

    def get_history(self, limit=None):
        """
        Generator that yields (path, rev, chg) tuples, one for each revision in which
        the node was changed. This generator will follow copies and moves of a
        node (if the underlying version control system supports that), which
        will be indicated by the first element of the tuple (i.e. the path)
        changing.
        Starts with an entry for the current revision.
        """
        raise NotImplementedError

    def get_properties(self):
        """
        Returns a dictionary containing the properties (meta-data) of the node.
        The set of properties depends on the version control system.
        """
        raise NotImplementedError

    def get_content_length(self):
        raise NotImplementedError
    content_length = property(lambda x: x.get_content_length())

    def get_content_type(self):
        raise NotImplementedError
    content_type = property(lambda x: x.get_content_type())

    def get_name(self):
        return self.path.split('/')[-1]
    name = property(lambda x: x.get_name())

    def get_last_modified(self):
        raise NotImplementedError
    last_modified = property(lambda x: x.get_last_modified())

    isdir = property(lambda x: x.kind == Node.DIRECTORY)
    isfile = property(lambda x: x.kind == Node.FILE)

class Changeset(object):
    """
    Represents a set of changes of a repository.
    """

    ADD = 'add'
    COPY = 'copy'
    DELETE = 'delete'
    EDIT = 'edit'
    MOVE = 'move'

    def __init__(self, rev, message, author, date):
        self.rev = rev
        self.message = message
        self.author = author
        self.date = date

    def get_changes(self):
        """
        Generator that produces a (path, kind, change, base_rev, base_path)
        tuple for every change in the changeset, where change can be one of
        Changeset.ADD, Changeset.COPY, Changeset.DELETE, Changeset.EDIT or
        Changeset.MOVE, and kind is one of Node.FILE or Node.DIRECTORY.
        """
        raise NotImplementedError

class PermissionDenied(PermissionError):
    """
    Exception raised by an authorizer if the user has insufficient permissions
    to view a specific part of the repository.
    """
    def __str__(self):
        return self.action

class Authorizer(object):
    """
    Base class for authorizers that are responsible to granting or denying
    access to view certain parts of a repository.
    """

    def assert_permission(self, path):
        if not self.has_permission(path):
            raise PermissionDenied, \
                  'Insufficient permissions to access %s' % path

    def assert_permission_for_changeset(self, rev):
        if not self.has_permission_for_changeset(rev):
            raise PermissionDenied, \
                  'Insufficient permissions to access changeset %s' % rev

    def has_permission(self, path):
        return 1

    def has_permission_for_changeset(self, rev):
        return 1

<|endoftext|>


<commit_before># Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from tensorflow.keras.__internal__.layers import BaseImageAugmentationLayer
from tensorflow.keras.layers import CenterCrop
from tensorflow.keras.layers import RandomBrightness
from tensorflow.keras.layers import RandomContrast
from tensorflow.keras.layers import RandomCrop
from tensorflow.keras.layers import RandomFlip
from tensorflow.keras.layers import RandomHeight
from tensorflow.keras.layers import RandomRotation
from tensorflow.keras.layers import RandomTranslation
from tensorflow.keras.layers import RandomWidth
from tensorflow.keras.layers import RandomZoom
from tensorflow.keras.layers import Rescaling
from tensorflow.keras.layers import Resizing

from keras_cv.layers.preprocessing.aug_mix import AugMix
from keras_cv.layers.preprocessing.auto_contrast import AutoContrast
from keras_cv.layers.preprocessing.channel_shuffle import ChannelShuffle
from keras_cv.layers.preprocessing.cut_mix import CutMix
from keras_cv.layers.preprocessing.equalization import Equalization
from keras_cv.layers.preprocessing.fourier_mix import FourierMix
from keras_cv.layers.preprocessing.grayscale import Grayscale
from keras_cv.layers.preprocessing.grid_mask import GridMask
from keras_cv.layers.preprocessing.mix_up import MixUp
from keras_cv.layers.preprocessing.posterization import Posterization
from keras_cv.layers.preprocessing.rand_augment import RandAugment
from keras_cv.layers.preprocessing.random_augmentation_pipeline import (
    RandomAugmentationPipeline,
)
from keras_cv.layers.preprocessing.random_channel_shift import RandomChannelShift
from keras_cv.layers.preprocessing.random_choice import RandomChoice
from keras_cv.layers.preprocessing.random_color_degeneration import (
    RandomColorDegeneration,
)
from keras_cv.layers.preprocessing.random_color_jitter import RandomColorJitter
from keras_cv.layers.preprocessing.random_cutout import RandomCutout
from keras_cv.layers.preprocessing.random_gaussian_blur import RandomGaussianBlur
from keras_cv.layers.preprocessing.random_hue import RandomHue
from keras_cv.layers.preprocessing.random_saturation import RandomSaturation
from keras_cv.layers.preprocessing.random_sharpness import RandomSharpness
from keras_cv.layers.preprocessing.random_shear import RandomShear
from keras_cv.layers.preprocessing.solarization import Solarization
from keras_cv.layers.regularization.dropblock_2d import DropBlock2D
from keras_cv.layers.regularization.stochastic_depth import StochasticDepth
<commit_msg>Correction in `layers.__init__.py` (#503)<commit_after># Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from tensorflow.keras.layers import CenterCrop
from tensorflow.keras.layers import RandomBrightness
from tensorflow.keras.layers import RandomContrast
from tensorflow.keras.layers import RandomCrop
from tensorflow.keras.layers import RandomFlip
from tensorflow.keras.layers import RandomHeight
from tensorflow.keras.layers import RandomRotation
from tensorflow.keras.layers import RandomTranslation
from tensorflow.keras.layers import RandomWidth
from tensorflow.keras.layers import RandomZoom
from tensorflow.keras.layers import Rescaling
from tensorflow.keras.layers import Resizing

from keras_cv.layers.preprocessing.aug_mix import AugMix
from keras_cv.layers.preprocessing.auto_contrast import AutoContrast
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
    BaseImageAugmentationLayer,
)
from keras_cv.layers.preprocessing.channel_shuffle import ChannelShuffle
from keras_cv.layers.preprocessing.cut_mix import CutMix
from keras_cv.layers.preprocessing.equalization import Equalization
from keras_cv.layers.preprocessing.fourier_mix import FourierMix
from keras_cv.layers.preprocessing.grayscale import Grayscale
from keras_cv.layers.preprocessing.grid_mask import GridMask
from keras_cv.layers.preprocessing.mix_up import MixUp
from keras_cv.layers.preprocessing.posterization import Posterization
from keras_cv.layers.preprocessing.rand_augment import RandAugment
from keras_cv.layers.preprocessing.random_augmentation_pipeline import (
    RandomAugmentationPipeline,
)
from keras_cv.layers.preprocessing.random_channel_shift import RandomChannelShift
from keras_cv.layers.preprocessing.random_choice import RandomChoice
from keras_cv.layers.preprocessing.random_color_degeneration import (
    RandomColorDegeneration,
)
from keras_cv.layers.preprocessing.random_color_jitter import RandomColorJitter
from keras_cv.layers.preprocessing.random_cutout import RandomCutout
from keras_cv.layers.preprocessing.random_gaussian_blur import RandomGaussianBlur
from keras_cv.layers.preprocessing.random_hue import RandomHue
from keras_cv.layers.preprocessing.random_saturation import RandomSaturation
from keras_cv.layers.preprocessing.random_sharpness import RandomSharpness
from keras_cv.layers.preprocessing.random_shear import RandomShear
from keras_cv.layers.preprocessing.solarization import Solarization
from keras_cv.layers.regularization.dropblock_2d import DropBlock2D
from keras_cv.layers.regularization.stochastic_depth import StochasticDepth
<|endoftext|>