<commit_before>import copy, os, hashlib, platform, re, sys, time

def clone(o):

    return copy.deepcopy(o)

class Exception(Exception):

    pass

DEBUG = 'DRAKE_DEBUG' in os.environ

def debug(msg):

    global DEBUG
    if DEBUG:
        print >> sys.stderr, msg

class Path:

    def __init__(self, path):

        self.absolute = False
        if path.__class__ == list:
            self.path = path
        elif path.__class__ == Path:
            self.path = clone(path.path)
            self.absolute = path.absolute
        else:
            assert path
            if platform.system() == 'Windows':
                self.path = re.split(r'/|\\', path)
                self.absolute = bool(self.path[0] == '' or re.compile('^[A-Z]:').match(self.path[0]))
            else:
                self.path = path.split('/')
                self.absolute = self.path[0] == ''

    def __getattr__(self, name):

        if name == 'extension':
            parts = self.path[-1].split('.')
            if len(parts) > 1:
                return parts[-1]
            else:
                return ''
        return object.__getattr__(self, name)

    def __setattr__(self, name, value):

        if name == 'extension':
            parts = self.path[-1].split('.')
            if len(parts) > 1:
                if value == '':
                    parts.pop()
                else:
                    parts[-1] = value
                self.path[-1] = '.'.join(parts)
            else:
                if value != '':
                    self.path[-1] += '.%s' % value
        else:
            self.__dict__[name] = value
        return value

    def __str__(self):

        return '/'.join(self.path)

    def __repr__(self):

        return 'Path(\"%s\")' % str(self)

    def __lt__(self, rhs):

        return str(self) < str(rhs)

    def __hash__(self):

        return hash(str(self))

    def exists(self):

        return os.path.exists(str(self))

    def basename(self):

        return Path(self.path[-1:])

    def dirname(self):

        return Path(self.path[0:-1])

    def touch(self):

        self.dirname().mkpath()
        if not os.path.exists(str(self)):
            open(str(self), 'w').close()

    def mkpath(self):

        if not os.path.exists(str(self)):
            os.makedirs(str(self))

    def __eq__(self, rhs):

        if rhs.__class__ != Path:
            rhs = Path(rhs)
        return self.path == rhs.path

    def __div__(self, rhs):

        rhs = Path(rhs)

        if self == '.':
            return rhs
        if rhs == Path('.'):
            return clone(self)

        res = clone(self)
        res.path += rhs.path
        return res

    def strip_prefix(self, rhs):

        if self.path[0:len(rhs.path)] != rhs.path:
            raise Exception("%s is not a prefix of %s" % (rhs, self))
        self.path = self.path[len(rhs.path):]
        if not self.path:
            self.path = ['.']

CACHEDIR = Path('.drake')

class DepFile:

    def __init__(self, builder, name):

        self.builder = builder
        self.name = name
        builder.dsts.sort()
        self._files = {}
        self._sha1 = {}

    def files(self):

        return self._files.values()

    def sha1s(self):

        return self._sha1

    def register(self, node):

        self._files[str(node.path())] = node

    def path(self):

        return self.builder.cachedir() / self.name

    def read(self):

        res = []

        self.path().touch()
        for line in open(str(self.path()), 'r'):
            sha1 = line[:40]
            src = Path(line[41:-1]) # Chomp the \n
            self._sha1[str(src)] = sha1

    def up_to_date(self):

        for path in self._sha1:
            assert str(path) in Node.nodes
            h = hashlib.sha1(open(str(node(path).path())).read()).hexdigest()
            if self._sha1[path] != h:
                debug('  Execution needed because hash is outdated: %s.' % path)
                return False

        return True

    def update(self):

        f = open(str(self.path()), 'w')
        for path in self._files:
            h = hashlib.sha1(open(path).read()).hexdigest()
            print >>f, '%s %s' % (h, self._files[path].id())

    def __repr__(self):

        return 'DepFile(%s)' % repr(self.node)

    def __str__(self):

        return 'DepFile(%s)' % self.node

class Node:

    nodes = {}
    uid = 0
    extensions = {}

    def __init__(self, path):

        if path.__class__ == str:
            path = Path(path)

        self.sym_path = path
        self.src_path = prefix() / path

        self.uid = Node.uid
        Node.uid += 1

        self.builder = None
        self.srctree = srctree()
        Node.nodes[str(self.id())] = self

    def clean(self):

        if self.builder is not None:
            self.builder.clean()
            if self.path().exists():
                print 'Deleting %s' % self
                os.remove(str(self.path()))

    def path(self):

        if self.src_path.absolute:
            assert self.builder is None
            return self.src_path

        if self.builder is None:
            return self.srctree / self.src_path
        else:
            return self.src_path

    def id(self):

        return self.src_path

    def build(self):

        debug('Building %s' % self)
        if self.builder is None:
            if not self.path().exists():
                raise Exception('no builder to make %s' % self)
            return

        self.builder.run()

    def __setattr__(self, name, value):

        if name == 'builder' and 'builder' in self.__dict__:
            del self.nodes[str(self.id())]
            self.__dict__[name] = value
            self.nodes[str(self.path())] = self
        else:
            self.__dict__[name] = value

    def __repr__(self):

        return str(self.path())

    def __str__(self):

        return str(self.path())

    def __lt__(self, rhs):

        return self.path() < rhs.path()

def node(path):

    if path.__class__ != Path:
        path = Path(path)

    if str(path) in Node.nodes:
        return Node.nodes[str(path)]

    if path.extension not in Node.extensions:
        raise Exception('unknown file type: %s' % path)

    return Node.extensions[path.extension](path)

def nodes(*paths):

    return map(node, paths)

class Builder:

    builders = []
    uid = 0

    name = 'build'
    _deps_handlers = {}

    @classmethod
    def register_deps_handler(self, name, f):
        self._deps_handlers[name] = f

    def __init__(self, srcs, dsts):

        assert srcs.__class__ == list
        self.srcs = {}
        for src in srcs:
            self.add_src(src)
#        self.srcs = srcs
        self.dsts = dsts
        for dst in dsts:
            if dst.builder is not None:
                raise Exception('builder redefinition for %s' % dst)
            dst.builder = self

        self.uid = Builder.uid
        Builder.uid += 1
        Builder.builders.append(self)

        self._depfiles = {}
        self._depfile = DepFile(self, 'drake')
        self.built = False
        self.dynsrc = {}

    def cachedir(self):

        path = self.dsts[0].path()
        res = prefix() / path.dirname() / CACHEDIR / path.basename()
        res.mkpath()
        return res

    def dependencies(self):

        return []

    def depfile(self, name):

        if name not in self._depfiles:
            self._depfiles[name] = DepFile(self, name)
        return self._depfiles[name]

    def add_dynsrc(self, name, node, data = None):

        self.depfile(name).register(node)
        self.dynsrc[str(node.path())] = node

    def run(self):

        # If we were already executed, just skip
        if self.built:
            debug('  Already built in this run.')
            return

        # The list of static dependencies is now fixed
        for path in self.srcs:
            self._depfile.register(self.srcs[path])

        # See Whether we need to execute or not
        execute = False

        # Reload dynamic dependencies
        if not execute:
            for f in os.listdir(str(self.cachedir())):
                if f == 'drake':
                    continue
                depfile = self.depfile(f)
                depfile.read()
                handler = self._deps_handlers[f]

                for path in depfile.sha1s():

                    if path in self.srcs or path in self.dynsrc:
                        continue

                    if path in Node.nodes:
                        node = Node.nodes[path]
                    else:
                        node = handler(self, path, None)

                    self.add_dynsrc(f, node, None)

        # Build all dependencies
        for path in self.srcs:
            self.srcs[path].build()
        for path in self.dynsrc:
            try:
                self.dynsrc[path].build()
            except Exception, e:
                debug('  Execution needed because dynamic dependency couldn\'t be built: %s.' % path)
                execute = True

        # If any target is missing, we must rebuild.
        if not execute:
            for dst in self.dsts:
                if not dst.path().exists():
                    debug('  Execution needed because of missing target: %s.' % dst.path())
                    execute = True

        # Load static dependencies
        self._depfile.read()

        # If a new dependency appeared, we must rebuild.
        if not execute:
            for p in self.srcs:
                path = self.srcs[p].id()
                if path not in self._depfile._sha1:
                    debug('  Execution needed because a new dependency appeared: %s.' % path)
                    execute = True
                    break

        # Check if we are up to date wrt all dependencies
        if not execute:
            if not self._depfile.up_to_date():
                execute = True
            for f in self._depfiles:
                if not self._depfiles[f].up_to_date():
                    execute = True

        if execute:

            # Regenerate dynamic dependencies
            self.dynsrc = {}
            self._depfiles = {}
            self.dependencies()
            for path in self.dynsrc:
                self.dynsrc[path].build()

            if not self.execute():
                raise Exception('%s failed' % self.name)
            for dst in self.dsts:
                if not dst.path().exists():
                    raise Exception('%s wasn\'t created by %s' % (dst, self))
            self._depfile.update()
            for name in self._depfiles:
                self._depfiles[name].update()
            self.built = True
        else:
            debug('  Everything is up to date.')

    def execute(self):

        raise Exception('execute is not implemented for %s' % self)

    def clean(self):

        for path in self.srcs:
            self.srcs[path].clean()

    def __str__(self):

        return self.__class__.__name__

    def cmd(self, fmt, *args):

        rg = re.compile('\'')

        args = map(str, args)
        for arg in args:
            if rg.match(arg):
                pass
        command = fmt % tuple(args)
        print command
        return os.system(command) == 0

    def add_src(self, src):

        self.srcs[str(src.path())] = src

    def all_srcs(self):

        res = []
        for src in self.srcs.values() + self.dynsrc.values():
            res.append(src)
            if src.builder is not None:
                res += src.builder.all_srcs()
        return res

