<commit_before>import sublime, sublime_plugin
import colt, colt_rpc
import os.path
import json
import re

from colt import ColtPreferences
from colt_rpc import ColtConnection

def getWordPosition(view):
        position = getPosition(view)
        return view.word(position).end()

def getPosition(view):
        for sel in view.sel() :
                return sel

        return None

def getPositionEnd(view):
        position = getPosition(view)
        if position is None :
                return - 1

        return position.end()

def getContent(view):
        return view.substr(sublime.Region(0, view.size()))

def isAutosaveEnabled():
        settings = sublime.load_settings(ColtPreferences.NAME)
        return settings.get("autosave", False)

class ToggleAutosaveCommand(sublime_plugin.ApplicationCommand):
        def run(self):
                currentValue = isAutosaveEnabled()
                settings = sublime.load_settings(ColtPreferences.NAME)
                settings.set("autosave", not currentValue)
                sublime.save_settings(ColtPreferences.NAME)

        def description(self):
                if isAutosaveEnabled() :
                        return "Disable Autosave"
                else :
                        return "Enable Autosave"

class ColtAutosaveListener(sublime_plugin.EventListener):

        def on_modified(self, view):
                if colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions() and isAutosaveEnabled() :
                        view.run_command("save")

class ColtCompletitions(sublime_plugin.EventListener):

        def on_query_completions(self, view, prefix, locations):
                if not colt.isColtFile(view) :
                        return []

                position = getPositionEnd(view)

                if view.substr(position - 1) == "." :
                        position = position - 1
                else :
                        wordStart = view.word(getPosition(view)).begin()
                        if view.substr(wordStart - 1) == "." :
                                position = wordStart - 1
                        else :
                                return []

                if not colt_rpc.isConnected() or not colt_rpc.hasActiveSessions() :
                        return []

                response = colt_rpc.getContextForPosition(view.file_name(), position, getContent(view), "PROPERTIES")
                if response.has_key("error") :
                        return []

                result = response["result"]
                if not result is None :
                        completitions = []
                        resultJSON = json.loads(result)
                        for resultStr in resultJSON :
                                if "{})" in resultStr :
                                        resultStr = resultStr.replace("{})", "")

                                replaceStr = resultStr
                                displayStr = resultStr
                                cursiveStr = ""

                                if "(" in resultStr :
                                        replaceStr = resultStr[:resultStr.index("(")]
                                        displayStr = replaceStr
                                        cursiveStr = resultStr

                                completitions.append((displayStr + "\t" + cursiveStr + "[COLT]", replaceStr))

                return completitions

class AbstractColtRunCommand(sublime_plugin.WindowCommand):
        def run(self):
                return

        def getSettings(self):
                settings = sublime.load_settings(ColtPreferences.NAME)
                if not settings.has("coltPath") :
                        sublime.error_message("COLT path is not specified, please enter the path")
                        self.window.show_input_panel("COLT Path:", "", self.onCOLTPathInput, None, None)
                        return

                settings = sublime.load_settings(ColtPreferences.NAME)
                coltPath = settings.get("coltPath")

                if not os.path.exists(coltPath) :
                        sublime.error_message("COLT path specified is invalid, please enter the correct path")
                        self.window.show_input_panel("COLT Path:", "", self.onCOLTPathInput, None, None)
                        return

                return sublime.load_settings(ColtPreferences.NAME)

        def onCOLTPathInput(self, inputPath):
                if inputPath and os.path.exists(inputPath) :
                        settings = sublime.load_settings(ColtPreferences.NAME)
                        settings.set("coltPath", inputPath)
                        sublime.save_settings(ColtPreferences.NAME)
                        self.run()
                else :
                        sublime.error_message("COLT path specified is invalid")

        def is_enabled(self):
                if self.window.active_view() is None :
                        return False
                return colt.isColtFile(self.window.active_view())

class GetAllCountsCommand(sublime_plugin.WindowCommand):
        ranges = []

        def run(self):
            # but 1st, clear every region this command could have created
            for p in GetAllCountsCommand.ranges:
                p[0].erase_regions(p[1])

            if ColtConnection.activeSessions > 0:

                # getMethodCounts
                # {u'jsonrpc': u'2.0', u'id': 77, u'result': [{u'count': 1, u'position': 339, u'filePath': u'/Users/makc/Downloads/d3/bubles.js'}, ...
                resultJSON = colt_rpc.getMethodCounts()

                if resultJSON.has_key("error") or resultJSON["result"] is None :
                        # sublime.error_message("Can't read method counts")
                        return

                for info in resultJSON["result"]:

                    position = info["position"]
                    filePath = info["filePath"]

                    count = info["count"]
                    if count > 0:
                        if count > 9:
                            count = "infinity"

                        for view in self.window.views():
                            if view.file_name() == filePath:
                                view.add_regions("counts." + str(position), [sublime.Region(position)],
                                    "scope", "../User/icons/" + str(count) + "@2x", sublime.HIDDEN | sublime.PERSISTENT)
                                GetAllCountsCommand.ranges.append([view, "counts." + str(position)])

class ShowLastErrorCommand(sublime_plugin.WindowCommand):
    def run(self):
        # but 1st, clear every region this command could have created
        for view in self.window.views():
            view.erase_regions("error.colt")

        if ColtConnection.activeSessions > 0:
            resultJSON = colt_rpc.getLastRuntimeError();

            if resultJSON.has_key("error") or resultJSON["result"] is None :
                # sublime.error_message("Can't read method counts")
                return

            for view in self.window.views():
                if view.file_name() == resultJSON["result"]["filePath"]:
                    view.add_regions("error.colt", [sublime.Region(resultJSON["result"]["position"])],
                        "scope", "../User/icons/error@2x", sublime.HIDDEN | sublime.PERSISTENT)

# ST2 version of http://www.sublimetext.com/docs/plugin-examples Idle Watcher
class IdleWatcher(sublime_plugin.EventListener):
    pending = 0

    def handleTimeout(self, view):
        self.pending = self.pending - 1
        if self.pending == 0:
            # There are no more queued up calls to handleTimeout, so it must have
            # been 3000ms since the last modification
            self.onIdle(view)

    def onModified(self, view):
        self.pending = self.pending + 1
        # Ask for handleTimeout to be called in 3000ms
        sublime.set_timeout(functools.partial(self.handleTimeout, view), 3000)

    def onIdle(self, view):
        #print "No activity in the past 3000ms"
        sublime.active_window().run_command("get_all_counts")
        sublime.active_window().run_command("show_last_error")
        sublime.set_timeout(functools.partial(self.onModified, view), 3000)

    def on_modified(self, view):
        self.onModified(view)

    def on_selection_modified(self, view):
        self.onModified(view)

    def on_activated(self, view):
        self.onModified(view)

class ColtGoToDeclarationCommand(sublime_plugin.WindowCommand):

        def run(self):
                view = self.window.active_view()

                fileName = view.file_name()
                position = getWordPosition(view)
                content = getContent(view)

                resultJSON = colt_rpc.getDeclarationPosition(fileName, position, content)

                if resultJSON.has_key("error") or resultJSON["result"] is None :
                        # sublime.error_message("Can't find a declaration")
                        return

                position = resultJSON["result"]["position"]
                filePath = resultJSON["result"]["filePath"]

                targetView = self.window.open_file(filePath)
                targetView.sel().clear()
                targetView.sel().add(sublime.Region(position))

                targetView.show_at_center(position)

                # work around sublime bug with caret position not refreshing
                bug = [s for s in targetView.sel()]
                targetView.add_regions("bug", bug, "bug", "dot", sublime.HIDDEN | sublime.PERSISTENT)
                targetView.erase_regions("bug")

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtRunFunctionCommand(sublime_plugin.WindowCommand):

        def run(self):
                view = self.window.active_view()

                fileName = view.file_name()
                position = getWordPosition(view)
                content = getContent(view)

                methodId = colt_rpc.getMethodId(fileName, position, content)

                if methodId is None :
                        sublime.error_message("Can't figure out the function ID")
                        return

                if methodId.startswith('"') :
                        methodId = methodId[1:len(methodId)-1]

                colt_rpc.runMethod(methodId)

                self.window.run_command("get_all_counts")

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtResetCallCountsCommand(sublime_plugin.WindowCommand):
        def run(self):
                colt_rpc.resetCallCounts()

                self.window.run_command("get_all_counts")

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtViewCallCountCommand(sublime_plugin.WindowCommand):
        def run(self):
                view = self.window.active_view()

                outputPanel = self.window.get_output_panel("COLT")
                outputPanel.set_scratch(True)
                outputPanel.set_read_only(False)
                outputPanel.set_name("COLT")
                self.window.run_command("show_panel", {"panel": "output.COLT"})
                self.window.set_view_index(outputPanel, 1, 0)

                position = getWordPosition(view)
                resultJSON = colt_rpc.getCallCount(view.file_name(), position, getContent(view))

                if resultJSON.has_key("result") :
                        position = getPosition(view)
                        word = view.word(position)

                        result = resultJSON["result"]
                        if result is None :
                                self.appendToConsole(outputPanel, "Call count is not available")
                        else :
                                self.appendToConsole(outputPanel, "Call count: " + str(result))

        def appendToConsole(self, outputPanel, text):
                edit = outputPanel.begin_edit("COLT output")
                edit = outputPanel.begin_edit()
                outputPanel.insert(edit, outputPanel.size(), text + '\n')
                outputPanel.end_edit(edit)
                outputPanel.show(outputPanel.size())

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtViewValueCommand(sublime_plugin.WindowCommand):
        def run(self):
                view = self.window.active_view()

                outputPanel = self.window.get_output_panel("COLT")
                outputPanel.set_scratch(True)
                outputPanel.set_read_only(False)
                outputPanel.set_name("COLT")
                self.window.run_command("show_panel", {"panel": "output.COLT"})
                self.window.set_view_index(outputPanel, 1, 0)

                position = getWordPosition(view)

                expression = None
                for sel in view.sel() :
                    if expression is None :
                        expression = view.substr(sel)

                resultJSON = colt_rpc.evaluateExpression(view.file_name(), expression, position, getContent(view))
                if resultJSON.has_key("result") :
                        position = getPosition(view)
                        word = view.word(position)

                        result = resultJSON["result"]
                        if result is None :
                                self.appendToConsole(outputPanel, view.substr(word) + " value: unknown")
                        else :
                                self.appendToConsole(outputPanel, result)

        def appendToConsole(self, outputPanel, text):
                edit = outputPanel.begin_edit("COLT output")
                edit = outputPanel.begin_edit()
                outputPanel.insert(edit, outputPanel.size(), text + '\n')
                outputPanel.end_edit(edit)
                outputPanel.show(outputPanel.size())

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtReloadCommand(sublime_plugin.WindowCommand):
        def run(self):
                colt_rpc.reload()

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtClearLogCommand(sublime_plugin.WindowCommand):
        def run(self):
                colt_rpc.clearLog()

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class StartColtCommand(AbstractColtRunCommand):

        def run(self):
                settings = self.getSettings()

                # TODO: detect if colt is running and skip running it if it is
                colt.runCOLT(settings)

