<commit_before>######################################################################
#  This file should be kept compatible with Python 2.3, see PEP 291. #
######################################################################
import sys, os

# find_library(name) returns the pathname of a library, or None.
if os.name == "nt":
    def find_library(name):
        # See MSDN for the REAL search order.
        for directory in os.environ['PATH'].split(os.pathsep):
            fname = os.path.join(directory, name)
            if os.path.exists(fname):
                return fname
            if fname.lower().endswith(".dll"):
                continue
            fname = fname + ".dll"
            if os.path.exists(fname):
                return fname
        return None

if os.name == "ce":
    # search path according to MSDN:
    # - absolute path specified by filename
    # - The .exe launch directory
    # - the Windows directory
    # - ROM dll files (where are they?)
    # - OEM specified search path: HKLM\Loader\SystemPath
    def find_library(name):
        return name

if os.name == "posix" and sys.platform == "darwin":
    from ctypes.macholib.dyld import dyld_find as _dyld_find
    def find_library(name):
        possible = ['lib%s.dylib' % name,
                    '%s.dylib' % name,
                    '%s.framework/%s' % (name, name)]
        for name in possible:
            try:
                return _dyld_find(name)
            except ValueError:
                continue
        return None

elif os.name == "posix":
    # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
    import re, tempfile, errno

    def _findLib_gcc(name):
        expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
        fdout, ccout = tempfile.mkstemp()
        os.close(fdout)
        cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \
              '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
        try:
            f = os.popen(cmd)
            trace = f.read()
            f.close()
        finally:
            try:
                os.unlink(ccout)
            except OSError, e:
                if e.errno != errno.ENOENT:
                    raise
        res = re.search(expr, trace)
        if not res:
            return None
        return res.group(0)

    def _get_soname(f):
        # assuming GNU binutils / ELF
        if not f:
            return None
        cmd = "objdump -p -j .dynamic 2>/dev/null " + f
        res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
        if not res:
            return None
        return res.group(1)

    if (sys.platform.startswith("freebsd")
        or sys.platform.startswith("openbsd")
        or sys.platform.startswith("dragonfly")):

        def _num_version(libname):
            # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
            parts = libname.split(".")
            nums = []
            try:
                while parts:
                    nums.insert(0, int(parts.pop()))
            except ValueError:
                pass
            return nums or [ sys.maxint ]

        def find_library(name):
            ename = re.escape(name)
            expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
            res = re.findall(expr,
                             os.popen('/sbin/ldconfig -r 2>/dev/null').read())
            if not res:
                return _get_soname(_findLib_gcc(name))
            res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y)))
            return res[-1]

    else:

        def _findLib_ldconfig(name):
            # XXX assuming GLIBC's ldconfig (with option -p)
            expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
            res = re.search(expr,
                            os.popen('/sbin/ldconfig -p 2>/dev/null').read())
            if not res:
                # Hm, this works only for libs needed by the python executable.
                cmd = 'ldd %s 2>/dev/null' % sys.executable
                res = re.search(expr, os.popen(cmd).read())
                if not res:
                    return None
            return res.group(0)

        def find_library(name):
            return _get_soname(_findLib_ldconfig(name) or _findLib_gcc(name))

################################################################
# test code

def test():
    from ctypes import cdll
    if os.name == "nt":
        print cdll.msvcrt
        print cdll.load("msvcrt")
        print find_library("msvcrt")

    if os.name == "posix":
        # find and load_version
        print find_library("m")
        print find_library("c")
        print find_library("bz2")

        # getattr
##        print cdll.m
##        print cdll.bz2

        # load
        if sys.platform == "darwin":
            print cdll.LoadLibrary("libm.dylib")
            print cdll.LoadLibrary("libcrypto.dylib")
            print cdll.LoadLibrary("libSystem.dylib")
            print cdll.LoadLibrary("System.framework/System")
        else:
            print cdll.LoadLibrary("libm.so")
            print cdll.LoadLibrary("libcrypt.so")
            print find_library("crypt")

if __name__ == "__main__":
    test()
<commit_msg>ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris. Fixes issue #1777530; will backport to release25-maint.<commit_after>######################################################################
#  This file should be kept compatible with Python 2.3, see PEP 291. #
######################################################################
import sys, os

# find_library(name) returns the pathname of a library, or None.
if os.name == "nt":
    def find_library(name):
        # See MSDN for the REAL search order.
        for directory in os.environ['PATH'].split(os.pathsep):
            fname = os.path.join(directory, name)
            if os.path.exists(fname):
                return fname
            if fname.lower().endswith(".dll"):
                continue
            fname = fname + ".dll"
            if os.path.exists(fname):
                return fname
        return None

if os.name == "ce":
    # search path according to MSDN:
    # - absolute path specified by filename
    # - The .exe launch directory
    # - the Windows directory
    # - ROM dll files (where are they?)
    # - OEM specified search path: HKLM\Loader\SystemPath
    def find_library(name):
        return name

if os.name == "posix" and sys.platform == "darwin":
    from ctypes.macholib.dyld import dyld_find as _dyld_find
    def find_library(name):
        possible = ['lib%s.dylib' % name,
                    '%s.dylib' % name,
                    '%s.framework/%s' % (name, name)]
        for name in possible:
            try:
                return _dyld_find(name)
            except ValueError:
                continue
        return None

elif os.name == "posix":
    # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
    import re, tempfile, errno

    def _findLib_gcc(name):
        expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
        fdout, ccout = tempfile.mkstemp()
        os.close(fdout)
        cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \
              '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
        try:
            f = os.popen(cmd)
            trace = f.read()
            f.close()
        finally:
            try:
                os.unlink(ccout)
            except OSError, e:
                if e.errno != errno.ENOENT:
                    raise
        res = re.search(expr, trace)
        if not res:
            return None
        return res.group(0)

    if sys.platform == "sunos5":
        # use /usr/ccs/bin/dump on solaris
        def _get_soname(f):
            if not f:
                return None
            cmd = "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f
            res = re.search(r'\[.*\]\sSONAME\s+([^\s]+)', os.popen(cmd).read())
            if not res:
                return None
            return res.group(1)
    else:
        def _get_soname(f):
            # assuming GNU binutils / ELF
            if not f:
                return None
            cmd = "objdump -p -j .dynamic 2>/dev/null " + f
            res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
            if not res:
                return None
            return res.group(1)

    if (sys.platform.startswith("freebsd")
        or sys.platform.startswith("openbsd")
        or sys.platform.startswith("dragonfly")):

        def _num_version(libname):
            # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
            parts = libname.split(".")
            nums = []
            try:
                while parts:
                    nums.insert(0, int(parts.pop()))
            except ValueError:
                pass
            return nums or [ sys.maxint ]

        def find_library(name):
            ename = re.escape(name)
            expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
            res = re.findall(expr,
                             os.popen('/sbin/ldconfig -r 2>/dev/null').read())
            if not res:
                return _get_soname(_findLib_gcc(name))
            res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y)))
            return res[-1]

    else:

        def _findLib_ldconfig(name):
            # XXX assuming GLIBC's ldconfig (with option -p)
            expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
            res = re.search(expr,
                            os.popen('/sbin/ldconfig -p 2>/dev/null').read())
            if not res:
                # Hm, this works only for libs needed by the python executable.
                cmd = 'ldd %s 2>/dev/null' % sys.executable
                res = re.search(expr, os.popen(cmd).read())
                if not res:
                    return None
            return res.group(0)

        def find_library(name):
            return _get_soname(_findLib_ldconfig(name) or _findLib_gcc(name))

################################################################
# test code

def test():
    from ctypes import cdll
    if os.name == "nt":
        print cdll.msvcrt
        print cdll.load("msvcrt")
        print find_library("msvcrt")

    if os.name == "posix":
        # find and load_version
        print find_library("m")
        print find_library("c")
        print find_library("bz2")

        # getattr
##        print cdll.m
##        print cdll.bz2

        # load
        if sys.platform == "darwin":
            print cdll.LoadLibrary("libm.dylib")
            print cdll.LoadLibrary("libcrypto.dylib")
            print cdll.LoadLibrary("libSystem.dylib")
            print cdll.LoadLibrary("System.framework/System")
        else:
            print cdll.LoadLibrary("libm.so")
            print cdll.LoadLibrary("libcrypt.so")
            print find_library("crypt")

if __name__ == "__main__":
    test()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from typing import List
import json
import sentencepiece as spm
import numpy as np
from onnxruntime import (
    InferenceSession, SessionOptions, GraphOptimizationLevel
)
from pythainlp.corpus import get_path_folder_corpus

class lst20ner_onnx:
    def __init__(self, providers: List[str] = ['CPUExecutionProvider']) -> None:
        self.model_name = "onnx_lst20ner"
        self.model_version = "1.0"
        self.options = SessionOptions()
        self.options.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL
        self.session = InferenceSession(
            get_path_folder_corpus(
                self.model_name,
                self.model_version,
                "lst20-ner-model.onnx"
            ),
            sess_options=self.options,
            providers=providers
        )
        self.session.disable_fallback()
        self.outputs_name = self.session.get_outputs()[0].name
        self.sp = spm.SentencePieceProcessor(
            model_file=get_path_folder_corpus(
                self.model_name,
                self.model_version,
                "sentencepiece.bpe.model"
            )
        )
        with open(
            get_path_folder_corpus(
                self.model_name,
                self.model_version,
                "config.json"
            ),
            encoding='utf-8-sig'
        ) as fh:
            self._json = json.load(fh)
            self.id2tag = self._json['id2label']

    def build_tokenizer(self, sent):
        _t = [5]+[i+4 for i in self.sp.encode(sent)]+[6]
        model_inputs = {}
        model_inputs["input_ids"] = np.array([_t], dtype=np.int64)
        model_inputs["attention_mask"] = np.array(
            [[1]*len(_t)], dtype=np.int64
        )
        return model_inputs

    def postprocess(self, logits_data):
        logits_t = logits_data[0]
        maxes = np.max(logits_t, axis=-1, keepdims=True)
        shifted_exp = np.exp(logits_t - maxes)
        scores = shifted_exp / shifted_exp.sum(axis=-1, keepdims=True)
        return scores

    def totag(self, post, sent):
        tag = []
        _s = self.sp.EncodeAsPieces(sent)
        for i in range(len(_s)):
            tag.append(
                (
                    _s[i],
                    self.id2tag[
                        str(list(post[i+1]).index(max(list(post[i+1]))))
                    ]
                )
            )
        return tag

    def get_ner(self, text: str):
        self._s=self.build_tokenizer(text)
        logits = self.session.run(
            output_names=[self.outputs_name],
            input_feed=self._s
        )[0]
        return self.totag(self.postprocess(logits), text)
