<commit_before>import urwid
import tempfile
import subprocess
import os
from wikicurses import formats
from wikicurses import settings
from wikicurses.wiki import Wiki
from wikicurses.htmlparse import parseDisambig

def tabComplete(text, matches):
    if not matches:
        return text
    matches = sorted(matches, key=len)
    if matches[0] == text and len(matches)>1:
        match = matches[1]
    else:
        match = matches[0]
    return match

class SearchBox(urwid.Edit):
    title = "Search"
    def keypress(self, size, key):
        if key == 'enter':
            loop.widget = mainwidget
            setContent(settings.wiki.search(self.edit_text or 'Main page'))
        elif key == 'tab':
            matches = settings.wiki.search_sugestions(self.edit_text)
            match = tabComplete(self.edit_text, matches)
            self.set_edit_text(match)
            self.edit_pos = len(match)
        else:
            return super().keypress(size, key)

class SelectorBox(urwid.ListBox):
    def __init__(self):
        def selectButton(radio_button, new_state, parameter):
            if new_state:
                loop.widget = mainwidget
                self._select(parameter)

        super().__init__(urwid.SimpleFocusListWalker([]))
        for i, item in enumerate(self._items()):
            if isinstance(item, tuple):
                name, selected, parameter = item
            else:
                parameter = name = item
                selected = False
            urwid.RadioButton(self.body, name, selected, selectButton, parameter)
            if selected:
                self.set_focus(i)

class Results(SelectorBox):
    title = "Results"
    def __init__(self, results):
        self.results = results
        super().__init__()

    def _items(self):
        return self.results

    def _select(self, title):
        setContent(settings.wiki.search(title))

class Toc(SelectorBox):
    title = "Table of Contents"
    def _items(self):
        return ((name, widgets.focus>=ind, ind) for name, ind in widgetnames)

    def _select(self, index):
       widgets.set_focus(index)

class Bmarks(SelectorBox):
    title = "Bookmarks"
    def _items(self):
        self.deleted = []
        return settings.bmarks

    def _select(self, name):
        setContent(settings.wiki.search(name))

    def keypress(self, size, key):
        #Undo Delete
        if key == 'u' and self.deleted:
            index, item = self.deleted.pop()
            settings.bmarks.add(item.label)
            self.body.insert(index, item)
            self.set_focus(index)
        elif key in ('meta [', 'x') and self.focus:
            settings.bmarks.discard(self.focus.label)
            self.deleted.append((self.focus_position, self.focus))
            self.body.remove(self.focus)
        else:
            return super().keypress(size, key)

class Wikis(SelectorBox):
    title = "Wikis"
    def _items(self):
        for name, url in settings.wikis().items():
            yield name, settings.wiki.siteurl == url, name

    def _select(self, name):
        settings.openWiki(name)
        setContent(settings.wiki.search('Main page'))

class Feeds(SelectorBox):
    title = "Feeds"
    def _items(self):
        return settings.wiki.list_featured_feeds()

    def _select(self, feed):
        setContent(settings.wiki.get_featured_feed(feed))

class Disambig(urwid.ListBox):
    title = "Disambiguation"
    def __init__(self, html):
        def selectButton(radio_button, new_state, title):
            if new_state:
                loop.widget = mainwidget
                setContent(settings.wiki.search(title))

        sections = parseDisambig(html)
        super().__init__(urwid.SimpleFocusListWalker([]))
        buttons = []

        for title, items in sections.items():
            if title:
                self.body.append(urwid.Text(['\n', ('h', title)], align='center'))
            for name, text in items:
                if name:
                    button = urwid.RadioButton(buttons, text, False,
                            selectButton, name)
                    self.body.append(button)
                else:
                    self.body.append(urwid.Text(text))

class Ex(urwid.Edit):
    def keypress(self, size, key):
        if key == 'esc' or (key == 'backspace' and not self.edit_text):
            self.exitexmode()
        elif key == 'tab':
            matches = [i for i in cmds if i.startswith(self.edit_text)]
            match = tabComplete(self.edit_text, matches)
            self.set_edit_text(match)
            self.edit_pos = len(match)
        elif key == 'enter':
            words = self.edit_text.split(' ')
            self.exitexmode()
            processCmd(*words)
        else:
            return super().keypress(size, key)

    def exitexmode(self):
        self.set_caption('')
        self.set_edit_text('')
        mainwidget.set_focus('body')

    def enterexmode(self):
        mainwidget.set_focus('footer')
        self.set_caption(':')

cmds = ('quit', 'bmark', 'bmarks', 'wikis', 'feeds', 'open', 'contents', 'edit')
def processCmd(cmd, *args):
    if cmd in ('q', 'quit'):
        raise urwid.ExitMainLoop
    elif cmd == 'bmark':
        settings.bmarks.add(header.text)
        notify("Bookmark Added")
    elif cmd in ('bmarks' ,'wikis', 'feeds', 'contents'):
        openOverlay({'bmarks':Bmarks,
                     'wikis':Wikis,
                     'feeds':Feeds,
                     'contents':Toc}[cmd]())
    elif cmd == 'open':
        if args:
            setContent(settings.wiki.search(' '.join(args)))
        else:
            openOverlay(urwid.ListBox([SearchBox()]), "Search", height=3)
    elif cmd == 'edit':
        title = header.text
        init = settings.wiki.init_edit(title)
        if not init:
            notify('Unable to Edit: Page Not Found')
            return

        if not settings.wiki.csrftoken: #If not logged in
            error = settings.wiki.login()
            if error:
                notify('Login Failed: ' + error)
                return

        text, verify = init
        with tempfile.NamedTemporaryFile('w', delete=False) as file:
            file.write(text)
            tmpname = file.name

        editor = os.environ.get('EDITOR', 'vim')
        subprocess.call([editor, tmpname])

        with open(tmpname) as file:
            newtext = file.read()

        if newtext == text:
            notify('Edit Canceled: No Change')
            return

        summary = urwid.Edit('Summary: ')
        minor = urwid.CheckBox('Minor Edit')
        def submit(button):
            loop.widget = mainwidget
            settings.wiki.commit_edit(title, newtext, summary.edit_text,
                    minor.get_state(), verify)
            setContent(settings.wiki.search(title))
        listbox = urwid.ListBox([summary, minor, urwid.Button('Submit', submit)])
        openOverlay(listbox, 'Edit', 5)

    elif cmd:
        notify(cmd + ': Unknown Command')

def notify(text):
    mainwidget.footer = urwid.Text(text)

def openOverlay(widget, title=None, height=('relative', 50), width=('relative', 50)):
    box = urwid.LineBox(widget, title or widget.title)
    overlay = urwid.Overlay(box, mainwidget, 'center', width, 'middle', height)
    loop.widget = overlay

def keymapper(input):
    #TODO: Implement gg and G

    cmdmap = settings.conf['keymap']
    if input == ':':
        mainwidget.footer.enterexmode()
    elif input in cmdmap and cmdmap[input]:
        processCmd(cmdmap[input])
    elif input == 'esc':
        loop.widget = mainwidget
    else:
       return False
    return True

def input_filter(keys, raw):
    if not isinstance(mainwidget.footer, urwid.Edit):
        mainwidget.footer = Ex()
    return keys

def setContent(page):
    if not page.exists:
        results = settings.wiki.search_sugestions(page.title)
        if results:
            openOverlay(Results(results))
            return
    elif 'disambiguation' in page.properties:
        openOverlay(Disambig(page.result['text']['*']))
        return

    widgets.clear()
    widgetnames.clear()
    header.set_text(page.title)
    for title, content in page.content.items():
        if title:
            h2 = urwid.Text([('h2', title), '\n'], align="center")
            widgets.append(h2)
            widgetnames.append((title, widgets.index(h2)))
        else:
            widgetnames.append((page.title, 0))
        widgets.append(urwid.Text(content))

palette = [('h1', 'bold', 'dark blue'),
           ('h2', 'underline', ''),
           ('h', 'underline', '')]

#(ITALIC, 'italic') does not work. No italics option?
outputfmt = (('b', 'bold'), ('blockquote', 'dark gray'))
for x in range(1, sum(formats) + 1):
    fmt = ','.join(j for i, j in outputfmt if x&formats[i])
    palette.append((x, fmt, ''))

widgets = urwid.SimpleFocusListWalker([])
widgetnames = []
pager = urwid.ListBox(widgets)

header = urwid.Text('Wikicurses', align='center')
mainwidget = urwid.Frame(pager, urwid.AttrMap(header, 'h1'), Ex())

urwid.command_map['k'] = 'cursor up'
urwid.command_map['j'] = 'cursor down'
urwid.command_map['ctrl b'] = 'cursor page up'
urwid.command_map['ctrl f'] = 'cursor page down'

loop = urwid.MainLoop(mainwidget, palette=palette, handle_mouse=False,
                     unhandled_input=keymapper, input_filter=input_filter)
<commit_msg>Unlink temporary file after use<commit_after>import urwid
import tempfile
import subprocess
import os
from wikicurses import formats
from wikicurses import settings
from wikicurses.wiki import Wiki
from wikicurses.htmlparse import parseDisambig

def tabComplete(text, matches):
    if not matches:
        return text
    matches = sorted(matches, key=len)
    if matches[0] == text and len(matches)>1:
        match = matches[1]
    else:
        match = matches[0]
    return match

class SearchBox(urwid.Edit):
    title = "Search"
    def keypress(self, size, key):
        if key == 'enter':
            loop.widget = mainwidget
            setContent(settings.wiki.search(self.edit_text or 'Main page'))
        elif key == 'tab':
            matches = settings.wiki.search_sugestions(self.edit_text)
            match = tabComplete(self.edit_text, matches)
            self.set_edit_text(match)
            self.edit_pos = len(match)
        else:
            return super().keypress(size, key)