class OpenInColtCommand(AbstractColtRunCommand):

        html = None

        def run(self):
                settings = self.getSettings()

                # Check the file name
                file = self.window.active_view().file_name()
                if re.match('.*\\.html?$', file): # r'' for v3
                    OpenInColtCommand.html = file

                if OpenInColtCommand.html is None :
                    # Error message
                    sublime.error_message('This tab is not html file. Please open project main html and try again.')
                    return

                # Export COLT project
                coltProjectFilePath = colt.exportProject(self.window, OpenInColtCommand.html)

                # Add project to workset file
                colt.addToWorkingSet(coltProjectFilePath)

                # Run COLT
                colt_rpc.initAndConnect(settings, coltProjectFilePath)

                # Authorize
                colt_rpc.runAfterAuthorization = None
                colt_rpc.authorize(self.window)

class RunWithColtCommand(AbstractColtRunCommand):

        def run(self):
                settings = self.getSettings()

                # Check the file name
                file = self.window.active_view().file_name()
                if re.match('.*\\.html?$', file): # r'' for v3
                    OpenInColtCommand.html = file

                if OpenInColtCommand.html is None :
                    # Error message
                    sublime.error_message('This tab is not html file. Please open project main html and try again.')
                    return

                # Export COLT project
                coltProjectFilePath = colt.exportProject(self.window, OpenInColtCommand.html)

                # Add project to workset file
                colt.addToWorkingSet(coltProjectFilePath)

                # Run COLT
                colt_rpc.initAndConnect(settings, coltProjectFilePath)

                # Authorize and start live
                colt_rpc.runAfterAuthorization = colt_rpc.startLive
                colt_rpc.authorize(self.window)
<commit_msg>User -> COLT, missing import<commit_after>import sublime, sublime_plugin
import colt, colt_rpc
import functools
import os.path
import json
import re

from colt import ColtPreferences
from colt_rpc import ColtConnection

def getWordPosition(view):
        position = getPosition(view)
        return view.word(position).end()

def getPosition(view):
        for sel in view.sel() :
                return sel

        return None

def getPositionEnd(view):
        position = getPosition(view)
        if position is None :
                return - 1

        return position.end()

def getContent(view):
        return view.substr(sublime.Region(0, view.size()))

def isAutosaveEnabled():
        settings = sublime.load_settings(ColtPreferences.NAME)
        return settings.get("autosave", False)

class ToggleAutosaveCommand(sublime_plugin.ApplicationCommand):
        def run(self):
                currentValue = isAutosaveEnabled()
                settings = sublime.load_settings(ColtPreferences.NAME)
                settings.set("autosave", not currentValue)
                sublime.save_settings(ColtPreferences.NAME)

        def description(self):
                if isAutosaveEnabled() :
                        return "Disable Autosave"
                else :
                        return "Enable Autosave"

class ColtAutosaveListener(sublime_plugin.EventListener):

        def on_modified(self, view):
                if colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions() and isAutosaveEnabled() :
                        view.run_command("save")

class ColtCompletitions(sublime_plugin.EventListener):

        def on_query_completions(self, view, prefix, locations):
                if not colt.isColtFile(view) :
                        return []

                position = getPositionEnd(view)

                if view.substr(position - 1) == "." :
                        position = position - 1
                else :
                        wordStart = view.word(getPosition(view)).begin()
                        if view.substr(wordStart - 1) == "." :
                                position = wordStart - 1
                        else :
                                return []

                if not colt_rpc.isConnected() or not colt_rpc.hasActiveSessions() :
                        return []

                response = colt_rpc.getContextForPosition(view.file_name(), position, getContent(view), "PROPERTIES")
                if response.has_key("error") :
                        return []

                result = response["result"]
                if not result is None :
                        completitions = []
                        resultJSON = json.loads(result)
                        for resultStr in resultJSON :
                                if "{})" in resultStr :
                                        resultStr = resultStr.replace("{})", "")

                                replaceStr = resultStr
                                displayStr = resultStr
                                cursiveStr = ""

                                if "(" in resultStr :
                                        replaceStr = resultStr[:resultStr.index("(")]
                                        displayStr = replaceStr
                                        cursiveStr = resultStr

                                completitions.append((displayStr + "\t" + cursiveStr + "[COLT]", replaceStr))

                return completitions

class AbstractColtRunCommand(sublime_plugin.WindowCommand):
        def run(self):
                return

        def getSettings(self):
                settings = sublime.load_settings(ColtPreferences.NAME)
                if not settings.has("coltPath") :
                        sublime.error_message("COLT path is not specified, please enter the path")
                        self.window.show_input_panel("COLT Path:", "", self.onCOLTPathInput, None, None)
                        return

                settings = sublime.load_settings(ColtPreferences.NAME)
                coltPath = settings.get("coltPath")

                if not os.path.exists(coltPath) :
                        sublime.error_message("COLT path specified is invalid, please enter the correct path")
                        self.window.show_input_panel("COLT Path:", "", self.onCOLTPathInput, None, None)
                        return

                return sublime.load_settings(ColtPreferences.NAME)

        def onCOLTPathInput(self, inputPath):
                if inputPath and os.path.exists(inputPath) :
                        settings = sublime.load_settings(ColtPreferences.NAME)
                        settings.set("coltPath", inputPath)
                        sublime.save_settings(ColtPreferences.NAME)
                        self.run()
                else :
                        sublime.error_message("COLT path specified is invalid")

        def is_enabled(self):
                if self.window.active_view() is None :
                        return False
                return colt.isColtFile(self.window.active_view())

class GetAllCountsCommand(sublime_plugin.WindowCommand):
        ranges = []

        def run(self):
            # but 1st, clear every region this command could have created
            for p in GetAllCountsCommand.ranges:
                p[0].erase_regions(p[1])

            if ColtConnection.activeSessions > 0:

                # getMethodCounts
                # {u'jsonrpc': u'2.0', u'id': 77, u'result': [{u'count': 1, u'position': 339, u'filePath': u'/Users/makc/Downloads/d3/bubles.js'}, ...
                resultJSON = colt_rpc.getMethodCounts()

                if resultJSON.has_key("error") or resultJSON["result"] is None :
                        # sublime.error_message("Can't read method counts")
                        return

                for info in resultJSON["result"]:

                    position = info["position"]
                    filePath = info["filePath"]

                    count = info["count"]
                    if count > 0:
                        if count > 9:
                            count = "infinity"

                        for view in self.window.views():
                            if view.file_name() == filePath:
                                view.add_regions("counts." + str(position), [sublime.Region(position)],
                                    "scope", "../COLT/icons/" + str(count) + "@2x", sublime.HIDDEN | sublime.PERSISTENT)
                                GetAllCountsCommand.ranges.append([view, "counts." + str(position)])

class ShowLastErrorCommand(sublime_plugin.WindowCommand):
    def run(self):
        # but 1st, clear every region this command could have created
        for view in self.window.views():
            view.erase_regions("error.colt")

        if ColtConnection.activeSessions > 0:
            resultJSON = colt_rpc.getLastRuntimeError();

            if resultJSON.has_key("error") or resultJSON["result"] is None :
                # sublime.error_message("Can't read method counts")
                return

            for view in self.window.views():
                if view.file_name() == resultJSON["result"]["filePath"]:
                    view.add_regions("error.colt", [sublime.Region(resultJSON["result"]["position"])],
                        "scope", "../COLT/icons/error@2x", sublime.HIDDEN | sublime.PERSISTENT)