<commit_msg>Clean output from lst20ner_onnx<commit_after># -*- coding: utf-8 -*-
from typing import List
import json
import sentencepiece as spm
import numpy as np
from onnxruntime import (
    InferenceSession, SessionOptions, GraphOptimizationLevel
)
from pythainlp.corpus import get_path_folder_corpus

class lst20ner_onnx:
    def __init__(self, providers: List[str] = ['CPUExecutionProvider']) -> None:
        self.model_name = "onnx_lst20ner"
        self.model_version = "1.0"
        self.options = SessionOptions()
        self.options.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL
        self.session = InferenceSession(
            get_path_folder_corpus(
                self.model_name,
                self.model_version,
                "lst20-ner-model.onnx"
            ),
            sess_options=self.options,
            providers=providers
        )
        self.session.disable_fallback()
        self.outputs_name = self.session.get_outputs()[0].name
        self.sp = spm.SentencePieceProcessor(
            model_file=get_path_folder_corpus(
                self.model_name,
                self.model_version,
                "sentencepiece.bpe.model"
            )
        )
        with open(
            get_path_folder_corpus(
                self.model_name,
                self.model_version,
                "config.json"
            ),
            encoding='utf-8-sig'
        ) as fh:
            self._json = json.load(fh)
            self.id2tag = self._json['id2label']

    def build_tokenizer(self, sent):
        _t = [5]+[i+4 for i in self.sp.encode(sent)]+[6]
        model_inputs = {}
        model_inputs["input_ids"] = np.array([_t], dtype=np.int64)
        model_inputs["attention_mask"] = np.array(
            [[1]*len(_t)], dtype=np.int64
        )
        return model_inputs

    def postprocess(self, logits_data):
        logits_t = logits_data[0]
        maxes = np.max(logits_t, axis=-1, keepdims=True)
        shifted_exp = np.exp(logits_t - maxes)
        scores = shifted_exp / shifted_exp.sum(axis=-1, keepdims=True)
        return scores

    def clean_output(self, list_text):
        new_list = []
        for i,j in list_text:
            if i.startswith("▁") and i != '▁':
                i = i.replace("▁", "", 1)
            elif i == '▁':
                i = " "
            new_list.append((i, j))
        return new_list

    def totag(self, post, sent):
        tag = []
        _s = self.sp.EncodeAsPieces(sent)
        for i in range(len(_s)):
            tag.append(
                (
                    _s[i],
                    self.id2tag[
                        str(list(post[i+1]).index(max(list(post[i+1]))))
                    ]
                )
            )
        return tag

    def get_ner(self, text: str):
        self._s = self.build_tokenizer(text)
        logits = self.session.run(
            output_names=[self.outputs_name],
            input_feed=self._s
        )[0]
        return self.clean_output(self.totag(self.postprocess(logits), text))
<|endoftext|>


<commit_before>__version__ = '4.1.0'
<commit_msg>bump version to 4.1.1<commit_after>__version__ = '4.1.1'
<|endoftext|>


<commit_before>from __future__ import division, print_function, absolute_import
__author__ = 'espin'

################################################################################
### Local Dependencies
################################################################################
from org.gesis.libs import graph as c
from org.gesis.libs.graph import DataMatrix
from org.gesis.libs.janus import JANUS

################################################################################
### Global Dependencies
################################################################################
from scipy.sparse import csr_matrix
import numpy as np
import os
import time

################################################################################
### CONSTANTS
################################################################################
ALGORITHM = 'coauthorship'
DEL=','
################################################################################
### Functions
################################################################################

def run_janus(algorithm,isdirected,isweighted,ismultigraph,dependency,output,kmax,klogscale,krank):

    ### 1. create data
    graph = DataMatrix(isdirected, isweighted, ismultigraph, dependency, algorithm, output)
    if graph.exists():
        graph.loadData()
    else:
        graph.extractData(getMatrix(['data'],output))
        graph.saveData()
    graph.showInfo()

    ### 2. init JANUS
    start = time.time()
    janus = JANUS(graph, output)

    ### 3. create hypotheses
    janus.createHypothesis('data')
    janus.createHypothesis('uniform')
    janus.createHypothesis('selfloop')
    janus.createHypothesis('M1: same-country',getMatrix(['same-country'],output))
    janus.createHypothesis('M2: nominations',getMatrix(['nominations'],output))
    janus.createHypothesis('M3: first-movie',getMatrix(['first-movie'],output))
    # janus.createHypothesis('M3: first-movie-decade',getMatrix(['first-movie-decade'],output))
    janus.createHypothesis('M4: transitivity',getMatrix(['transitivity'],output))

    # ### 4. evidences
    janus.generateEvidences(kmax,klogscale)
    stop = time.time()
    janus.showRank(krank)
    janus.saveEvidencesToFile()
    janus.plotEvidences(krank,bboxx=0.8,bboxy=0.8)
    janus.plotBayesFactors(krank,bboxx=0.8,bboxy=0.8)
    janus.saveReadme(start,stop)

def getMatrix(datasets,output):
    data = None
    for dataset in datasets:
        fn = os.path.join(output,'{}.{}'.format(dataset,'matrix'))
        if os.path.exists(fn):
            if data is None:
                data = np.loadtxt(fn,delimiter=DEL)
            else:
                data = np.add(data,np.loadtxt(fn,delimiter=DEL))
    return csr_matrix(data)

################################################################################
### main
################################################################################
if __name__ == '__main__':
    isdirected = False
    isweighted = False
    ismultigraph = True
    dependency = c.LOCAL
    kmax = 10
    klogscale = False
    krank = 10
    algorithm = ALGORITHM
    output = '../resources/actors-{}'.format(dependency)

    if not os.path.exists(output):
        os.makedirs(output)

    run_janus(algorithm,isdirected,isweighted,ismultigraph,dependency,output,kmax,klogscale,krank)
<commit_msg>deleting old examples<commit_after><|endoftext|>


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

import discord
import aiohttp
import urllib.parse
import urllib.request
import subprocess
import re
import psutil
import os
import hashlib

import sys
sys.path.append("..")
import jauxiliar as jaux
import josecommon as jcommon
import joseconfig as jconfig

from random import SystemRandom
random = SystemRandom()

JOSE_URL = 'https://github.com/lkmnds/jose/blob/master'

docsdict = {
    "modules": "{}/doc/modules.md".format(JOSE_URL),
    "events": "{}/doc/events.md".format(JOSE_URL),
    "queries": "{}/doc/cmd/queries.md".format(JOSE_URL),
    "queries-pt": "{}/doc/cmd/queries-pt.md".format(JOSE_URL),
    "manual": "{}/doc/manual.md".format(JOSE_URL),
    "eapi": "{}/doc/extension_api.md".format(JOSE_URL),

    "magicwords": "{}/doc/cmd/magicwords.md".format(JOSE_URL),
    "magicwords-pt": "{}/doc/cmd/magicwords-pt.md".format(JOSE_URL),

    "commands": "{}/doc/cmd/listcmd.md".format(JOSE_URL),
    "josespeak": "{}/doc/josespeak.md".format(JOSE_URL),

    "contributing": "{}/CONTRIBUTING.md".format(JOSE_URL),
    "jcoin": "{}/doc/jcoin.md".format(JOSE_URL),
}

COLOR_API = 'http://www.colourlovers.com/api'

_NUMERALS = '0123456789abcdefABCDEF'
_HEXDEC = {v: int(v, 16) for v in (x+y for x in _NUMERALS for y in _NUMERALS)}
LOWERCASE, UPPERCASE = 'x', 'X'

def hex_to_rgb(triplet):
    triplet = triplet.lstrip('#')
    return _HEXDEC[triplet[0:2]], _HEXDEC[triplet[2:4]], _HEXDEC[triplet[4:6]]

def rgb_to_hex(triplet):
    return '%02x%02x%02x' % triplet