class ShellCommand(Builder):

    def __init__(self, srcs, dsts, fmt, *args):

        Builder.__init__(self, srcs, dsts)
        self.fmt = fmt
        self.args = args

    def execute(self):

        return self.cmd(self.fmt, *self.args)

def shell_escape(str):

    # FIXME: escape only if needed
    # FIXME: complete that
    return '"%s"' % str

_prefix = Path('.')

def prefix():
    return _prefix

_srctree = Path('.')

def set_srctree(path):

    global _srctree
    _srctree = Path(path)

def srctree():

    global _srctree
    return _srctree

def strip_srctree(path):

    global _srctree
    res = clone(path)
    if not path.absolute:
        res.strip_prefix(_srctree)
    return res

class Module:

    def __init__(self, globals):

        self.globals = globals

    def __getattr__(self, name):

        return self.globals[name]

def include(path, *args, **kwargs):

    global _prefix

    path = Path(path)
    previous_prefix = _prefix
    _prefix = _prefix / path
    res = raw_include(str(previous_prefix / path / 'drakefile.py'), *args, **kwargs)
    _prefix = previous_prefix
    return res

def raw_include(path, *args, **kwargs):

    g = {}
    execfile(str(srctree() / path), g)
    res = Module(g)
    res.configure(*args, **kwargs)
    return res

def dot(*filters):

    def take(path):
        for filter in filters:
            if re.compile(filter).search(path):
                return False
        return True

    print 'digraph'
    print '{'
    for path in Node.nodes:
        if take(path):
            node = Node.nodes[path]
            print '  node_%s [label="%s"]' % (node.uid, node.path())
    print
    for builder in Builder.builders:
        print '  builder_%s [label="%s", shape=rect]' % (builder.uid, builder.__class__)
        for src in builder.srcs:
            if take(src):
                print '  node_%s -> builder_%s' % (builder.srcs[src].uid, builder.uid)
        for src in builder.dynsrc:
            if take(src):
                print '  node_%s -> builder_%s' % (builder.dynsrc[src].uid, builder.uid)
        for dst in builder.dsts:
            if take(str(dst.path())):
                print '  builder_%s -> node_%s' % (builder.uid, dst.uid)
    print '}'

# Architectures
x86 = 0

# OSes
linux = 0
macos = 1
windows = 2

<commit_msg>Add debug, automatize indentation.<commit_after>import copy, os, hashlib, platform, re, sys, time

def clone(o):

    return copy.deepcopy(o)

class Exception(Exception):

    pass

DEBUG = 'DRAKE_DEBUG' in os.environ
DEBUG_LVL = 0

def debug(msg):

    global DEBUG
    if DEBUG:
        print >> sys.stderr, '%s%s' % (' ' * DEBUG_LVL * 2, msg)

class indentation:
    def __enter__(self):
        global DEBUG_LVL
        DEBUG_LVL += 1
    def __exit__(self, type, value, traceback):
        global DEBUG_LVL
        DEBUG_LVL -= 1

class Path:

    def __init__(self, path):

        self.absolute = False
        if path.__class__ == list:
            self.path = path
        elif path.__class__ == Path:
            self.path = clone(path.path)
            self.absolute = path.absolute
        else:
            assert path
            if platform.system() == 'Windows':
                self.path = re.split(r'/|\\', path)
                self.absolute = bool(self.path[0] == '' or re.compile('^[A-Z]:').match(self.path[0]))
            else:
                self.path = path.split('/')
                self.absolute = self.path[0] == ''

    def __getattr__(self, name):

        if name == 'extension':
            parts = self.path[-1].split('.')
            if len(parts) > 1:
                return parts[-1]
            else:
                return ''
        return object.__getattr__(self, name)

    def __setattr__(self, name, value):

        if name == 'extension':
            parts = self.path[-1].split('.')
            if len(parts) > 1:
                if value == '':
                    parts.pop()
                else:
                    parts[-1] = value
                self.path[-1] = '.'.join(parts)
            else:
                if value != '':
                    self.path[-1] += '.%s' % value
        else:
            self.__dict__[name] = value
        return value

    def __str__(self):

        return '/'.join(self.path)

    def __repr__(self):

        return 'Path(\"%s\")' % str(self)

    def __lt__(self, rhs):

        return str(self) < str(rhs)

    def __hash__(self):

        return hash(str(self))

    def exists(self):

        return os.path.exists(str(self))

    def basename(self):

        return Path(self.path[-1:])

    def dirname(self):

        return Path(self.path[0:-1])

    def touch(self):

        self.dirname().mkpath()
        if not os.path.exists(str(self)):
            open(str(self), 'w').close()

    def mkpath(self):

        if not os.path.exists(str(self)):
            os.makedirs(str(self))

    def __eq__(self, rhs):

        if rhs.__class__ != Path:
            rhs = Path(rhs)
        return self.path == rhs.path

    def __div__(self, rhs):

        rhs = Path(rhs)

        if self == '.':
            return rhs
        if rhs == Path('.'):
            return clone(self)

        res = clone(self)
        res.path += rhs.path
        return res

    def strip_prefix(self, rhs):

        if self.path[0:len(rhs.path)] != rhs.path:
            raise Exception("%s is not a prefix of %s" % (rhs, self))
        self.path = self.path[len(rhs.path):]
        if not self.path:
            self.path = ['.']

CACHEDIR = Path('.drake')

class DepFile:

    def __init__(self, builder, name):

        self.builder = builder
        self.name = name
        builder.dsts.sort()
        self._files = {}
        self._sha1 = {}

    def files(self):

        return self._files.values()

    def sha1s(self):

        return self._sha1

    def register(self, node):

        self._files[str(node.path())] = node

    def path(self):

        return self.builder.cachedir() / self.name

    def read(self):

        res = []

        self.path().touch()
        for line in open(str(self.path()), 'r'):
            sha1 = line[:40]
            src = Path(line[41:-1]) # Chomp the \n
            self._sha1[str(src)] = sha1

    def up_to_date(self):

        for path in self._sha1:
            assert str(path) in Node.nodes
            h = hashlib.sha1(open(str(node(path).path())).read()).hexdigest()
            if self._sha1[path] != h:
                debug('Execution needed because hash is outdated: %s.' % path)
                return False

        return True

    def update(self):

        f = open(str(self.path()), 'w')
        for path in self._files:
            h = hashlib.sha1(open(path).read()).hexdigest()
            print >>f, '%s %s' % (h, self._files[path].id())

    def __repr__(self):

        return 'DepFile(%s)' % repr(self.node)

    def __str__(self):

        return 'DepFile(%s)' % self.node

class Node:

    nodes = {}
    uid = 0
    extensions = {}

    def __init__(self, path):

        if path.__class__ == str:
            path = Path(path)

        self.sym_path = path
        self.src_path = prefix() / path

        self.uid = Node.uid
        Node.uid += 1

        self.builder = None
        self.srctree = srctree()
        Node.nodes[str(self.id())] = self

    def clean(self):

        if self.builder is not None:
            self.builder.clean()
            if self.path().exists():
                print 'Deleting %s' % self
                os.remove(str(self.path()))

    def path(self):

        if self.src_path.absolute:
            assert self.builder is None
            return self.src_path

        if self.builder is None:
            return self.srctree / self.src_path
        else:
            return self.src_path

    def id(self):

        return self.src_path

    def build(self):

        debug('Building %s' % self)
        with indentation():
            if self.builder is None:
                if not self.path().exists():
                    raise Exception('no builder to make %s' % self)
                return

            self.builder.run()

    def __setattr__(self, name, value):

        if name == 'builder' and 'builder' in self.__dict__:
            del self.nodes[str(self.id())]
            self.__dict__[name] = value
            self.nodes[str(self.path())] = self
        else:
            self.__dict__[name] = value

    def __repr__(self):

        return str(self.path())

    def __str__(self):

        return str(self.path())

    def __lt__(self, rhs):

        return self.path() < rhs.path()

def node(path):

    if path.__class__ != Path:
        path = Path(path)

    if str(path) in Node.nodes:
        return Node.nodes[str(path)]

    if path.extension not in Node.extensions:
        raise Exception('unknown file type: %s' % path)

    return Node.extensions[path.extension](path)