# ST2 version of http://www.sublimetext.com/docs/plugin-examples Idle Watcher
class IdleWatcher(sublime_plugin.EventListener):
    pending = 0

    def handleTimeout(self, view):
        self.pending = self.pending - 1
        if self.pending == 0:
            # There are no more queued up calls to handleTimeout, so it must have
            # been 3000ms since the last modification
            self.onIdle(view)

    def onModified(self, view):
        self.pending = self.pending + 1
        # Ask for handleTimeout to be called in 3000ms
        sublime.set_timeout(functools.partial(self.handleTimeout, view), 3000)

    def onIdle(self, view):
        #print "No activity in the past 3000ms"
        sublime.active_window().run_command("get_all_counts")
        sublime.active_window().run_command("show_last_error")
        sublime.set_timeout(functools.partial(self.onModified, view), 3000)

    def on_modified(self, view):
        self.onModified(view)

    def on_selection_modified(self, view):
        self.onModified(view)

    def on_activated(self, view):
        self.onModified(view)

class ColtGoToDeclarationCommand(sublime_plugin.WindowCommand):

        def run(self):
                view = self.window.active_view()

                fileName = view.file_name()
                position = getWordPosition(view)
                content = getContent(view)

                resultJSON = colt_rpc.getDeclarationPosition(fileName, position, content)

                if resultJSON.has_key("error") or resultJSON["result"] is None :
                        # sublime.error_message("Can't find a declaration")
                        return

                position = resultJSON["result"]["position"]
                filePath = resultJSON["result"]["filePath"]

                targetView = self.window.open_file(filePath)
                targetView.sel().clear()
                targetView.sel().add(sublime.Region(position))

                targetView.show_at_center(position)

                # work around sublime bug with caret position not refreshing
                bug = [s for s in targetView.sel()]
                targetView.add_regions("bug", bug, "bug", "dot", sublime.HIDDEN | sublime.PERSISTENT)
                targetView.erase_regions("bug")

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtRunFunctionCommand(sublime_plugin.WindowCommand):

        def run(self):
                view = self.window.active_view()

                fileName = view.file_name()
                position = getWordPosition(view)
                content = getContent(view)

                methodId = colt_rpc.getMethodId(fileName, position, content)

                if methodId is None :
                        sublime.error_message("Can't figure out the function ID")
                        return

                if methodId.startswith('"') :
                        methodId = methodId[1:len(methodId)-1]

                colt_rpc.runMethod(methodId)

                self.window.run_command("get_all_counts")

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtResetCallCountsCommand(sublime_plugin.WindowCommand):
        def run(self):
                colt_rpc.resetCallCounts()

                self.window.run_command("get_all_counts")

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtViewCallCountCommand(sublime_plugin.WindowCommand):
        def run(self):
                view = self.window.active_view()

                outputPanel = self.window.get_output_panel("COLT")
                outputPanel.set_scratch(True)
                outputPanel.set_read_only(False)
                outputPanel.set_name("COLT")
                self.window.run_command("show_panel", {"panel": "output.COLT"})
                self.window.set_view_index(outputPanel, 1, 0)

                position = getWordPosition(view)
                resultJSON = colt_rpc.getCallCount(view.file_name(), position, getContent(view))

                if resultJSON.has_key("result") :
                        position = getPosition(view)
                        word = view.word(position)

                        result = resultJSON["result"]
                        if result is None :
                                self.appendToConsole(outputPanel, "Call count is not available")
                        else :
                                self.appendToConsole(outputPanel, "Call count: " + str(result))

        def appendToConsole(self, outputPanel, text):
                edit = outputPanel.begin_edit("COLT output")
                edit = outputPanel.begin_edit()
                outputPanel.insert(edit, outputPanel.size(), text + '\n')
                outputPanel.end_edit(edit)
                outputPanel.show(outputPanel.size())

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtViewValueCommand(sublime_plugin.WindowCommand):
        def run(self):
                view = self.window.active_view()

                outputPanel = self.window.get_output_panel("COLT")
                outputPanel.set_scratch(True)
                outputPanel.set_read_only(False)
                outputPanel.set_name("COLT")
                self.window.run_command("show_panel", {"panel": "output.COLT"})
                self.window.set_view_index(outputPanel, 1, 0)

                position = getWordPosition(view)

                expression = None
                for sel in view.sel() :
                    if expression is None :
                        expression = view.substr(sel)

                resultJSON = colt_rpc.evaluateExpression(view.file_name(), expression, position, getContent(view))
                if resultJSON.has_key("result") :
                        position = getPosition(view)
                        word = view.word(position)

                        result = resultJSON["result"]
                        if result is None :
                                self.appendToConsole(outputPanel, view.substr(word) + " value: unknown")
                        else :
                                self.appendToConsole(outputPanel, result)

        def appendToConsole(self, outputPanel, text):
                edit = outputPanel.begin_edit("COLT output")
                edit = outputPanel.begin_edit()
                outputPanel.insert(edit, outputPanel.size(), text + '\n')
                outputPanel.end_edit(edit)
                outputPanel.show(outputPanel.size())

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt.isColtFile(view) and colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtReloadCommand(sublime_plugin.WindowCommand):
        def run(self):
                colt_rpc.reload()

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class ColtClearLogCommand(sublime_plugin.WindowCommand):
        def run(self):
                colt_rpc.clearLog()

        def is_enabled(self):
                view = self.window.active_view()
                if view is None :
                        return False
                return colt_rpc.isConnected() and colt_rpc.hasActiveSessions()

class StartColtCommand(AbstractColtRunCommand):

        def run(self):
                settings = self.getSettings()

                # TODO: detect if colt is running and skip running it if it is
                colt.runCOLT(settings)

class OpenInColtCommand(AbstractColtRunCommand):

        html = None

        def run(self):
                settings = self.getSettings()

                # Check the file name
                file = self.window.active_view().file_name()
                if re.match('.*\\.html?$', file): # r'' for v3
                    OpenInColtCommand.html = file

                if OpenInColtCommand.html is None :
                    # Error message
                    sublime.error_message('This tab is not html file. Please open project main html and try again.')
                    return

                # Export COLT project
                coltProjectFilePath = colt.exportProject(self.window, OpenInColtCommand.html)

                # Add project to workset file
                colt.addToWorkingSet(coltProjectFilePath)

                # Run COLT
                colt_rpc.initAndConnect(settings, coltProjectFilePath)

                # Authorize
                colt_rpc.runAfterAuthorization = None
                colt_rpc.authorize(self.window)

class RunWithColtCommand(AbstractColtRunCommand):

        def run(self):
                settings = self.getSettings()

                # Check the file name
                file = self.window.active_view().file_name()
                if re.match('.*\\.html?$', file): # r'' for v3
                    OpenInColtCommand.html = file

                if OpenInColtCommand.html is None :
                    # Error message
                    sublime.error_message('This tab is not html file. Please open project main html and try again.')
                    return

                # Export COLT project
                coltProjectFilePath = colt.exportProject(self.window, OpenInColtCommand.html)

                # Add project to workset file
                colt.addToWorkingSet(coltProjectFilePath)

                # Run COLT
                colt_rpc.initAndConnect(settings, coltProjectFilePath)

                # Authorize and start live
                colt_rpc.runAfterAuthorization = colt_rpc.startLive
                colt_rpc.authorize(self.window)
<|endoftext|>


<commit_before>class Coerce(object):
    @staticmethod
    def bool(space, w_obj):
        return space.is_true(w_obj)

    @staticmethod
    def symbol(space, w_obj):
        if w_obj.is_kind_of(space, space.w_symbol):
            return space.symbol_w(w_obj)
        else:
            w_str = space.convert_type(w_obj, space.w_string, "to_str", False)
            if w_str is space.w_nil:
                w_inspect_str = space.send(w_obj, space.newsymbol("inspect"))
                if not w_inspect_str.is_kind_of(space, space.w_string):
                    inspect_str = "#<%s:0x%x>" % (
                        space.getclass(w_obj).name,
                        space.int_w(space.send(self, space.newsymbol("__id__")))
                    )
                else:
                    inspect_str = space.str_w(w_inspect_str)
                raise space.error(space.w_TypeError, "%s is not a symbol" % inspect_str)
            else:
                return space.str_w(w_str)

    @staticmethod
    def int(space, w_obj):
        if w_obj.is_kind_of(space, space.w_fixnum):
            return space.int_w(w_obj)
        else:
            return space.int_w(space.convert_type(w_obj, space.w_fixnum, "to_int"))

    @staticmethod
    def float(space, w_obj):
        if w_obj.is_kind_of(space, space.w_numeric):
            return space.float_w(w_obj)
        else:
            return space.float_w(space.send(w_obj, space.newsymbol("Float"), [w_obj]))

    @staticmethod
    def str(space, w_obj):
        if w_obj.is_kind_of(space, space.w_string) or w_obj.is_kind_of(space, space.w_symbol):
            return space.str_w(w_obj)
        else:
            return space.str_w(space.convert_type(w_obj, space.w_string, "to_str"))

    @staticmethod
    def path(space, w_obj):
        if w_obj is space.w_nil:
            return None
        return space.str_w(w_obj)