class SelectorBox(urwid.ListBox):
    def __init__(self):
        def selectButton(radio_button, new_state, parameter):
            if new_state:
                loop.widget = mainwidget
                self._select(parameter)

        super().__init__(urwid.SimpleFocusListWalker([]))
        for i, item in enumerate(self._items()):
            if isinstance(item, tuple):
                name, selected, parameter = item
            else:
                parameter = name = item
                selected = False
            urwid.RadioButton(self.body, name, selected, selectButton, parameter)
            if selected:
                self.set_focus(i)

class Results(SelectorBox):
    title = "Results"
    def __init__(self, results):
        self.results = results
        super().__init__()

    def _items(self):
        return self.results

    def _select(self, title):
        setContent(settings.wiki.search(title))

class Toc(SelectorBox):
    title = "Table of Contents"
    def _items(self):
        return ((name, widgets.focus>=ind, ind) for name, ind in widgetnames)

    def _select(self, index):
       widgets.set_focus(index)

class Bmarks(SelectorBox):
    title = "Bookmarks"
    def _items(self):
        self.deleted = []
        return settings.bmarks

    def _select(self, name):
        setContent(settings.wiki.search(name))

    def keypress(self, size, key):
        #Undo Delete
        if key == 'u' and self.deleted:
            index, item = self.deleted.pop()
            settings.bmarks.add(item.label)
            self.body.insert(index, item)
            self.set_focus(index)
        elif key in ('meta [', 'x') and self.focus:
            settings.bmarks.discard(self.focus.label)
            self.deleted.append((self.focus_position, self.focus))
            self.body.remove(self.focus)
        else:
            return super().keypress(size, key)

class Wikis(SelectorBox):
    title = "Wikis"
    def _items(self):
        for name, url in settings.wikis().items():
            yield name, settings.wiki.siteurl == url, name

    def _select(self, name):
        settings.openWiki(name)
        setContent(settings.wiki.search('Main page'))

class Feeds(SelectorBox):
    title = "Feeds"
    def _items(self):
        return settings.wiki.list_featured_feeds()

    def _select(self, feed):
        setContent(settings.wiki.get_featured_feed(feed))

class Disambig(urwid.ListBox):
    title = "Disambiguation"
    def __init__(self, html):
        def selectButton(radio_button, new_state, title):
            if new_state:
                loop.widget = mainwidget
                setContent(settings.wiki.search(title))

        sections = parseDisambig(html)
        super().__init__(urwid.SimpleFocusListWalker([]))
        buttons = []

        for title, items in sections.items():
            if title:
                self.body.append(urwid.Text(['\n', ('h', title)], align='center'))
            for name, text in items:
                if name:
                    button = urwid.RadioButton(buttons, text, False,
                            selectButton, name)
                    self.body.append(button)
                else:
                    self.body.append(urwid.Text(text))

class Ex(urwid.Edit):
    def keypress(self, size, key):
        if key == 'esc' or (key == 'backspace' and not self.edit_text):
            self.exitexmode()
        elif key == 'tab':
            matches = [i for i in cmds if i.startswith(self.edit_text)]
            match = tabComplete(self.edit_text, matches)
            self.set_edit_text(match)
            self.edit_pos = len(match)
        elif key == 'enter':
            words = self.edit_text.split(' ')
            self.exitexmode()
            processCmd(*words)
        else:
            return super().keypress(size, key)

    def exitexmode(self):
        self.set_caption('')
        self.set_edit_text('')
        mainwidget.set_focus('body')

    def enterexmode(self):
        mainwidget.set_focus('footer')
        self.set_caption(':')

cmds = ('quit', 'bmark', 'bmarks', 'wikis', 'feeds', 'open', 'contents', 'edit')
def processCmd(cmd, *args):
    if cmd in ('q', 'quit'):
        raise urwid.ExitMainLoop
    elif cmd == 'bmark':
        settings.bmarks.add(header.text)
        notify("Bookmark Added")
    elif cmd in ('bmarks' ,'wikis', 'feeds', 'contents'):
        openOverlay({'bmarks':Bmarks,
                     'wikis':Wikis,
                     'feeds':Feeds,
                     'contents':Toc}[cmd]())
    elif cmd == 'open':
        if args:
            setContent(settings.wiki.search(' '.join(args)))
        else:
            openOverlay(urwid.ListBox([SearchBox()]), "Search", height=3)
    elif cmd == 'edit':
        title = header.text
        init = settings.wiki.init_edit(title)
        if not init:
            notify('Unable to Edit: Page Not Found')
            return

        if not settings.wiki.csrftoken: #If not logged in
            error = settings.wiki.login()
            if error:
                notify('Login Failed: ' + error)
                return

        text, verify = init
        with tempfile.NamedTemporaryFile('w', delete=False) as file:
            file.write(text)
            tmpname = file.name

        editor = os.environ.get('EDITOR', 'vim')
        subprocess.call([editor, tmpname])

        with open(tmpname) as file:
            newtext = file.read()
        os.unlink(tmpname)

        if newtext == text:
            notify('Edit Canceled: No Change')
            return

        summary = urwid.Edit('Summary: ')
        minor = urwid.CheckBox('Minor Edit')
        def submit(button):
            loop.widget = mainwidget
            settings.wiki.commit_edit(title, newtext, summary.edit_text,
                    minor.get_state(), verify)
            setContent(settings.wiki.search(title))
        listbox = urwid.ListBox([summary, minor, urwid.Button('Submit', submit)])
        openOverlay(listbox, 'Edit', 5)

    elif cmd:
        notify(cmd + ': Unknown Command')

def notify(text):
    mainwidget.footer = urwid.Text(text)

def openOverlay(widget, title=None, height=('relative', 50), width=('relative', 50)):
    box = urwid.LineBox(widget, title or widget.title)
    overlay = urwid.Overlay(box, mainwidget, 'center', width, 'middle', height)
    loop.widget = overlay

def keymapper(input):
    #TODO: Implement gg and G

    cmdmap = settings.conf['keymap']
    if input == ':':
        mainwidget.footer.enterexmode()
    elif input in cmdmap and cmdmap[input]:
        processCmd(cmdmap[input])
    elif input == 'esc':
        loop.widget = mainwidget
    else:
       return False
    return True

def input_filter(keys, raw):
    if not isinstance(mainwidget.footer, urwid.Edit):
        mainwidget.footer = Ex()
    return keys

def setContent(page):
    if not page.exists:
        results = settings.wiki.search_sugestions(page.title)
        if results:
            openOverlay(Results(results))
            return
    elif 'disambiguation' in page.properties:
        openOverlay(Disambig(page.result['text']['*']))
        return

    widgets.clear()
    widgetnames.clear()
    header.set_text(page.title)
    for title, content in page.content.items():
        if title:
            h2 = urwid.Text([('h2', title), '\n'], align="center")
            widgets.append(h2)
            widgetnames.append((title, widgets.index(h2)))
        else:
            widgetnames.append((page.title, 0))
        widgets.append(urwid.Text(content))

palette = [('h1', 'bold', 'dark blue'),
           ('h2', 'underline', ''),
           ('h', 'underline', '')]

#(ITALIC, 'italic') does not work. No italics option?
outputfmt = (('b', 'bold'), ('blockquote', 'dark gray'))
for x in range(1, sum(formats) + 1):
    fmt = ','.join(j for i, j in outputfmt if x&formats[i])
    palette.append((x, fmt, ''))

widgets = urwid.SimpleFocusListWalker([])
widgetnames = []
pager = urwid.ListBox(widgets)

header = urwid.Text('Wikicurses', align='center')
mainwidget = urwid.Frame(pager, urwid.AttrMap(header, 'h1'), Ex())

urwid.command_map['k'] = 'cursor up'
urwid.command_map['j'] = 'cursor down'
urwid.command_map['ctrl b'] = 'cursor page up'
urwid.command_map['ctrl f'] = 'cursor page down'

loop = urwid.MainLoop(mainwidget, palette=palette, handle_mouse=False,
                     unhandled_input=keymapper, input_filter=input_filter)
<|endoftext|>


<commit_before># Copyright (C) 2011 Victor Semionov
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#  * Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#  * Neither the name of the copyright holder nor the names of the contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import socket
import logging
import random

import modules
import net
import cmdparser
import db
import cache
import handlers

logger = None

_fqdn = ""
_server_string = ""
_domain = ""

def _send_banner(conn):
    local = "{}.{}".format(random.randint(0, 9999), random.randint(0, 9999))
    msg_id = "<{}@{}>".format(local, _domain)
    conn.write_status(220, "{} {} {}".format(_fqdn, _server_string, msg_id))

def _session(conn):
    try:
        with modules.db().Backend() as backend, modules.cache().Cache() as cacher:
            _send_banner(conn)

            end = False
            while not end:
                line = conn.read_line()
                correct, command = cmdparser.parse_command(line)
                if correct:
                    end = handlers.handle_command(conn, backend, cacher, command)
                else:
                    handlers.handle_syntax_error(conn, command)
    except (db.BackendError, cache.CacheError):
        conn.write_status(420, "Server temporarily unavailable")
    except (IOError, EOFError, UnicodeDecodeError, BufferError):
        pass
    except Exception as ex:
        logger.exception("unexpected error")

def configure(config):
    global _fqdn, _server_string, _domain
    _fqdn = socket.getfqdn()
    _server_string = config.get("server", "wordbase")
    _domain = config.get("domain", "example.com")

    global logger
    logger = logging.getLogger(__name__)

    cmdparser.init()

    info = config.get("info", "")
    handlers.configure(_server_string, info)