def nodes(*paths):

    return map(node, paths)

class Builder:

    builders = []
    uid = 0

    name = 'build'
    _deps_handlers = {}

    @classmethod
    def register_deps_handler(self, name, f):
        self._deps_handlers[name] = f

    def __init__(self, srcs, dsts):

        assert srcs.__class__ == list
        self.srcs = {}
        for src in srcs:
            self.add_src(src)
#        self.srcs = srcs
        self.dsts = dsts
        for dst in dsts:
            if dst.builder is not None:
                raise Exception('builder redefinition for %s' % dst)
            dst.builder = self

        self.uid = Builder.uid
        Builder.uid += 1
        Builder.builders.append(self)

        self._depfiles = {}
        self._depfile = DepFile(self, 'drake')
        self.built = False
        self.dynsrc = {}

    def cachedir(self):

        path = self.dsts[0].path()
        res = prefix() / path.dirname() / CACHEDIR / path.basename()
        res.mkpath()
        return res

    def dependencies(self):

        return []

    def depfile(self, name):

        if name not in self._depfiles:
            self._depfiles[name] = DepFile(self, name)
        return self._depfiles[name]

    def add_dynsrc(self, name, node, data = None):

        self.depfile(name).register(node)
        self.dynsrc[str(node.path())] = node

    def run(self):

        # If we were already executed, just skip
        if self.built:
            debug('Already built in this run.')
            return

        # The list of static dependencies is now fixed
        for path in self.srcs:
            self._depfile.register(self.srcs[path])

        # See Whether we need to execute or not
        execute = False

        # Reload dynamic dependencies
        if not execute:
            for f in os.listdir(str(self.cachedir())):
                if f == 'drake':
                    continue
                debug('Considering dependencies file %s' % f)
                depfile = self.depfile(f)
                depfile.read()
                handler = self._deps_handlers[f]

                with indentation():
                    for path in depfile.sha1s():

                        if path in self.srcs or path in self.dynsrc:
                            debug('File %s is already in our sources.' % path)
                            continue

                        if path in Node.nodes:
                            node = Node.nodes[path]
                        else:
                            debug('File %s is unknown, calling handler.' % path)
                            node = handler(self, path, None)

                        debug('Adding %s to our sources.' % path)
                        self.add_dynsrc(f, node, None)

        # Build dynamic dependencies
        for path in self.dynsrc:
            try:
                self.dynsrc[path].build()
            except Exception, e:
                debug('Execution needed because dynamic dependency couldn\'t be built: %s.' % path)
                execute = True

        # Build static dependencies
        for path in self.srcs:
            self.srcs[path].build()

        # If any target is missing, we must rebuild.
        if not execute:
            for dst in self.dsts:
                if not dst.path().exists():
                    debug('Execution needed because of missing target: %s.' % dst.path())
                    execute = True

        # Load static dependencies
        self._depfile.read()

        # If a new dependency appeared, we must rebuild.
        if not execute:
            for p in self.srcs:
                path = self.srcs[p].id()
                if path not in self._depfile._sha1:
                    debug('Execution needed because a new dependency appeared: %s.' % path)
                    execute = True
                    break

        # Check if we are up to date wrt all dependencies
        if not execute:
            if not self._depfile.up_to_date():
                execute = True
            for f in self._depfiles:
                if not self._depfiles[f].up_to_date():
                    execute = True

        if execute:

            debug('Executing builder')
            # Regenerate dynamic dependencies
            self.dynsrc = {}
            self._depfiles = {}
            self.dependencies()
            for path in self.dynsrc:
                self.dynsrc[path].build()

            if not self.execute():
                raise Exception('%s failed' % self.name)
            for dst in self.dsts:
                if not dst.path().exists():
                    raise Exception('%s wasn\'t created by %s' % (dst, self))
            self._depfile.update()
            for name in self._depfiles:
                self._depfiles[name].update()
            self.built = True
        else:
            debug('Everything is up to date.')

    def execute(self):

        raise Exception('execute is not implemented for %s' % self)

    def clean(self):

        for path in self.srcs:
            self.srcs[path].clean()

    def __str__(self):

        return self.__class__.__name__

    def cmd(self, fmt, *args):

        rg = re.compile('\'')

        args = map(str, args)
        for arg in args:
            if rg.match(arg):
                pass
        command = fmt % tuple(args)
        print command
        return os.system(command) == 0

    def add_src(self, src):

        self.srcs[str(src.path())] = src

    def all_srcs(self):

        res = []
        for src in self.srcs.values() + self.dynsrc.values():
            res.append(src)
            if src.builder is not None:
                res += src.builder.all_srcs()
        return res

class ShellCommand(Builder):

    def __init__(self, srcs, dsts, fmt, *args):

        Builder.__init__(self, srcs, dsts)
        self.fmt = fmt
        self.args = args

    def execute(self):

        return self.cmd(self.fmt, *self.args)

def shell_escape(str):

    # FIXME: escape only if needed
    # FIXME: complete that
    return '"%s"' % str

_prefix = Path('.')

def prefix():
    return _prefix

_srctree = Path('.')

def set_srctree(path):

    global _srctree
    _srctree = Path(path)

def srctree():

    global _srctree
    return _srctree

def strip_srctree(path):

    global _srctree
    res = clone(path)
    if not path.absolute:
        res.strip_prefix(_srctree)
    return res

class Module:

    def __init__(self, globals):

        self.globals = globals

    def __getattr__(self, name):

        return self.globals[name]

def include(path, *args, **kwargs):

    global _prefix

    path = Path(path)
    previous_prefix = _prefix
    _prefix = _prefix / path
    res = raw_include(str(previous_prefix / path / 'drakefile.py'), *args, **kwargs)
    _prefix = previous_prefix
    return res

def raw_include(path, *args, **kwargs):

    g = {}
    execfile(str(srctree() / path), g)
    res = Module(g)
    res.configure(*args, **kwargs)
    return res

def dot(*filters):

    def take(path):
        for filter in filters:
            if re.compile(filter).search(path):
                return False
        return True

    print 'digraph'
    print '{'
    for path in Node.nodes:
        if take(path):
            node = Node.nodes[path]
            print '  node_%s [label="%s"]' % (node.uid, node.path())
    print
    for builder in Builder.builders:
        print '  builder_%s [label="%s", shape=rect]' % (builder.uid, builder.__class__)
        for src in builder.srcs:
            if take(src):
                print '  node_%s -> builder_%s' % (builder.srcs[src].uid, builder.uid)
        for src in builder.dynsrc:
            if take(src):
                print '  node_%s -> builder_%s' % (builder.dynsrc[src].uid, builder.uid)
        for dst in builder.dsts:
            if take(str(dst.path())):
                print '  builder_%s -> node_%s' % (builder.uid, dst.uid)
    print '}'

# Architectures
x86 = 0

# OSes
linux = 0
macos = 1
windows = 2

<|endoftext|>


<commit_before>"""
A command-line tool to test the optimizations performed to a program
"""
import os
import sys
import time
import subprocess
from reticular import argument, command

@argument('prog_file', help='File of the program to test')
def benchmark(prog_file):
    """
    Benchmark execution time of a program
    """
    if not os.path.isfile(prog_file):
        raise RuntimeError('File not found: %s' % prog_file)

    compile(prog_file, out="o0.exe")
    compile(prog_file, olevel=1, out="o1.exe")
    compile(prog_file, olevel=3, native=True, out="o3.exe")

    with open('o0.stats', 'w') as f:
        execute("o0.exe", 3, statsfile=f)

    with open('o1.stats', 'w') as f:
        execute("o1.exe", 3, statsfile=f)

    with open('o3.stats', 'w') as f:
        execute("o3.exe", 3, statsfile=f)

@argument('-s', '--stats', help='Clean .stats files', action='store_true')
def clean(stats):
    """
    Cleans fast-generated files of the current directory
    """
    ends = ['.exe']

    if stats:
        ends.append('.stats')

    remove_files = (f for f in os.listdir('.') if os.path.splitext(f)[-1] in ends)

    for f in remove_files:
        os.remove(f)
        print 'Removed: %s' % f

def compile(source, compiler='gcc', olevel=0, debug=False, profiling=False, native=False, out='_exec'):
    cmd = [compiler, '-O%d' % olevel]

    if debug:
        cmd.append('-g')
    if profiling:
        cmd.append('-pg')
    if native:
        cmd.append('-march=native')

    cmd.extend(['-o', out])
    cmd.append(source)

    ret = subprocess.call(cmd)
    return ret == 0

def run(exe, *args):
    cmd = ["./%s"%exe]
    cmd.extend(map(str, args))

    start = time.time()
    subprocess.call(cmd, stdout=open("/dev/null"))
    elapsed = time.time() - start
    # TODO: Check correctness of output
    return elapsed