<commit_msg>fix copy'n'paste error<commit_after>class Coerce(object):
    @staticmethod
    def bool(space, w_obj):
        return space.is_true(w_obj)

    @staticmethod
    def symbol(space, w_obj):
        if w_obj.is_kind_of(space, space.w_symbol):
            return space.symbol_w(w_obj)
        else:
            w_str = space.convert_type(w_obj, space.w_string, "to_str", False)
            if w_str is space.w_nil:
                w_inspect_str = space.send(w_obj, space.newsymbol("inspect"))
                if not w_inspect_str.is_kind_of(space, space.w_string):
                    inspect_str = "#<%s:0x%x>" % (
                        space.getclass(w_obj).name,
                        space.int_w(space.send(w_obj, space.newsymbol("__id__")))
                    )
                else:
                    inspect_str = space.str_w(w_inspect_str)
                raise space.error(space.w_TypeError, "%s is not a symbol" % inspect_str)
            else:
                return space.str_w(w_str)

    @staticmethod
    def int(space, w_obj):
        if w_obj.is_kind_of(space, space.w_fixnum):
            return space.int_w(w_obj)
        else:
            return space.int_w(space.convert_type(w_obj, space.w_fixnum, "to_int"))

    @staticmethod
    def float(space, w_obj):
        if w_obj.is_kind_of(space, space.w_numeric):
            return space.float_w(w_obj)
        else:
            return space.float_w(space.send(w_obj, space.newsymbol("Float"), [w_obj]))

    @staticmethod
    def str(space, w_obj):
        if w_obj.is_kind_of(space, space.w_string) or w_obj.is_kind_of(space, space.w_symbol):
            return space.str_w(w_obj)
        else:
            return space.str_w(space.convert_type(w_obj, space.w_string, "to_str"))

    @staticmethod
    def path(space, w_obj):
        if w_obj is space.w_nil:
            return None
        return space.str_w(w_obj)
<|endoftext|>


<commit_before># Licensed under a 3-clause BSD style license - see LICENSES
from __future__ import print_function

from os.path import dirname, join

import numpy as np
from numpy.testing import assert_allclose, assert_almost_equal
from astropy.extern import six

import sncosmo

def test_read_griddata_ascii():

    # Write a temporary test file.
    f = six.StringIO()
    f.write("0. 0. 0.\n"
            "0. 1. 0.\n"
            "0. 2. 0.\n"
            "1. 0. 0.\n"
            "1. 1. 0.\n"
            "1. 2. 0.\n")
    f.seek(0)

    x0, x1, y = sncosmo.read_griddata_ascii(f)

    assert_allclose(x0, np.array([0., 1.]))
    assert_allclose(x1, np.array([0., 1., 2.]))

def test_write_griddata_ascii():

    x0 = np.array([0., 1.])
    x1 = np.array([0., 1., 2.])
    y = np.zeros((2, 3))

    f = six.StringIO()
    sncosmo.write_griddata_ascii(x0, x1, y, f)

    # Read it back
    f.seek(0)
    x0_in, x1_in, y_in = sncosmo.read_griddata_ascii(f)
    assert_allclose(x0_in, x0)
    assert_allclose(x1_in, x1)
    assert_allclose(y_in, y)

def test_griddata_fits():
    """Round tripping with write_griddata_fits() and read_griddata_fits()"""

    x0 = np.array([0., 1.])
    x1 = np.array([0., 1., 2.])
    y = np.zeros((2, 3))

    f = six.StringIO()
    sncosmo.write_griddata_fits(x0, x1, y, f)

    # Read it back
    f.seek(0)
    x0_in, x1_in, y_in = sncosmo.read_griddata_fits(f)
    assert_allclose(x0_in, x0)
    assert_allclose(x1_in, x1)
    assert_allclose(y_in, y)

def test_read_salt2():
    fname = join(dirname(__file__), "data", "salt2_example.dat")
    data = sncosmo.read_lc(fname, format="salt2")

    # Test a few columns
    assert_allclose(data["Date"], [52816.54, 52824.59, 52795.59, 52796.59])
    assert_allclose(data["ZP"], [27.091335, 27.091335, 25.913054, 25.913054])
    assert np.all(data["Filter"] == np.array(["MEGACAM::g", "MEGACAM::g",
                                              "MEGACAM::i", "MEGACAM::i"]))
    assert np.all(data["MagSys"] == "VEGA")

    # Test a bit of metadata
    assert_allclose(data.meta["Z_HELIO"], 0.285)
    assert_allclose(data.meta["RA"], 333.690959)
    assert data.meta["z_source"] == "H"

def test_read_salt2_old():
    dname = join(dirname(__file__), "data", "SNLS3-04D3gx")
    data = sncosmo.read_lc(dname, format="salt2-old")

    # Test length and column names:
    assert len(data) == 25 + 37 + 38 + 18  # g + r + i + z lengths
    assert data.colnames == ["Date", "Flux", "Fluxerr", "ZP", "Filter",
                             "MagSys"]

    # Test a bit of metadata and data
    assert data.meta["NAME"] == "04D3gx"
    assert_allclose(data.meta["Redshift"], 0.91)
    assert_allclose(data.meta["RA"], 215.056948)
    assert np.all(data["MagSys"] == "VEGA")
<commit_msg>add round trip test for json format<commit_after># Licensed under a 3-clause BSD style license - see LICENSES
from __future__ import print_function

import os
from os.path import dirname, join
from tempfile import NamedTemporaryFile

import numpy as np
from numpy.testing import assert_allclose, assert_almost_equal
from astropy.table import Table
from astropy.extern import six

import sncosmo

# Dummy data used for read_lc/write_lc round-tripping tests
time = [1., 2., 3., 4.]
band = ['sdssg', 'sdssr', 'sdssi', 'sdssz']
zp = [25., 25., 25., 25.]
zpsys = ['ab', 'ab', 'ab', 'ab']
flux = [1., 1., 1., 1.]
fluxerr = [0.1, 0.1, 0.1, 0.1]
lcdata = Table(data=(time, band, flux, fluxerr, zp, zpsys),
               names=('time', 'band', 'flux', 'fluxerr', 'zp', 'zpsys'),
               meta={'a': 1, 'b': 1.0, 'c': 'one'})

def test_read_griddata_ascii():

    # Write a temporary test file.
    f = six.StringIO()
    f.write("0. 0. 0.\n"
            "0. 1. 0.\n"
            "0. 2. 0.\n"
            "1. 0. 0.\n"
            "1. 1. 0.\n"
            "1. 2. 0.\n")
    f.seek(0)

    x0, x1, y = sncosmo.read_griddata_ascii(f)

    assert_allclose(x0, np.array([0., 1.]))
    assert_allclose(x1, np.array([0., 1., 2.]))

def test_write_griddata_ascii():

    x0 = np.array([0., 1.])
    x1 = np.array([0., 1., 2.])
    y = np.zeros((2, 3))

    f = six.StringIO()
    sncosmo.write_griddata_ascii(x0, x1, y, f)

    # Read it back
    f.seek(0)
    x0_in, x1_in, y_in = sncosmo.read_griddata_ascii(f)
    assert_allclose(x0_in, x0)
    assert_allclose(x1_in, x1)
    assert_allclose(y_in, y)

def test_griddata_fits():
    """Round tripping with write_griddata_fits() and read_griddata_fits()"""

    x0 = np.array([0., 1.])
    x1 = np.array([0., 1., 2.])
    y = np.zeros((2, 3))

    f = six.StringIO()
    sncosmo.write_griddata_fits(x0, x1, y, f)

    # Read it back
    f.seek(0)
    x0_in, x1_in, y_in = sncosmo.read_griddata_fits(f)
    assert_allclose(x0_in, x0)
    assert_allclose(x1_in, x1)
    assert_allclose(y_in, y)

def test_read_salt2():
    fname = join(dirname(__file__), "data", "salt2_example.dat")
    data = sncosmo.read_lc(fname, format="salt2")

    # Test a few columns
    assert_allclose(data["Date"], [52816.54, 52824.59, 52795.59, 52796.59])
    assert_allclose(data["ZP"], [27.091335, 27.091335, 25.913054, 25.913054])
    assert np.all(data["Filter"] == np.array(["MEGACAM::g", "MEGACAM::g",
                                              "MEGACAM::i", "MEGACAM::i"]))
    assert np.all(data["MagSys"] == "VEGA")

    # Test a bit of metadata
    assert_allclose(data.meta["Z_HELIO"], 0.285)
    assert_allclose(data.meta["RA"], 333.690959)
    assert data.meta["z_source"] == "H"

def test_read_salt2_old():
    dname = join(dirname(__file__), "data", "SNLS3-04D3gx")
    data = sncosmo.read_lc(dname, format="salt2-old")

    # Test length and column names:
    assert len(data) == 25 + 37 + 38 + 18  # g + r + i + z lengths
    assert data.colnames == ["Date", "Flux", "Fluxerr", "ZP", "Filter",
                             "MagSys"]

    # Test a bit of metadata and data
    assert data.meta["NAME"] == "04D3gx"
    assert_allclose(data.meta["Redshift"], 0.91)
    assert_allclose(data.meta["RA"], 215.056948)
    assert np.all(data["MagSys"] == "VEGA")