class joseXtra(jaux.Auxiliar):
    def __init__(self, _client):
        jaux.Auxiliar.__init__(self, _client)
        self.docs = docsdict
        self.msgcount = 0

        # every minute, show josé's usage
        self.cbk_new("jxtra.msgcount", self.message_count, 60)

    async def ext_load(self):
        return True, ''

    async def ext_unload(self):
        self.cbk_remove('jxtra.msgcount')
        return True, ''

    async def message_count(self):
        if self.msgcount > 0:
            self.logger.info("Processed %d messages/minute", self.msgcount)
        self.msgcount = 0

    async def e_any_message(self, message, cxt):
        self.msgcount += 1

    async def c_xkcd(self, message, args, cxt):
        '''`j!xkcd` - latest xkcd
        `j!xkcd [num]` - xkcd number `num`
        `j!xkcd rand` - random xkcd
        '''
        n = False
        if len(args) > 1:
            n = args[1]

        await self.jcoin_pricing(cxt, jcommon.API_TAX_PRICE)

        url = "http://xkcd.com/info.0.json"
        r = await aiohttp.request('GET', url)
        content = await r.text()

        info_latest = info = await self.json_load(content)
        info = None
        try:
            if not n:
                info = info_latest
                n = info['num']
            elif n == 'random' or n == 'r' or n == 'rand':
                rn_xkcd = random.randint(0, info_latest['num'])

                url = "http://xkcd.com/{0}/info.0.json".format(rn_xkcd)
                r = await aiohttp.request('GET', url)
                content = await r.text()

                info = await self.json_load(content)
            else:
                url = "http://xkcd.com/{0}/info.0.json".format(n)
                r = await aiohttp.request('GET', url)
                content = await r.text()
                info = await self.json_load(content)
            await cxt.say('xkcd %s : %s', (n, info['img']))

        except Exception as e:
            await cxt.say("err: %r", (e,))

    async def c_tm(self, message, args, cxt):
        await cxt.say('%s™', (' '.join(args[1:]),))

    async def c_loteria(self, message, args, cxt):
        await cxt.say("nao")

    async def c_awoo(self, message, args, cxt):
        await cxt.say("https://cdn.discordapp.com/attachments/\
202055538773721099/257717450135568394/awooo.gif")

    async def c_report(self, message, args, cxt):
        '''`j!report` - show important stuff'''
        res = []

        # get memory usage
        process = psutil.Process(os.getpid())
        mem_bytes = process.memory_info().rss
        mem_mb = mem_bytes / 1024 / 1024
        res.append("Memory Usage: %.2f MB" % mem_mb)

        # get num of servers
        res.append("Guilds: %d" % len(self.client.servers))

        # num of channels
        res.append("Channels: %s" % len(list(self.client.get_all_channels())))

        # num of users
        res.append("Members: %s" % len(list(self.client.get_all_members())))

        members = [m for m in self.client.get_all_members() if not m.bot]

        # num of actual numbers that aren't bots
        res.append("Users: %s" % len(members))

        # unique users
        res.append("Unique Users: %s" % len(set(members)))

        await cxt.say(self.codeblock("", '\n'.join(res)))

    async def c_status(self, message, args, cxt):
        '''`j!status` get josé's status to some servers'''
        await self.is_admin(message.author.id)

        msg = await self.client.send_message(message.channel, "Status:")

        discordapp_ping = subprocess.Popen(
            ["ping", "-c", "3", "discordapp.com"],
            stdout = subprocess.PIPE,
            stderr = subprocess.PIPE
        )

        d_out, d_error = discordapp_ping.communicate()
        matcher = re.compile("rtt min/avg/max/mdev = (\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)")
        d_rtt = matcher.search(d_out.decode('utf-8')).groups()

        edit1 = await self.client.edit_message(msg, msg.content + """
%s : min %sms avg %sms max %sms
""" % ("discordapp.com", d_rtt[0], d_rtt[1], d_rtt[2]))

        google_ping = subprocess.Popen(
            ["ping", "-c", "3", "google.com"],
            stdout = subprocess.PIPE,
            stderr = subprocess.PIPE
        )

        g_out, g_error = google_ping.communicate()
        matcher = re.compile("rtt min/avg/max/mdev = (\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)")
        g_rtt = matcher.search(g_out.decode('utf-8')).groups()

        await self.client.edit_message(edit1, edit1.content + """
%s : min %sms avg %sms max %sms
""" % ("google.com", g_rtt[0], g_rtt[1], g_rtt[2]))

    async def c_info(self, message, args, cxt):
        await cxt.say("""
José v%s
JTE, José Testing Enviroment: https://discord.gg/5ASwg4C
José is Licensed through the Don't Be a Dick Public License.
See the source at https://github.com/lkmnds/jose

Made with :heart: by Luna Mendes""" % (jcommon.JOSE_VERSION))

    async def c_docs(self, message, args, cxt):
        '''`j!docs <topic>` - José's Documentation, use `j!docs list`'''
        if len(args) < 2:
            await cxt.say(self.c_docs.__doc__)
            return

        topic = ' '.join(args[1:])

        if topic == 'list':
            topics = ' '.join(sorted(self.docs))
            await cxt.say(topics)
        else:
            if topic in self.docs:
                await cxt.say(self.docs[topic])
            else:
                await cxt.say("%s: tópico não encontrado", (topic,))

    async def c_commands(self, message, args, cxt):
        await cxt.say(self.docs['commands'])

    async def c_yt(self, message, args, cxt):
        '''`j!yt tunak tunak tun` - youtube searches'''
        if len(args) < 2:
            await cxt.say(self.c_yt.__doc__)
            return

        search_term = ' '.join(args[1:])

        self.logger.info("Youtube request @ %s : %s", \
            message.author, search_term)

        query_string = urllib.parse.urlencode({"search_query" : search_term})

        await self.jcoin_pricing(cxt, jcommon.OP_TAX_PRICE)

        url = "http://www.youtube.com/results?{}".format(query_string)
        html_content = await self.http_get(url)

        # run in a thread
        future_re = self.loop.run_in_executor(None, re.findall, \
            r'href=\"\/watch\?v=(.{11})', html_content)
        search_results = await future_re

        if len(search_results) < 2:
            await cxt.say("!yt: No results found.")
            return

        await cxt.say("http://www.youtube.com/watch?v={}".format(search_results[0]))

    async def c_sndc(self, message, args, cxt):
        '''`j!sndc [stuff]` - Soundcloud search'''
        if len(args) < 2:
            await cxt.say(self.c_sndc.__doc__)
            return

        query = ' '.join(args[1:])

        self.logger.info("Soundcloud request: %s", query)

        if len(query) < 3:
            await cxt.say("preciso de mais coisas para pesquisar(length < 3)")
            return

        await self.jcoin_pricing(cxt, jcommon.API_TAX_PRICE)

        search_url = 'https://api.soundcloud.com/search?q=%s&facet=model&limit=10&offset=0&linked_partitioning=1&client_id='+jconfig.soundcloud_id
        url = search_url % urllib.parse.quote(query)

        while url:
            response = await aiohttp.request('GET', url)

            if response.status != 200:
                await cxt.say("sndc: error: status code != 200(st = %d)", \
                    (response.status))
                return

            try:
                doc = await response.json()
            except Exception as e:
                await cxt.say("sndc: py_err %s" % str(e))
                return

            for entity in doc['collection']:
                if entity['kind'] == 'track':
                    await cxt.say(entity['permalink_url'])
                    return

            await cxt.say("No results found")
            return

    def mkcolor(self, name):
        colorval = int(hashlib.md5(name.encode("utf-8")).hexdigest()[:6], 16)
        return discord.Colour(colorval)

    async def c_profile(self, message, args, cxt):
        '''`j!profile [@mention]` - profile card stuff'''
        await cxt.send_typing()

        user = message.author
        acc_id = message.author.id

        try:
            _acc_id = await jcommon.parse_id(args[1])
            if _acc_id is not None:
                acc_id = _acc_id
        except IndexError:
            pass
        except Exception as err:
            await cxt.say("`%r`", (err,))
            return

        if acc_id != message.author.id:
            user = await self.client.get_user_info(acc_id)

        em = discord.Embed(colour=self.mkcolor(user.name))

        if hasattr(user, 'nick'):
            if user.nick is not None:
                em.add_field(name='Name', value='{} ({})'.format( \
                    user.nick, user.name))
            else:
                em.add_field(name='Name', value='{}'.format(user.name))
        else:
            em.add_field(name='Name', value='{}'.format(user.name))

        if acc_id in self.jcoin.data:
            account = self.jcoin.data.get(acc_id)

            _gaccounts = [userid for userid in self.jcoin.data \
                if message.server.get_member(userid) is not None]

            gacc_sorted = sorted(_gaccounts, key=lambda userid: \
                self.jcoin.data[userid]['amount'], reverse=True)

            sorted_data = sorted(self.jcoin.data, key=lambda userid: \
                self.jcoin.data[userid]['amount'], reverse=True)

            # index start from 0
            guildrank = gacc_sorted.index(acc_id) + 1
            globalrank = sorted_data.index(acc_id) + 1

            em.add_field(name='JoséCoin Rank', value='{}/{} ({}/{} globally)'.format( \
                guildrank, len(gacc_sorted), \
                globalrank, len(sorted_data))
            )

            em.add_field(name='JoséCoin Wallet', value='{}JC'.format(account['amount']))
            em.add_field(name='Tax paid', value='{}JC'.format(account['taxpaid']))

            em.add_field(name='Stealing', value='{} tries, {} success'.format( \
                account['times_stolen'], account['success_steal']))

        await cxt.say_embed(em)

    async def c_color(self, message, args, cxt):
        '''`j!color "rand"|#aabbcc|red,green,blue` - show colors'''

        if len(args) < 2:
            await cxt.say(self.c_color.__doc__)
            return

        await self.jcoin_pricing(cxt, jcommon.API_TAX_PRICE)

        color = (0, 0, 0)
        mk_rand = False

        if args[1] == 'rand':
            mk_rand = True

        elif args[1].startswith('#'):
            # parse hex #aabbcc
            if len(args[1]) > 7:
                await cxt.say("`#aabbcc` format not recognized")
                return
            color = hex_to_rgb(args[1])

        elif args[1].find(','):
            # parse rgb int,int,int
            sp = args[1].split(',')

            if len(sp) != 3:
                await cxt.say("Error parsing rgb(RED, GRN, BLU)")
                return

            for (i, el) in enumerate(sp):
                try:
                    sp[i] = int(el)
                    if sp[i] < 0 or sp[i] >= 255:
                        await cxt.say("`%r` out or range 0-255")
                        return
                except:
                    await cxt.say("Error converting `%r` to Integer base 10", \
                        (el,))
                    return

            red, green, blue = sp
            color = (red, green, blue)

        if mk_rand:
            red = random.randint(0x0, 0xff)
            green = random.randint(0x0, 0xff)
            blue = random.randint(0x0, 0xff)
            color = (red, green, blue)

        # get color from a color api
        _colordata = await self.json_from_url('{}/color/{}?format=json'.format \
            (COLOR_API, rgb_to_hex(color)))

        try:
            colordata = _colordata[0]
            imageurl = colordata['imageUrl']
        except Exception as err:
            await cxt.say("```\n%r\n```", (err,))
            return

        await cxt.say('Color `%s`: %s', (color, imageurl))
<commit_msg>jxtra: color: show more debug info on error<commit_after>#!/usr/bin/env python3

import discord
import aiohttp
import urllib.parse
import urllib.request
import subprocess
import re
import psutil
import os
import hashlib

import sys
sys.path.append("..")
import jauxiliar as jaux
import josecommon as jcommon
import joseconfig as jconfig

from random import SystemRandom
random = SystemRandom()

JOSE_URL = 'https://github.com/lkmnds/jose/blob/master'

docsdict = {
    "modules": "{}/doc/modules.md".format(JOSE_URL),
    "events": "{}/doc/events.md".format(JOSE_URL),
    "queries": "{}/doc/cmd/queries.md".format(JOSE_URL),
    "queries-pt": "{}/doc/cmd/queries-pt.md".format(JOSE_URL),
    "manual": "{}/doc/manual.md".format(JOSE_URL),
    "eapi": "{}/doc/extension_api.md".format(JOSE_URL),

    "magicwords": "{}/doc/cmd/magicwords.md".format(JOSE_URL),
    "magicwords-pt": "{}/doc/cmd/magicwords-pt.md".format(JOSE_URL),

    "commands": "{}/doc/cmd/listcmd.md".format(JOSE_URL),
    "josespeak": "{}/doc/josespeak.md".format(JOSE_URL),

    "contributing": "{}/CONTRIBUTING.md".format(JOSE_URL),
    "jcoin": "{}/doc/jcoin.md".format(JOSE_URL),
}

COLOR_API = 'http://www.colourlovers.com/api'

_NUMERALS = '0123456789abcdefABCDEF'
_HEXDEC = {v: int(v, 16) for v in (x+y for x in _NUMERALS for y in _NUMERALS)}
LOWERCASE, UPPERCASE = 'x', 'X'

def hex_to_rgb(triplet):
    triplet = triplet.lstrip('#')
    return _HEXDEC[triplet[0:2]], _HEXDEC[triplet[2:4]], _HEXDEC[triplet[4:6]]

def rgb_to_hex(triplet):
    return '%02x%02x%02x' % triplet