def execute(exe, NEXEC, NMIN=500, NMAX=10000, NSTEP=500, statsfile=sys.stdout):
    for decimals in xrange(NMIN, NMAX+1, NSTEP):
        times = []
        for i in xrange(NEXEC):
            times.append(run(exe, decimals))
        statsfile.write("%d %.4f\n" % (decimals, sum(times)/NEXEC))
<commit_msg>Adding some interdasting stuff.<commit_after>"""
A command-line tool to test the optimizations performed to a program
"""
import os
import re
import sys
import subprocess
from reticular import argument

@argument('prog_file', help='File of the program to benchmark')
def benchmark(prog_file):
    """
    Compiles with O0, O1 and O3 and
    """
    if not os.path.isfile(prog_file):
        raise RuntimeError('File not found: %s' % prog_file)

    executables = [
        compile(prog_file),
        compile(prog_file, olevel=1),
        compile(prog_file, olevel=3, native=True)
    ]

    for executable in executables:
        stats(executable)

@argument('executable', help='Executable file')
@argument('-q', '--quiet', help="Don't print the stats table", action='store_true')
def stats(executable, quiet=False, executions=3, decimals_min=500, decimals_max=10000, step=500):
    name, ext = os.path.splitext(executable)

    print "Generating stats for %s..." % executable
    with open("%s.stats" % name, 'w') as f:
        generate_stats(executable, executions, quiet=quiet, statsfile=f, NMIN=decimals_min, NMAX=decimals_max,
                       NSTEP=step)

@argument('-s', '--stats', help='Clean .stats files', action='store_true')
def clean(stats):
    """
    Cleans fast-generated files of the current directory
    """
    ends = ['.exe']

    if stats:
        ends.append('.stats')

    remove_files = (f for f in os.listdir('.') if os.path.splitext(f)[-1] in ends)

    for f in remove_files:
        os.remove(f)
        print 'Removed: %s' % f

def compile(source, compiler='gcc', olevel=0, debug=False, profiling=False, native=False, out=None):
    cmd = [compiler, '-O%d' % olevel]

    if out is None:
        prog_name, ext = os.path.splitext(source)
        out = "%s_o%d.exe" % (prog_name, olevel)

    if debug:
        cmd.append('-g')
    if profiling:
        cmd.append('-pg')
    if native:
        cmd.append('-march=native')

    cmd.extend(['-o', out])
    cmd.append(source)

    print "Compiling %s..." % source
    if subprocess.call(cmd):
        raise RuntimeError("Error when compiling: %s" % source)

    return out

def run(exe, *args):
    cmd = ['time', '-f', 'fast|%e|fast', "./%s" % exe]
    cmd.extend(map(str, args))

    process = subprocess.Popen(cmd, stdout=open("/dev/null"), stderr=subprocess.PIPE)
    out, err = process.communicate()

    elapsed = float(re.search(r'fast\|(\d+\.\d+)\|fast', err).group(1))

    return elapsed

def generate_stats(exe, NEXEC, statsfile=None, NMIN=500, NMAX=10000, NSTEP=500, quiet=False):
    for decimals in xrange(NMIN, NMAX+1, NSTEP):
        times = []

        for i in xrange(NEXEC):
            times.append(run(exe, decimals))

        row = "%d %.4f\n" % (decimals, sum(times)/NEXEC)

        if statsfile:
            statsfile.write(row)

        if not quiet:
            sys.stdout.write(row)
<|endoftext|>


<commit_before>import unittest
import asninja

class TestRefLibrary(unittest.TestCase):

    def test_lib_name(self):
        reflib = asninja.RefLibrary('', 'Center')
        self.assertEquals('libCenter', reflib.lib_name(False))
        self.assertEquals('libCenter.a', reflib.lib_name(True))

    def test_full_name(self):
        reflib = asninja.RefLibrary('Path', 'Center')
        self.assertEquals('Path/Debug/libCenter.a', reflib.full_name('Debug'))

    def test_extract_name(self):
        self.assertEquals('Center', asninja.RefLibrary.extract_name('Center'))
        self.assertEquals('Center', asninja.RefLibrary.extract_name('libCenter'))
        self.assertEquals('Center', asninja.RefLibrary.extract_name('Center.a'))
        self.assertEquals('Center', asninja.RefLibrary.extract_name('libCenter.a'))

class TestFunctions(unittest.TestCase):

    def test_strip_updir(self):
        self.assertEquals('Path', asninja.strip_updir('Path'))
        self.assertEquals('Path', asninja.strip_updir('../Path'))
        self.assertEquals('Path', asninja.strip_updir('../../Path'))
        self.assertEquals('ath', asninja.strip_updir('..Path'))

    def test_detect_linker_script(self):
        self.assertEquals('linker_script', asninja.detect_linker_script(['bla -T../linker_script']))
        self.assertEquals('linker_script', asninja.detect_linker_script(['bla -T../linker_script bla']))
        self.assertIsNone(asninja.detect_linker_script(['linker_script']))

if __name__ == '__main__':
    unittest.main()
<commit_msg>Add tests<commit_after>import os
import unittest
import asninja

class TestAtmelStudioProject(unittest.TestCase):

    def setUp(self):
        self.asp = asninja.AtmelStudioProject('Korsar3.cproj', 'Korsar3')

    def tearDown(self):
        self.asp = None

    def test_init(self):
        self.assertIsNone(self.asp.config_group)
        self.assertFalse(self.asp.is_cpp)
        self.assertFalse(self.asp.is_lib)
        self.assertEquals('Korsar3', self.asp.output_name)
        self.assertEquals('.elf', self.asp.output_ext)
        self.assertEquals('ArmGcc', self.asp.toolchain_settings)
        self.assertIsNotNone(self.asp.ref_libs)
        self.assertIsInstance(self.asp.ref_libs, list)
        self.assertLess(0, len(self.asp.ref_libs))

    def test_output(self):
        self.assertEquals('Korsar3.elf', self.asp.output())

    def test_select_config(self):
        self.assertTrue(self.asp.select_config('Debug'))
        self.assertIsNotNone(self.asp.config_group)

        self.assertTrue(self.asp.select_config('Release'))
        self.assertIsNotNone(self.asp.config_group)

        self.assertFalse(self.asp.select_config('NonExists'))
        self.assertIsNone(self.asp.config_group)

    def test_key_raw(self):
        self.assertTrue(self.asp.select_config('Debug'))

        self.assertIsNotNone(self.asp.key_raw('armgcc.common.outputfiles.hex'))
        self.assertIsNone(self.asp.key_raw('NonExists'))

    def test_key_as_bool(self):
        self.assertTrue(self.asp.select_config('Debug'))

        self.assertTrue(self.asp.key_as_bool('armgcc.compiler.warnings.AllWarnings', False))
        self.assertFalse(self.asp.key_as_bool('armgcc.common.outputfiles.hex', True))
        self.assertFalse(self.asp.key_as_bool('NonExists', False))
        self.assertTrue(self.asp.key_as_bool('NonExists', True))

    def test_key_as_str(self):
        self.assertTrue(self.asp.select_config('Debug'))

        self.assertEquals('bla-Maximum (-g3)',
                          self.asp.key_as_str('armgcc.compiler.optimization.DebugLevel', 'bla-{}', '',))
        self.assertEquals('Dummy', self.asp.key_as_str('NonExists', 'bla-{}', 'Dummy',))

    def test_key_as_strlist(self):
        self.assertTrue(self.asp.select_config('Debug'))

        keys = self.asp.key_as_strlist('armgcc.compiler.symbols.DefSymbols', 'bla-{}')
        self.assertIsNotNone(keys)
        self.assertIsInstance(keys, list)
        self.assertLess(0, len(keys))
        for key in keys:
            self.assertIn('bla-', key)

        keys = self.asp.key_as_strlist('NonExists', 'bla-{}')
        self.assertIsNotNone(keys)
        self.assertIsInstance(keys, list)
        self.assertEquals(0, len(keys))

    def test_src_files(self):
        files = self.asp.src_files()
        self.assertIsNotNone(files)
        self.assertIsInstance(files, list)
        self.assertLess(0, len(files))
        for file in files:
            __, file_ext = os.path.splitext(file)
            self.assertTrue(file_ext in ['.c', '.cpp', '.h'], 'unexpected srcfile ext ' + file_ext)

    def test_compiler_flags(self):
        self.assertTrue(self.asp.select_config('Debug'))

        flags = self.asp.compiler_flags('armgcc', add_defs=['TestDef'])
        self.assertIsNotNone(flags)
        self.assertIsInstance(flags, list)
        self.assertLess(0, len(flags))

        self.assertTrue('-DTestDef' in flags)

    def test_compiler_flags_with_empty_def(self):
        self.assertTrue(self.asp.select_config('Debug'))

        flags = self.asp.compiler_flags('armgcc', add_defs=[''])
        self.assertFalse('-D' in flags)

    def test_linker_flags(self):
        self.assertTrue(self.asp.select_config('Debug'))

        flags = self.asp.linker_flags('Debug')
        self.assertIsNotNone(flags)
        self.assertIsInstance(flags, list)
        self.assertLess(0, len(flags))

    def test_archiver_flags(self):
        self.assertTrue(self.asp.select_config('Debug'))

        flags = self.asp.archiver_flags()
        self.assertIsNotNone(flags)
        self.assertIsInstance(flags, list)
        self.assertEquals(1, len(flags))
        self.assertEquals('-r', flags[0])