def process_session(sock, addr):
    with sock, net.Connection(sock) as conn:
        host, port = addr
        logger.info("session started from address %s:%d", host, port)
        try:
            _session(conn)
        finally:
            logger.info("session ended")
<commit_msg>Refactoring.<commit_after># Copyright (C) 2011 Victor Semionov
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#  * Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#  * Neither the name of the copyright holder nor the names of the contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import socket
import logging
import random

import modules
import net
import cmdparser
import db
import cache
import handlers

logger = None

_fqdn = ""
_server_string = ""
_domain = ""

def _send_banner(conn):
    local = "{}.{}".format(random.randint(0, 9999), random.randint(0, 9999))
    msg_id = "<{}@{}>".format(local, _domain)
    conn.write_status(220, "{} {} {}".format(_fqdn, _server_string, msg_id))

def _session(conn):
    try:
        with modules.db().Backend() as backend, modules.cache().Cache() as cacher:
            _send_banner(conn)

            end = False
            while not end:
                line = conn.read_line()
                correct, command = cmdparser.parse_command(line)
                if correct:
                    end = handlers.handle_command(conn, backend, cacher, command)
                else:
                    handlers.handle_syntax_error(conn, command)
    except (db.BackendError, cache.CacheError):
        conn.write_status(420, "Server temporarily unavailable")
    except (IOError, EOFError, UnicodeDecodeError, BufferError):
        pass
    except Exception:
        logger.exception("unexpected error")

def configure(config):
    global _fqdn, _server_string, _domain
    _fqdn = socket.getfqdn()
    _server_string = config.get("server", "wordbase")
    _domain = config.get("domain", "example.com")

    global logger
    logger = logging.getLogger(__name__)

    cmdparser.init()

    info = config.get("info", "")
    handlers.configure(_server_string, info)

def process_session(sock, addr):
    with sock, net.Connection(sock) as conn:
        host, port = addr
        logger.info("session started from address %s:%d", host, port)
        try:
            _session(conn)
        finally:
            logger.info("session ended")
<|endoftext|>


<commit_before># linter.py
# Part of SublimeLinter, a code checking framework for Sublime Text 3
#
# Project: https://github.com/SublimeLinter/sublimelinter
# License: MIT

import re
import sublime

from .highlight import Highlight
from . import persist
from . import util

SYNTAX_RE = re.compile(r'/([^/]+)\.tmLanguage$')
WARNING_RE = re.compile(r'^w(?:arn(?:ing)?)?$', re.IGNORECASE)

class Registrar(type):
    '''This metaclass registers the linter when the class is declared.'''
    def __init__(cls, name, bases, attrs):
        if bases:
            persist.register_linter(cls, name, attrs)

class Linter(metaclass=Registrar):
    '''
    The base class for linters. Subclasses must at a minimum define
    the attributes language, cmd, and regex.
    '''

    #
    # Public attributes
    #

    # The name of the linter's language for display purposes.
    # By convention this is all lowercase.
    language = ''

    # The name of the executable used to do linting. If specified by a linter,
    # the executable's existence is checked, and if it is not available,
    # the linter is disabled.
    executable = None

    # If executable is specified and is available, this is set to the full path
    # of the executable. If the executable is not available, it is set an empty string.
    # If executable is not specified, it is None. Subclasses should consider this read only.
    executable_path = None

    # A string, tuple or callable that returns a string or tuple, containing the
    # command line arguments used to lint.
    cmd = ''

    # A regex pattern used to extract information from the linter's executable output.
    regex = ''

    # Set to True if the linter outputs multiple errors or multiline errors. When True,
    # regex will be created with the re.MULTILINE flag.
    multiline = False

    # If you want to set flags on the regex other than re.MULTILINE, set this.
    re_flags = 0

    # If the linter executable cannot receive from stdin and requires a temp file,
    # set this attribute to the suffix of the temp file.
    tempfile_suffix = None

    # Tab width
    tab_width = 1

    # If you want to limit the linter to specific portions of the source
    # based on a scope selector, set this attribute to the selector. For example,
    # in an html file with embedded php, you would set the selector for a php
    # linter to 'source.php'.
    selector = None

    # If you want to provide default settings for the linter, set this attribute.
    defaults = None

    #
    # Internal class storage, do not set
    #
    errors = None
    highlight = None
    lint_settings = None

    def __init__(self, view, syntax, filename=None):
        self.view = view
        self.syntax = syntax
        self.filename = filename

        if self.regex:
            if self.multiline:
                self.re_flags |= re.MULTILINE

            try:
                self.regex = re.compile(self.regex, self.re_flags)
            except:
                persist.debug('error compiling regex for {}'.format(self.language))

        self.highlight = Highlight()

    @classmethod
    def get_settings(cls):
        '''Return the default settings for this linter, merged with the user settings.'''
        linters = persist.settings.get('linters', {})
        settings = cls.defaults or {}
        settings.update(linters.get(cls.__name__, {}))
        return settings

    @property
    def settings(self):
        return self.get_settings()

    @classmethod
    def assign(cls, view, reassign=False):
        '''
        Assign a view to an instance of a linter.
        Find a linter for a specified view if possible, then add it to our view <--> lint class map and return it.
        Each view has its own linter so that linters can store persistent data about a view.
        '''
        vid = view.id()
        persist.views[vid] = view

        settings = view.settings()
        syntax = settings.get('syntax')

        if not syntax:
            cls.remove(vid)
            return

        match = SYNTAX_RE.search(syntax)

        if match:
            syntax = match.group(1)
        else:
            syntax = ''

        if syntax:
            if vid in persist.linters and persist.linters[vid] and not reassign:
                # If a linter in the set of linters for the given view
                # already handles the view's syntax, we have nothing more to do.
                for linter in tuple(persist.linters[vid]):
                    if linter.syntax == syntax:
                        return

            linters = set()

            for name, linter_class in persist.languages.items():
                if linter_class.can_lint(syntax):
                    linter = linter_class(view, syntax, view.file_name())
                    linters.add(linter)

            persist.linters[vid] = linters
            return linters

        cls.remove(vid)

    @classmethod
    def remove(cls, vid):
        '''Remove a the mapping between a view and its set of linters.'''
        if vid in persist.linters:
            for linters in persist.linters[vid]:
                linters.clear()

            del persist.linters[vid]

    @classmethod
    def reload(cls, mod=None):
        '''Reload all linters, optionally filtering by module.'''

        # Merge linter default settings with user settings
        linter_settings = persist.settings.get('linters', {})

        for name, linter in persist.languages.items():
            settings = linter_settings.get(name, {})
            defaults = (linter.defaults or {}).copy()
            defaults.update(settings)
            linter.lint_settings = defaults

        for vid, linters in persist.linters.items():
            for linter in linters:
                if mod and linter.__module__ != mod:
                    continue

                linter.clear()
                persist.linters[vid].remove(linter)
                linter = persist.languages[linter.name](linter.view, linter.syntax, linter.filename)
                persist.linters[vid].add(linter)
                linter.draw()

        return

    @classmethod
    def text(cls, view):
        '''Returns the entire text of a view.'''
        return view.substr(sublime.Region(0, view.size()))

    @classmethod
    def get_view(cls, vid):
        '''Returns the view object with the given id.'''
        return persist.views.get(vid)

    @classmethod
    def get_linters(cls, vid):
        '''Returns a tuple of linters for the view with the given id.'''
        if vid in persist.linters:
            return tuple(persist.linters[vid])

        return ()

    @classmethod
    def get_selectors(cls, vid):
        '''Returns a list of scope selectors for all linters for the view with the given id.'''
        return [
            (linter.selector, linter)
            for linter in cls.get_linters(vid)
            if linter.selector
        ]

    @classmethod
    def lint_view(cls, vid, filename, code, sections, callback):
        if not code or vid not in persist.linters:
            return

        linters = list(persist.linters.get(vid))

        if not linters:
            return

        filename = filename or 'untitled'
        #linter_list = (', '.join(l.name for l in linters))
        #persist.debug('lint \'{}\' as {}'.format(filename, linter_list))

        for linter in linters:
            if linter.settings.get('disable'):
                continue

            if not linter.selector:
                linter.reset(code, filename=filename)
                linter.lint()

        selectors = Linter.get_selectors(vid)

        for sel, linter in selectors:
            linters.append(linter)

            if sel in sections:
                linter.reset(code, filename=filename)
                errors = {}

                for line_offset, left, right in sections[sel]:
                    linter.highlight.move_to(line_offset, left)
                    linter.code = code[left:right]
                    linter.errors = {}
                    linter.lint()

                    for line, error in linter.errors.items():
                        errors[line + line_offset] = error

                linter.errors = errors

        # Merge our result back to the main thread
        callback(cls.get_view(vid), linters)

    def reset(self, code, filename=None, highlight=None):
        self.errors = {}
        self.code = code
        self.filename = filename or self.filename
        self.highlight = highlight or Highlight(self.code)

    def lint(self):
        if not (self.language and self.cmd and self.regex):
            raise NotImplementedError

        if callable(self.cmd):
            cmd = self.cmd()
        else:
            cmd = self.cmd

        if not cmd:
            return

        if not isinstance(cmd, (tuple, list)):
            cmd = (cmd,)

        output = self.run(cmd, self.code)

        if not output:
            return

        #print('{} output:\n'.format(self.__class__.__name__) + output)

        for match, row, col, error_type, message, near in self.find_errors(output):
            if match and row is not None:
                if error_type and WARNING_RE.match(error_type) is not None:
                    error_type = Highlight.WARNING
                else:
                    error_type = Highlight.ERROR

                if col is not None:
                    # Adjust column numbers to match the linter's tabs if necessary
                    if self.tab_width > 1:
                        start, end = self.highlight.full_line(row)
                        code_line = self.code[start:end]
                        diff = 0

                        for i in range(len(code_line)):
                            if code_line[i] == '\t':
                                diff += (self.tab_width - 1)

                            if col - diff <= i:
                                col = i
                                break

                    self.highlight.range(row, col, error_type=error_type)
                elif near:
                    self.highlight.near(row, near, error_type)

                self.error(row, col, message, error_type)

    def draw(self):
        self.highlight.draw(self.view)

    def clear(self):
        self.highlight.clear(self.view)

    # Helper methods

    @classmethod
    def can_lint(cls, language):
        '''Determines if a linter can lint a given language. Subclasses which '''
        return Linter.linter_can_lint(cls, language)

    @staticmethod
    def linter_can_lint(cls, language):
        can = False
        language = language.lower()

        if cls.language:
            if language == cls.language:
                can = True
            elif isinstance(cls.language, (tuple, list)) and language in cls.language:
                can = True

        if can and cls.executable and cls.executable_path is None:
            cls.executable_path = util.which(cls.executable) or ''
            can = cls.executable_path != ''

        return can

    def error(self, line, col, error, error_type):
        self.highlight.line(line, error_type)
        error = ((col or 0), str(error))

        if line in self.errors:
            self.errors[line].append(error)
        else:
            self.errors[line] = [error]

    def find_errors(self, output):
        if self.multiline:
            errors = self.regex.finditer(output)

            if errors:
                for error in errors:
                    yield self.split_match(error)
            else:
                yield self.split_match(None)
        else:
            for line in output.splitlines():
                yield self.match_error(self.regex, line.strip())

    def split_match(self, match):
        if match:
            items = {'line': None, 'col': None, 'type': None, 'error': '', 'near': None}
            items.update(match.groupdict())
            row, col, error_type, error, near = [items[k] for k in ('line', 'col', 'type', 'error', 'near')]

            row = int(row) - 1

            if col:
                col = int(col) - 1

            return match, row, col, error_type, error, near

        return match, None, None, None, '', None

    def match_error(self, r, line):
        return self.split_match(r.match(line))

    # Subclasses may need to override this in complex cases
    def run(self, cmd, code):
        if self.tempfile_suffix:
            return self.tmpfile(cmd, suffix=self.tempfile_suffix)
        else:
            return self.communicate(cmd, code)

    # popen wrappers
    def communicate(self, cmd, code):
        return util.communicate(cmd, code)

    def tmpfile(self, cmd, code, suffix=''):
        return util.tmpfile(cmd, code, suffix or self.tempfile_suffix)

    def tmpdir(self, cmd, files, code):
        return util.tmpdir(cmd, files, self.filename, code)

    def popen(self, cmd, env=None):
        return util.popen(cmd, env)