class joseXtra(jaux.Auxiliar):
    def __init__(self, _client):
        jaux.Auxiliar.__init__(self, _client)
        self.docs = docsdict
        self.msgcount = 0

        # every minute, show josé's usage
        self.cbk_new("jxtra.msgcount", self.message_count, 60)

    async def ext_load(self):
        return True, ''

    async def ext_unload(self):
        self.cbk_remove('jxtra.msgcount')
        return True, ''

    async def message_count(self):
        if self.msgcount > 0:
            self.logger.info("Processed %d messages/minute", self.msgcount)
        self.msgcount = 0

    async def e_any_message(self, message, cxt):
        self.msgcount += 1

    async def c_xkcd(self, message, args, cxt):
        '''`j!xkcd` - latest xkcd
        `j!xkcd [num]` - xkcd number `num`
        `j!xkcd rand` - random xkcd
        '''
        n = False
        if len(args) > 1:
            n = args[1]

        await self.jcoin_pricing(cxt, jcommon.API_TAX_PRICE)

        url = "http://xkcd.com/info.0.json"
        r = await aiohttp.request('GET', url)
        content = await r.text()

        info_latest = info = await self.json_load(content)
        info = None
        try:
            if not n:
                info = info_latest
                n = info['num']
            elif n == 'random' or n == 'r' or n == 'rand':
                rn_xkcd = random.randint(0, info_latest['num'])

                url = "http://xkcd.com/{0}/info.0.json".format(rn_xkcd)
                r = await aiohttp.request('GET', url)
                content = await r.text()

                info = await self.json_load(content)
            else:
                url = "http://xkcd.com/{0}/info.0.json".format(n)
                r = await aiohttp.request('GET', url)
                content = await r.text()
                info = await self.json_load(content)
            await cxt.say('xkcd %s : %s', (n, info['img']))

        except Exception as e:
            await cxt.say("err: %r", (e,))

    async def c_tm(self, message, args, cxt):
        await cxt.say('%s™', (' '.join(args[1:]),))

    async def c_loteria(self, message, args, cxt):
        await cxt.say("nao")

    async def c_awoo(self, message, args, cxt):
        await cxt.say("https://cdn.discordapp.com/attachments/\
202055538773721099/257717450135568394/awooo.gif")

    async def c_report(self, message, args, cxt):
        '''`j!report` - show important stuff'''
        res = []

        # get memory usage
        process = psutil.Process(os.getpid())
        mem_bytes = process.memory_info().rss
        mem_mb = mem_bytes / 1024 / 1024
        res.append("Memory Usage: %.2f MB" % mem_mb)

        # get num of servers
        res.append("Guilds: %d" % len(self.client.servers))

        # num of channels
        res.append("Channels: %s" % len(list(self.client.get_all_channels())))

        # num of users
        res.append("Members: %s" % len(list(self.client.get_all_members())))

        members = [m for m in self.client.get_all_members() if not m.bot]

        # num of actual numbers that aren't bots
        res.append("Users: %s" % len(members))

        # unique users
        res.append("Unique Users: %s" % len(set(members)))

        await cxt.say(self.codeblock("", '\n'.join(res)))

    async def c_status(self, message, args, cxt):
        '''`j!status` get josé's status to some servers'''
        await self.is_admin(message.author.id)

        msg = await self.client.send_message(message.channel, "Status:")

        discordapp_ping = subprocess.Popen(
            ["ping", "-c", "3", "discordapp.com"],
            stdout = subprocess.PIPE,
            stderr = subprocess.PIPE
        )

        d_out, d_error = discordapp_ping.communicate()
        matcher = re.compile("rtt min/avg/max/mdev = (\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)")
        d_rtt = matcher.search(d_out.decode('utf-8')).groups()

        edit1 = await self.client.edit_message(msg, msg.content + """
%s : min %sms avg %sms max %sms
""" % ("discordapp.com", d_rtt[0], d_rtt[1], d_rtt[2]))

        google_ping = subprocess.Popen(
            ["ping", "-c", "3", "google.com"],
            stdout = subprocess.PIPE,
            stderr = subprocess.PIPE
        )

        g_out, g_error = google_ping.communicate()
        matcher = re.compile("rtt min/avg/max/mdev = (\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)")
        g_rtt = matcher.search(g_out.decode('utf-8')).groups()

        await self.client.edit_message(edit1, edit1.content + """
%s : min %sms avg %sms max %sms
""" % ("google.com", g_rtt[0], g_rtt[1], g_rtt[2]))

    async def c_info(self, message, args, cxt):
        await cxt.say("""
José v%s
JTE, José Testing Enviroment: https://discord.gg/5ASwg4C
José is Licensed through the Don't Be a Dick Public License.
See the source at https://github.com/lkmnds/jose

Made with :heart: by Luna Mendes""" % (jcommon.JOSE_VERSION))

    async def c_docs(self, message, args, cxt):
        '''`j!docs <topic>` - José's Documentation, use `j!docs list`'''
        if len(args) < 2:
            await cxt.say(self.c_docs.__doc__)
            return

        topic = ' '.join(args[1:])

        if topic == 'list':
            topics = ' '.join(sorted(self.docs))
            await cxt.say(topics)
        else:
            if topic in self.docs:
                await cxt.say(self.docs[topic])
            else:
                await cxt.say("%s: tópico não encontrado", (topic,))

    async def c_commands(self, message, args, cxt):
        await cxt.say(self.docs['commands'])

    async def c_yt(self, message, args, cxt):
        '''`j!yt tunak tunak tun` - youtube searches'''
        if len(args) < 2:
            await cxt.say(self.c_yt.__doc__)
            return

        search_term = ' '.join(args[1:])

        self.logger.info("Youtube request @ %s : %s", \
            message.author, search_term)

        query_string = urllib.parse.urlencode({"search_query" : search_term})

        await self.jcoin_pricing(cxt, jcommon.OP_TAX_PRICE)

        url = "http://www.youtube.com/results?{}".format(query_string)
        html_content = await self.http_get(url)

        # run in a thread
        future_re = self.loop.run_in_executor(None, re.findall, \
            r'href=\"\/watch\?v=(.{11})', html_content)
        search_results = await future_re

        if len(search_results) < 2:
            await cxt.say("!yt: No results found.")
            return

        await cxt.say("http://www.youtube.com/watch?v={}".format(search_results[0]))

    async def c_sndc(self, message, args, cxt):
        '''`j!sndc [stuff]` - Soundcloud search'''
        if len(args) < 2:
            await cxt.say(self.c_sndc.__doc__)
            return

        query = ' '.join(args[1:])

        self.logger.info("Soundcloud request: %s", query)

        if len(query) < 3:
            await cxt.say("preciso de mais coisas para pesquisar(length < 3)")
            return

        await self.jcoin_pricing(cxt, jcommon.API_TAX_PRICE)

        search_url = 'https://api.soundcloud.com/search?q=%s&facet=model&limit=10&offset=0&linked_partitioning=1&client_id='+jconfig.soundcloud_id
        url = search_url % urllib.parse.quote(query)

        while url:
            response = await aiohttp.request('GET', url)

            if response.status != 200:
                await cxt.say("sndc: error: status code != 200(st = %d)", \
                    (response.status))
                return

            try:
                doc = await response.json()
            except Exception as e:
                await cxt.say("sndc: py_err %s" % str(e))
                return

            for entity in doc['collection']:
                if entity['kind'] == 'track':
                    await cxt.say(entity['permalink_url'])
                    return

            await cxt.say("No results found")
            return

    def mkcolor(self, name):
        colorval = int(hashlib.md5(name.encode("utf-8")).hexdigest()[:6], 16)
        return discord.Colour(colorval)

    async def c_profile(self, message, args, cxt):
        '''`j!profile [@mention]` - profile card stuff'''
        await cxt.send_typing()

        user = message.author
        acc_id = message.author.id

        try:
            _acc_id = await jcommon.parse_id(args[1])
            if _acc_id is not None:
                acc_id = _acc_id
        except IndexError:
            pass
        except Exception as err:
            await cxt.say("`%r`", (err,))
            return

        if acc_id != message.author.id:
            user = await self.client.get_user_info(acc_id)

        em = discord.Embed(colour=self.mkcolor(user.name))

        if hasattr(user, 'nick'):
            if user.nick is not None:
                em.add_field(name='Name', value='{} ({})'.format( \
                    user.nick, user.name))
            else:
                em.add_field(name='Name', value='{}'.format(user.name))
        else:
            em.add_field(name='Name', value='{}'.format(user.name))

        if acc_id in self.jcoin.data:
            account = self.jcoin.data.get(acc_id)

            _gaccounts = [userid for userid in self.jcoin.data \
                if message.server.get_member(userid) is not None]

            gacc_sorted = sorted(_gaccounts, key=lambda userid: \
                self.jcoin.data[userid]['amount'], reverse=True)

            sorted_data = sorted(self.jcoin.data, key=lambda userid: \
                self.jcoin.data[userid]['amount'], reverse=True)

            # index start from 0
            guildrank = gacc_sorted.index(acc_id) + 1
            globalrank = sorted_data.index(acc_id) + 1

            em.add_field(name='JoséCoin Rank', value='{}/{} ({}/{} globally)'.format( \
                guildrank, len(gacc_sorted), \
                globalrank, len(sorted_data))
            )

            em.add_field(name='JoséCoin Wallet', value='{}JC'.format(account['amount']))
            em.add_field(name='Tax paid', value='{}JC'.format(account['taxpaid']))

            em.add_field(name='Stealing', value='{} tries, {} success'.format( \
                account['times_stolen'], account['success_steal']))

        await cxt.say_embed(em)

    async def c_color(self, message, args, cxt):
        '''`j!color "rand"|#aabbcc|red,green,blue` - show colors'''

        if len(args) < 2:
            await cxt.say(self.c_color.__doc__)
            return

        await self.jcoin_pricing(cxt, jcommon.API_TAX_PRICE)

        color = (0, 0, 0)
        mk_rand = False

        if args[1] == 'rand':
            mk_rand = True

        elif args[1].startswith('#'):
            # parse hex #aabbcc
            if len(args[1]) > 7:
                await cxt.say("`#aabbcc` format not recognized")
                return
            color = hex_to_rgb(args[1])

        elif args[1].find(','):
            # parse rgb int,int,int
            sp = args[1].split(',')

            if len(sp) != 3:
                await cxt.say("Error parsing rgb(RED, GRN, BLU)")
                return

            for (i, el) in enumerate(sp):
                try:
                    sp[i] = int(el)
                    if sp[i] < 0 or sp[i] >= 255:
                        await cxt.say("`%r` out or range 0-255")
                        return
                except:
                    await cxt.say("Error converting `%r` to Integer base 10", \
                        (el,))
                    return

            red, green, blue = sp
            color = (red, green, blue)

        if mk_rand:
            red = random.randint(0x0, 0xff)
            green = random.randint(0x0, 0xff)
            blue = random.randint(0x0, 0xff)
            color = (red, green, blue)

        # get color from a color api
        _colordata = await self.json_from_url('{}/color/{}?format=json'.format \
            (COLOR_API, rgb_to_hex(color)))

        try:
            colordata = _colordata[0]
            imageurl = colordata['imageUrl']
        except Exception as err:
            await cxt.say("```\nError getting color %r: %r\n```", (color, err,))
            return

        await cxt.say('Color `%s`: %s', (color, imageurl))