class TestRefLibrary(unittest.TestCase):

    def test_lib_name(self):
        reflib = asninja.RefLibrary('', 'Center')

        self.assertEquals('libCenter', reflib.lib_name(False))
        self.assertEquals('libCenter.a', reflib.lib_name(True))

    def test_full_name(self):
        reflib = asninja.RefLibrary('Path', 'Center')

        self.assertEquals('Path/Debug/libCenter.a', reflib.full_name('Debug'))

    def test_extract_name(self):
        self.assertEquals('Center', asninja.RefLibrary.extract_name('Center'))
        self.assertEquals('Center', asninja.RefLibrary.extract_name('libCenter'))
        self.assertEquals('Center', asninja.RefLibrary.extract_name('Center.a'))
        self.assertEquals('Center', asninja.RefLibrary.extract_name('libCenter.a'))

class TestFunctions(unittest.TestCase):

    def test_strip_updir(self):
        self.assertEquals('Path', asninja.strip_updir('Path'))
        self.assertEquals('Path', asninja.strip_updir('../Path'))
        self.assertEquals('Path', asninja.strip_updir('../../Path'))
        self.assertEquals('ath', asninja.strip_updir('..Path'))

    def test_detect_linker_script(self):
        self.assertEquals('linker_script', asninja.detect_linker_script(['bla -T../linker_script']))
        self.assertEquals('linker_script', asninja.detect_linker_script(['bla -T../linker_script bla']))
        self.assertIsNone(asninja.detect_linker_script(['linker_script']))

class TestConvert(unittest.TestCase):

    def test_(self):
        pass

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


<commit_before>from django.db import models

class Category(models.Model):
    title = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True, editable=False)

    def __str__(self):
        """We want the string representation of Category model to be its title"""
        return self.title
<commit_msg>Making sure the pluralized text of Category is Categories.<commit_after>from django.db import models

class Category(models.Model):
    title = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True, editable=False)

    def __str__(self):
        """We want the string representation of Category model to be its title"""
        return self.title

    class Meta:
        """Django will call this as Categorys, lets make it grammatically correct!"""
        verbose_name_plural = "categories"
<|endoftext|>


<commit_before># Copyright 2014-2020 Tecnativa - Pedro M. Baeza
# Copyright 2020 Tecnativa - Manuel Calero
{
    "name": "Sales commissions",
    "version": "13.0.1.4.1",
    "author": "Tecnativa," "Odoo Community Association (OCA)",
    "category": "Sales Management",
    "license": "AGPL-3",
    "depends": ["account", "product", "sale_management"],
    "website": "https://github.com/OCA/commission",
    "development_status": "Mature",
    "maintainers": ["pedrobaeza"],
    "data": [
        "security/ir.model.access.csv",
        "security/sale_commission_security.xml",
        "views/sale_commission_view.xml",
        "views/sale_commission_mixin_views.xml",
        "views/product_template_view.xml",
        "views/res_partner_view.xml",
        "views/sale_order_view.xml",
        "views/account_move_views.xml",
        "views/sale_commission_settlement_view.xml",
        "views/sale_commission_settlement_report.xml",
        "views/report_settlement_templates.xml",
        "report/sale_commission_analysis_report_view.xml",
        "wizard/wizard_settle.xml",
        "wizard/wizard_invoice.xml",
    ],
    "demo": ["demo/sale_agent_demo.xml"],
    "installable": True,
}
<commit_msg>sale_commission 13.0.1.5.0<commit_after># Copyright 2014-2020 Tecnativa - Pedro M. Baeza
# Copyright 2020 Tecnativa - Manuel Calero
{
    "name": "Sales commissions",
    "version": "13.0.1.5.0",
    "author": "Tecnativa," "Odoo Community Association (OCA)",
    "category": "Sales Management",
    "license": "AGPL-3",
    "depends": ["account", "product", "sale_management"],
    "website": "https://github.com/OCA/commission",
    "development_status": "Mature",
    "maintainers": ["pedrobaeza"],
    "data": [
        "security/ir.model.access.csv",
        "security/sale_commission_security.xml",
        "views/sale_commission_view.xml",
        "views/sale_commission_mixin_views.xml",
        "views/product_template_view.xml",
        "views/res_partner_view.xml",
        "views/sale_order_view.xml",
        "views/account_move_views.xml",
        "views/sale_commission_settlement_view.xml",
        "views/sale_commission_settlement_report.xml",
        "views/report_settlement_templates.xml",
        "report/sale_commission_analysis_report_view.xml",
        "wizard/wizard_settle.xml",
        "wizard/wizard_invoice.xml",
    ],
    "demo": ["demo/sale_agent_demo.xml"],
    "installable": True,
}
<|endoftext|>


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

import urllib.request
from urllib.error import HTTPError

import cherrypy
from bs4 import BeautifulSoup
from jinja2 import Template
import re

"""
Prototype of the web app
"""

__author__ = "Eric \"Fistons\""
__copyright__ = "Copyright 2016"
__license__ = "MIT License"
__version__ = "1.0-SNAPSHOT"
__status__ = "prototype"

TEMPLATE_RSS = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
    <channel>
        <title>Tweets of {{ tweet_account }}</title>
        <description>Tweets of {{ tweet_account }}</description>
        <link>https://twitter.com/{{ tweet_acccount }}</link>
        {% for tweet in tweets %}
        <item>
            <title><![CDATA[ {{ tweet.tweet }} ]]></title>
            <link>{{ tweet.link }}</link>
            <pubDate>{{ tweet.date }}</pubDate>
            <guid>{{ tweet.id }}</guid>
            <description><![CDATA[ {{ tweet.tweet }} ]]></description>
            {% for img in tweet.images %}
                <media:group>
                    <media:content url="{{ img }}" medium="image" type="image/jpeg"/>
                </media:group>
            {% endfor %}
        </item>
        {% endfor %}
    </channel>
</rss>
"""

CONFIG_FILE_NAME = "Tweet2Rss.conf"

class FuckingTweet:
    """
    Class representig a fucking tweet
    """

    def __init__(self, tweet_id, tweet, date, author_name, author_account, link, is_retweet, images = []):
        self.is_retweet = is_retweet
        self.id = tweet_id
        self.date = date
        self.tweet = tweet
        self.author_name = author_name
        self.author_account = author_account
        self.link = link
        self.images = images

        if (self.is_retweet):
            self.tweet = "RT {} ({}): {}".format(self.author_account, self.author_name, self.tweet)

    def __str__(self):
        return "{} by {} [{}] the {} - [Id. {}] - Link: {} - is a retweet: {}".format(self.tweet, self.author_name
                                                                   , self.author_account, self.date
                                                                   , self.id, self.link
                                                                   , self.is_retweet)

class ShittyParser:
    """
    Shitty twitter html parser
    """
    TWITTER_BASE_URL = 'https://twitter.com'
    HEADERS = {'Accept-Language': "en,en_US"}

    def __init__(self):
        self.tweets = []

    def parse(self, twitter_account):
        self.tweets.clear()
        with urllib.request.urlopen(ShittyParser.TWITTER_BASE_URL + "/" + twitter_account) as f:
            response = f.read().decode('utf-8')

        soup = BeautifulSoup(response, 'html.parser')
        l = soup.findAll('div', attrs={'class': 'original-tweet'})
        for text in l:
            tweet = text.find('p', attrs={'class': 'tweet-text'}).get_text()
            time = text.find('a', attrs={'class': 'tweet-timestamp'})['title']
            author = text['data-name']
            username = "@" + text['data-screen-name']
            tweet_id = text['data-tweet-id']
            link = ShittyParser.TWITTER_BASE_URL + text['data-permalink-path']
            is_retweet = text.has_attr('data-retweet-id')
            images = []
            for img in text.parent.find_all("img", attrs={'class': None}):
                images.append(img['src'])
            self.tweets.append(FuckingTweet(tweet_id, tweet, time, author, username, link, is_retweet, images))

class Tweet2Rss(object):
    def __init__(self):
        self.parser = ShittyParser()
        self.template = Template(TEMPLATE_RSS)

    @cherrypy.expose
    def index(self):
        return "Yeah, so?"

    @cherrypy.expose
    def tw(self, username):
        try:
            self.parser.parse(username)
            return self.template.render(tweets=self.parser.tweets, tweet_account=username)
        except HTTPError as e:
            if e.errno == 404:
                return "Unkown fucking account"
            else:
                return "Dumb twitter is down again or some retarded bullshits"

if __name__ == "__main__":
    cherrypy.config.update(CONFIG_FILE_NAME)
    cherrypy.quickstart(Tweet2Rss(), "/", CONFIG_FILE_NAME)

<commit_msg>remove unused import re<commit_after>#!/usr/bin/env python3

import urllib.request
from urllib.error import HTTPError

import cherrypy
from bs4 import BeautifulSoup
from jinja2 import Template

"""
Prototype of the web app
"""

__author__ = "Eric \"Fistons\""
__copyright__ = "Copyright 2016"
__license__ = "MIT License"
__version__ = "1.0-SNAPSHOT"
__status__ = "prototype"

TEMPLATE_RSS = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
    <channel>
        <title>Tweets of {{ tweet_account }}</title>
        <description>Tweets of {{ tweet_account }}</description>
        <link>https://twitter.com/{{ tweet_acccount }}</link>
        {% for tweet in tweets %}
        <item>
            <title><![CDATA[ {{ tweet.tweet }} ]]></title>
            <link>{{ tweet.link }}</link>
            <pubDate>{{ tweet.date }}</pubDate>
            <guid>{{ tweet.id }}</guid>
            <description><![CDATA[ {{ tweet.tweet }} ]]></description>
            {% for img in tweet.images %}
                <media:group>
                    <media:content url="{{ img }}" medium="image" type="image/jpeg"/>
                </media:group>
            {% endfor %}
        </item>
        {% endfor %}
    </channel>
</rss>
"""