<commit_msg>Show linter output if debug mode is on<commit_after># linter.py
# Part of SublimeLinter, a code checking framework for Sublime Text 3
#
# Project: https://github.com/SublimeLinter/sublimelinter
# License: MIT

import re
import sublime

from .highlight import Highlight
from . import persist
from . import util

SYNTAX_RE = re.compile(r'/([^/]+)\.tmLanguage$')
WARNING_RE = re.compile(r'^w(?:arn(?:ing)?)?$', re.IGNORECASE)

class Registrar(type):
    '''This metaclass registers the linter when the class is declared.'''
    def __init__(cls, name, bases, attrs):
        if bases:
            persist.register_linter(cls, name, attrs)

class Linter(metaclass=Registrar):
    '''
    The base class for linters. Subclasses must at a minimum define
    the attributes language, cmd, and regex.
    '''

    #
    # Public attributes
    #

    # The name of the linter's language for display purposes.
    # By convention this is all lowercase.
    language = ''

    # The name of the executable used to do linting. If specified by a linter,
    # the executable's existence is checked, and if it is not available,
    # the linter is disabled.
    executable = None

    # If executable is specified and is available, this is set to the full path
    # of the executable. If the executable is not available, it is set an empty string.
    # If executable is not specified, it is None. Subclasses should consider this read only.
    executable_path = None

    # A string, tuple or callable that returns a string or tuple, containing the
    # command line arguments used to lint.
    cmd = ''

    # A regex pattern used to extract information from the linter's executable output.
    regex = ''

    # Set to True if the linter outputs multiple errors or multiline errors. When True,
    # regex will be created with the re.MULTILINE flag.
    multiline = False

    # If you want to set flags on the regex other than re.MULTILINE, set this.
    re_flags = 0

    # If the linter executable cannot receive from stdin and requires a temp file,
    # set this attribute to the suffix of the temp file.
    tempfile_suffix = None

    # Tab width
    tab_width = 1

    # If you want to limit the linter to specific portions of the source
    # based on a scope selector, set this attribute to the selector. For example,
    # in an html file with embedded php, you would set the selector for a php
    # linter to 'source.php'.
    selector = None

    # If you want to provide default settings for the linter, set this attribute.
    defaults = None

    #
    # Internal class storage, do not set
    #
    errors = None
    highlight = None
    lint_settings = None

    def __init__(self, view, syntax, filename=None):
        self.view = view
        self.syntax = syntax
        self.filename = filename

        if self.regex:
            if self.multiline:
                self.re_flags |= re.MULTILINE

            try:
                self.regex = re.compile(self.regex, self.re_flags)
            except:
                persist.debug('error compiling regex for {}'.format(self.language))

        self.highlight = Highlight()

    @classmethod
    def get_settings(cls):
        '''Return the default settings for this linter, merged with the user settings.'''
        linters = persist.settings.get('linters', {})
        settings = cls.defaults or {}
        settings.update(linters.get(cls.__name__, {}))
        return settings

    @property
    def settings(self):
        return self.get_settings()

    @classmethod
    def assign(cls, view, reassign=False):
        '''
        Assign a view to an instance of a linter.
        Find a linter for a specified view if possible, then add it to our view <--> lint class map and return it.
        Each view has its own linter so that linters can store persistent data about a view.
        '''
        vid = view.id()
        persist.views[vid] = view

        settings = view.settings()
        syntax = settings.get('syntax')

        if not syntax:
            cls.remove(vid)
            return

        match = SYNTAX_RE.search(syntax)

        if match:
            syntax = match.group(1)
        else:
            syntax = ''

        if syntax:
            if vid in persist.linters and persist.linters[vid] and not reassign:
                # If a linter in the set of linters for the given view
                # already handles the view's syntax, we have nothing more to do.
                for linter in tuple(persist.linters[vid]):
                    if linter.syntax == syntax:
                        return

            linters = set()

            for name, linter_class in persist.languages.items():
                if linter_class.can_lint(syntax):
                    linter = linter_class(view, syntax, view.file_name())
                    linters.add(linter)

            persist.linters[vid] = linters
            return linters

        cls.remove(vid)

    @classmethod
    def remove(cls, vid):
        '''Remove a the mapping between a view and its set of linters.'''
        if vid in persist.linters:
            for linters in persist.linters[vid]:
                linters.clear()

            del persist.linters[vid]

    @classmethod
    def reload(cls, mod=None):
        '''Reload all linters, optionally filtering by module.'''

        # Merge linter default settings with user settings
        linter_settings = persist.settings.get('linters', {})

        for name, linter in persist.languages.items():
            settings = linter_settings.get(name, {})
            defaults = (linter.defaults or {}).copy()
            defaults.update(settings)
            linter.lint_settings = defaults

        for vid, linters in persist.linters.items():
            for linter in linters:
                if mod and linter.__module__ != mod:
                    continue

                linter.clear()
                persist.linters[vid].remove(linter)
                linter = persist.languages[linter.name](linter.view, linter.syntax, linter.filename)
                persist.linters[vid].add(linter)
                linter.draw()

        return

    @classmethod
    def text(cls, view):
        '''Returns the entire text of a view.'''
        return view.substr(sublime.Region(0, view.size()))

    @classmethod
    def get_view(cls, vid):
        '''Returns the view object with the given id.'''
        return persist.views.get(vid)

    @classmethod
    def get_linters(cls, vid):
        '''Returns a tuple of linters for the view with the given id.'''
        if vid in persist.linters:
            return tuple(persist.linters[vid])

        return ()

    @classmethod
    def get_selectors(cls, vid):
        '''Returns a list of scope selectors for all linters for the view with the given id.'''
        return [
            (linter.selector, linter)
            for linter in cls.get_linters(vid)
            if linter.selector
        ]

    @classmethod
    def lint_view(cls, vid, filename, code, sections, callback):
        if not code or vid not in persist.linters:
            return

        linters = list(persist.linters.get(vid))

        if not linters:
            return

        filename = filename or 'untitled'
        #linter_list = (', '.join(l.name for l in linters))
        #persist.debug('lint \'{}\' as {}'.format(filename, linter_list))

        for linter in linters:
            if linter.settings.get('disable'):
                continue

            if not linter.selector:
                linter.reset(code, filename=filename)
                linter.lint()

        selectors = Linter.get_selectors(vid)

        for sel, linter in selectors:
            linters.append(linter)

            if sel in sections:
                linter.reset(code, filename=filename)
                errors = {}

                for line_offset, left, right in sections[sel]:
                    linter.highlight.move_to(line_offset, left)
                    linter.code = code[left:right]
                    linter.errors = {}
                    linter.lint()

                    for line, error in linter.errors.items():
                        errors[line + line_offset] = error

                linter.errors = errors

        # Merge our result back to the main thread
        callback(cls.get_view(vid), linters)

    def reset(self, code, filename=None, highlight=None):
        self.errors = {}
        self.code = code
        self.filename = filename or self.filename
        self.highlight = highlight or Highlight(self.code)

    def lint(self):
        if not (self.language and self.cmd and self.regex):
            raise NotImplementedError

        if callable(self.cmd):
            cmd = self.cmd()
        else:
            cmd = self.cmd

        if not cmd:
            return

        if not isinstance(cmd, (tuple, list)):
            cmd = (cmd,)

        output = self.run(cmd, self.code)

        if not output:
            return

        persist.debug('{} output:\n{}'.format(self.__class__.__name__, output.strip()))

        for match, row, col, error_type, message, near in self.find_errors(output):
            if match and row is not None:
                if error_type and WARNING_RE.match(error_type) is not None:
                    error_type = Highlight.WARNING
                else:
                    error_type = Highlight.ERROR

                if col is not None:
                    # Adjust column numbers to match the linter's tabs if necessary
                    if self.tab_width > 1:
                        start, end = self.highlight.full_line(row)
                        code_line = self.code[start:end]
                        diff = 0

                        for i in range(len(code_line)):
                            if code_line[i] == '\t':
                                diff += (self.tab_width - 1)

                            if col - diff <= i:
                                col = i
                                break

                    self.highlight.range(row, col, error_type=error_type)
                elif near:
                    self.highlight.near(row, near, error_type)

                self.error(row, col, message, error_type)

    def draw(self):
        self.highlight.draw(self.view)

    def clear(self):
        self.highlight.clear(self.view)

    # Helper methods

    @classmethod
    def can_lint(cls, language):
        '''Determines if a linter can lint a given language. Subclasses which '''
        return Linter.linter_can_lint(cls, language)

    @staticmethod
    def linter_can_lint(cls, language):
        can = False
        language = language.lower()

        if cls.language:
            if language == cls.language:
                can = True
            elif isinstance(cls.language, (tuple, list)) and language in cls.language:
                can = True

        if can and cls.executable and cls.executable_path is None:
            cls.executable_path = util.which(cls.executable) or ''
            can = cls.executable_path != ''

        return can

    def error(self, line, col, error, error_type):
        self.highlight.line(line, error_type)
        error = ((col or 0), str(error))

        if line in self.errors:
            self.errors[line].append(error)
        else:
            self.errors[line] = [error]

    def find_errors(self, output):
        if self.multiline:
            errors = self.regex.finditer(output)

            if errors:
                for error in errors:
                    yield self.split_match(error)
            else:
                yield self.split_match(None)
        else:
            for line in output.splitlines():
                yield self.match_error(self.regex, line.strip())

    def split_match(self, match):
        if match:
            items = {'line': None, 'col': None, 'type': None, 'error': '', 'near': None}
            items.update(match.groupdict())
            row, col, error_type, error, near = [items[k] for k in ('line', 'col', 'type', 'error', 'near')]

            row = int(row) - 1

            if col:
                col = int(col) - 1

            return match, row, col, error_type, error, near

        return match, None, None, None, '', None

    def match_error(self, r, line):
        return self.split_match(r.match(line))

    # Subclasses may need to override this in complex cases
    def run(self, cmd, code):
        if self.tempfile_suffix:
            return self.tmpfile(cmd, suffix=self.tempfile_suffix)
        else:
            return self.communicate(cmd, code)

    # popen wrappers
    def communicate(self, cmd, code):
        return util.communicate(cmd, code)

    def tmpfile(self, cmd, code, suffix=''):
        return util.tmpfile(cmd, code, suffix or self.tempfile_suffix)

    def tmpdir(self, cmd, files, code):
        return util.tmpdir(cmd, files, self.filename, code)

    def popen(self, cmd, env=None):
        return util.popen(cmd, env)