<|endoftext|>


<commit_before>import sys
import os
import operator
from optparse import make_option

from django.apps import apps
from django.core.management.base import BaseCommand, CommandError
from django.db import connections, DEFAULT_DB_ALIAS, migrations
from django.db.migrations.migration import Migration
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.questioner import MigrationQuestioner, InteractiveMigrationQuestioner
from django.db.migrations.state import ProjectState
from django.db.migrations.writer import MigrationWriter
from django.utils.six.moves import reduce

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--dry-run', action='store_true', dest='dry_run', default=False,
            help="Just show what migrations would be made; don't actually write them."),
        make_option('--merge', action='store_true', dest='merge', default=False,
            help="Enable fixing of migration conflicts."),
        make_option('--empty', action='store_true', dest='empty', default=False,
            help="Create an empty migration."),
    )

    help = "Creates new migration(s) for apps."
    usage_str = "Usage: ./manage.py makemigrations [--dry-run] [app [app ...]]"
    args = "[app_label [app_label ...]]"

    def handle(self, *app_labels, **options):

        self.verbosity = int(options.get('verbosity'))
        self.interactive = options.get('interactive')
        self.dry_run = options.get('dry_run', False)
        self.merge = options.get('merge', False)
        self.empty = options.get('empty', False)

        # Make sure the app they asked for exists
        app_labels = set(app_labels)
        bad_app_labels = set()
        for app_label in app_labels:
            try:
                apps.get_app_config(app_label)
            except LookupError:
                bad_app_labels.add(app_label)
        if bad_app_labels:
            for app_label in bad_app_labels:
                self.stderr.write("App '%s' could not be found. Is it in INSTALLED_APPS?" % app_label)
            sys.exit(2)

        # Load the current graph state. Pass in None for the connection so
        # the loader doesn't try to resolve replaced migrations from DB.
        loader = MigrationLoader(None)

        # Before anything else, see if there's conflicting apps and drop out
        # hard if there are any and they don't want to merge
        conflicts = loader.detect_conflicts()
        if conflicts and not self.merge:
            name_str = "; ".join(
                "%s in %s" % (", ".join(names), app)
                for app, names in conflicts.items()
            )
            raise CommandError("Conflicting migrations detected (%s).\nTo fix them run 'python manage.py makemigrations --merge'" % name_str)

        # If they want to merge and there's nothing to merge, then politely exit
        if self.merge and not conflicts:
            self.stdout.write("No conflicts detected to merge.")
            return

        # If they want to merge and there is something to merge, then
        # divert into the merge code
        if self.merge and conflicts:
            return self.handle_merge(loader, conflicts)

        # Set up autodetector
        autodetector = MigrationAutodetector(
            loader.project_state(),
            ProjectState.from_apps(apps),
            InteractiveMigrationQuestioner(specified_apps=app_labels),
        )

        # If they want to make an empty migration, make one for each app
        if self.empty:
            if not app_labels:
                raise CommandError("You must supply at least one app label when using --empty.")
            # Make a fake changes() result we can pass to arrange_for_graph
            changes = dict(
                (app, [Migration("custom", app)])
                for app in app_labels
            )
            changes = autodetector.arrange_for_graph(changes, loader.graph)
            self.write_migration_files(changes)
            return

        # Detect changes
        changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)

        # No changes? Tell them.
        if not changes and self.verbosity >= 1:
            if len(app_labels) == 1:
                self.stdout.write("No changes detected in app '%s'" % app_labels.pop())
            elif len(app_labels) > 1:
                self.stdout.write("No changes detected in apps '%s'" % ("', '".join(app_labels)))
            else:
                self.stdout.write("No changes detected")
            return

        self.write_migration_files(changes)

    def write_migration_files(self, changes):
        """
        Takes a changes dict and writes them out as migration files.
        """
        directory_created = {}
        for app_label, app_migrations in changes.items():
            if self.verbosity >= 1:
                self.stdout.write(self.style.MIGRATE_HEADING("Migrations for '%s':" % app_label) + "\n")
            for migration in app_migrations:
                # Describe the migration
                writer = MigrationWriter(migration)
                if self.verbosity >= 1:
                    self.stdout.write("  %s:\n" % (self.style.MIGRATE_LABEL(writer.filename),))
                    for operation in migration.operations:
                        self.stdout.write("    - %s\n" % operation.describe())
                # Write it
                if not self.dry_run:
                    migrations_directory = os.path.dirname(writer.path)
                    if not directory_created.get(app_label, False):
                        if not os.path.isdir(migrations_directory):
                            os.mkdir(migrations_directory)
                        init_path = os.path.join(migrations_directory, "__init__.py")
                        if not os.path.isfile(init_path):
                            open(init_path, "w").close()
                        # We just do this once per app
                        directory_created[app_label] = True
                    migration_string = writer.as_string()
                    with open(writer.path, "wb") as fh:
                        fh.write(migration_string)

    def handle_merge(self, loader, conflicts):
        """
        Handles merging together conflicted migrations interactively,
        if it's safe; otherwise, advises on how to fix it.
        """
        if self.interactive:
            questioner = InteractiveMigrationQuestioner()
        else:
            questioner = MigrationQuestioner()
        for app_label, migration_names in conflicts.items():
            # Grab out the migrations in question, and work out their
            # common ancestor.
            merge_migrations = []
            for migration_name in migration_names:
                migration = loader.get_migration(app_label, migration_name)
                migration.ancestry = loader.graph.forwards_plan((app_label, migration_name))
                merge_migrations.append(migration)
            common_ancestor = None
            for level in zip(*[m.ancestry for m in merge_migrations]):
                if reduce(operator.eq, level):
                    common_ancestor = level[0]
                else:
                    break
            if common_ancestor is None:
                raise ValueError("Could not find common ancestor of %s" % migration_names)
            # Now work out the operations along each divergent branch
            for migration in merge_migrations:
                migration.branch = migration.ancestry[
                    (migration.ancestry.index(common_ancestor) + 1):
                ]
                migration.merged_operations = []
                for node_app, node_name in migration.branch:
                    migration.merged_operations.extend(
                        loader.get_migration(node_app, node_name).operations
                    )
            # In future, this could use some of the Optimizer code
            # (can_optimize_through) to automatically see if they're
            # mergeable. For now, we always just prompt the user.
            if self.verbosity > 0:
                self.stdout.write(self.style.MIGRATE_HEADING("Merging %s" % app_label))
                for migration in merge_migrations:
                    self.stdout.write(self.style.MIGRATE_LABEL("  Branch %s" % migration.name))
                    for operation in migration.merged_operations:
                        self.stdout.write("    - %s\n" % operation.describe())
            if questioner.ask_merge(app_label):
                # If they still want to merge it, then write out an empty
                # file depending on the migrations needing merging.
                numbers = [
                    MigrationAutodetector.parse_number(migration.name)
                    for migration in merge_migrations
                ]
                try:
                    biggest_number = max([x for x in numbers if x is not None])
                except ValueError:
                    biggest_number = 1
                subclass = type("Migration", (migrations.Migration, ), {
                    "dependencies": [(app_label, migration.name) for migration in merge_migrations],
                })
                new_migration = subclass("%04i_merge" % (biggest_number + 1), app_label)
                writer = MigrationWriter(new_migration)
                with open(writer.path, "wb") as fh:
                    fh.write(writer.as_string())
                if self.verbosity > 0:
                    self.stdout.write("\nCreated new merge migration %s" % writer.path)
<commit_msg>[1.7.x] Removed unused imports in makemigrations<commit_after>import sys
import os
import operator
from optparse import make_option