CONFIG_FILE_NAME = "Tweet2Rss.conf"

class FuckingTweet:
    """
    Class representig a fucking tweet
    """

    def __init__(self, tweet_id, tweet, date, author_name, author_account, link, is_retweet, images = []):
        self.is_retweet = is_retweet
        self.id = tweet_id
        self.date = date
        self.tweet = tweet
        self.author_name = author_name
        self.author_account = author_account
        self.link = link
        self.images = images

        if (self.is_retweet):
            self.tweet = "RT {} ({}): {}".format(self.author_account, self.author_name, self.tweet)

    def __str__(self):
        return "{} by {} [{}] the {} - [Id. {}] - Link: {} - is a retweet: {}".format(self.tweet, self.author_name
                                                                   , self.author_account, self.date
                                                                   , self.id, self.link
                                                                   , self.is_retweet)

class ShittyParser:
    """
    Shitty twitter html parser
    """
    TWITTER_BASE_URL = 'https://twitter.com'
    HEADERS = {'Accept-Language': "en,en_US"}

    def __init__(self):
        self.tweets = []

    def parse(self, twitter_account):
        self.tweets.clear()
        with urllib.request.urlopen(ShittyParser.TWITTER_BASE_URL + "/" + twitter_account) as f:
            response = f.read().decode('utf-8')

        soup = BeautifulSoup(response, 'html.parser')
        l = soup.findAll('div', attrs={'class': 'original-tweet'})
        for text in l:
            tweet = text.find('p', attrs={'class': 'tweet-text'}).get_text()
            time = text.find('a', attrs={'class': 'tweet-timestamp'})['title']
            author = text['data-name']
            username = "@" + text['data-screen-name']
            tweet_id = text['data-tweet-id']
            link = ShittyParser.TWITTER_BASE_URL + text['data-permalink-path']
            is_retweet = text.has_attr('data-retweet-id')
            images = []
            for img in text.parent.find_all("img", attrs={'class': None}):
                images.append(img['src'])
            self.tweets.append(FuckingTweet(tweet_id, tweet, time, author, username, link, is_retweet, images))

class Tweet2Rss(object):
    def __init__(self):
        self.parser = ShittyParser()
        self.template = Template(TEMPLATE_RSS)

    @cherrypy.expose
    def index(self):
        return "Yeah, so?"

    @cherrypy.expose
    def tw(self, username):
        try:
            self.parser.parse(username)
            return self.template.render(tweets=self.parser.tweets, tweet_account=username)
        except HTTPError as e:
            if e.errno == 404:
                return "Unkown fucking account"
            else:
                return "Dumb twitter is down again or some retarded bullshits"

if __name__ == "__main__":
    cherrypy.config.update(CONFIG_FILE_NAME)
    cherrypy.quickstart(Tweet2Rss(), "/", CONFIG_FILE_NAME)

<|endoftext|>


<commit_before>__title__ = 'asana'
__version__ = '0.5.1'
__license__ = 'MIT'
__copyright__ = 'Copyright 2016 Asana, Inc.'

from .client import Client
<commit_msg>Bump version<commit_after>__title__ = 'asana'
__version__ = '0.6.0'
__license__ = 'MIT'
__copyright__ = 'Copyright 2016 Asana, Inc.'

from .client import Client
<|endoftext|>


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

import urllib.request
from urllib.error import HTTPError

import cherrypy
from bs4 import BeautifulSoup
from jinja2 import Template

"""
Protoype of the web app
"""

__author__ = "Eric \"Fistons\""
__copyright__ = "Copyright 2016"
__license__ = "MIT License"
__version__ = "1.0-SNAPSHOT"
__status__ = "prototype"

TEMPLATE = """<!DOCTYPE html>

    <title>Tweets of {{ tweet_account }}</title>

    {% for tweet in tweets %}

{{ tweet.tweet }}

by {{ tweet.author_name }} [{{ tweet.author_account }}] - {{ tweet.date }}

    {% endfor %}

"""

TEMPLATE_RSS ="""<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <title>Tweets of {{ tweet_account }}</title>
        <description>Tweets of {{ tweet_account }}</description>
        <link>https://twitter.com/{{ tweet_acccount }}</link>
        {% for tweet in tweets %}
        <item>
            <title>{{ tweet.tweet }}</title>
            <link>{{ tweet.link }}</link>
            <pubDate>{{ tweet.date }}</pubDate>
            <guid>{{ tweet.id }}</guid>
            <description>{{ tweet.tweet }}</description>
        </item>
        {% endfor %}
    </channel>
</rss>
"""

class FuckingTweet:
    """
    Class representig a fucking tweet
    """

    def __init__(self, tweet_id, tweet, date, author_name, author_account, link):
        self.id = tweet_id
        self.date = date
        self.tweet = tweet
        self.author_name = author_name
        self.author_account = author_account
        self.link = link

    def __str__(self):
        return "{} by {} [{}] the {} - [Id. {}] - Link: {}".format(self.tweet, self.author_name
                                                                   , self.author_account, self.date
                                                                   , self.id, self.link)

class ShittyParser:
    """
    Shitty twitter html parser
    """
    TWITTER_BASE_URL = 'https://twitter.com'
    HEADERS = {'Accept-Language': "en,en_US"}

    def __init__(self):
        self.tweets = []

    def parse(self, twitter_account):
        self.tweets.clear()
        with urllib.request.urlopen(ShittyParser.TWITTER_BASE_URL + "/" + twitter_account) as f:
            response = f.read().decode('utf-8')

        soup = BeautifulSoup(response, 'html.parser')
        l = soup.findAll('div', attrs={'class': 'original-tweet'})
        for text in l:
            tweet = text.find('p', attrs={'class': 'tweet-text'}).get_text()
            time = text.find('a', attrs={'class': 'tweet-timestamp'})['title']
            author = text['data-name']
            username = "@" + text['data-screen-name']
            tweet_id = text['data-tweet-id']
            link = ShittyParser.TWITTER_BASE_URL + text['data-permalink-path']
            self.tweets.append(FuckingTweet(tweet_id, tweet, time, author, username, link))

class Tweet2Rss(object):
    def __init__(self):
        self.parser = ShittyParser()
        self.template = Template(TEMPLATE_RSS)

    @cherrypy.expose
    def index(self):
        return "Yeah, so?"

    @cherrypy.expose
    def tw(self, username):
        try:
            self.parser.parse(username)
            return self.template.render(tweets=self.parser.tweets, tweet_account=username)
        except HTTPError as e:
            if e.errno == 404:
                return "Unkown fucking account"
            else:
                return "Dumb twitter is down again or some retarded bullshits"

if __name__ == "__main__":
    cherrypy.quickstart(Tweet2Rss())
<commit_msg>Read the config file<commit_after>#!/usr/bin/env python

import urllib.request
from urllib.error import HTTPError

import cherrypy
from bs4 import BeautifulSoup
from jinja2 import Template

"""
Prototype of the web app
"""

__author__ = "Eric \"Fistons\""
__copyright__ = "Copyright 2016"
__license__ = "MIT License"
__version__ = "1.0-SNAPSHOT"
__status__ = "prototype"

TEMPLATE = """<!DOCTYPE html>

    <title>Tweets of {{ tweet_account }}</title>

    {% for tweet in tweets %}

{{ tweet.tweet }}

by {{ tweet.author_name }} [{{ tweet.author_account }}] - {{ tweet.date }}

    {% endfor %}

"""