<|endoftext|>


<commit_before>import csv
from giantcellsim_trial import giantcellsim_trial
import itertools
import numpy

def giantcellsim_motifoutput(parameterlist,masterprefix,testprefix,trials,growthIterations,max_strand_nr,maxStrandLength,numCells,numRounds,motif,elong,bias):
	pop_tracker = []

	with open(masterprefix+ testprefix +'_MotifData_motif{motif}_len{maxStrandLength}_bias{bias}_elong{elong}_{trials}trials_numRound{numRounds}.csv'.format(motif = motif, maxStrandLength = maxStrandLength, bias=bias, elong=elong, trials=trials, numRounds=numRounds), 'wb') as f:
		writer = csv.writer(f)
		writer.writerow(parameterlist)

		for trial in range(trials):
			pop_tracker.append([])
			nr_motifs, nr_strands, nr_cells_with_motif, pop_tracker[trial] = giantcellsim_trial(motif,growthIterations,max_strand_nr,maxStrandLength,numCells,numRounds,elong,bias)

			motif_freq = [motifs / float(total) for motifs,total in itertools.izip(nr_motifs,nr_strands)]
			strands_freq = [strands / float(max_strand_nr*numCells) for strands in nr_strands]
			cells_with_freq = [cells / float(numCells) for cells in nr_cells_with_motif]

			writer.writerow(motif_freq)
			writer.writerow(strands_freq)
			writer.writerow(cells_with_freq)

			if trial == 0:
				motif_freq_aggregate = motif_freq
				strands_freq_aggregate = strands_freq
				cells_with_freq_aggregate = cells_with_freq
				nr_strands_per_time = nr_strands
			else:
				motif_freq_aggregate = [list(round_data) for round_data in zip(motif_freq_aggregate,motif_freq)]
				strands_freq_aggregate = [list(round_data) for round_data in zip(strands_freq_aggregate,strands_freq)]
				cells_with_freq_aggregate = [list(round_data) for round_data in zip(cells_with_freq_aggregate,cells_with_freq)]
				nr_strands_per_time = [list(round_data) for round_data in zip(nr_strands_per_time,nr_strands)]

		means = []
		stdevs = []

		for iterator in range(3):
			means.append([])
			stdevs.append([])

		for time_point in range(numRounds):
			means[0].append(numpy.mean(motif_freq_aggregate[time_point]))
			stdevs[0].append(numpy.std(motif_freq_aggregate[time_point]))
			means[1].append(numpy.mean(strands_freq_aggregate[time_point]))
			stdevs[1].append(numpy.std(strands_freq_aggregate[time_point]))
			means[2].append(numpy.mean(cells_with_freq_aggregate[time_point]))
			stdevs[2].append(numpy.std(cells_with_freq_aggregate[time_point]))

		for mean_data in means:
			writer.writerow(mean_data)

		for stdev_data in stdevs:
			writer.writerow(stdev_data)
	f.close()

	return pop_tracker, nr_strands_per_time
<commit_msg>update motifoutput to comply with numpy 1.7.1 of cluster<commit_after>import csv
from giantcellsim_trial import giantcellsim_trial
import itertools
import numpy

def giantcellsim_motifoutput(parameterlist,masterprefix,testprefix,trials,growthIterations,max_strand_nr,maxStrandLength,numCells,numRounds,motif,elong,bias):
	pop_tracker = []

	with open(masterprefix+ testprefix +'_MotifData_motif{motif}_len{maxStrandLength}_bias{bias}_elong{elong}_{trials}trials_numRound{numRounds}.csv'.format(motif = motif, maxStrandLength = maxStrandLength, bias=bias, elong=elong, trials=trials, numRounds=numRounds), 'wb') as f:
		writer = csv.writer(f)
		writer.writerow(parameterlist)

		for trial in range(trials):
			pop_tracker.append([])
			nr_motifs, nr_strands, nr_cells_with_motif, pop_tracker[trial] = giantcellsim_trial(motif,growthIterations,max_strand_nr,maxStrandLength,numCells,numRounds,elong,bias)

			motif_freq = [motifs / float(total) for motifs,total in itertools.izip(nr_motifs,nr_strands)]
			strands_freq = [strands / float(max_strand_nr*numCells) for strands in nr_strands]
			cells_with_freq = [cells / float(numCells) for cells in nr_cells_with_motif]

			writer.writerow(motif_freq)
			writer.writerow(strands_freq)
			writer.writerow(cells_with_freq)

			if trial == 0:
				motif_freq_aggregate = motif_freq
				strands_freq_aggregate = strands_freq
				cells_with_freq_aggregate = cells_with_freq
				nr_strands_per_time = nr_strands
			else:
				motif_freq_aggregate = [list(round_data) for round_data in zip(motif_freq_aggregate,motif_freq)]
				strands_freq_aggregate = [list(round_data) for round_data in zip(strands_freq_aggregate,strands_freq)]
				cells_with_freq_aggregate = [list(round_data) for round_data in zip(cells_with_freq_aggregate,cells_with_freq)]
				nr_strands_per_time = [list(round_data) for round_data in zip(nr_strands_per_time,nr_strands)]

		means = []
		stdevs = []

		for iterator in range(3):
			means.append([])
			stdevs.append([])

		for time_point in range(numRounds):
			means[0].append(numpy.mean(numpy.asarray(motif_freq_aggregate[time_point])))
			stdevs[0].append(numpy.std(numpy.asarray(motif_freq_aggregate[time_point])))
			means[1].append(numpy.mean(numpy.asarray(strands_freq_aggregate[time_point])))
			stdevs[1].append(numpy.std(numpy.asarray(strands_freq_aggregate[time_point])))
			means[2].append(numpy.mean(numpy.asarray(cells_with_freq_aggregate[time_point])))
			stdevs[2].append(numpy.std(numpy.asarray(cells_with_freq_aggregate[time_point])))

		for mean_data in means:
			writer.writerow(mean_data)

		for stdev_data in stdevs:
			writer.writerow(stdev_data)
	f.close()

	return pop_tracker, nr_strands_per_time
<|endoftext|>


<commit_before>#
# linter.py
# Part of SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by Ryan Hileman and Aparajita Fishman
#
# Project: https://github.com/SublimeLinter/SublimeLinter3
# License: MIT
#

import re
import shlex
import sublime

from . import highlight as hilite
from . import persist
from . import util