from django.apps import apps
from django.core.management.base import BaseCommand, CommandError
from django.db.migrations import Migration
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.questioner import MigrationQuestioner, InteractiveMigrationQuestioner
from django.db.migrations.state import ProjectState
from django.db.migrations.writer import MigrationWriter
from django.utils.six.moves import reduce

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--dry-run', action='store_true', dest='dry_run', default=False,
            help="Just show what migrations would be made; don't actually write them."),
        make_option('--merge', action='store_true', dest='merge', default=False,
            help="Enable fixing of migration conflicts."),
        make_option('--empty', action='store_true', dest='empty', default=False,
            help="Create an empty migration."),
    )

    help = "Creates new migration(s) for apps."
    usage_str = "Usage: ./manage.py makemigrations [--dry-run] [app [app ...]]"
    args = "[app_label [app_label ...]]"

    def handle(self, *app_labels, **options):

        self.verbosity = int(options.get('verbosity'))
        self.interactive = options.get('interactive')
        self.dry_run = options.get('dry_run', False)
        self.merge = options.get('merge', False)
        self.empty = options.get('empty', False)

        # Make sure the app they asked for exists
        app_labels = set(app_labels)
        bad_app_labels = set()
        for app_label in app_labels:
            try:
                apps.get_app_config(app_label)
            except LookupError:
                bad_app_labels.add(app_label)
        if bad_app_labels:
            for app_label in bad_app_labels:
                self.stderr.write("App '%s' could not be found. Is it in INSTALLED_APPS?" % app_label)
            sys.exit(2)

        # Load the current graph state. Pass in None for the connection so
        # the loader doesn't try to resolve replaced migrations from DB.
        loader = MigrationLoader(None)

        # Before anything else, see if there's conflicting apps and drop out
        # hard if there are any and they don't want to merge
        conflicts = loader.detect_conflicts()
        if conflicts and not self.merge:
            name_str = "; ".join(
                "%s in %s" % (", ".join(names), app)
                for app, names in conflicts.items()
            )
            raise CommandError("Conflicting migrations detected (%s).\nTo fix them run 'python manage.py makemigrations --merge'" % name_str)

        # If they want to merge and there's nothing to merge, then politely exit
        if self.merge and not conflicts:
            self.stdout.write("No conflicts detected to merge.")
            return

        # If they want to merge and there is something to merge, then
        # divert into the merge code
        if self.merge and conflicts:
            return self.handle_merge(loader, conflicts)

        # Set up autodetector
        autodetector = MigrationAutodetector(
            loader.project_state(),
            ProjectState.from_apps(apps),
            InteractiveMigrationQuestioner(specified_apps=app_labels),
        )

        # If they want to make an empty migration, make one for each app
        if self.empty:
            if not app_labels:
                raise CommandError("You must supply at least one app label when using --empty.")
            # Make a fake changes() result we can pass to arrange_for_graph
            changes = dict(
                (app, [Migration("custom", app)])
                for app in app_labels
            )
            changes = autodetector.arrange_for_graph(changes, loader.graph)
            self.write_migration_files(changes)
            return

        # Detect changes
        changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)

        # No changes? Tell them.
        if not changes and self.verbosity >= 1:
            if len(app_labels) == 1:
                self.stdout.write("No changes detected in app '%s'" % app_labels.pop())
            elif len(app_labels) > 1:
                self.stdout.write("No changes detected in apps '%s'" % ("', '".join(app_labels)))
            else:
                self.stdout.write("No changes detected")
            return

        self.write_migration_files(changes)

    def write_migration_files(self, changes):
        """
        Takes a changes dict and writes them out as migration files.
        """
        directory_created = {}
        for app_label, app_migrations in changes.items():
            if self.verbosity >= 1:
                self.stdout.write(self.style.MIGRATE_HEADING("Migrations for '%s':" % app_label) + "\n")
            for migration in app_migrations:
                # Describe the migration
                writer = MigrationWriter(migration)
                if self.verbosity >= 1:
                    self.stdout.write("  %s:\n" % (self.style.MIGRATE_LABEL(writer.filename),))
                    for operation in migration.operations:
                        self.stdout.write("    - %s\n" % operation.describe())
                # Write it
                if not self.dry_run:
                    migrations_directory = os.path.dirname(writer.path)
                    if not directory_created.get(app_label, False):
                        if not os.path.isdir(migrations_directory):
                            os.mkdir(migrations_directory)
                        init_path = os.path.join(migrations_directory, "__init__.py")
                        if not os.path.isfile(init_path):
                            open(init_path, "w").close()
                        # We just do this once per app
                        directory_created[app_label] = True
                    migration_string = writer.as_string()
                    with open(writer.path, "wb") as fh:
                        fh.write(migration_string)

    def handle_merge(self, loader, conflicts):
        """
        Handles merging together conflicted migrations interactively,
        if it's safe; otherwise, advises on how to fix it.
        """
        if self.interactive:
            questioner = InteractiveMigrationQuestioner()
        else:
            questioner = MigrationQuestioner()
        for app_label, migration_names in conflicts.items():
            # Grab out the migrations in question, and work out their
            # common ancestor.
            merge_migrations = []
            for migration_name in migration_names:
                migration = loader.get_migration(app_label, migration_name)
                migration.ancestry = loader.graph.forwards_plan((app_label, migration_name))
                merge_migrations.append(migration)
            common_ancestor = None
            for level in zip(*[m.ancestry for m in merge_migrations]):
                if reduce(operator.eq, level):
                    common_ancestor = level[0]
                else:
                    break
            if common_ancestor is None:
                raise ValueError("Could not find common ancestor of %s" % migration_names)
            # Now work out the operations along each divergent branch
            for migration in merge_migrations:
                migration.branch = migration.ancestry[
                    (migration.ancestry.index(common_ancestor) + 1):
                ]
                migration.merged_operations = []
                for node_app, node_name in migration.branch:
                    migration.merged_operations.extend(
                        loader.get_migration(node_app, node_name).operations
                    )
            # In future, this could use some of the Optimizer code
            # (can_optimize_through) to automatically see if they're
            # mergeable. For now, we always just prompt the user.
            if self.verbosity > 0:
                self.stdout.write(self.style.MIGRATE_HEADING("Merging %s" % app_label))
                for migration in merge_migrations:
                    self.stdout.write(self.style.MIGRATE_LABEL("  Branch %s" % migration.name))
                    for operation in migration.merged_operations:
                        self.stdout.write("    - %s\n" % operation.describe())
            if questioner.ask_merge(app_label):
                # If they still want to merge it, then write out an empty
                # file depending on the migrations needing merging.
                numbers = [
                    MigrationAutodetector.parse_number(migration.name)
                    for migration in merge_migrations
                ]
                try:
                    biggest_number = max([x for x in numbers if x is not None])
                except ValueError:
                    biggest_number = 1
                subclass = type("Migration", (Migration, ), {
                    "dependencies": [(app_label, migration.name) for migration in merge_migrations],
                })
                new_migration = subclass("%04i_merge" % (biggest_number + 1), app_label)
                writer = MigrationWriter(new_migration)
                with open(writer.path, "wb") as fh:
                    fh.write(writer.as_string())
                if self.verbosity > 0:
                    self.stdout.write("\nCreated new merge migration %s" % writer.path)
<|endoftext|>


<commit_before>from globals import *
import life as lfe

import references
import judgement
import movement
import rawparse
import action
import chunks
import speech
import combat
import brain
import items
import sight
import stats
import maps

import logging
import numbers
import random
import time

def _get_need_amount(life, need):
	if need['amount_callback']:
		return need['amount_callback'](life, need)

	if need['amount']:
		return need['amount']

def add_needed_item(life, item_match, amount=1, amount_callback=None, satisfy_if=None, satisfy_callback=None):
	life['needs'][str(life['need_id'])] = {'type': 'item',
	                      'match': item_match,
	                      'meet_with': [],
	                      'could_meet_with': [],
	                      'amount': amount,
	                      'amount_callback': amount_callback,
	                      'satisfy_if': satisfy_if,
	                      'satisfy_callback': satisfy_callback}

	logging.debug('Added item need: %s' % item_match)

	life['need_id'] += 1
	return str(life['need_id']-1)

def delete_needed_item(life, need_id):
	del life['needs'][need_id]

	logging.debug('Remove item need: %s' % need_id)

def process(life):
	for need in life['needs'].values():
		if need['type'] == 'item':
			_has_items = []
			_potential_items = []

			for item in brain.get_matching_remembered_items(life, need['match'], no_owner=True):
				_potential_items.append(item)

			for item in lfe.get_all_inventory_items(life, matches=[need['match']]):
				_has_items.append(item['uid'])

			if len(_has_items) >= _get_need_amount(life, need):
				need['meet_with'] = _has_items
			elif _potential_items:
				need['could_meet_with'] = _potential_items
			else:
				need['meet_with'] = []
				need['could_meet_with'] = []

def is_need_met(life, need):
	if need['meet_with']:
		return True

	return False

def needs_to_satisfy(life, need):
	return action.execute_small_script(life, need['satisfy_if'])

def can_satisfy(life, need):
	if not is_need_met(life, need):
		return False

	return True

def can_potentially_satisfy(life, need):
	return need['could_meet_with']

def satisfy(life, need):
	if action.execute_small_script(life, need['satisfy_if']):
		if need['type'] == 'item':
			_callback = action.execute_small_script(life, need['satisfy_callback'])
			_callback(life, need['meet_with'][0])
			return True

	return False

#TODO: Remove reference from dialog and delete
def is_in_need_matches(life, match):
	_matches = []

	for root_need in life['needs']:
		for item in root_need['matches']:
			_break = False

			for key in match:
				if not key in item or not item[key] == match[key]:
					_break = True
					break

			if _break:
				continue

			_matches.append(root_need)

	return _matches

def get_matched_needs(life, match):
	_matches = []

	for root_need in life['needs']:
		_break = False
		for need in root_need:
			for key in match:
				if not key in need or not need[key] == match[key]:
					_break = True
					break

			if _break:
				break

			_matches.append(root_need)

	return _matches

def _has_inventory_item(life, matches={}):
	return lfe.get_all_inventory_items(life, matches=[matches])

def check_all_needs(life):
	for need in life['needs']:
		need_is_met(life, need)

def need_is_met(life, need):
	_res = []

	for meet_callback in need['need_callback']:
		_res.extend(meet_callback(life, matches=need['need']))

	need['matches'] = _res

	if len(_res)>=need['min_matches']:
		need['num_above_needed'] = (len(_res)-need['min_matches'])+1
		return True

	#logging.info('%s is not meeting a need: %s' % (' '.join(life['name']), need['need']))
	need['num_above_needed'] = 0
	return False

def generate_needs(life):
	if stats.desires_weapon(life):
		brain.flag(life, 'no_weapon')
	else:
		brain.unflag(life, 'no_weapon')

	if combat.get_weapons(life):
		if combat.has_usable_weapon(life):
			_needed_ammo = brain.get_flag(life, 'needs_ammo')

			if _needed_ammo:
				for _needed_ammo in _needed_ammo:
					delete_needed_item(life, _needed_ammo)
		else:
			for weapon in combat.get_weapons(life):
				_weapon_uid = weapon['uid']
				_flag_name = '%s_needs_ammo' % _weapon_uid

				if not brain.get_flag(life, _flag_name):
					_n1 = add_needed_item(life,
						                {'type': 'bullet', 'owner': None, 'ammotype': weapon['ammotype']},
						                satisfy_if=action.make_small_script(function='get_flag',
						                                                    args={'flag': _flag_name}),
						                satisfy_callback=action.make_small_script(return_function='pick_up_and_hold_item'))
					_n2 = add_needed_item(life,
						                {'type': weapon['feed'], 'owner': None, 'ammotype': weapon['ammotype']},
						                satisfy_if=action.make_small_script(function='get_flag',
						                                                    args={'flag': _flag_name}),
						                satisfy_callback=action.make_small_script(return_function='pick_up_and_hold_item'))
					brain.flag(life, _flag_name, value=[_n1, _n2])

def manage_hands(life):
	for item in [lfe.get_inventory_item(life, item) for item in lfe.get_held_items(life)]:
		judgement.judge_item(life, item['uid'])
		_known_item = brain.get_remembered_item(life, item['uid'])

		if _known_item['score']:
			continue

		_equip_action = {'action': 'equipitem',
				'item': item['uid']}

		if len(lfe.find_action(life, matches=[_equip_action])):
			continue

		if lfe.can_wear_item(life, item['uid']):
			lfe.add_action(life, _equip_action,
				401,
				delay=lfe.get_item_access_time(life, item['uid']))
			continue

		if not 'CAN_WEAR' in item['flags'] and lfe.get_all_storage(life):
			_store_action = {'action': 'storeitem',
				'item': item['uid'],
				'container': lfe.get_all_storage(life)[0]['uid']}

			if len(lfe.find_action(life, matches=[_store_action])):
				continue

			lfe.add_action(life,_store_action,
				401,
				delay=lfe.get_item_access_time(life, item['uid']))

def manage_inventory(life):
	if not lfe.get_open_hands(life):
		return manage_hands(life)

	for weapon_uid in combat.get_equipped_weapons(life):
		if not combat.weapon_is_working(life, weapon_uid):
			if not combat.reload_weapon(life, weapon_uid):
				return True

	_item_to_wear = {'score': 0, 'item_uid': None}
	_item_to_equip = {'score': 0, 'item_uid': None}

	for item in [lfe.get_inventory_item(life, item) for item in lfe.get_all_unequipped_items(life)]:
		judgement.judge_item(life, item['uid'])
		_known_item = brain.get_remembered_item(life, item['uid'])

		if _known_item['score']:
			if lfe.can_wear_item(life, item['uid']):
				if _known_item['score'] > _item_to_wear['score']:
					_item_to_wear['score'] = _known_item['score']
					_item_to_wear['item_uid'] = item['uid']
			else:
				if rawparse.raw_has_section(life, 'items') and rawparse.raw_section_has_identifier(life, 'items', item['type']):
					_action = lfe.execute_raw(life, 'items', item['type'])

					if _action == 'equip':
						if _known_item['score'] > _item_to_equip['score']:
							_item_to_equip['score'] = _known_item['score']
							_item_to_equip['item_uid'] = item['uid']

	_item = None
	if _item_to_wear['score'] > _item_to_equip['score']:
		_item = _item_to_wear['item_uid']
	elif _item_to_equip['item_uid']:
		_item = _item_to_equip['item_uid']

	if _item:
		_equip_action = {'action': 'equipitem', 'item': _item}

		if len(lfe.find_action(life, matches=[_equip_action])):
			return False

		lfe.focus_on(life)

		lfe.add_action(life,
			_equip_action,
			401,
			delay=lfe.get_item_access_time(life, _item))