def test_json():
    f = NamedTemporaryFile(delete=False)
    f.close()  # close to ensure that we can open it in write_lc()

    sncosmo.write_lc(lcdata, f.name, format='json')
    data = sncosmo.read_lc(f.name, format='json')

    for key in lcdata.colnames:
        assert np.all(data[key] == lcdata[key])
    for key in lcdata.meta:
        assert data.meta[key] == lcdata.meta[key]

    os.unlink(f.name)
<|endoftext|>


<commit_before>"""Collection of convenient functions that will work with my anaconda or uvcdat install.

Functions:
  adjust_lon_range   -- Express longitude values in desired 360 degree interval
  apply_lon_filter   -- Set values outside of specified longitude range to zero
  broadcast_array    -- Broadcast a one dimensional array to a target shape
  calc_significance  -- Perform significance test
  coordinate_paris   -- Generate lat/lon pairs
  dict_filter        -- Filter dictionary according to specified keys
  find_nearest       -- Find the closest array item to value
  find_duplicates    -- Return list of duplicates in a list
  fix_label          -- Fix formatting of an axis label taken from the command line
  get_threshold      -- Turn the user input threshold into a numeric threshold
  hi_lo              -- Determine the new highest and lowest value.
  list_kwargs        -- List keyword arguments of a function
  match_dates        -- Take list of dates and match with the corresponding times
                        in a detailed time axis
  single2list        -- Check if item is a list, then convert if not
  units_info         -- Make the units taken from a file LaTeX math compliant

"""

import numpy
from scipy import stats
import pdb, re
import inspect

def adjust_lon_range(lons, radians=True, start=0.0):
    """Express longitude values in a 360 degree (or 2*pi radians) interval.

    Args:
      lons (list/tuple): Longitude axis values (monotonically increasing)
      radians (bool): Specify whether input data are in radians (True) or
        degrees (False). Output will be the same units.
      start (float, optional): Start value for the output interval (add 360 degrees or 2*pi
        radians to get the end point)

    """

    lons = single2list(lons, numpy_array=True)

    interval360 = 2.0*numpy.pi if radians else 360.0
    end = start + interval360

    less_than_start = numpy.ones([len(lons),])
    while numpy.sum(less_than_start) != 0:
        lons = numpy.where(lons < start, lons + interval360, lons)
        less_than_start = lons < start

    more_than_end = numpy.ones([len(lons),])
    while numpy.sum(more_than_end) != 0:
        lons = numpy.where(lons >= end, lons - interval360, lons)
        more_than_end = lons >= end

    return lons

def apply_lon_filter(data, lon_bounds):
    """Set values outside of specified longitude range to zero.

    Args:
      data (numpy.ndarray): Array of longitude values.
      lon_bounds (list/tuple): Specified longitude range (min, max)

    """

    # Convert to common bounds (0, 360)
    lon_min = adjust_lon_range(lon_bounds[0], radians=False, start=0.0)
    lon_max = adjust_lon_range(lon_bounds[1], radians=False, start=0.0)
    lon_axis = adjust_lon_range(data.getLongitude()[:], radians=False, start=0.0)

    # Make required values zero
    ntimes, nlats, nlons = data.shape
    lon_axis_tiled = numpy.tile(lon_axis, (ntimes, nlats, 1))

    new_data = numpy.where(lon_axis_tiled < lon_min, 0.0, data)

    return numpy.where(lon_axis_tiled > lon_max, 0.0, new_data)

def broadcast_array(array, axis_index, shape):
    """Broadcast a one dimensional array to a target shape.

    Args:
      array (numpy.ndarray): One dimensional array
      axis_index (int): Postion in the target shape that the array
        corresponds to (e.g. if array corresponds to lat in (time, depth
        lat, lon) array then index = 2
      shape (tuple): shape to broadcast to

    """

    dim = axis_index - 1
    while dim >= 0:
        array = array[numpy.newaxis, ...]
        array = numpy.repeat(array, shape[dim], axis=0)
        dim = dim - 1

    dim = axis_index + 1
    while dim < len(shape):
        array = array[..., numpy.newaxis]
        array = numpy.repeat(array, shape[dim], axis=-1)
        dim = dim + 1

    return array

def calc_significance(data_subset, data_all, standard_name):
    """Perform significance test.

    One sample t-test, with sample size adjusted for autocorrelation.

    Reference:
      Zieba (2010). doi:10.2478/v10178-010-0001-0

    """

    from statsmodels.tsa.stattools import acf

    # Data must be three dimensional, with time first
    assert len(data_subset.shape) == 3, "Input data must be 3 dimensional"

    # Define autocorrelation function
    n = data_subset.shape[0]
    autocorr_func = numpy.apply_along_axis(acf, 0, data_subset, nlags=n - 2)

    # Calculate effective sample size (formula from Zieba2010, eq 12)
    k = numpy.arange(1, n - 1)

    r_k_sum = ((n - k[:, None, None]) / float(n)) * autocorr_func[1:]
    n_eff = float(n) / (1 + 2 * r_k_sum.sum(axis=0))

    # Calculate significance
    var_x = data_subset.var(axis=0) / n_eff
    tvals = (data_subset.mean(axis=0) - data_all.mean(axis=0)) / numpy.sqrt(var_x)
    pvals = stats.t.sf(numpy.abs(tvals), n - 1) * 2  # two-sided pvalue = Prob(abs(t)>tt)

    notes = "One sample t-test, with sample size adjusted for autocorrelation (Zieba2010, eq 12)"
    pval_atts = {'standard_name': standard_name,
                 'long_name': standard_name,
                 'units': ' ',
                 'notes': notes,}

    return pvals, pval_atts

def coordinate_pairs(lat_axis, lon_axis):
    """Take the latitude and longitude values from given grid axes
    and produce a flattened lat and lon array, with element-wise pairs
    corresponding to every grid point."""

    lon_mesh, lat_mesh = numpy.meshgrid(lon_axis, lat_axis)  # This is the correct order

    return lat_mesh.flatten(), lon_mesh.flatten()

def dict_filter(indict, key_list):
    """Filter dictionary according to specified keys."""

    return dict((key, value) for key, value in indict.iteritems() if key in key_list)

def find_duplicates(inlist):
    """Return list of duplicates in a list."""

    D = defaultdict(list)
    for i,item in enumerate(mylist):
        D[item].append(i)
    D = {k:v for k,v in D.items() if len(v)>1}

    return D

def find_nearest(array, value):
    """Find the closest array item to value."""

    idx = (numpy.abs(numpy.array(array) - value)).argmin()
    return array[idx]

def fix_label(label):
    """Fix axis label taken from the command line."""

    replace_dict = {'_': ' ',
                    'degE': '$^{\circ}$E',
                    'ms-1': '$m s^{-1}$',
                    'm.s-1': '$m s^{-1}$',
                    '1000000 m2.s-1': '$10^6$m$^2$s$^{-1}$'
                   }

    for value, replacement in replace_dict.iteritems():
        label = label.replace(value, replacement)

    return label

def get_threshold(data, threshold_str, axis=None):
    """Turn the user input threshold into a numeric threshold."""

    if 'pct' in threshold_str:
        value = float(re.sub('pct', '', threshold_str))
        threshold_float = numpy.percentile(data, value, axis=axis)
    else:
        threshold_float = float(threshold_str)

    return threshold_float

def hi_lo(data_series, current_max, current_min):
    """Determine the new highest and lowest value."""

    try:
        highest = numpy.max(data_series)
    except:
        highest = max(data_series)

    if highest > current_max:
        new_max = highest
    else:
        new_max = current_max

    try:
        lowest = numpy.min(data_series)
    except:
        lowest = min(data_series)

    if lowest < current_min:
        new_min = lowest
    else:
        new_min = current_min

    return new_max, new_min

def list_kwargs(func):
    """List keyword arguments of a function."""

    details = inspect.getargspec(func)
    nopt = len(details.defaults)

    return details.args[-nopt:]

def match_dates(datetimes, datetime_axis):
    """Take list of datetimes and match with the corresponding datetimes in a time axis.

    Args:
      datetimes (list/tuple)
      datetime_axis (list/tuple)

    """

    dates = map(split_dt, datetimes)
    date_axis = map(split_dt, datetime_axis[:])

    match_datetimes = []
    miss_datetimes = []

    for i in range(0, len(datetime_axis)):
        if date_axis[i] in dates:
            match_datetimes.append(datetime_axis[i])
        else:
            miss_datetimes.append(datetime_axis[i])

    return match_datetimes, miss_datetimes

def split_dt(dt):
    """Split a numpy.datetime64 value so as to just keep the date part."""

    return str(dt).split('T')[0]

def single2list(item, numpy_array=False):
    """Check if item is a list, then convert if not."""

    if type(item) == list or type(item) == tuple or type(item) == numpy.ndarray:
        output = item
    elif type(item) == str:
        output = [item,]
    else:
        try:
            test = len(item)
        except TypeError:
            output = [item,]

    if numpy_array and not isinstance(output, numpy.ndarray):
        return numpy.array(output)
    else:
        return output

def units_info(units):
    """Make the units taken from a file LaTeX math compliant.

    This function particularly deals with powers:
      e.g. 10^22 J
    """

    index = units.find('^')
    units = units[:index + 1] + '{' + units[index + 1:]

    index = units.find('J')
    units = units[:index - 1] + '}' + units[index - 1:]

    tex_units = '$'+units+'$'
    exponent = tex_units.split('}')[0].split('{')[1]

    return tex_units, exponent