SYNTAX_RE = re.compile(r'/([^/]+)\.tmLanguage$')
WARNING_RE = re.compile(r'^w(?:arn(?:ing)?)?$', re.IGNORECASE)

class Registrar(type):
    '''This metaclass registers the linter when the class is declared.'''
    def __init__(cls, name, bases, attrs):
        if bases:
            persist.register_linter(cls, name, attrs)

class Linter(metaclass=Registrar):
    '''
    The base class for linters. Subclasses must at a minimum define
    the attributes language, cmd, and regex.
    '''

    #
    # Public attributes
    #

    # The name of the linter's language for display purposes.
    # By convention this is all lowercase.
    language = ''

    # A string, tuple or callable that returns a string or tuple, containing the
    # command line arguments used to lint.
    cmd = ''

    # If the name of the executable cannot be determined by the first element of cmd
    # (for example when cmd is a method that dynamically generates the command line arguments),
    # this can be set to the name of the executable used to do linting.
    #
    # Once the executable's name is determined, its existence is checked in the user's path.
    # If it is not available, the linter is disabled.
    executable = None

    # If the executable is available, this is set to the full path of the executable.
    # If the executable is not available, it is set an empty string.
    # Subclasses should consider this read only.
    executable_path = None

    # A regex pattern used to extract information from the linter's executable output.
    regex = ''

    # Set to True if the linter outputs multiline error messages. When True,
    # regex will be created with the re.MULTILINE flag.
    multiline = False

    # If you want to set flags on the regex other than re.MULTILINE, set this.
    re_flags = 0

    # If the linter executable cannot receive from stdin and requires a temp file,
    # set this attribute to the suffix of the temp file (including leading '.').
    tempfile_suffix = None

    # Tab width
    tab_width = 1

    # If you want to limit the linter to specific portions of the source
    # based on a scope selector, set this attribute to the selector. For example,
    # in an html file with embedded php, you would set the selector for a php
    # linter to 'source.php'.
    selector = None

    # If you want to provide default settings for the linter, set this attribute.
    defaults = None

    #
    # Internal class storage, do not set
    #
    errors = None
    highlight = None
    lint_settings = None

    def __init__(self, view, syntax, filename=None):
        self.view = view
        self.syntax = syntax
        self.filename = filename

        if self.regex:
            if self.multiline:
                self.re_flags |= re.MULTILINE

            try:
                self.regex = re.compile(self.regex, self.re_flags)
            except:
                persist.debug('error compiling regex for {}'.format(self.language))

        self.highlight = hilite.Highlight()

    @classmethod
    def get_settings(cls):
        '''Return the default settings for this linter, merged with the user settings.'''
        linters = persist.settings.get('linters', {})
        settings = cls.defaults or {}
        settings.update(linters.get(cls.__name__, {}))
        return settings

    @property
    def settings(self):
        return self.get_settings()

    @classmethod
    def assign(cls, view, reassign=False):
        '''
        Assign a view to an instance of a linter.
        Find a linter for a specified view if possible, then add it to our view <--> lint class map and return it.
        Each view has its own linter so that linters can store persistent data about a view.
        '''
        vid = view.id()
        persist.views[vid] = view

        settings = view.settings()
        syntax = settings.get('syntax')

        if not syntax:
            cls.remove(vid)
            return

        match = SYNTAX_RE.search(syntax)

        if match:
            syntax = match.group(1)
        else:
            syntax = ''

        if syntax:
            if vid in persist.linters and persist.linters[vid] and not reassign:
                # If a linter in the set of linters for the given view
                # already handles the view's syntax, we have nothing more to do.
                for linter in tuple(persist.linters[vid]):
                    if linter.syntax == syntax:
                        return

            linters = set()

            for name, linter_class in persist.languages.items():
                if linter_class.can_lint(syntax):
                    linter = linter_class(view, syntax, view.file_name())
                    linters.add(linter)

            if linters:
                persist.linters[vid] = linters
            elif reassign and not linters and vid in persist.linters:
                del persist.linters[vid]

            return linters

        cls.remove(vid)

    @classmethod
    def remove(cls, vid):
        '''Remove a the mapping between a view and its set of linters.'''
        if vid in persist.linters:
            for linters in persist.linters[vid]:
                linters.clear()

            del persist.linters[vid]

    @classmethod
    def reload(cls, mod=None):
        '''Reload all linters, optionally filtering by module.'''

        # Merge linter default settings with user settings
        linter_settings = persist.settings.get('linters', {})

        for name, linter in persist.languages.items():
            settings = linter_settings.get(name, {})
            defaults = (linter.defaults or {}).copy()
            defaults.update(settings)
            linter.lint_settings = defaults

        for vid, linters in persist.linters.items():
            for linter in linters:
                if mod and linter.__module__ != mod:
                    continue

                linter.clear()
                persist.linters[vid].remove(linter)
                linter = persist.languages[linter.name](linter.view, linter.syntax, linter.filename)
                persist.linters[vid].add(linter)

        cls.redraw_all()

    @classmethod
    def apply_to_all(cls, action):
        for w in sublime.windows():
            for view in w.views():
                highlights = persist.highlights.get(view.id())

                if highlights:
                    getattr(highlights, action)(view)

    @classmethod
    def clear_all(cls):
        cls.apply_to_all('reset')
        persist.errors.clear()

    @classmethod
    def redraw_all(cls):
        cls.apply_to_all('redraw')

    @classmethod
    def text(cls, view):
        '''Returns the entire text of a view.'''
        return view.substr(sublime.Region(0, view.size()))

    @classmethod
    def get_view(cls, vid):
        '''Returns the view object with the given id.'''
        return persist.views.get(vid)

    @classmethod
    def get_linters(cls, vid):
        '''Returns a tuple of linters for the view with the given id.'''
        if vid in persist.linters:
            return tuple(persist.linters[vid])

        return ()

    @classmethod
    def get_selectors(cls, vid):
        '''Returns a list of scope selectors for all linters for the view with the given id.'''
        return [
            (linter.selector, linter)
            for linter in cls.get_linters(vid)
            if linter.selector
        ]

    @classmethod
    def lint_view(cls, vid, filename, code, sections, hit_time, callback):
        if not code or vid not in persist.linters:
            return

        linters = list(persist.linters.get(vid))

        if not linters:
            return

        filename = filename or 'untitled'

        for linter in linters:
            if linter.settings.get('disable'):
                continue

            if not linter.selector:
                linter.reset(code, filename=filename)
                linter.lint()

        selectors = Linter.get_selectors(vid)

        for sel, linter in selectors:
            linters.append(linter)

            if sel in sections:
                linter.reset(code, filename=filename)
                errors = {}

                for line_offset, left, right in sections[sel]:
                    linter.hilite.move_to(line_offset, left)
                    linter.code = code[left:right]
                    linter.errors = {}
                    linter.lint()

                    for line, error in linter.errors.items():
                        errors[line + line_offset] = error

                linter.errors = errors

        # Merge our result back to the main thread
        callback(cls.get_view(vid), linters, hit_time)

    def reset(self, code, filename=None, highlight=None):
        self.errors = {}
        self.code = code
        self.filename = filename or self.filename
        self.highlight = highlight or hilite.Highlight(self.code)

    def get_cmd(self):
        if callable(self.cmd):
            cmd = self.cmd()
        else:
            cmd = self.cmd

        if not cmd:
            return

        if isinstance(cmd, str):
            cmd = shlex.split(cmd)

        return tuple(cmd)

    def lint(self):
        if not (self.language and (self.cmd or self.cmd is None) and self.regex):
            raise NotImplementedError

        cmd = self.get_cmd()

        if cmd is not None and not cmd:
            return

        output = self.run(cmd, self.code)

        if not output:
            return

        persist.debug('{} output:\n{}'.format(self.__class__.__name__, output.strip()))

        for match, row, col, error_type, message, near in self.find_errors(output):
            if match and row is not None:
                if error_type and WARNING_RE.match(error_type) is not None:
                    error_type = hilite.WARNING
                else:
                    error_type = hilite.ERROR

                if col is not None:
                    # Adjust column numbers to match the linter's tabs if necessary
                    if self.tab_width > 1:
                        start, end = self.highlight.full_line(row)
                        code_line = self.code[start:end]
                        diff = 0

                        for i in range(len(code_line)):
                            if code_line[i] == '\t':
                                diff += (self.tab_width - 1)

                            if col - diff <= i:
                                col = i
                                break

                    self.highlight.range(row, col, error_type=error_type)
                elif near:
                    col = self.highlight.near(row, near, error_type)
                else:
                    self.highlight.range(row, 0, length=0, error_type=error_type)

                self.error(row, col, message, error_type)

    def draw(self):
        self.highlight.draw(self.view)

    def clear(self):
        self.highlight.clear(self.view)

    # Helper methods

    @classmethod
    def can_lint(cls, language):
        can = False
        language = language.lower()

        if cls.language:
            if isinstance(cls.language, (tuple, list)) and language in cls.language:
                can = True
            elif language == cls.language:
                can = True

        if can and cls.executable_path is None:
            executable = ''

            if not callable(cls.cmd):
                if isinstance(cls.cmd, (tuple, list)):
                    executable = (cls.cmd or [''])[0]
                elif isinstance(cls.cmd, str):
                    executable = cls.cmd

            if not executable and cls.executable:
                executable = cls.executable

            if executable:
                cls.executable_path = util.which(executable) or ''
            elif cls.cmd is None:
                cls.executable_path = '<builtin>'
            else:
                cls.executable_path = ''

            can = cls.executable_path != ''

            if can:
                can = cls.can_lint_language(language)

            persist.printf('{} {}'.format(
                cls.__name__,
                'enabled ({})'.format(cls.executable_path) if can
                else 'disabled, cannot locate \'{}\''.format(executable)
            ))

        return can

    @classmethod
    def can_lint_language(cls, language):
        '''
        Subclass hook to determine if a linter can lint a given language. Subclasses may override this
        if the built in mechanism is not sufficient. Note that this will only be called if the executable
        path has not yet been determined and the built in checks succeed first. When this is called,
        cls.executable_path has been set.
        '''
        return True

    def error(self, line, col, error, error_type):
        self.highlight.line(line, error_type)

        # Capitalize the first word
        error = error[0].upper() + error[1:]

        # Strip trailing space and period
        error = ((col or 0), str(error).rstrip(' .'))

        if line in self.errors:
            self.errors[line].append(error)
        else:
            self.errors[line] = [error]

    def find_errors(self, output):
        if self.multiline:
            errors = self.regex.finditer(output)

            if errors:
                for error in errors:
                    yield self.split_match(error)
            else:
                yield self.split_match(None)
        else:
            for line in output.splitlines():
                yield self.match_error(self.regex, line.strip())

    def split_match(self, match):
        if match:
            items = {'line': None, 'col': None, 'type': None, 'error': '', 'near': None}
            items.update(match.groupdict())
            row, col, error_type, error, near = [items[k] for k in ('line', 'col', 'type', 'error', 'near')]

            if row is not None:
                row = int(row) - 1

            if col is not None:
                col = int(col) - 1

            return match, row, col, error_type, error, near
        else:
            return match, None, None, None, '', None

    def match_error(self, r, line):
        return self.split_match(r.match(line))

    # Subclasses may need to override this in complex cases
    def run(self, cmd, code):
        if self.tempfile_suffix:
            return self.tmpfile(cmd, code, suffix=self.tempfile_suffix)
        else:
            return self.communicate(cmd, code)

    # popen wrappers
    def communicate(self, cmd, code):
        return util.communicate(cmd, code)

    def tmpfile(self, cmd, code, suffix=''):
        return util.tmpfile(cmd, code, suffix or self.tempfile_suffix)

    def tmpdir(self, cmd, files, code):
        return util.tmpdir(cmd, files, self.filename, code)

    def popen(self, cmd, env=None):
        return util.popen(cmd, env)