def explore_known_chunks(life):
	#Our first order of business is to figure out exactly what we're looking for.
	#There's a big difference between exploring the map looking for a purpose and
	#exploring the map with a purpose. Both will use similar routines but I wager
	#it'll actually be a lot harder to do it without there being some kind of goal
	#to at least start us in the right direction.

	#This function will kick in only if the ALife is idling, so looting is handled
	#automatically.

	#Note: Determining whether this fuction should run at all needs to be done inside
	#the module itself.
	_chunk_key = brain.retrieve_from_memory(life, 'explore_chunk')
	_chunk = maps.get_chunk(_chunk_key)

	if life['path'] and chunks.position_is_in_chunk(lfe.path_dest(life), _chunk_key):
		return True

	if chunks.is_in_chunk(life, '%s,%s' % (_chunk['pos'][0], _chunk['pos'][1])):
		life['known_chunks'][_chunk_key]['last_visited'] = WORLD_INFO['ticks']
		return False

	if not _chunk['ground']:
		return False

	_pos_in_chunk = random.choice(_chunk['ground'])
	lfe.clear_actions(life)
	lfe.add_action(life,{'action': 'move','to': _pos_in_chunk},200)
	return True

def explore_unknown_chunks(life):
	if life['path']:
		return True

	#_chunk_key = references.path_along_reference(life, 'buildings')

	#if not _chunk_key:
	_chunk_key = references.path_along_reference(life, 'roads')

	if not _chunk_key:
		_best_reference = references._find_best_unknown_reference(life, 'roads')['reference']
		if not _best_reference:
			return False

		_chunk_key = references.find_nearest_key_in_reference(life, _best_reference, unknown=True)

	if not _chunk_key:
		return False

	_walkable_area = chunks.get_walkable_areas(_chunk_key)
	if not _walkable_area:
		print 'no walkable area'
		return False

	_closest_pos = {'pos': None, 'distance': -1}
	for pos in _walkable_area:
		_distance = numbers.distance(life['pos'], pos)

		if _distance <= 1:
			_closest_pos['pos'] = pos
			break

		if not _closest_pos['pos'] or _distance<_closest_pos['distance']:
			_closest_pos['pos'] = pos
			_closest_pos['distance'] = _distance

	lfe.clear_actions(life)
	lfe.add_action(life,{'action': 'move','to': _closest_pos['pos']},200)

	return True
<commit_msg>Sorting out item needs<commit_after>from globals import *
import life as lfe

import references
import judgement
import movement
import rawparse
import weapons
import action
import chunks
import speech
import combat
import brain
import items
import sight
import stats
import maps

import logging
import numbers
import random
import time

def _get_need_amount(life, need):
	if need['amount_callback']:
		return need['amount_callback'](life, need)

	if need['amount']:
		return need['amount']

def add_needed_item(life, item_match, amount=1, amount_callback=None, satisfy_if=None, satisfy_callback=None):
	life['needs'][str(life['need_id'])] = {'type': 'item',
	                      'match': item_match,
	                      'meet_with': [],
	                      'could_meet_with': [],
	                      'amount': amount,
	                      'amount_callback': amount_callback,
	                      'satisfy_if': satisfy_if,
	                      'satisfy_callback': satisfy_callback}

	logging.debug('Added item need: %s' % item_match)

	life['need_id'] += 1
	return str(life['need_id']-1)

def delete_needed_item(life, need_id):
	del life['needs'][need_id]

	logging.debug('Remove item need: %s' % need_id)

def process(life):
	for need in life['needs'].values():
		if need['type'] == 'item':
			_has_items = []
			_potential_items = []

			for item in brain.get_matching_remembered_items(life, need['match'], no_owner=True):
				_potential_items.append(item)

			for item in lfe.get_all_inventory_items(life, matches=[need['match']]):
				_has_items.append(item['uid'])

			if len(_has_items) >= _get_need_amount(life, need):
				need['meet_with'] = _has_items
			elif _potential_items:
				need['could_meet_with'] = _potential_items
			else:
				need['meet_with'] = []
				need['could_meet_with'] = []

def is_need_met(life, need):
	if need['meet_with']:
		return True

	return False

def needs_to_satisfy(life, need):
	return action.execute_small_script(life, need['satisfy_if'])

def can_satisfy(life, need):
	if not is_need_met(life, need):
		return False

	return True

def can_potentially_satisfy(life, need):
	return need['could_meet_with']

def satisfy(life, need):
	if action.execute_small_script(life, need['satisfy_if']):
		if need['type'] == 'item':
			_callback = action.execute_small_script(life, need['satisfy_callback'])
			_callback(life, need['meet_with'][0])
			return True

	return False

#TODO: Remove reference from dialog and delete
def is_in_need_matches(life, match):
	_matches = []

	for root_need in life['needs']:
		for item in root_need['matches']:
			_break = False

			for key in match:
				if not key in item or not item[key] == match[key]:
					_break = True
					break

			if _break:
				continue

			_matches.append(root_need)

	return _matches

def get_matched_needs(life, match):
	_matches = []

	for root_need in life['needs']:
		_break = False
		for need in root_need:
			for key in match:
				if not key in need or not need[key] == match[key]:
					_break = True
					break

			if _break:
				break

			_matches.append(root_need)

	return _matches

def _has_inventory_item(life, matches={}):
	return lfe.get_all_inventory_items(life, matches=[matches])

def check_all_needs(life):
	for need in life['needs']:
		need_is_met(life, need)

def need_is_met(life, need):
	_res = []

	for meet_callback in need['need_callback']:
		_res.extend(meet_callback(life, matches=need['need']))

	need['matches'] = _res

	if len(_res)>=need['min_matches']:
		need['num_above_needed'] = (len(_res)-need['min_matches'])+1
		return True

	#logging.info('%s is not meeting a need: %s' % (' '.join(life['name']), need['need']))
	need['num_above_needed'] = 0
	return False

def generate_needs(life):
	if stats.desires_weapon(life):
		brain.flag(life, 'no_weapon')
	else:
		brain.unflag(life, 'no_weapon')

	if combat.get_weapons(life):
		if combat.has_usable_weapon(life):
			for weapon in combat.get_weapons(life):
				_weapon_uid = weapon['uid']

				for _flag in ['ammo', 'feed']:
					_flag_name = '%s_needs_%s' % (_weapon_uid, _flag)

					if not brain.get_flag(life, _flag_name):
						continue

					_need = brain.get_flag(life, _flag_name)

					if _need:
						delete_needed_item(life, _need)
		else:
			_weapon_with_feed = None
			for weapon in combat.get_weapons(life):
				if weapons.get_feed(weapon):
					_weapon_with_feed = weapon['uid']
					break

			if _weapon_with_feed:
				_weapon_uid = weapon['uid']
				_flag_name = '%s_needs_ammo' % _weapon_uid
				_n = add_needed_item(life,
				                     {'type': 'bullet', 'owner': None, 'ammotype': weapon['ammotype']},
				                     satisfy_if=action.make_small_script(function='get_flag',
				                                                         args={'flag': _flag_name}),
				                     satisfy_callback=action.make_small_script(return_function='pick_up_and_hold_item'))

				brain.flag(life, _flag_name, value=_n)
			else:
				for weapon in combat.get_weapons(life):
					_weapon_uid = weapon['uid']
					_flag_name = '%s_needs_feed' % _weapon_uid

					if not brain.get_flag(life, _flag_name):
						_n = add_needed_item(life,
						                     {'type': weapon['feed'], 'owner': None, 'ammotype': weapon['ammotype']},
						                     satisfy_if=action.make_small_script(function='get_flag',
						                                                         args={'flag': _flag_name}),
						                     satisfy_callback=action.make_small_script(return_function='pick_up_and_hold_item'))

						brain.flag(life, _flag_name, value=_n)

def manage_hands(life):
	for item in [lfe.get_inventory_item(life, item) for item in lfe.get_held_items(life)]:
		judgement.judge_item(life, item['uid'])
		_known_item = brain.get_remembered_item(life, item['uid'])

		if _known_item['score']:
			continue

		_equip_action = {'action': 'equipitem',
				'item': item['uid']}

		if len(lfe.find_action(life, matches=[_equip_action])):
			continue

		if lfe.can_wear_item(life, item['uid']):
			lfe.add_action(life, _equip_action,
				401,
				delay=lfe.get_item_access_time(life, item['uid']))
			continue

		if not 'CAN_WEAR' in item['flags'] and lfe.get_all_storage(life):
			_store_action = {'action': 'storeitem',
				'item': item['uid'],
				'container': lfe.get_all_storage(life)[0]['uid']}

			if len(lfe.find_action(life, matches=[_store_action])):
				continue

			lfe.add_action(life,_store_action,
				401,
				delay=lfe.get_item_access_time(life, item['uid']))

def manage_inventory(life):
	if not lfe.get_open_hands(life):
		return manage_hands(life)

	for weapon_uid in combat.get_equipped_weapons(life):
		if not combat.weapon_is_working(life, weapon_uid):
			if not combat.reload_weapon(life, weapon_uid):
				return True

	_item_to_wear = {'score': 0, 'item_uid': None}
	_item_to_equip = {'score': 0, 'item_uid': None}

	for item in [lfe.get_inventory_item(life, item) for item in lfe.get_all_unequipped_items(life)]:
		judgement.judge_item(life, item['uid'])
		_known_item = brain.get_remembered_item(life, item['uid'])

		if _known_item['score']:
			if lfe.can_wear_item(life, item['uid']):
				if _known_item['score'] > _item_to_wear['score']:
					_item_to_wear['score'] = _known_item['score']
					_item_to_wear['item_uid'] = item['uid']
			else:
				if rawparse.raw_has_section(life, 'items') and rawparse.raw_section_has_identifier(life, 'items', item['type']):
					_action = lfe.execute_raw(life, 'items', item['type'])

					if _action == 'equip':
						if _known_item['score'] > _item_to_equip['score']:
							_item_to_equip['score'] = _known_item['score']
							_item_to_equip['item_uid'] = item['uid']

	_item = None
	if _item_to_wear['score'] > _item_to_equip['score']:
		_item = _item_to_wear['item_uid']
	elif _item_to_equip['item_uid']:
		_item = _item_to_equip['item_uid']

	if _item:
		_equip_action = {'action': 'equipitem', 'item': _item}

		if len(lfe.find_action(life, matches=[_equip_action])):
			return False

		lfe.focus_on(life)

		lfe.add_action(life,
			_equip_action,
			401,
			delay=lfe.get_item_access_time(life, _item))