<commit_msg>Generalised broadcasting to multi-dimensional arrays<commit_after>"""Collection of convenient functions that will work with my anaconda or uvcdat install.

Functions:
  adjust_lon_range   -- Express longitude values in desired 360 degree interval
  apply_lon_filter   -- Set values outside of specified longitude range to zero
  broadcast_array    -- Broadcast an array to a target shape
  calc_significance  -- Perform significance test
  coordinate_paris   -- Generate lat/lon pairs
  dict_filter        -- Filter dictionary according to specified keys
  find_nearest       -- Find the closest array item to value
  find_duplicates    -- Return list of duplicates in a list
  fix_label          -- Fix formatting of an axis label taken from the command line
  get_threshold      -- Turn the user input threshold into a numeric threshold
  hi_lo              -- Determine the new highest and lowest value.
  list_kwargs        -- List keyword arguments of a function
  match_dates        -- Take list of dates and match with the corresponding times
                        in a detailed time axis
  single2list        -- Check if item is a list, then convert if not
  units_info         -- Make the units taken from a file LaTeX math compliant

"""

import numpy
from scipy import stats
import pdb, re
import inspect

def adjust_lon_range(lons, radians=True, start=0.0):
    """Express longitude values in a 360 degree (or 2*pi radians) interval.

    Args:
      lons (list/tuple): Longitude axis values (monotonically increasing)
      radians (bool): Specify whether input data are in radians (True) or
        degrees (False). Output will be the same units.
      start (float, optional): Start value for the output interval (add 360 degrees or 2*pi
        radians to get the end point)

    """

    lons = single2list(lons, numpy_array=True)

    interval360 = 2.0*numpy.pi if radians else 360.0
    end = start + interval360

    less_than_start = numpy.ones([len(lons),])
    while numpy.sum(less_than_start) != 0:
        lons = numpy.where(lons < start, lons + interval360, lons)
        less_than_start = lons < start

    more_than_end = numpy.ones([len(lons),])
    while numpy.sum(more_than_end) != 0:
        lons = numpy.where(lons >= end, lons - interval360, lons)
        more_than_end = lons >= end

    return lons

def apply_lon_filter(data, lon_bounds):
    """Set values outside of specified longitude range to zero.

    Args:
      data (numpy.ndarray): Array of longitude values.
      lon_bounds (list/tuple): Specified longitude range (min, max)

    """

    # Convert to common bounds (0, 360)
    lon_min = adjust_lon_range(lon_bounds[0], radians=False, start=0.0)
    lon_max = adjust_lon_range(lon_bounds[1], radians=False, start=0.0)
    lon_axis = adjust_lon_range(data.getLongitude()[:], radians=False, start=0.0)

    # Make required values zero
    ntimes, nlats, nlons = data.shape
    lon_axis_tiled = numpy.tile(lon_axis, (ntimes, nlats, 1))

    new_data = numpy.where(lon_axis_tiled < lon_min, 0.0, data)

    return numpy.where(lon_axis_tiled > lon_max, 0.0, new_data)

def broadcast_array(array, axis_index, shape):
    """Broadcast an array to a target shape.

    Args:
      array (numpy.ndarray): One dimensional array
      axis_index (int or tuple): Postion in the target shape that the
        axis/axes of the array corresponds to
          e.g. if array corresponds to (lat, lon) in (time, depth lat, lon)
          then axis_index = [2, 3]
          e.g. if array corresponds to (lat) in (time, depth lat, lon)
          then axis_index = 2
      shape (tuple): shape to broadcast to

    For a one dimensional array, make start_axis_index = end_axis_index

    """

    if type(axis_index) in [float, int]:
        start_axis_index = end_axis_index = axis_index
    else:
        assert len(axis_index) == 2
        start_axis_index, end_axis_index = axis_index

    dim = start_axis_index - 1
    while dim >= 0:
        array = array[numpy.newaxis, ...]
        array = numpy.repeat(array, shape[dim], axis=0)
        dim = dim - 1

    dim = end_axis_index + 1
    while dim < len(shape):
        array = array[..., numpy.newaxis]
        array = numpy.repeat(array, shape[dim], axis=-1)
        dim = dim + 1

    return array

def calc_significance(data_subset, data_all, standard_name):
    """Perform significance test.

    One sample t-test, with sample size adjusted for autocorrelation.

    Reference:
      Zieba (2010). doi:10.2478/v10178-010-0001-0

    """

    from statsmodels.tsa.stattools import acf

    # Data must be three dimensional, with time first
    assert len(data_subset.shape) == 3, "Input data must be 3 dimensional"

    # Define autocorrelation function
    n = data_subset.shape[0]
    autocorr_func = numpy.apply_along_axis(acf, 0, data_subset, nlags=n - 2)

    # Calculate effective sample size (formula from Zieba2010, eq 12)
    k = numpy.arange(1, n - 1)

    r_k_sum = ((n - k[:, None, None]) / float(n)) * autocorr_func[1:]
    n_eff = float(n) / (1 + 2 * r_k_sum.sum(axis=0))

    # Calculate significance
    var_x = data_subset.var(axis=0) / n_eff
    tvals = (data_subset.mean(axis=0) - data_all.mean(axis=0)) / numpy.sqrt(var_x)
    pvals = stats.t.sf(numpy.abs(tvals), n - 1) * 2  # two-sided pvalue = Prob(abs(t)>tt)

    notes = "One sample t-test, with sample size adjusted for autocorrelation (Zieba2010, eq 12)"
    pval_atts = {'standard_name': standard_name,
                 'long_name': standard_name,
                 'units': ' ',
                 'notes': notes,}

    return pvals, pval_atts

def coordinate_pairs(lat_axis, lon_axis):
    """Take the latitude and longitude values from given grid axes
    and produce a flattened lat and lon array, with element-wise pairs
    corresponding to every grid point."""

    lon_mesh, lat_mesh = numpy.meshgrid(lon_axis, lat_axis)  # This is the correct order

    return lat_mesh.flatten(), lon_mesh.flatten()

def dict_filter(indict, key_list):
    """Filter dictionary according to specified keys."""

    return dict((key, value) for key, value in indict.iteritems() if key in key_list)

def find_duplicates(inlist):
    """Return list of duplicates in a list."""

    D = defaultdict(list)
    for i,item in enumerate(mylist):
        D[item].append(i)
    D = {k:v for k,v in D.items() if len(v)>1}

    return D

def find_nearest(array, value):
    """Find the closest array item to value."""

    idx = (numpy.abs(numpy.array(array) - value)).argmin()
    return array[idx]

def fix_label(label):
    """Fix axis label taken from the command line."""

    replace_dict = {'_': ' ',
                    'degE': '$^{\circ}$E',
                    'ms-1': '$m s^{-1}$',
                    'm.s-1': '$m s^{-1}$',
                    '1000000 m2.s-1': '$10^6$m$^2$s$^{-1}$'
                   }

    for value, replacement in replace_dict.iteritems():
        label = label.replace(value, replacement)

    return label

def get_threshold(data, threshold_str, axis=None):
    """Turn the user input threshold into a numeric threshold."""

    if 'pct' in threshold_str:
        value = float(re.sub('pct', '', threshold_str))
        threshold_float = numpy.percentile(data, value, axis=axis)
    else:
        threshold_float = float(threshold_str)

    return threshold_float

def hi_lo(data_series, current_max, current_min):
    """Determine the new highest and lowest value."""

    try:
        highest = numpy.max(data_series)
    except:
        highest = max(data_series)

    if highest > current_max:
        new_max = highest
    else:
        new_max = current_max

    try:
        lowest = numpy.min(data_series)
    except:
        lowest = min(data_series)

    if lowest < current_min:
        new_min = lowest
    else:
        new_min = current_min

    return new_max, new_min

def list_kwargs(func):
    """List keyword arguments of a function."""

    details = inspect.getargspec(func)
    nopt = len(details.defaults)

    return details.args[-nopt:]

def match_dates(datetimes, datetime_axis):
    """Take list of datetimes and match with the corresponding datetimes in a time axis.

    Args:
      datetimes (list/tuple)
      datetime_axis (list/tuple)

    """

    dates = map(split_dt, datetimes)
    date_axis = map(split_dt, datetime_axis[:])

    match_datetimes = []
    miss_datetimes = []

    for i in range(0, len(datetime_axis)):
        if date_axis[i] in dates:
            match_datetimes.append(datetime_axis[i])
        else:
            miss_datetimes.append(datetime_axis[i])

    return match_datetimes, miss_datetimes

def split_dt(dt):
    """Split a numpy.datetime64 value so as to just keep the date part."""

    return str(dt).split('T')[0]

def single2list(item, numpy_array=False):
    """Check if item is a list, then convert if not."""

    if type(item) == list or type(item) == tuple or type(item) == numpy.ndarray:
        output = item
    elif type(item) == str:
        output = [item,]
    else:
        try:
            test = len(item)
        except TypeError:
            output = [item,]

    if numpy_array and not isinstance(output, numpy.ndarray):
        return numpy.array(output)
    else:
        return output