<commit_msg>Module filtering is no longer necessary since linter plugins are reloaded automatically by ST.<commit_after>#
# linter.py
# Part of SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by Ryan Hileman and Aparajita Fishman
#
# Project: https://github.com/SublimeLinter/SublimeLinter3
# License: MIT
#

import re
import shlex
import sublime

from . import highlight as hilite
from . import persist
from . import util

SYNTAX_RE = re.compile(r'/([^/]+)\.tmLanguage$')
WARNING_RE = re.compile(r'^w(?:arn(?:ing)?)?$', re.IGNORECASE)

class Registrar(type):
    '''This metaclass registers the linter when the class is declared.'''
    def __init__(cls, name, bases, attrs):
        if bases:
            persist.register_linter(cls, name, attrs)

class Linter(metaclass=Registrar):
    '''
    The base class for linters. Subclasses must at a minimum define
    the attributes language, cmd, and regex.
    '''

    #
    # Public attributes
    #

    # The name of the linter's language for display purposes.
    # By convention this is all lowercase.
    language = ''

    # A string, tuple or callable that returns a string or tuple, containing the
    # command line arguments used to lint.
    cmd = ''

    # If the name of the executable cannot be determined by the first element of cmd
    # (for example when cmd is a method that dynamically generates the command line arguments),
    # this can be set to the name of the executable used to do linting.
    #
    # Once the executable's name is determined, its existence is checked in the user's path.
    # If it is not available, the linter is disabled.
    executable = None

    # If the executable is available, this is set to the full path of the executable.
    # If the executable is not available, it is set an empty string.
    # Subclasses should consider this read only.
    executable_path = None

    # A regex pattern used to extract information from the linter's executable output.
    regex = ''

    # Set to True if the linter outputs multiline error messages. When True,
    # regex will be created with the re.MULTILINE flag.
    multiline = False

    # If you want to set flags on the regex other than re.MULTILINE, set this.
    re_flags = 0

    # If the linter executable cannot receive from stdin and requires a temp file,
    # set this attribute to the suffix of the temp file (including leading '.').
    tempfile_suffix = None

    # Tab width
    tab_width = 1

    # If you want to limit the linter to specific portions of the source
    # based on a scope selector, set this attribute to the selector. For example,
    # in an html file with embedded php, you would set the selector for a php
    # linter to 'source.php'.
    selector = None

    # If you want to provide default settings for the linter, set this attribute.
    defaults = None

    #
    # Internal class storage, do not set
    #
    errors = None
    highlight = None
    lint_settings = None

    def __init__(self, view, syntax, filename=None):
        self.view = view
        self.syntax = syntax
        self.filename = filename

        if self.regex:
            if self.multiline:
                self.re_flags |= re.MULTILINE

            try:
                self.regex = re.compile(self.regex, self.re_flags)
            except:
                persist.debug('error compiling regex for {}'.format(self.language))

        self.highlight = hilite.Highlight()

    @classmethod
    def get_settings(cls):
        '''Return the default settings for this linter, merged with the user settings.'''
        linters = persist.settings.get('linters', {})
        settings = cls.defaults or {}
        settings.update(linters.get(cls.__name__, {}))
        return settings

    @property
    def settings(self):
        return self.get_settings()

    @classmethod
    def assign(cls, view, reassign=False):
        '''
        Assign a view to an instance of a linter.
        Find a linter for a specified view if possible, then add it to our view <--> lint class map and return it.
        Each view has its own linter so that linters can store persistent data about a view.
        '''
        vid = view.id()
        persist.views[vid] = view

        settings = view.settings()
        syntax = settings.get('syntax')

        if not syntax:
            cls.remove(vid)
            return

        match = SYNTAX_RE.search(syntax)

        if match:
            syntax = match.group(1)
        else:
            syntax = ''

        if syntax:
            if vid in persist.linters and persist.linters[vid] and not reassign:
                # If a linter in the set of linters for the given view
                # already handles the view's syntax, we have nothing more to do.
                for linter in tuple(persist.linters[vid]):
                    if linter.syntax == syntax:
                        return

            linters = set()

            for name, linter_class in persist.languages.items():
                if linter_class.can_lint(syntax):
                    linter = linter_class(view, syntax, view.file_name())
                    linters.add(linter)

            if linters:
                persist.linters[vid] = linters
            elif reassign and not linters and vid in persist.linters:
                del persist.linters[vid]

            return linters

        cls.remove(vid)

    @classmethod
    def remove(cls, vid):
        '''Remove a the mapping between a view and its set of linters.'''
        if vid in persist.linters:
            for linters in persist.linters[vid]:
                linters.clear()

            del persist.linters[vid]

    @classmethod
    def reload(cls):
        '''Reload all linters, optionally relinting all views.'''

        # Merge linter default settings with user settings
        linter_settings = persist.settings.get('linters', {})

        for name, linter in persist.languages.items():
            settings = linter_settings.get(name, {})
            defaults = (linter.defaults or {}).copy()
            defaults.update(settings)
            linter.lint_settings = defaults

        for vid, linters in persist.linters.items():
            for linter in linters:
                linter.clear()
                persist.linters[vid].remove(linter)
                linter = persist.languages[linter.name](linter.view, linter.syntax, linter.filename)
                persist.linters[vid].add(linter)

    @classmethod
    def apply_to_all(cls, action):
        for w in sublime.windows():
            for view in w.views():
                highlights = persist.highlights.get(view.id())

                if highlights:
                    getattr(highlights, action)(view)

    @classmethod
    def clear_all(cls):
        cls.apply_to_all('reset')
        persist.errors.clear()

    @classmethod
    def redraw_all(cls):
        cls.apply_to_all('redraw')

    @classmethod
    def text(cls, view):
        '''Returns the entire text of a view.'''
        return view.substr(sublime.Region(0, view.size()))

    @classmethod
    def get_view(cls, vid):
        '''Returns the view object with the given id.'''
        return persist.views.get(vid)

    @classmethod
    def get_linters(cls, vid):
        '''Returns a tuple of linters for the view with the given id.'''
        if vid in persist.linters:
            return tuple(persist.linters[vid])

        return ()

    @classmethod
    def get_selectors(cls, vid):
        '''Returns a list of scope selectors for all linters for the view with the given id.'''
        return [
            (linter.selector, linter)
            for linter in cls.get_linters(vid)
            if linter.selector
        ]

    @classmethod
    def lint_view(cls, vid, filename, code, sections, hit_time, callback):
        if not code or vid not in persist.linters:
            return

        linters = list(persist.linters.get(vid))

        if not linters:
            return

        filename = filename or 'untitled'

        for linter in linters:
            if linter.settings.get('disable'):
                continue

            if not linter.selector:
                linter.reset(code, filename=filename)
                linter.lint()

        selectors = Linter.get_selectors(vid)

        for sel, linter in selectors:
            linters.append(linter)

            if sel in sections:
                linter.reset(code, filename=filename)
                errors = {}

                for line_offset, left, right in sections[sel]:
                    linter.hilite.move_to(line_offset, left)
                    linter.code = code[left:right]
                    linter.errors = {}
                    linter.lint()

                    for line, error in linter.errors.items():
                        errors[line + line_offset] = error

                linter.errors = errors

        # Merge our result back to the main thread
        callback(cls.get_view(vid), linters, hit_time)

    def reset(self, code, filename=None, highlight=None):
        self.errors = {}
        self.code = code
        self.filename = filename or self.filename
        self.highlight = highlight or hilite.Highlight(self.code)

    def get_cmd(self):
        if callable(self.cmd):
            cmd = self.cmd()
        else:
            cmd = self.cmd

        if not cmd:
            return

        if isinstance(cmd, str):
            cmd = shlex.split(cmd)

        return tuple(cmd)

    def lint(self):
        if not (self.language and (self.cmd or self.cmd is None) and self.regex):
            raise NotImplementedError

        cmd = self.get_cmd()

        if cmd is not None and not cmd:
            return

        output = self.run(cmd, self.code)

        if not output:
            return

        persist.debug('{} output:\n{}'.format(self.__class__.__name__, output.strip()))

        for match, row, col, error_type, message, near in self.find_errors(output):
            if match and row is not None:
                if error_type and WARNING_RE.match(error_type) is not None:
                    error_type = hilite.WARNING
                else:
                    error_type = hilite.ERROR

                if col is not None:
                    # Adjust column numbers to match the linter's tabs if necessary
                    if self.tab_width > 1:
                        start, end = self.highlight.full_line(row)
                        code_line = self.code[start:end]
                        diff = 0

                        for i in range(len(code_line)):
                            if code_line[i] == '\t':
                                diff += (self.tab_width - 1)

                            if col - diff <= i:
                                col = i
                                break

                    self.highlight.range(row, col, error_type=error_type)
                elif near:
                    col = self.highlight.near(row, near, error_type)
                else:
                    self.highlight.range(row, 0, length=0, error_type=error_type)

                self.error(row, col, message, error_type)

    def draw(self):
        self.highlight.draw(self.view)

    def clear(self):
        self.highlight.clear(self.view)

    # Helper methods

    @classmethod
    def can_lint(cls, language):
        can = False
        language = language.lower()

        if cls.language:
            if isinstance(cls.language, (tuple, list)) and language in cls.language:
                can = True
            elif language == cls.language:
                can = True

        if can and cls.executable_path is None:
            executable = ''

            if not callable(cls.cmd):
                if isinstance(cls.cmd, (tuple, list)):
                    executable = (cls.cmd or [''])[0]
                elif isinstance(cls.cmd, str):
                    executable = cls.cmd

            if not executable and cls.executable:
                executable = cls.executable

            if executable:
                cls.executable_path = util.which(executable) or ''
            elif cls.cmd is None:
                cls.executable_path = '<builtin>'
            else:
                cls.executable_path = ''

            can = cls.executable_path != ''

            if can:
                can = cls.can_lint_language(language)

            persist.printf('{} {}'.format(
                cls.__name__,
                'enabled ({})'.format(cls.executable_path) if can
                else 'disabled, cannot locate \'{}\''.format(executable)
            ))

        return can

    @classmethod
    def can_lint_language(cls, language):
        '''
        Subclass hook to determine if a linter can lint a given language. Subclasses may override this
        if the built in mechanism is not sufficient. Note that this will only be called if the executable
        path has not yet been determined and the built in checks succeed first. When this is called,
        cls.executable_path has been set.
        '''
        return True

    def error(self, line, col, error, error_type):
        self.highlight.line(line, error_type)

        # Capitalize the first word
        error = error[0].upper() + error[1:]

        # Strip trailing space and period
        error = ((col or 0), str(error).rstrip(' .'))

        if line in self.errors:
            self.errors[line].append(error)
        else:
            self.errors[line] = [error]

    def find_errors(self, output):
        if self.multiline:
            errors = self.regex.finditer(output)

            if errors:
                for error in errors:
                    yield self.split_match(error)
            else:
                yield self.split_match(None)
        else:
            for line in output.splitlines():
                yield self.match_error(self.regex, line.strip())

    def split_match(self, match):
        if match:
            items = {'line': None, 'col': None, 'type': None, 'error': '', 'near': None}
            items.update(match.groupdict())
            row, col, error_type, error, near = [items[k] for k in ('line', 'col', 'type', 'error', 'near')]

            if row is not None:
                row = int(row) - 1

            if col is not None:
                col = int(col) - 1

            return match, row, col, error_type, error, near
        else:
            return match, None, None, None, '', None

    def match_error(self, r, line):
        return self.split_match(r.match(line))

    # Subclasses may need to override this in complex cases
    def run(self, cmd, code):
        if self.tempfile_suffix:
            return self.tmpfile(cmd, code, suffix=self.tempfile_suffix)
        else:
            return self.communicate(cmd, code)

    # popen wrappers
    def communicate(self, cmd, code):
        return util.communicate(cmd, code)

    def tmpfile(self, cmd, code, suffix=''):
        return util.tmpfile(cmd, code, suffix or self.tempfile_suffix)

    def tmpdir(self, cmd, files, code):
        return util.tmpdir(cmd, files, self.filename, code)

    def popen(self, cmd, env=None):
        return util.popen(cmd, env)