def explore_known_chunks(life):
	#Our first order of business is to figure out exactly what we're looking for.
	#There's a big difference between exploring the map looking for a purpose and
	#exploring the map with a purpose. Both will use similar routines but I wager
	#it'll actually be a lot harder to do it without there being some kind of goal
	#to at least start us in the right direction.

	#This function will kick in only if the ALife is idling, so looting is handled
	#automatically.

	#Note: Determining whether this fuction should run at all needs to be done inside
	#the module itself.
	_chunk_key = brain.retrieve_from_memory(life, 'explore_chunk')
	_chunk = maps.get_chunk(_chunk_key)

	if life['path'] and chunks.position_is_in_chunk(lfe.path_dest(life), _chunk_key):
		return True

	if chunks.is_in_chunk(life, '%s,%s' % (_chunk['pos'][0], _chunk['pos'][1])):
		life['known_chunks'][_chunk_key]['last_visited'] = WORLD_INFO['ticks']
		return False

	if not _chunk['ground']:
		return False

	_pos_in_chunk = random.choice(_chunk['ground'])
	lfe.clear_actions(life)
	lfe.add_action(life,{'action': 'move','to': _pos_in_chunk},200)
	return True

def explore_unknown_chunks(life):
	if life['path']:
		return True

	#_chunk_key = references.path_along_reference(life, 'buildings')

	#if not _chunk_key:
	_chunk_key = references.path_along_reference(life, 'roads')

	if not _chunk_key:
		_best_reference = references._find_best_unknown_reference(life, 'roads')['reference']
		if not _best_reference:
			return False

		_chunk_key = references.find_nearest_key_in_reference(life, _best_reference, unknown=True)

	if not _chunk_key:
		return False

	_walkable_area = chunks.get_walkable_areas(_chunk_key)
	if not _walkable_area:
		print 'no walkable area'
		return False

	_closest_pos = {'pos': None, 'distance': -1}
	for pos in _walkable_area:
		_distance = numbers.distance(life['pos'], pos)

		if _distance <= 1:
			_closest_pos['pos'] = pos
			break

		if not _closest_pos['pos'] or _distance<_closest_pos['distance']:
			_closest_pos['pos'] = pos
			_closest_pos['distance'] = _distance

	lfe.clear_actions(life)
	lfe.add_action(life,{'action': 'move','to': _closest_pos['pos']},200)

	return True
<|endoftext|>


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

"""
Problem Definition :

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.

"""

__author__ = 'vivek'

import time
import math

startTime = time.clock()

curious_numbers = []

for number in xrange(3,2500000) : #2540161
    temp = number
    add = 0
    while temp>0 :
        digit = temp%10
        temp = temp/10
        add = add + math.factorial(digit)
    if number == add :
        curious_numbers.append(number)

print(curious_numbers)
print(sum(curious_numbers))

print "Run time...{} secs \n".format(round(time.clock() - startTime, 4))

<commit_msg>Fun update<commit_after>#!/usr/bin/env python

"""
Problem Definition :

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.

"""

__author__ = 'vivek'

import time
import math

def main():
    start_time = time.clock()

    curious_numbers = []

    for number in xrange(3, 2500000):
        temp = number
        add = 0
        while temp > 0:
            digit = temp % 10
            temp /= 10
            add += math.factorial(digit)
        if number == add:
            curious_numbers.append(number)

    print(curious_numbers)
    print(sum(curious_numbers))

    print "Run time...{} secs \n".format(round(time.clock() - start_time, 4))

if __name__ == '__main__':
    main()

<|endoftext|>


<commit_before>import os
import colorama
import logging
import sys

__copyright__ = """
   Copyright 2016 Anthony Shaw

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""
__version__ = '1.0.0'

__doc__ = """Mocker.

Usage:
  mocker pull <name>[<tag>]
  mocker run <name>
  mocker images
  mocker (-h | --help)
  mocker --version

Options:
  -h --help     Show this screen.
  --version     Show version.
"""

_base_dir_ = os.path.join(os.path.expanduser('~'), 'mocker')

if not os.path.exists(_base_dir_):
    os.makedirs(_base_dir_)

# Courtesy http://plumberjack.blogspot.com/2010/12/colorizing-logging-output-in-terminals.html
# Tweaked to use colorama for the coloring

class ColorizingStreamHandler(logging.StreamHandler):
    color_map = {
        logging.DEBUG: colorama.Style.DIM + colorama.Fore.CYAN,
        logging.WARNING: colorama.Fore.YELLOW,
        logging.ERROR: colorama.Fore.RED,
        logging.CRITICAL: colorama.Back.RED,
    }

    def __init__(self, stream, color_map=None):
        logging.StreamHandler.__init__(self,
                                       colorama.AnsiToWin32(stream).stream)
        if color_map is not None:
            self.color_map = color_map

    @property
    def is_tty(self):
        isatty = getattr(self.stream, 'isatty', None)
        return isatty and isatty()

    def format(self, record):
        message = logging.StreamHandler.format(self, record)
        if self.is_tty:
            # Don't colorize a traceback
            parts = message.split('\n', 1)
            parts[0] = self.colorize(parts[0], record)
            message = '\n'.join(parts)
        return message

    def colorize(self, message, record):
        try:
            return (self.color_map[record.levelno] + message +
                    colorama.Style.RESET_ALL)
        except KeyError:
            return message

log = logging.getLogger()
handler = ColorizingStreamHandler(sys.stdout)
formatter = log.Formatter('%(message)s')
handler.setFormatter(formatter)
log.addHandler(handler)
log.setLevel(logging.DEBUG)
<commit_msg>fix the logging<commit_after>import os
import colorama
import logging
import sys

__copyright__ = """
   Copyright 2016 Anthony Shaw

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""
__version__ = '1.0.0'

__doc__ = """Mocker.

Usage:
  mocker pull <name>[<tag>]
  mocker run <name>
  mocker images
  mocker (-h | --help)
  mocker --version

Options:
  -h --help     Show this screen.
  --version     Show version.
"""

_base_dir_ = os.path.join(os.path.expanduser('~'), 'mocker')

if not os.path.exists(_base_dir_):
    os.makedirs(_base_dir_)

# Courtesy http://plumberjack.blogspot.com/2010/12/colorizing-logging-output-in-terminals.html
# Tweaked to use colorama for the coloring

class ColorizingStreamHandler(logging.StreamHandler):
    color_map = {
        logging.DEBUG: colorama.Style.DIM + colorama.Fore.CYAN,
        logging.WARNING: colorama.Fore.YELLOW,
        logging.ERROR: colorama.Fore.RED,
        logging.CRITICAL: colorama.Back.RED,
    }

    def __init__(self, stream, color_map=None):
        logging.StreamHandler.__init__(self,
                                       colorama.AnsiToWin32(stream).stream)
        if color_map is not None:
            self.color_map = color_map

    @property
    def is_tty(self):
        isatty = getattr(self.stream, 'isatty', None)
        return isatty and isatty()

    def format(self, record):
        message = logging.StreamHandler.format(self, record)
        if self.is_tty:
            # Don't colorize a traceback
            parts = message.split('\n', 1)
            parts[0] = self.colorize(parts[0], record)
            message = '\n'.join(parts)
        return message

    def colorize(self, message, record):
        try:
            return (self.color_map[record.levelno] + message +
                    colorama.Style.RESET_ALL)
        except KeyError:
            return message

log = logging.getLogger()
handler = ColorizingStreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
log.addHandler(handler)
log.setLevel(logging.DEBUG)
<|endoftext|>


<commit_before>import django

if django.VERSION > (1, 8,):
    from collections import OrderedDict
else:
    from django.utils.datastructures import SortedDict as OrderedDict  # noqa

if django.VERSION > (1, 8,):
    from django.template.loader import render_to_string
else:
    from django.template.loader import render_to_string as _render_to_string
    from django.template import RequestContext

    # Wire the Django >= 1.8 API to the Django < 1.7 implementation.
    def render_to_string(
            template_name, context=None, request=None, using=None):
        assert (
            using is None, "Multiple template engines required Django >= 1.8")
        return _render_to_string(
            template_name, context, RequestContext(request))

try:
    from urllib.parse import parse_qsl, parse_qs, urlparse, urlunparse
except ImportError:
    from urlparse import parse_qsl, parse_qs, urlparse, urlunparse  # noqa

try:
    import importlib
except ImportError:
    from django.utils import importlib  # noqa

if django.VERSION >= (1, 9, 0):
    from django.contrib.auth.password_validation import validate_password
else:
    def validate_password(password, user=None, password_validators=None):
        pass
<commit_msg>flake8 caught bad assert, fixed<commit_after>import django

if django.VERSION > (1, 8,):
    from collections import OrderedDict
else:
    from django.utils.datastructures import SortedDict as OrderedDict  # noqa

if django.VERSION > (1, 8,):
    from django.template.loader import render_to_string
else:
    from django.template.loader import render_to_string as _render_to_string
    from django.template import RequestContext

    # Wire the Django >= 1.8 API to the Django < 1.7 implementation.
    def render_to_string(
            template_name, context=None, request=None, using=None):
        assert using is None, \
            "Multiple template engines required Django >= 1.8"
        return _render_to_string(
            template_name, context, RequestContext(request))

try:
    from urllib.parse import parse_qsl, parse_qs, urlparse, urlunparse
except ImportError:
    from urlparse import parse_qsl, parse_qs, urlparse, urlunparse  # noqa

try:
    import importlib
except ImportError:
    from django.utils import importlib  # noqa

if django.VERSION >= (1, 9, 0):
    from django.contrib.auth.password_validation import validate_password
else:
    def validate_password(password, user=None, password_validators=None):
        pass
<|endoftext|>


<commit_before>import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('', 8889))
serversocket.listen(5) # become a server socket, maximum 5 connections

print("Listening...")

while True:
    conn, address = serversocket.accept()
    print 'Connected with ', address
    buf = conn.recv(1024)
    if len(buf) > 0:
        print buf

        #TODO: configure cobbler according to the server info in buff.
        conn.send("reboot")
        continue
<commit_msg>modify service port<commit_after>import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('', 8888))
serversocket.listen(5) # become a server socket, maximum 5 connections

print("Listening...")

while True:
    conn, address = serversocket.accept()
    print 'Connected with ', address
    buf = conn.recv(1024)
    if len(buf) > 0:
        print buf

        #TODO: configure cobbler according to the server info in buff.
        conn.send("reboot")
        continue
<|endoftext|>