def units_info(units):
    """Make the units taken from a file LaTeX math compliant.

    This function particularly deals with powers:
      e.g. 10^22 J
    """

    index = units.find('^')
    units = units[:index + 1] + '{' + units[index + 1:]

    index = units.find('J')
    units = units[:index - 1] + '}' + units[index - 1:]

    tex_units = '$'+units+'$'
    exponent = tex_units.split('}')[0].split('{')[1]

    return tex_units, exponent

<|endoftext|>


<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 uralbash <root@uralbash.ru>
#
# Distributed under terms of the MIT license.

"""
SQLAlchemy helpers
"""
import ast
import inspect
import json
import os
import uuid
from datetime import datetime

import sqlalchemy

class TableProperty(object):
    def __init__(self, func):
        self.func = func

    def __get__(self, inst, cls):
        return self.func(cls.__table__)

def get_attrname_by_colname(instance, name):
    """ Get value from SQLAlchemy instance by column name

    :Parameters:
        - `instance`: SQLAlchemy model instance.
        - `name`:  Column name

    :Examples:

    >>> from sqlalchemy import Column, Integer
    >>> from sqlalchemy.ext.declarative import declarative_base
    >>> Base = declarative_base()
    >>> class MPTTPages(Base):
    ...     __tablename__ = "mptt_pages"
    ...     id = Column(Integer, primary_key=True)
    ...     left = Column("lft", Integer, nullable=False)
    >>> get_attrname_by_colname(MPTTPages(), 'lft')
    'left'
    """
    for attr, column in list(sqlalchemy.inspect(instance.__class__).c.items()):
        if column.name == name:
            return attr

def set_instance_name(instance, cols):
    """ It gives you opportunity to get instance attr by column name
        Like this: row.__getattribute__(col.instance_name)

    :Parameters:
        - `instance`: SQLAlchemy model instance.
        - `cols`:  list of SQLAlchemy column

    :Examples:

    >>> from sqlalchemy import Column, Integer
    >>> from sqlalchemy.ext.declarative import declarative_base
    >>> Base = declarative_base()
    >>> class MPTTPages(Base):
    ...     __tablename__ = "mptt_pages"
    ...     id = Column(Integer, primary_key=True)
    ...     left = Column("lft", Integer, nullable=False)
    >>> columns = set_instance_name(MPTTPages(), MPTTPages.__table__.c).items()
    >>> columns[1][1].instance_name
    'left'

    """
    for col in cols:
        if isinstance(col, sqlalchemy.Column):
            setattr(col, "instance_name", get_attrname_by_colname(instance,
                                                                  col.name))
    return cols

def get_pk(obj):
    """ Return primary key name by model class or instance.

    :Parameters:
        - `obj`: SQLAlchemy model instance or class.

    :Examples:

    >>> from sqlalchemy import Column, Integer
    >>> from sqlalchemy.ext.declarative import declarative_base
    >>> Base = declarative_base()
    >>> class User(Base):
    ...     __tablename__ = 'users'
    ...     id = Column(Integer, primary_key=True)
    >>> get_pk(User())
    (Column('id', Integer(), table=<users>, primary_key=True, nullable=False),)
    >>> get_pk(User)
    (Column('id', Integer(), table=<users>, primary_key=True, nullable=False),)
    """
    if inspect.isclass(obj):
        pk_list = sqlalchemy.inspect(obj).primary_key
    else:
        pk_list = obj.__mapper__.primary_key
    return pk_list

def pk_to_list(obj, as_json=False):
    pk_list = []
    primary_keys = get_pk(obj)
    for item in primary_keys:
        item_name = get_attrname_by_colname(obj, item.name)
        pk_list.append(item_name)
        pk_list.append(getattr(obj, item_name))
    if as_json:
        return json.dumps(pk_list)
    return pk_list

def delete_fileobj(table, obj, key):
    """ Delete atached file.
    """
    if hasattr(table, '__table__'):
        table = table.__table__
    abspath = table.columns[key].type.abspath
    path = os.path.join(abspath, os.path.basename(getattr(obj, key)))
    if not obj or not os.path.isfile(path):
        return
    os.remove(path)

def store_file(request, key, path):
    """ Load atached file.
    """
    # ``filename`` contains the name of the file in string format.
    #
    # WARNING: this example does not deal with the fact that IE sends an
    # absolute file *path* as the filename.  This example is naive; it
    # trusts user input.
    filename = request[key].filename

    # ``input_file`` contains the actual file data which needs to be
    # stored somewhere.

    input_file = request[key].file

    # Using the filename like this without cleaning it is very
    # insecure so please keep that in mind when writing your own
    # file handlingself.
    file_path = os.path.join(path, filename)
    output_file = open(file_path, 'wb')

    # Finally write the data to the output file
    input_file.seek(0)
    while 1:
        data = input_file.read(2 << 16)
        if not data:
            break
        output_file.write(bytearray(data))
    output_file.close()

class ObjPreprocessing(object):
    def __init__(self, obj):
        self.obj = obj
        self.table = obj.__table__

    def delete(self):
        # XXX: think about same update
        for col in self.table.columns:
            if col.type.__class__.__name__ == 'FileStore':
                if not getattr(self.obj, col.name):
                    continue  # pragma: no cover
                delete_fileobj(self.table, self.obj, col.name)
        return self.obj

class RequestPreprocessing(object):
    def __init__(self, request):
        self.request = request
        self.types_list = {'Boolean': self._check_boolean,
                           'FileStore': self._check_filestore,
                           'HSTORE': self._check_hstore,
                           'Date': self._check_date,
                           'DateTime': self._check_date,
                           'BYTEA': self._check_bytea,
                           'LargeBinary': self._check_bytea,
                           }

    def _check_boolean(self, value):
        value = False if value == '0' else True
        value = True if value else False
        return value

    def _check_bytea(self, value):
        return bytearray(value, 'utf-8')

    def _check_filestore(self, value):
        fileobj = value
        if hasattr(fileobj, 'filename'):
            extension = fileobj.filename.split(".")[-1]
            fileobj.filename = str(uuid.uuid4()) + "." + extension
            abspath = self.column.type.abspath
            store_file(self.request, self.key, abspath)
            value = fileobj.filename
        return value

    def _check_hstore(self, value):
        try:
            return ast.literal_eval(str(value))
        except:
            raise TypeError("HSTORE: does't suppot '%s' format. %s" %
                            (value, 'Valid example: {"foo": "bar", u"baz": u"biz"}'))

    def _check_date(self, value):
        return datetime.strptime(value, '%Y-%m-%d')

    def check_type(self, table, key):
        self.key = key
        self.column = table.__table__.columns[key]
        column_type = self.column.type.__class__.__name__
        value = self.request[key]
        if type(value) in (list, tuple):
            value = value[0]

        if not value and not hasattr(value, 'filename'):
            return None

        if column_type in list(self.types_list.keys()):
            check = self.types_list[column_type]
            return check(value)

        return value
<commit_msg>fix tests<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 uralbash <root@uralbash.ru>
#
# Distributed under terms of the MIT license.

"""
SQLAlchemy helpers
"""
import ast
import inspect
import json
import os
import uuid
from datetime import datetime

import sqlalchemy

class TableProperty(object):
    def __init__(self, func):
        self.func = func

    def __get__(self, inst, cls):
        return self.func(cls.__table__)

def get_attrname_by_colname(instance, name):
    """ Get value from SQLAlchemy instance by column name

    :Parameters:
        - `instance`: SQLAlchemy model instance.
        - `name`:  Column name

    :Examples:

    >>> from sqlalchemy import Column, Integer
    >>> from sqlalchemy.ext.declarative import declarative_base
    >>> Base = declarative_base()
    >>> class MPTTPages(Base):
    ...     __tablename__ = "mptt_pages"
    ...     id = Column(Integer, primary_key=True)
    ...     left = Column("lft", Integer, nullable=False)
    >>> get_attrname_by_colname(MPTTPages(), 'lft')
    'left'
    """
    for attr, column in list(sqlalchemy.inspect(instance.__class__).c.items()):
        if column.name == name:
            return attr

def set_instance_name(instance, cols):
    """ It gives you opportunity to get instance attr by column name
        Like this: row.__getattribute__(col.instance_name)

    :Parameters:
        - `instance`: SQLAlchemy model instance.
        - `cols`:  list of SQLAlchemy column

    :Examples:

    >>> from sqlalchemy import Column, Integer
    >>> from sqlalchemy.ext.declarative import declarative_base
    >>> Base = declarative_base()
    >>> class MPTTPages(Base):
    ...     __tablename__ = "mptt_pages"
    ...     id = Column(Integer, primary_key=True)
    ...     left = Column("lft", Integer, nullable=False)
    >>> columns = set_instance_name(MPTTPages(), MPTTPages.__table__.c).items()
    >>> columns[1][1].instance_name
    'left'

    """
    for col in cols:
        if isinstance(col, sqlalchemy.Column):
            setattr(col, "instance_name", get_attrname_by_colname(instance,
                                                                  col.name))
    return cols

def get_pk(obj):
    """ Return primary key name by model class or instance.

    :Parameters:
        - `obj`: SQLAlchemy model instance or class.

    :Examples:

    >>> from sqlalchemy import Column, Integer
    >>> from sqlalchemy.ext.declarative import declarative_base
    >>> Base = declarative_base()
    >>> class User(Base):
    ...     __tablename__ = 'users'
    ...     id = Column(Integer, primary_key=True)
    >>> get_pk(User())
    (Column('id', Integer(), table=<users>, primary_key=True, nullable=False),)
    >>> get_pk(User)
    (Column('id', Integer(), table=<users>, primary_key=True, nullable=False),)
    """
    if inspect.isclass(obj):
        pk_list = sqlalchemy.inspect(obj).primary_key
    else:
        pk_list = obj.__mapper__.primary_key
    return pk_list