<|endoftext|>


<commit_before>from utils import *
from copy import deepcopy
import datetime
import pytz
tz_used = pytz.timezone("US/Central")

def break_into_quarters(b):
    newbs = []
    print b.keys()

    for i in range(len(quarter_breaks) - 1):
        newb = deepcopy(b)
        times = b["times"]
        left = np.argmax(times == quarter_breaks[i])
        right = np.argmax(times == quarter_breaks[i+1])
        newb["times"]  = b["times"][left:right]
        newb["kwhs"]   = (b["kwhs"][0][left:right],  b["kwhs"][1][left:right])
        newb["temps"]  = (b["temps"][0][left:right], b["temps"][1][left:right])
        newb["bid"]    = str(b["bid"]) + "q" + str(i+1)
        newbs.append(newb)
    return newbs

if __name__ == "__main__":
    #March 31
    q1 = datetime.datetime(the_year, 3, 31, 23, 0, tzinfo = tz_used)
    #June 30
    q2 = datetime.datetime(the_year, 6, 30, 23, 0, tzinfo = tz_used)
    #Sept 30
    q3 = datetime.datetime(the_year, 9, 30, 23, 0, tzinfo = tz_used)
    #Dec 31
    q4 = datetime.datetime(the_year, 12, 31, 23, 0, tzinfo = tz_used)
    #q4 = datetime.datetime(the_year, 12, 30, 23, 0, tzinfo = tz_used)

    start = datetime.datetime(the_year, 1, 1, 0, 0, tzinfo = tz_used)
    quarter_breaks = [start, q1, q2, q3, q4]#date_time_objects
    quarter_break_inds = [0] #indices, start at start of year

    finn = "state_b_records_" + str(the_year) + "_with_temps_cleaned.pkl"

    brecs, desc = qload(finn)
    newbs = []
    for b in brecs:
        newbs += break_into_quarters(b)

    qdump((newbs, desc + "(Broken into quarters.)"), "state_b_records_" + str(the_year) + "_quarters.pkl")

<commit_msg>Delete brec_to_quarters.py<commit_after><|endoftext|>


<commit_before>import sublime
import sublime_plugin

def wait_a_sec():
    sublime.run_command('tmp_test_hey')
sublime.set_timeout(wait_a_sec, 1000)

class TmpTestHeyCommand(sublime_plugin.ApplicationCommand):
    def run(self):
        f = open('/tmp/hey3', 'w')
        f.write('hello')
        f.close()
<commit_msg>Exploring notes from package_control<commit_after>import sublime
import sublime_plugin

# TODO: This wait loop should look for plugin_host running
def wait_a_sec():
    sublime.run_command('tmp_test_hey')
sublime.set_timeout(wait_a_sec, 1000)
print(sublime.version())

class TmpTestHeyCommand(sublime_plugin.ApplicationCommand):
    def run(self):
        f = open('/tmp/hey3', 'w')
        f.write('hello')
        f.close()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

from parsers.main_parser import MainParser

if __name__ == '__main__':

    src = r"D:\nhl\official_and_json\2016-17\2017-02\2017-02-01.zip"
    # src = r"D:\nhl\official_and_json\2016-17\2017-02\2017-02-20"
    # src = r"D:\nhl\official_and_json\_2015-16\2016-05\2016-05-09.zip"

    src_dir = r"D:\nhl\official_and_json\_2014-15\2014-12"
    src_dir = r"D:\nhl\official_and_json\2016-17\2016-10"
    src_dir = r"D:\nhl\official_and_json\_2015-16\2016-04"

    files = os.listdir(src_dir)
    files = [src]

    # for f in files[17:18]:
    for f in files[:1]:
        print(f)
        if not os.path.splitext(f)[-1].lower().endswith(".zip"):
            continue
        src = os.path.join(src_dir, f)
        mp = MainParser(src)
        print(mp.game_ids)

        for game_id in mp.game_ids[:1]:
            mp.parse_single_game(game_id)

<commit_msg>Utilize disposal function for parser object<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

from parsers.main_parser import MainParser

if __name__ == '__main__':

    src = r"D:\nhl\official_and_json\2016-17\2017-02\2017-02-01.zip"
    # src = r"D:\nhl\official_and_json\2016-17\2017-02\2017-02-20"
    # src = r"D:\nhl\official_and_json\_2015-16\2016-05\2016-05-09.zip"
    src = r"D:\nhl\official_and_json\_2015-16\2016-04\2016-04-10.zip"

    src_dir = r"D:\nhl\official_and_json\_2014-15\2014-12"
    src_dir = r"D:\nhl\official_and_json\2016-17\2016-10"
    src_dir = r"D:\nhl\official_and_json\_2015-16\2016-04"

    files = os.listdir(src_dir)
    files = [src]

    # for f in files[17:18]:
    for f in files[:1]:
        print(f)
        if not os.path.splitext(f)[-1].lower().endswith(".zip"):
            continue
        src = os.path.join(src_dir, f)
        mp = MainParser(src)
        print(mp.game_ids)

        for game_id in mp.game_ids[:]:
            mp.parse_single_game(game_id)

        mp.dispose()
<|endoftext|>