TEMPLATE_RSS = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <title>Tweets of {{ tweet_account }}</title>
        <description>Tweets of {{ tweet_account }}</description>
        <link>https://twitter.com/{{ tweet_acccount }}</link>
        {% for tweet in tweets %}
        <item>
            <title>{{ tweet.tweet }}</title>
            <link>{{ tweet.link }}</link>
            <pubDate>{{ tweet.date }}</pubDate>
            <guid>{{ tweet.id }}</guid>
            <description>{{ tweet.tweet }}</description>
        </item>
        {% endfor %}
    </channel>
</rss>
"""

CONFIG_FILE_NAME = "Tweet2Rss.conf"

class FuckingTweet:
    """
    Class representig a fucking tweet
    """

    def __init__(self, tweet_id, tweet, date, author_name, author_account, link):
        self.id = tweet_id
        self.date = date
        self.tweet = tweet
        self.author_name = author_name
        self.author_account = author_account
        self.link = link

    def __str__(self):
        return "{} by {} [{}] the {} - [Id. {}] - Link: {}".format(self.tweet, self.author_name
                                                                   , self.author_account, self.date
                                                                   , self.id, self.link)

class ShittyParser:
    """
    Shitty twitter html parser
    """
    TWITTER_BASE_URL = 'https://twitter.com'
    HEADERS = {'Accept-Language': "en,en_US"}

    def __init__(self):
        self.tweets = []

    def parse(self, twitter_account):
        self.tweets.clear()
        with urllib.request.urlopen(ShittyParser.TWITTER_BASE_URL + "/" + twitter_account) as f:
            response = f.read().decode('utf-8')

        soup = BeautifulSoup(response, 'html.parser')
        l = soup.findAll('div', attrs={'class': 'original-tweet'})
        for text in l:
            tweet = text.find('p', attrs={'class': 'tweet-text'}).get_text()
            time = text.find('a', attrs={'class': 'tweet-timestamp'})['title']
            author = text['data-name']
            username = "@" + text['data-screen-name']
            tweet_id = text['data-tweet-id']
            link = ShittyParser.TWITTER_BASE_URL + text['data-permalink-path']
            self.tweets.append(FuckingTweet(tweet_id, tweet, time, author, username, link))

class Tweet2Rss(object):
    def __init__(self):
        self.parser = ShittyParser()
        self.template = Template(TEMPLATE_RSS)

    @cherrypy.expose
    def index(self):
        return "Yeah, so?"

    @cherrypy.expose
    def tw(self, username):
        try:
            self.parser.parse(username)
            return self.template.render(tweets=self.parser.tweets, tweet_account=username)
        except HTTPError as e:
            if e.errno == 404:
                return "Unkown fucking account"
            else:
                return "Dumb twitter is down again or some retarded bullshits"

if __name__ == "__main__":
    cherrypy.config.update(CONFIG_FILE_NAME)
    cherrypy.quickstart(Tweet2Rss(), "/", CONFIG_FILE_NAME)
<|endoftext|>


<commit_before>

import time
import numpy
import copy
import math
import sys
import wx

from data_util.calcthread import CalcThread
from sans.fit.AbstractFitEngine import FitData2D, FitData1D, SansAssembly

class CalcChisqr1D(CalcThread):
    """
       Compute chisqr
    """
    def __init__(self, data1d, model,
                 smearer=None,
                 qmin=None,
                 qmax=None,
                 completefn = None,
                 updatefn   = None,
                 yieldtime  = 0.01,
                 worktime   = 0.01
                 ):
        CalcThread.__init__(self,completefn,
                 updatefn,
                 yieldtime,
                 worktime)

        if model ==None or data1d ==None:
            raise ValueError, "Need data and model to compute chisqr"

        if data1d.__class__.__name__ !="Data1D":
            msg= str(data1d.__class__.__name__)
            raise ValueError, "need Data1D to compute chisqr. Current class %s"%msg

        self.fitdata= FitData1D(data1d, smearer=smearer)
        self.fitdata.setFitRange(qmin=qmin,qmax=qmax)
        self.model = model

        self.starttime = 0

    def isquit(self):
        """
             @raise KeyboardInterrupt: when the thread is interrupted
        """
        try:
            CalcThread.isquit(self)
        except KeyboardInterrupt:
            raise KeyboardInterrupt

    def compute(self):
        """
            Compute the data given a model function
        """
        self.starttime = time.time()

        output= None
        res=[]
        try:
            res = self.fitdata.residuals(self.model.evalDistribution)
            sum=0
            for item in res:
                # Check whether we need to bail out
                self.isquit()
                if numpy.isfinite(item):
                    sum +=item*item
            if len(res)>0:
                output = sum/ len(res)

            elapsed = time.time()-self.starttime
            self.complete(output= output,  elapsed=elapsed)

        except KeyboardInterrupt:
            # Thread was interrupted, just proceed and re-raise.
            # Real code should not print, but this is an example...
            raise

class CalcChisqr2D(CalcThread):
    """
       Compute chisqr
    """

    def __init__(self,data2d, model,
                 qmin,
                 qmax,
                 completefn = None,
                 updatefn   = None,
                 yieldtime  = 0.01,
                 worktime   = 0.01
                 ):
        CalcThread.__init__(self,completefn,
                 updatefn,
                 yieldtime,
                 worktime)

        if model ==None or data2d ==None:
            raise ValueError, "Need data and model to compute chisqr"

        if data2d.__class__.__name__ !="Data2D":
            msg= str(data2d.__class__.__name__)
            raise ValueError, "need Data2D to compute chisqr. Current class %s"%msg

        self.fitdata = FitData2D(data2d)
        self.fitdata.setFitRange(qmin=qmin,qmax=qmax)

        self.model = model

        self.starttime = 0

    def isquit(self):
        """
             @raise KeyboardInterrupt: when the thread is interrupted
        """
        try:
            CalcThread.isquit(self)
        except KeyboardInterrupt:
            raise KeyboardInterrupt

    def compute(self):
        """
            Compute the data given a model function
        """
        self.starttime = time.time()

        output= None
        res=[]
        try:
            res = self.fitdata.residuals(self.model.evalDistribution)
            sum=0
            for item in res:
                # Check whether we need to bail out
                self.isquit()
                if numpy.isfinite(item):
                    sum +=item*item
            if len(res)>0:
                output = sum/ len(res)

            elapsed = time.time()-self.starttime
            self.complete(output= output,  elapsed=elapsed)

        except KeyboardInterrupt:
            # Thread was interrupted, just proceed and re-raise.
            # Real code should not print, but this is an example...
            raise
       <commit_msg>guithread change on data2D<commit_after>

import time
import numpy
import copy
import math
import sys
import wx

from data_util.calcthread import CalcThread
from sans.fit.AbstractFitEngine import FitData2D, FitData1D, SansAssembly

class CalcChisqr1D(CalcThread):
    """
       Compute chisqr
    """
    def __init__(self, data1d, model,
                 smearer=None,
                 qmin=None,
                 qmax=None,
                 completefn = None,
                 updatefn   = None,
                 yieldtime  = 0.01,
                 worktime   = 0.01
                 ):
        CalcThread.__init__(self,completefn,
                 updatefn,
                 yieldtime,
                 worktime)

        if model ==None or data1d ==None:
            raise ValueError, "Need data and model to compute chisqr"

        if data1d.__class__.__name__ !="Data1D":
            msg= str(data1d.__class__.__name__)
            raise ValueError, "need Data1D to compute chisqr. Current class %s"%msg

        self.fitdata= FitData1D(x=data1d.x,y=data1d.y,dx=data1d.dx,dy=data1d.dy, smearer=smearer)
        self.fitdata.setFitRange(qmin=qmin,qmax=qmax)
        self.model = model

        self.starttime = 0

    def isquit(self):
        """
             @raise KeyboardInterrupt: when the thread is interrupted
        """
        try:
            CalcThread.isquit(self)
        except KeyboardInterrupt:
            raise KeyboardInterrupt

    def compute(self):
        """
            Compute the data given a model function
        """
        self.starttime = time.time()

        output= None
        res=[]
        try:
            res = self.fitdata.residuals(self.model.evalDistribution)
            sum=0
            for item in res:
                # Check whether we need to bail out
                self.isquit()
                if numpy.isfinite(item):
                    sum +=item*item
            if len(res)>0:
                output = sum/ len(res)

            elapsed = time.time()-self.starttime
            self.complete(output= output,  elapsed=elapsed)

        except KeyboardInterrupt:
            # Thread was interrupted, just proceed and re-raise.
            # Real code should not print, but this is an example...
            raise

class CalcChisqr2D(CalcThread):
    """
       Compute chisqr
    """

    def __init__(self,data2d, model,
                 qmin,
                 qmax,
                 completefn = None,
                 updatefn   = None,
                 yieldtime  = 0.01,
                 worktime   = 0.01
                 ):
        CalcThread.__init__(self,completefn,
                 updatefn,
                 yieldtime,
                 worktime)

        if model ==None or data2d ==None:
            raise ValueError, "Need data and model to compute chisqr"

        if data2d.__class__.__name__ !="Data2D":
            msg= str(data2d.__class__.__name__)
            raise ValueError, "need Data2D to compute chisqr. Current class %s"%msg

        self.fitdata = FitData2D(sans_data2d=data2d ,data=data2d.data, err_data=data2d.err_data)
        self.fitdata.setFitRange(qmin=qmin,qmax=qmax)

        self.model = model

        self.starttime = 0

    def isquit(self):
        """
             @raise KeyboardInterrupt: when the thread is interrupted
        """
        try:
            CalcThread.isquit(self)
        except KeyboardInterrupt:
            raise KeyboardInterrupt

    def compute(self):
        """
            Compute the data given a model function
        """
        self.starttime = time.time()

        output= None
        res=[]
        try:
            res = self.fitdata.residuals(self.model.evalDistribution)
            sum=0
            for item in res:
                # Check whether we need to bail out
                self.isquit()
                if numpy.isfinite(item):
                    sum +=item*item
            if len(res)>0:
                output = sum/ len(res)

            elapsed = time.time()-self.starttime
            self.complete(output= output,  elapsed=elapsed)

        except KeyboardInterrupt:
            # Thread was interrupted, just proceed and re-raise.
            # Real code should not print, but this is an example...
            raise
       <|endoftext|>


<commit_before># Copyright 2021 Binovo IT Human Project SL
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
    "name": "TicketBAI (API) - Batuz - "
            "declaración de todas las operaciones de venta realizadas por las personas "
            " y entidades que desarrollan actividades económicas en Bizkaia",
    "version": "11.0.2.0.0",
    "category": "Accounting & Finance",
    "website": "http://www.binovo.es",
    "author": "Binovo,"
              "Odoo Community Association (OCA)",
    "license": "AGPL-3",
    "application": False,
    "installable": True,
    "auto_install": False,
    "development_status": "Alpha",
    "maintainers": [
        'Binovo'
    ],
    "depends": [
        "l10n_es_ticketbai_api",
    ],
    "external_dependencies": {
        "python": [
            "xmltodict",
            "requests_pkcs12"
        ],
    },
    "data": [
        "security/ir.model.access.csv",
        "data/tax_agency_data.xml",
        "data/lroe_chapter_data.xml",
        "views/res_company_views.xml",
        "views/lroe_operation_views.xml"
    ],
}
<commit_msg>l10n_es_ticketbai_api_batuz 11.0.2.0.1<commit_after># Copyright 2021 Binovo IT Human Project SL
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
    "name": "TicketBAI (API) - Batuz - "
            "declaración de todas las operaciones de venta realizadas por las personas "
            " y entidades que desarrollan actividades económicas en Bizkaia",
    "version": "11.0.2.0.1",
    "category": "Accounting & Finance",
    "website": "http://www.binovo.es",
    "author": "Binovo,"
              "Odoo Community Association (OCA)",
    "license": "AGPL-3",
    "application": False,
    "installable": True,
    "auto_install": False,
    "development_status": "Alpha",
    "maintainers": [
        'Binovo'
    ],
    "depends": [
        "l10n_es_ticketbai_api",
    ],
    "external_dependencies": {
        "python": [
            "xmltodict",
            "requests_pkcs12"
        ],
    },
    "data": [
        "security/ir.model.access.csv",
        "data/tax_agency_data.xml",
        "data/lroe_chapter_data.xml",
        "views/res_company_views.xml",
        "views/lroe_operation_views.xml"
    ],
}
<|endoftext|>


<commit_before>import random
import json

from discord.ext import commands
from utils.tools import *
from utils.config import Config
from utils import checks
from utils.language import Language
from utils.logger import log
config = Config()

# This is the limit to how many posts are selected
limit = config.max_nsfw_count

class NSFW():
    def __init__(self, bot):
        self.bot = bot

    @checks.is_nsfw_channel()
    @commands.command()
    async def rule34(self, ctx, *, tags:str):
        try:
            await ctx.channel.trigger_typing()
            try:
                data = json.loads(requests.get("http://rule34.xxx/index.php?page=dapi&s=post&q=index&json=1&limit={}&tags={}".format(limit, tags)).text)
            except json.JSONDecodeError:
                await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
                return

            count = len(data)
            if count == 0:
                await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
                return
            image_count = 4
            if count < 4:
                image_count = count
            images = []
            for i in range(image_count):
                image = data[random.randint(0, count)]
                images.append("http://img.rule34.xxx/images/{}/{}".format(image["directory"], image["image"]))
            await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))
        except IndexError:
            log.error("it fucked up - " + str(count))
            log.error(data)

    @checks.is_nsfw_channel()
    @commands.command()
    async def e621(self, ctx, *, tags:str):
        """Searches e621.net for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://e621.net/post/index.json?limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            images.append(data[random.randint(0, count)]["file_url"])
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

    @checks.is_nsfw_channel()
    @commands.command()
    async def yandere(self, ctx, *, tags:str):
        """Searches yande.re for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://yande.re/post/index.json?limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            images.append(data[random.randint(0, count)]["file_url"])
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

    @checks.is_nsfw_channel()
    @commands.command()
    async def danbooru(self, ctx, *, tags:str):
        """Searches danbooru.donmai.us for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://danbooru.donmai.us/post/index.json?limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            try:
                images.append("http://danbooru.donmai.us{}".format(data[random.randint(0, count)]["file_url"]))
            except KeyError:
                await ctx.send(data["message"])
                return
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

    @checks.is_nsfw_channel()
    @commands.command()
    async def gelbooru(self, ctx, *, tags:str):
        """Searches gelbooru.com for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            try:
                images.append("http:{}".format(data[random.randint(0, count)]["file_url"]))
            except KeyError:
                await ctx.send(data["message"])
                return
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

    @checks.is_nsfw_channel()
    @commands.command()
    async def xbooru(self, ctx, *, tags: str):
        """Searches xbooru.com for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://xbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            try:
                post = data[random.randint(0, count)]
                images.append("http://img3.xbooru.com/images/{}/{}".format(post["directory"], post["image"]))
            except KeyError:
                await ctx.send(data["message"])
                return
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