def pk_to_list(obj, as_json=False):
    pk_list = []
    primary_keys = get_pk(obj)
    for item in primary_keys:
        item_name = get_attrname_by_colname(obj, item.name)
        pk_list.append(item_name)
        pk_list.append(getattr(obj, item_name))
    if as_json:
        return json.dumps(pk_list)
    return pk_list

def delete_fileobj(table, obj, key):
    """ Delete atached file.
    """
    if hasattr(table, '__table__'):
        table = table.__table__
    abspath = table.columns[key].type.abspath
    path = os.path.join(abspath, os.path.basename(getattr(obj, key)))
    if not obj or not os.path.isfile(path):
        return
    os.remove(path)

def store_file(request, key, path):
    """ Load atached file.
    """
    # ``filename`` contains the name of the file in string format.
    #
    # WARNING: this example does not deal with the fact that IE sends an
    # absolute file *path* as the filename.  This example is naive; it
    # trusts user input.
    obj = request[key]
    if isinstance(obj, (list, tuple)):
        obj = obj[0]
    filename = obj.filename

    # ``input_file`` contains the actual file data which needs to be
    # stored somewhere.

    input_file = obj.file

    # Using the filename like this without cleaning it is very
    # insecure so please keep that in mind when writing your own
    # file handlingself.
    file_path = os.path.join(path, filename)
    output_file = open(file_path, 'wb')

    # Finally write the data to the output file
    input_file.seek(0)
    while 1:
        data = input_file.read(2 << 16)
        if not data:
            break
        try:
            output_file.write(bytearray(data, 'utf-8'))
        except UnicodeDecodeError:
            output_file.write(bytearray(data))
    output_file.close()

class ObjPreprocessing(object):
    def __init__(self, obj):
        self.obj = obj
        self.table = obj.__table__

    def delete(self):
        # XXX: think about same update
        for col in self.table.columns:
            if col.type.__class__.__name__ == 'FileStore':
                if not getattr(self.obj, col.name):
                    continue  # pragma: no cover
                delete_fileobj(self.table, self.obj, col.name)
        return self.obj

class RequestPreprocessing(object):
    def __init__(self, request):
        self.request = request
        self.types_list = {'Boolean': self._check_boolean,
                           'FileStore': self._check_filestore,
                           'HSTORE': self._check_hstore,
                           'Date': self._check_date,
                           'DateTime': self._check_date,
                           'BYTEA': self._check_bytea,
                           'LargeBinary': self._check_bytea,
                           }

    def _check_boolean(self, value):
        value = False if value == '0' else True
        value = True if value else False
        return value

    def _check_bytea(self, value):
        return bytearray(value, 'utf-8')

    def _check_filestore(self, value):
        fileobj = value
        if hasattr(fileobj, 'filename'):
            extension = fileobj.filename.split(".")[-1]
            fileobj.filename = str(uuid.uuid4()) + "." + extension
            abspath = self.column.type.abspath
            store_file(self.request, self.key, abspath)
            value = fileobj.filename
        return value

    def _check_hstore(self, value):
        try:
            return ast.literal_eval(str(value))
        except:
            raise TypeError("HSTORE: does't suppot '%s' format. %s" %
                            (value, 'Valid example: {"foo": "bar", u"baz": u"biz"}'))

    def _check_date(self, value):
        return datetime.strptime(value, '%Y-%m-%d')

    def check_type(self, table, key):
        self.key = key
        self.column = table.__table__.columns[key]
        column_type = self.column.type.__class__.__name__
        value = self.request[key]
        if type(value) in (list, tuple):
            value = value[0]

        if not value and not hasattr(value, 'filename'):
            return None

        if column_type in list(self.types_list.keys()):
            check = self.types_list[column_type]
            return check(value)

        return value
<|endoftext|>


<commit_before>from __future__ import print_function
# Time:  O(n)
# Space: O(1)
#
# The API: int read4(char *buf) reads 4 characters at a time from a file.
#
# The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
#
# By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
#
# Note:
# The read function will only be called once for each test case.
#

# The read4 API is already defined for you.
# @param buf, a list of characters
# @return an integer
def read4(buf):
    global file_content
    i = 0
    while i < len(file_content) and i < 4:
        buf[i] = file_content[i]
        i += 1

    if len(file_content) > 4:
        file_content = file_content[4:]
    else:
        file_content = ""
    return i

class Solution(object):
    def read(self, buf, n):
        """
        :type buf: Destination buffer (List[str])
        :type n: Maximum number of characters to read (int)
        :rtype: The number of characters read (int)
        """
        read_bytes = 0
        buffer = [''] * 4
        for i in xrange(n / 4 + 1):
            size = read4(buffer)
            if size:
                buf[read_bytes:read_bytes+size] = buffer
                read_bytes += size
            else:
                break
        return min(read_bytes, n)

if __name__ == "__main__":
    global file_content
    buf = ['' for _ in xrange(100)]
    file_content = "a"
    print(buf[:Solution().read(buf, 9)])
    file_content = "abcdefghijklmnop"
    print(buf[:Solution().read(buf, 9)])
<commit_msg>Update read-n-characters-given-read4.py<commit_after>from __future__ import print_function
# Time:  O(n)
# Space: O(1)
#
# The API: int read4(char *buf) reads 4 characters at a time from a file.
#
# The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
#
# By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
#
# Note:
# The read function will only be called once for each test case.
#

# The read4 API is already defined for you.
# @param buf, a list of characters
# @return an integer
def read4(buf):
    global file_content
    i = 0
    while i < len(file_content) and i < 4:
        buf[i] = file_content[i]
        i += 1

    if len(file_content) > 4:
        file_content = file_content[4:]
    else:
        file_content = ""
    return i

class Solution(object):
    def read(self, buf, n):
        """
        :type buf: Destination buffer (List[str])
        :type n: Maximum number of characters to read (int)
        :rtype: The number of characters read (int)
        """
        read_bytes = 0
        buffer = [''] * 4
        globalN = n
        for i in xrange(n / 4 + 1):
            size = read4(buffer)
            if size:
                toRead = min(globalN,size)
                buf[read_bytes:read_bytes+toRead] = buffer
                read_bytes += toRead
                globalN -= toRead
            else:
                break
        return read_bytes

if __name__ == "__main__":
    global file_content
    buf = ['' for _ in xrange(100)]
    file_content = "a"
    print(buf[:Solution().read(buf, 9)])
    file_content = "abcdefghijklmnop"
    print(buf[:Solution().read(buf, 9)])
<|endoftext|>


<commit_before>import os
import ycm_core
from clang_helpers import PrepareClangFlags

# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
'-Wextra',
'-Werror',
'-fexceptions',
'-DUSE_CLANG_COMPLETER',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
#'-std=c++03',
'-std=c99'
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c',
'-isystem',
# This path will only work on OS X, but extra paths that don't exist are not
# harmful
'/System/Library/Frameworks/Python.framework/Headers',
'-isystem',
'../llvm/include',
'-isystem',
'../llvm/tools/clang/include',
'-I',
'.',
'-I',
'/usr/include',
'-I',
'./ClangCompleter',
'-isystem',
'./tests/gmock/gtest',
'-isystem',
'./tests/gmock/gtest/include',
'-isystem',
'./tests/gmock',
'-isystem',
'./tests/gmock/include'
]

# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''

if compilation_database_folder:
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )

def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return flags
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags

def FlagsForFile( filename ):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = database.GetCompilationInfoForFile( filename )
    final_flags = PrepareClangFlags(
        MakeRelativePathsInFlagsAbsolute(
            compilation_info.compiler_flags_,
            compilation_info.compiler_working_dir_ ),
        filename )
  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return {
    'flags': final_flags,
    'do_cache': True
  }
<commit_msg>include src in include search<commit_after>import os
import ycm_core
from clang_helpers import PrepareClangFlags

# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
'-Wextra',
'-Werror',
'-fexceptions',
'-DUSE_CLANG_COMPLETER',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
#'-std=c++03',
'-std=c99'
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c',
'-isystem',
# This path will only work on OS X, but extra paths that don't exist are not
# harmful
'/System/Library/Frameworks/Python.framework/Headers',
'-isystem',
'../llvm/include',
'-isystem',
'../llvm/tools/clang/include',
'-I',
'.',
'-I',
'./src',
'-I',
'./ClangCompleter',
'-isystem',
'./tests/gmock/gtest',
'-isystem',
'./tests/gmock/gtest/include',
'-isystem',
'./tests/gmock',
'-isystem',
'./tests/gmock/include'
]

# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''

if compilation_database_folder:
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )

def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return flags
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags

def FlagsForFile( filename ):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = database.GetCompilationInfoForFile( filename )
    final_flags = PrepareClangFlags(
        MakeRelativePathsInFlagsAbsolute(
            compilation_info.compiler_flags_,
            compilation_info.compiler_working_dir_ ),
        filename )
  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return {
    'flags': final_flags,
    'do_cache': True
  }
<|endoftext|>