def setup(bot):
    bot.add_cog(NSFW(bot))
<commit_msg>go away<commit_after>import random
import json

from discord.ext import commands
from utils.tools import *
from utils.config import Config
from utils import checks
from utils.language import Language
from utils.logger import log
config = Config()

# This is the limit to how many posts are selected
limit = config.max_nsfw_count

class NSFW():
    def __init__(self, bot):
        self.bot = bot

    @checks.is_nsfw_channel()
    @commands.command()
    async def rule34(self, ctx, *, tags:str):
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("http://rule34.xxx/index.php?page=dapi&s=post&q=index&json=1&limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return

        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            image = data[random.randint(0, count)]
            images.append("http://img.rule34.xxx/images/{}/{}".format(image["directory"], image["image"]))
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

    @checks.is_nsfw_channel()
    @commands.command()
    async def e621(self, ctx, *, tags:str):
        """Searches e621.net for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://e621.net/post/index.json?limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            images.append(data[random.randint(0, count)]["file_url"])
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

    @checks.is_nsfw_channel()
    @commands.command()
    async def yandere(self, ctx, *, tags:str):
        """Searches yande.re for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://yande.re/post/index.json?limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            images.append(data[random.randint(0, count)]["file_url"])
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

    @checks.is_nsfw_channel()
    @commands.command()
    async def danbooru(self, ctx, *, tags:str):
        """Searches danbooru.donmai.us for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://danbooru.donmai.us/post/index.json?limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            try:
                images.append("http://danbooru.donmai.us{}".format(data[random.randint(0, count)]["file_url"]))
            except KeyError:
                await ctx.send(data["message"])
                return
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

    @checks.is_nsfw_channel()
    @commands.command()
    async def gelbooru(self, ctx, *, tags:str):
        """Searches gelbooru.com for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            try:
                images.append("http:{}".format(data[random.randint(0, count)]["file_url"]))
            except KeyError:
                await ctx.send(data["message"])
                return
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

    @checks.is_nsfw_channel()
    @commands.command()
    async def xbooru(self, ctx, *, tags: str):
        """Searches xbooru.com for the specified tagged images"""
        await ctx.channel.trigger_typing()
        try:
            data = json.loads(requests.get("https://xbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit={}&tags={}".format(limit, tags)).text)
        except json.JSONDecodeError:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        count = len(data)
        if count == 0:
            await ctx.send(Language.get("nsfw.no_results_found", ctx).format(tags))
            return
        image_count = 4
        if count < 4:
            image_count = count
        images = []
        for i in range(image_count):
            try:
                post = data[random.randint(0, count)]
                images.append("http://img3.xbooru.com/images/{}/{}".format(post["directory"], post["image"]))
            except KeyError:
                await ctx.send(data["message"])
                return
        await ctx.send(Language.get("nsfw.results", ctx).format(image_count, count, tags, "\n".join(images)))

def setup(bot):
    bot.add_cog(NSFW(bot))
<|endoftext|>