<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 Jason Davies
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

from couchdb import Database, Document, ResourceNotFound, Row, Server, \
    ViewResults
import errno
import fuse
import os
import stat
import sys
from time import time
try:
    import simplejson as json
except ImportError:
    import json # Python 2.6
from urllib import quote, unquote

fuse.fuse_python_api = (0, 2)

class CouchStat(fuse.Stat):
    def __init__(self):
        self.st_mode = 0
        self.st_ino = 0
        self.st_dev = 0
        self.st_nlink = 0
        self.st_uid = 0
        self.st_gid = 0
        self.st_size = 4096
        self.st_atime = 0
        self.st_mtime = 0
        self.st_ctime = 0

class CouchFSDocument(fuse.Fuse):
    def __init__(self, mountpoint, uri=None, *args, **kwargs):
        fuse.Fuse.__init__(self, *args, **kwargs)
        db_uri, doc_id = uri.rsplit('/', 1)
        self.doc_id = unquote(doc_id)
        self.db = Database(db_uri)

    def readdir(self, path, offset):
        for r in '.', '..':
            yield fuse.Direntry(r)
        for att in self.db[self.doc_id].get('_attachments', {}).keys():
            yield fuse.Direntry(att.encode('utf-8'))

    def getattr(self, path):
        try:
            st = CouchStat()
            if path == '/':
                st.st_mode = stat.S_IFDIR | 0755
                st.st_nlink = 2
            else:
                att = self.db[self.doc_id].get('_attachments', {})
                data = att[path[1:]]
                st.st_mode = stat.S_IFREG | 0644
                st.st_nlink = 1
                st.st_size = data['length']
            return st
        except (KeyError, ResourceNotFound):
            return -errno.ENOENT

    def open(self, path, flags):
        try:
            data = self.db.get_attachment(self.db[self.doc_id], path.split('/')[-1])
            #att = self.db[self.doc_id].get('_attachments', {})
            #data = att[path.split('/')[-1]]
            return 0
        except (KeyError, ResourceNotFound):
            return -errno.ENOENT
        #accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
        #if (flags & accmode) != os.O_RDONLY:
        #    return -errno.EACCES

    def read(self, path, size, offset):
        try:
            data = self.db.get_attachment(self.db[self.doc_id], path.split('/')[-1])
            slen = len(data)
            if offset < slen:
                if offset + size > slen:
                    size = slen - offset
                buf = data[offset:offset+size]
            else:
                buf = ''
            return buf
        except (KeyError, ResourceNotFound):
            pass
        return -errno.ENOENT

    def write(self, path, buf, offset):
        try:
            data = self.db.get_attachment(self.db[self.doc_id], path.split('/')[-1])
            data = data[0:offset] + buf + data[offset+len(buf):]
            self.db.put_attachment(self.db[self.doc_id], data, filename=path.split('/')[-1])
            return len(buf)
        except (KeyError, ResourceNotFound):
            pass
        return -errno.ENOENT

    def mknod(self, path, mode, dev):
        name = path.split('/')[-1]
        self.db.put_attachment(self.db[self.doc_id], u'', filename=name)
        return 0

    def unlink(self, path):
        name = path.split('/')[-1]
        self.db.delete_attachment(self.db[self.doc_id], name)
        return 0

    def truncate(self, path, size):
        name = path.split('/')[-1]
        self.db.put_attachment(self.db[self.doc_id], u'', filename=name)
        return 0

    def utime(self, path, times):
        return 0

    def mkdir(self, path, mode):
        return 0

    def rmdir(self, path):
        return 0

    def rename(self, pathfrom, pathto):
        return 0

    def fsync(self, path, isfsyncfile):
        return 0

    def statfs(self):
        """
        Should return a tuple with the following 6 elements:
            - blocksize - size of file blocks, in bytes
            - totalblocks - total number of blocks in the filesystem
            - freeblocks - number of free blocks
            - availblocks - number of blocks available to non-superuser
            - totalfiles - total number of file inodes
            - freefiles - nunber of free file inodes

        Feel free to set any of the above values to 0, which tells
        the kernel that the info is not available.
        """
        st = fuse.StatVfs()
        block_size = 1024
        blocks = 1024 * 1024
        blocks_free = blocks
        blocks_avail = blocks_free
        files = 0
        files_free = 0
        st.f_bsize = block_size
        st.f_frsize = block_size
        st.f_blocks = blocks
        st.f_bfree = blocks_free
        st.f_bavail = blocks_avail
        st.f_files = files
        st.f_ffree = files_free
        return st

class CouchFS(fuse.Fuse):
    """FUSE interface to a CouchDB database."""

    def __init__(self, mountpoint, uri=None, *args, **kw):
        fuse.Fuse.__init__(self, *args, **kw)
        self.fuse_args.mountpoint = mountpoint
        if uri is not None:
            self.server = Server(uri)
        else:
            self.server = Server()

    def getcouchattrs(self, path):
        attr = self.server
        attrs = [attr]
        parts = [x for x in path[1:].split('/') if x != '']
        i = 0
        for part in parts:
            if isinstance(attr, Database):
                if part == '_view':
                    attr = attr.view('_all_docs')['_design/':'_design/ZZZ']
                    attr.is_view = True
                elif part == '_all_docs':
                    attr = attr.view('_all_docs')
            elif isinstance(attr, ViewResults):
                if getattr(attr, 'is_view', False):
                    attr = list(attr['_design/'+part])[0]
                    attr.is_view = True
                else:
                    if attr.view.name != '_all_docs':
                        part = json.loads(unquote(part))
                    results = list(attr.view()[part])
                    if len(results) == 1:
                        attr = results[0]
                    else:
                        attr = attr.view()[part+'/':part+'/ZZZ']
                        if i + 1 < len(parts):
                            parts[i+1] = '%s/%s' % (part, parts[i+1])
            elif isinstance(attr, Row):
                if getattr(attr, 'is_view', False):
                    db = self.server[parts[0]]
                    attr = db.view('_view/' + '/'.join(parts[i-1:i+1]), group=True)
                elif part == 'value':
                    attr = attr.value
            else:
                attr = attr[part]
            attrs.append(attr)
            i += 1
        return attrs

    def getattr(self, path):
        try:
            st = CouchStat()
            attr = self.getcouchattrs(path)[-1]
            if (isinstance(attr, Server) or isinstance(attr, Database) or
                    isinstance(attr, Document) or isinstance(attr, ViewResults)
                    or isinstance(attr, Row)):
                st.st_mode = stat.S_IFDIR | 0755
                st.st_nlink = 2
            else:
                data = json.dumps(attr)
                st.st_mode = stat.S_IFREG | 0444
                st.st_nlink = 1
                st.st_size = len(data)
            return st
        except (KeyError, ResourceNotFound):
            return -errno.ENOENT

    def readdir(self, path, offset):
        attr = self.getcouchattrs(path)[-1]
        for r in '.', '..':
            yield fuse.Direntry(r)
        if isinstance(attr, Server):
            for db_name in self.server:
                yield fuse.Direntry(db_name.encode('utf-8'))
            return
        if isinstance(attr, Database):
            yield fuse.Direntry('_all_docs')
            yield fuse.Direntry('_view')
            return
        if isinstance(attr, ViewResults):
            is_view = getattr(attr, 'is_view', False)
            for row in list(attr):
                dirname = row.key
                if attr.view.name != '_all_docs':
                    dirname = quote(json.dumps(row.key))
                else:
                    dirname = dirname.split('/')[attr.options.get('startkey', '').count('/')]
                if is_view:
                    dirname = dirname.split('/')[-1]
                yield fuse.Direntry(dirname.encode('utf-8'))
            return
        if isinstance(attr, Row):
            is_view = getattr(attr, 'is_view', False)
            if is_view:
                db = self.getcouchattrs(path)[1]
                for r in db[attr.id]['views']:
                    yield fuse.Direntry(r.encode('utf-8'))
            else:
                yield fuse.Direntry('value')
            return
        for r in attr.keys():
            yield fuse.Direntry(r.encode('utf-8'))

    def open(self, path, flags):
        try:
            attr = self.getcouchattrs(path)[-1]
        except (KeyError, ResourceNotFound):
            return -errno.ENOENT
        accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
        if (flags & accmode) != os.O_RDONLY:
            return -errno.EACCES

    def read(self, path, size, offset):
        try:
            attr = self.getcouchattrs(path)[-1]
            data = json.dumps(attr)
            slen = len(data)
            if offset < slen:
                if offset + size > slen:
                    size = slen - offset
                buf = data[offset:offset+size]
            else:
                buf = ''
            return buf
        except (KeyError, ResourceNotFound):
            pass
        return -errno.ENOENT

    def write(self, path, buf, offset):
        pass

    def unlink(self, path):
        attr = self.getcouchattrs(path)[-1]

    def mkdir(self, path, mode):
        server = self.getcouchattrs(path)[0]
        if isinstance(server, Server):
            server.create(path.split('/')[-1])

def main():
    args = sys.argv[1:]
    if len(args) not in (2,):
        print "CouchDB FUSE Connector: Allows you to browse the _attachments of"
        print " any CouchDB document on your own filesystem!"
        print
        print "Remember to URL-encode your <doc_id> appropriately."
        print
        print "Usage: python couchdbfuse.py <http://hostname:port/db/doc_id> <mount-point>"
        sys.exit(-1)

    if len(args) == 1:
        fs = CouchFS(args[0])
    elif len(args) == 2:
        fs = CouchFSDocument(args[1], args[0])

    fs.parse(errex=1)
    fs.main()
<commit_msg>First stab at supporting mkdir().<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 Jason Davies
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

from couchdb import Database, Document, ResourceNotFound, Row, Server, \
    ViewResults
import errno
import fuse
import os
import stat
import sys
from time import time
try:
    import simplejson as json
except ImportError:
    import json # Python 2.6
from urllib import quote, unquote

fuse.fuse_python_api = (0, 2)

class CouchStat(fuse.Stat):
    def __init__(self):
        self.st_mode = 0
        self.st_ino = 0
        self.st_dev = 0
        self.st_nlink = 0
        self.st_uid = 0
        self.st_gid = 0
        self.st_size = 4096
        self.st_atime = 0
        self.st_mtime = 0
        self.st_ctime = 0

class CouchFSDocument(fuse.Fuse):
    def __init__(self, mountpoint, uri=None, *args, **kwargs):
        fuse.Fuse.__init__(self, *args, **kwargs)
        db_uri, doc_id = uri.rsplit('/', 1)
        self.doc_id = unquote(doc_id)
        self.db = Database(db_uri)
        self.dir_cache = set()

    def readdir(self, path, offset):
        for r in '.', '..':
            yield fuse.Direntry(r)
        for att in self.db[self.doc_id].get('_attachments', {}).keys() + list(self.dir_cache):
            yield fuse.Direntry(att.encode('utf-8'))

    def getattr(self, path):
        try:
            st = CouchStat()
            if path == '/' or path in self.dir_cache:
                st.st_mode = stat.S_IFDIR | 0755
                st.st_nlink = 2
            else:
                att = self.db[self.doc_id].get('_attachments', {})
                data = att[path[1:]]
                st.st_mode = stat.S_IFREG | 0644
                st.st_nlink = 1
                st.st_size = data['length']
            return st
        except (KeyError, ResourceNotFound):
            return -errno.ENOENT

    def open(self, path, flags):
        try:
            data = self.db.get_attachment(self.db[self.doc_id], path.split('/')[-1])
            #att = self.db[self.doc_id].get('_attachments', {})
            #data = att[path.split('/')[-1]]
            return 0
        except (KeyError, ResourceNotFound):
            return -errno.ENOENT
        #accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
        #if (flags & accmode) != os.O_RDONLY:
        #    return -errno.EACCES

    def read(self, path, size, offset):
        try:
            data = self.db.get_attachment(self.db[self.doc_id], path.split('/')[-1])
            slen = len(data)
            if offset < slen:
                if offset + size > slen:
                    size = slen - offset
                buf = data[offset:offset+size]
            else:
                buf = ''
            return buf
        except (KeyError, ResourceNotFound):
            pass
        return -errno.ENOENT

    def write(self, path, buf, offset):
        try:
            data = self.db.get_attachment(self.db[self.doc_id], path.split('/')[-1])
            data = data[0:offset] + buf + data[offset+len(buf):]
            self.db.put_attachment(self.db[self.doc_id], data, filename=path.split('/')[-1])
            return len(buf)
        except (KeyError, ResourceNotFound):
            pass
        return -errno.ENOENT

    def mknod(self, path, mode, dev):
        name = path.split('/')[-1]
        self.db.put_attachment(self.db[self.doc_id], u'', filename=name)
        return 0

    def unlink(self, path):
        name = path.split('/')[-1]
        self.db.delete_attachment(self.db[self.doc_id], name)
        return 0

    def truncate(self, path, size):
        name = path.split('/')[-1]
        self.db.put_attachment(self.db[self.doc_id], u'', filename=name)
        return 0

    def utime(self, path, times):
        return 0

    def mkdir(self, path, mode):
        self.dir_cache.add(path)
        return 0

    def rmdir(self, path):
        return 0

    def rename(self, pathfrom, pathto):
        return 0

    def fsync(self, path, isfsyncfile):
        return 0

    def statfs(self):
        """
        Should return a tuple with the following 6 elements:
            - blocksize - size of file blocks, in bytes
            - totalblocks - total number of blocks in the filesystem
            - freeblocks - number of free blocks
            - availblocks - number of blocks available to non-superuser
            - totalfiles - total number of file inodes
            - freefiles - nunber of free file inodes

        Feel free to set any of the above values to 0, which tells
        the kernel that the info is not available.
        """
        st = fuse.StatVfs()
        block_size = 1024
        blocks = 1024 * 1024
        blocks_free = blocks
        blocks_avail = blocks_free
        files = 0
        files_free = 0
        st.f_bsize = block_size
        st.f_frsize = block_size
        st.f_blocks = blocks
        st.f_bfree = blocks_free
        st.f_bavail = blocks_avail
        st.f_files = files
        st.f_ffree = files_free
        return st

class CouchFS(fuse.Fuse):
    """FUSE interface to a CouchDB database."""

    def __init__(self, mountpoint, uri=None, *args, **kw):
        fuse.Fuse.__init__(self, *args, **kw)
        self.fuse_args.mountpoint = mountpoint
        if uri is not None:
            self.server = Server(uri)
        else:
            self.server = Server()

    def getcouchattrs(self, path):
        attr = self.server
        attrs = [attr]
        parts = [x for x in path[1:].split('/') if x != '']
        i = 0
        for part in parts:
            if isinstance(attr, Database):
                if part == '_view':
                    attr = attr.view('_all_docs')['_design/':'_design/ZZZ']
                    attr.is_view = True
                elif part == '_all_docs':
                    attr = attr.view('_all_docs')
            elif isinstance(attr, ViewResults):
                if getattr(attr, 'is_view', False):
                    attr = list(attr['_design/'+part])[0]
                    attr.is_view = True
                else:
                    if attr.view.name != '_all_docs':
                        part = json.loads(unquote(part))
                    results = list(attr.view()[part])
                    if len(results) == 1:
                        attr = results[0]
                    else:
                        attr = attr.view()[part+'/':part+'/ZZZ']
                        if i + 1 < len(parts):
                            parts[i+1] = '%s/%s' % (part, parts[i+1])
            elif isinstance(attr, Row):
                if getattr(attr, 'is_view', False):
                    db = self.server[parts[0]]
                    attr = db.view('_view/' + '/'.join(parts[i-1:i+1]), group=True)
                elif part == 'value':
                    attr = attr.value
            else:
                attr = attr[part]
            attrs.append(attr)
            i += 1
        return attrs

    def getattr(self, path):
        try:
            st = CouchStat()
            attr = self.getcouchattrs(path)[-1]
            if (isinstance(attr, Server) or isinstance(attr, Database) or
                    isinstance(attr, Document) or isinstance(attr, ViewResults)
                    or isinstance(attr, Row)):
                st.st_mode = stat.S_IFDIR | 0755
                st.st_nlink = 2
            else:
                data = json.dumps(attr)
                st.st_mode = stat.S_IFREG | 0444
                st.st_nlink = 1
                st.st_size = len(data)
            return st
        except (KeyError, ResourceNotFound):
            return -errno.ENOENT

    def readdir(self, path, offset):
        attr = self.getcouchattrs(path)[-1]
        for r in '.', '..':
            yield fuse.Direntry(r)
        if isinstance(attr, Server):
            for db_name in self.server:
                yield fuse.Direntry(db_name.encode('utf-8'))
            return
        if isinstance(attr, Database):
            yield fuse.Direntry('_all_docs')
            yield fuse.Direntry('_view')
            return
        if isinstance(attr, ViewResults):
            is_view = getattr(attr, 'is_view', False)
            for row in list(attr):
                dirname = row.key
                if attr.view.name != '_all_docs':
                    dirname = quote(json.dumps(row.key))
                else:
                    dirname = dirname.split('/')[attr.options.get('startkey', '').count('/')]
                if is_view:
                    dirname = dirname.split('/')[-1]
                yield fuse.Direntry(dirname.encode('utf-8'))
            return
        if isinstance(attr, Row):
            is_view = getattr(attr, 'is_view', False)
            if is_view:
                db = self.getcouchattrs(path)[1]
                for r in db[attr.id]['views']:
                    yield fuse.Direntry(r.encode('utf-8'))
            else:
                yield fuse.Direntry('value')
            return
        for r in attr.keys():
            yield fuse.Direntry(r.encode('utf-8'))

    def open(self, path, flags):
        try:
            attr = self.getcouchattrs(path)[-1]
        except (KeyError, ResourceNotFound):
            return -errno.ENOENT
        accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
        if (flags & accmode) != os.O_RDONLY:
            return -errno.EACCES

    def read(self, path, size, offset):
        try:
            attr = self.getcouchattrs(path)[-1]
            data = json.dumps(attr)
            slen = len(data)
            if offset < slen:
                if offset + size > slen:
                    size = slen - offset
                buf = data[offset:offset+size]
            else:
                buf = ''
            return buf
        except (KeyError, ResourceNotFound):
            pass
        return -errno.ENOENT

    def write(self, path, buf, offset):
        pass

    def unlink(self, path):
        attr = self.getcouchattrs(path)[-1]

    def mkdir(self, path, mode):
        server = self.getcouchattrs(path)[0]
        if isinstance(server, Server):
            server.create(path.split('/')[-1])

def main():
    args = sys.argv[1:]
    if len(args) not in (2,):
        print "CouchDB FUSE Connector: Allows you to browse the _attachments of"
        print " any CouchDB document on your own filesystem!"
        print
        print "Remember to URL-encode your <doc_id> appropriately."
        print
        print "Usage: python couchdbfuse.py <http://hostname:port/db/doc_id> <mount-point>"
        sys.exit(-1)

    if len(args) == 1:
        fs = CouchFS(args[0])
    elif len(args) == 2:
        fs = CouchFSDocument(args[1], args[0])

    fs.parse(errex=1)
    fs.main()
<|endoftext|>


<commit_before>import json

from persoa_main.constants.database import MAX_CHAR_LENGTH
from persoa_main.models.abstract import AbstractPersOAModel

class AbstractTrait(AbstractPersOAModel):
    """
    Part of a person's personality
    """

    class Meta(AbstractPersOAModel.Meta):
        abstract = True

    name = models.CharField(
    	max_length=MAX_CHAR_LENGTH,
        blank=False)
    desc = models.TextField(blank=True)
    defn = models.CharField(
    	max_length=Max_CHAR_LENGTH,
        blank=True)

    def generate(self, *args, **kwargs):
        """
        Returns a choice from this trait
        """
        return NotImplemented

    def details(self, *args, **kwargs):
        """
        Returns a dict with the trait's details
        """
        details = {
            'type': NotImplemented,
            'name': self.name,
            'desc': self.desc,
            'defn': self.defn,
        }
        includes = kwargs.get('includes', None)
        if includes != None and 'choices' in includes:
            choices = [choice.details()]
            details.update({'choices': choices})
        return details

class BasicTrait(AbstractTrait):
    """
    A trait which has a list of choices
    """

class LinearTrait(AbstractTrait):
    """
    A trait with its choices on a scale
    """

    neg_name = models.CharField(
    	max_length=MAX_CHAR_LENGTH,
        blank=False)
    pos_name = models.CharField(
    	max_length=MAX_CHAR_LENGTH,
        blank=False)

    def details(self, *args, **kwargs):
        details = super(LinearTrait, self).details(*args, **kwargs)
        details.update({
            'type': 'scale',
            'neg': self.neg_name,
            'pos': self.pos_name,
        })
        return details

class MultiTrait(AbstractTrait):
    """
    A trait which can be selected multiple times
    """

    def_num = models.PositiveSmallIntegerField(blank=False, null=False)

    def details(self, *args, **kwargs):
        details = super(LinearTrait, self).details()
        details.update({
            'type': 'multi_choice',
            'default_num': self.def_num
        })
        return details
<commit_msg>Fixed more errors.<commit_after>from django.db import models
import json

from persoa_main.constants.database import MAX_CHAR_LENGTH
from persoa_main.models.abstract import AbstractPersOAModel

class AbstractTrait(AbstractPersOAModel):
    """
    Part of a person's personality
    """

    class Meta(AbstractPersOAModel.Meta):
        abstract = True

    name = models.CharField(
    	max_length=MAX_CHAR_LENGTH,
        blank=False)
    desc = models.TextField(blank=True)
    defn = models.CharField(
    	max_length=MAX_CHAR_LENGTH,
        blank=True)

    def generate(self, *args, **kwargs):
        """
        Returns a choice from this trait
        """
        return NotImplemented

    def details(self, *args, **kwargs):
        """
        Returns a dict with the trait's details
        """
        details = {
            'type': NotImplemented,
            'name': self.name,
            'desc': self.desc,
            'defn': self.defn,
        }
        includes = kwargs.get('includes', None)
        if includes != None and 'choices' in includes:
            choices = [choice.details()]
            details.update({'choices': choices})
        return details

class BasicTrait(AbstractTrait):
    """
    A trait which has a list of choices
    """

class LinearTrait(AbstractTrait):
    """
    A trait with its choices on a scale
    """

    neg_name = models.CharField(
    	max_length=MAX_CHAR_LENGTH,
        blank=False)
    pos_name = models.CharField(
    	max_length=MAX_CHAR_LENGTH,
        blank=False)

    def details(self, *args, **kwargs):
        details = super(LinearTrait, self).details(*args, **kwargs)
        details.update({
            'type': 'scale',
            'neg': self.neg_name,
            'pos': self.pos_name,
        })
        return details

class MultiTrait(AbstractTrait):
    """
    A trait which can be selected multiple times
    """

    def_num = models.PositiveSmallIntegerField(blank=False, null=False)

    def details(self, *args, **kwargs):
        details = super(LinearTrait, self).details()
        details.update({
            'type': 'multi_choice',
            'default_num': self.def_num
        })
        return details
<|endoftext|>


<commit_before>import os
import argparse
import platform

SUPPORTED_LANGUAGES = ['c++', 'c']

class MakefileGenerator(object):
    def __init__(self):
        self.__directory = ""
        self.__compiler = "g++"
        self.__flags = "-g -Wall -std=c++11"
        self.__executable = ""
        self.__args = ""
        self.__lib = ""
        self.__lang = ""
        self.parse_command_line_input()

    def create_parser(self):
        parser = argparse.ArgumentParser(description="Generate makefile for files in the specified directory")
        parser.add_argument('dir', help="Directory with the file(s)")
        parser.add_argument('-flags', required=False, help="Flag(s) to use when compiling, enclosed in \"\" (Default: %s)" % self.__flags)
        parser.add_argument('-cc', required=False, help="Compiler (Default: %s)" % self.__compiler)
        parser.add_argument('-exec', required=False, help="Executable name")
        parser.add_argument('-lang', required=False, choices=SUPPORTED_LANGUAGES, help="Use the default configs for the selected language")
        parser.add_argument('-lib', required=False, help="Libraries (if there are multiple, must be separated by a space)")
        parser.add_argument('-mode', required=False, help="If specified, user will enter in data via command line prompts", default=False, action='store_true')
        return parser

    def parse_command_line_input(self):
        parser = self.create_parser()
        self.__args = vars(parser.parse_args())

        if not os.path.isdir(self.__args["dir"]) and not os.path.exists(self.__args["dir"]):
            print "Invalid directory %s, exiting..." % self.__args["dir"]
            exit(1)

        self.__directory = self.__args['dir']

        if self.__args['mode']:
            self.prompt_user_for_input()
            return 0
        if self.__args["cc"]:
            self.__compiler = self.__args["cc"]
        if self.__args["flags"]:
            self.__flags = self.__args["flags"]
        if self.__args["exec"]:
            self.__executable = self.__args["exec"]
        if self.__args['lang']:
            self.__lang = self.__args['lang']
            if self.__args['lang'].lower() == "c++":
                self.__compiler = 'g++'
                self.__flags = '-g -Wall -std=c++11'
            else:
                # mode is c
                self.__compiler = 'gcc'
                self.__flags = '-g -Wall -std=c11'
        if self.__args['lib']:
            self.__lib = self.__args['lib']

        self.makefile_exists()

    def write_to_file(self, file_name):
        VALID_EXTENSIONS = [".cpp", ".c"]
        file_list = list(filter(lambda x: os.path.splitext(x)[1] in VALID_EXTENSIONS, os.listdir(os.path.dirname(file_name))))
        if len(file_list) == 0:
            print "No valid files were found in %s, exiting..." % os.path.dirname(file_name)
            exit(1)
        the_file = open(file_name, 'w')
        file_name_list = []
        write_str = ""
        for a_file in file_list:
            print "Processing File: %s" % a_file
            name = os.path.splitext(a_file)[0]
            file_name_list.append(name)
            write_str += "%s.o:\t%s" % (name, a_file)
            write_str += "\n\t%s $(%s) -c %s -o %s\n\n" % (self.__compiler, "FLAGS", a_file, name + ".o")

        the_file.write("FLAGS = %s\n\n" % self.__flags)
        the_file.write("all:\tMain\n\n")
        file_names = ""
        i = 0
        for name in file_name_list:
            if i+1 < len(file_name_list):
                file_names += name + ".o "
            else:
                file_names += name + ".o"
            i+=1
        the_file.write("Main:\tclean %s\n" % file_names)
        if self.__executable:
            if self.__lib:
                the_file.write("\t%s %s -o %s -l %s\n\n" % (self.__compiler, file_names, self.__executable, self.__lib))
            else:
                the_file.write("\t%s %s -o %s\n\n" % (self.__compiler, file_names, self.__executable))
        else:
            if self.__lib:
                the_file.write("\t%s %s -l %s\n\n" % (self.__compiler, file_names, self.__lib))
            else:
                the_file.write("\t%s %s\n\n" % (self.__compiler, file_names))

        the_file.write(write_str)
        if self.__executable:
            the_file.write("clean:\n\trm -f *.o %s\n" % self.__executable)
        else:
            the_file.write("clean:\n\trm -f *.o %s\n" % ("a.exe" if platform.system().lower() == "windows" else "a.out"))

        the_file.close()
        print "%s successfully saved." % file_name

    def prompt_user_for_input(self):
        print "Please fill out the following, or press <return> to ignore and use the default."
        lang = raw_input("Language: if specified, defaults for the selected language will be used and you will not be able to further customize anything. Select from %s: " % self.list_to_string(SUPPORTED_LANGUAGES))
        if lang:
            self.__lang = lang.strip().lower()
            if self.__lang == 'c++':
                self.__compiler = 'g++'
                self.__flags = '-g -Wall -std=c++11'
            elif self.__lang == 'c':
                # mode is c
                self.__compiler = 'gcc'
                self.__flags = '-g -Wall -std=c11'
            else:
                print "%s is not a valid selection, select from %s" % (self.__lang, self.list_to_string(SUPPORTED_LANGUAGES))
                exit(1)
        else:
            compiler = raw_input("Compiler: ").strip()
            if compiler:
                self.__compiler = compiler
            flags = raw_input("Flags: ").strip()
            if flags:
                self.__flags = flags
            lib = raw_input("Extra libraries, separated by a space: ").strip()
            if lib:
                self.__lib = lib
            executable = raw_input("Executable Name: ").strip()
            if executable:
                self.__executable = executable

        self.makefile_exists()

    def makefile_exists(self):
        makefile_name = "makefile"
        the_file = os.path.join(self.__directory, makefile_name)
        # note the exists funciton is case insensitive
        if os.path.exists(the_file):
            if os.path.isfile(the_file):
                answer = raw_input("%s already exists, overwrite? (y/n): " % os.path.basename(the_file))
                if answer.strip().lower() == 'y':
                    self.write_to_file(the_file)
                else:
                    print "File not overwritten, exiting..."
                    exit(0)
            else:
                print "%s is not a valid file and/or path to a file. Is %s a directory?" % (the_file, the_file)
        else:
            self.write_to_file(the_file)

    def list_to_string(self, the_list):
        return '[' + ', '.join(the_list) + ']'

if __name__ == '__main__':
    MakefileGenerator()
 <commit_msg>fixed issue where specifying presets wouldnt allow for exec name customization, extra libs<commit_after>import os
import argparse
import platform

SUPPORTED_LANGUAGES = ['c++', 'c']

class MakefileGenerator(object):
    def __init__(self):
        self.__directory = ""
        self.__compiler = "g++"
        self.__flags = "-g -Wall -std=c++11"
        self.__executable = ""
        self.__args = ""
        self.__lib = ""
        self.__lang = ""
        self.parse_command_line_input()

    def create_parser(self):
        parser = argparse.ArgumentParser(description="Generate makefile for files in the specified directory")
        parser.add_argument('dir', help="Directory with the file(s)")
        parser.add_argument('-flags', required=False, help="Flag(s) to use when compiling, enclosed in \"\" (Default: %s)" % self.__flags)
        parser.add_argument('-cc', required=False, help="Compiler (Default: %s)" % self.__compiler)
        parser.add_argument('-exec', required=False, help="Executable name")
        parser.add_argument('-lang', required=False, choices=SUPPORTED_LANGUAGES, help="Use the default configs for the selected language")
        parser.add_argument('-lib', required=False, help="Libraries (if there are multiple, must be separated by a space)")
        parser.add_argument('-mode', required=False, help="If specified, user will enter in data via command line prompts", default=False, action='store_true')
        return parser

    def parse_command_line_input(self):
        parser = self.create_parser()
        self.__args = vars(parser.parse_args())

        if not os.path.isdir(self.__args["dir"]) and not os.path.exists(self.__args["dir"]):
            print "Invalid directory %s, exiting..." % self.__args["dir"]
            exit(1)

        self.__directory = self.__args['dir']

        if self.__args['mode']:
            self.prompt_user_for_input()
            return 0
        if self.__args["cc"]:
            self.__compiler = self.__args["cc"]
        if self.__args["flags"]:
            self.__flags = self.__args["flags"]
        if self.__args["exec"]:
            self.__executable = self.__args["exec"]
        if self.__args['lang']:
            self.__lang = self.__args['lang']
            if self.__args['lang'].lower() == "c++":
                self.__compiler = 'g++'
                self.__flags = '-g -Wall -std=c++11'
            else:
                # mode is c
                self.__compiler = 'gcc'
                self.__flags = '-g -Wall -std=c11'
        if self.__args['lib']:
            self.__lib = self.__args['lib']

        self.makefile_exists()

    def write_to_file(self, file_name):
        VALID_EXTENSIONS = [".cpp", ".c"]
        file_list = list(filter(lambda x: os.path.splitext(x)[1] in VALID_EXTENSIONS, os.listdir(os.path.dirname(file_name))))
        if len(file_list) == 0:
            print "No valid files were found in %s, exiting..." % os.path.dirname(file_name)
            exit(1)
        the_file = open(file_name, 'w')
        file_name_list = []
        write_str = ""
        for a_file in file_list:
            print "Processing File: %s" % a_file
            name = os.path.splitext(a_file)[0]
            file_name_list.append(name)
            write_str += "%s.o:\t%s" % (name, a_file)
            write_str += "\n\t%s $(%s) -c %s -o %s\n\n" % (self.__compiler, "FLAGS", a_file, name + ".o")

        the_file.write("FLAGS = %s\n\n" % self.__flags)
        the_file.write("all:\tMain\n\n")
        file_names = ""
        i = 0
        for name in file_name_list:
            if i+1 < len(file_name_list):
                file_names += name + ".o "
            else:
                file_names += name + ".o"
            i+=1
        the_file.write("Main:\tclean %s\n" % file_names)
        if self.__executable:
            if self.__lib:
                the_file.write("\t%s %s -o %s -l %s\n\n" % (self.__compiler, file_names, self.__executable, self.__lib))
            else:
                the_file.write("\t%s %s -o %s\n\n" % (self.__compiler, file_names, self.__executable))
        else:
            if self.__lib:
                the_file.write("\t%s %s -l %s\n\n" % (self.__compiler, file_names, self.__lib))
            else:
                the_file.write("\t%s %s\n\n" % (self.__compiler, file_names))

        the_file.write(write_str)
        if self.__executable:
            the_file.write("clean:\n\trm -f *.o %s\n" % self.__executable)
        else:
            the_file.write("clean:\n\trm -f *.o %s\n" % ("a.exe" if platform.system().lower() == "windows" else "a.out"))

        the_file.close()
        print "%s successfully saved." % file_name

    def prompt_user_for_input(self):
        print "Please fill out the following, or press <return> to ignore and use the default."
        lang = raw_input("Language: if specified, defaults for the selected language will be used and you will not be able to further customize anything. Select from %s: " % self.list_to_string(SUPPORTED_LANGUAGES))
        if lang:
            self.__lang = lang.strip().lower()
            if self.__lang == 'c++':
                self.__compiler = 'g++'
                self.__flags = '-g -Wall -std=c++11'
            elif self.__lang == 'c':
                # mode is c
                self.__compiler = 'gcc'
                self.__flags = '-g -Wall -std=c11'
            else:
                print "%s is not a valid selection, select from %s" % (self.__lang, self.list_to_string(SUPPORTED_LANGUAGES))
                exit(1)
        else:
            compiler = raw_input("Compiler: ").strip()
            if compiler:
                self.__compiler = compiler
            flags = raw_input("Flags: ").strip()
            if flags:
                self.__flags = flags

        lib = raw_input("Extra libraries, separated by a space: ").strip()
        if lib:
            self.__lib = lib
        executable = raw_input("Executable Name: ").strip()
        if executable:
            self.__executable = executable

        self.makefile_exists()

    def makefile_exists(self):
        makefile_name = "makefile"
        the_file = os.path.join(self.__directory, makefile_name)
        # note the exists funciton is case insensitive
        if os.path.exists(the_file):
            if os.path.isfile(the_file):
                answer = raw_input("%s already exists, overwrite? (y/n): " % os.path.basename(the_file))
                if answer.strip().lower() == 'y':
                    self.write_to_file(the_file)
                else:
                    print "File not overwritten, exiting..."
                    exit(0)
            else:
                print "%s is not a valid file and/or path to a file. Is %s a directory?" % (the_file, the_file)
        else:
            self.write_to_file(the_file)

    def list_to_string(self, the_list):
        return '[' + ', '.join(the_list) + ']'

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


<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals

from django.core.management.base import BaseCommand
from django.utils.translation import ugettext_lazy as _

from ...logic import ProjectSearch, AppSearch
from ...utils import get_app

class Command(BaseCommand):
    help = 'List unused template tags'

    def add_arguments(self, parser):
        parser.add_argument(
            '-a', '--app', nargs='?', type=str, action='store', required=False,
            help=_('The name of the application that needs to be scanned'))

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

        app_name = options.get('app')
        if app_name:
            app = get_app(app_name)
            AppSearch(app=app)
        else:
            ProjectSearch()
<commit_msg>Update management command<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

from django.core.management.base import BaseCommand
from django.utils.translation import ugettext_lazy as _

from ...search import ProjectSearch, AppSearch
from ...utils import get_app

class Command(BaseCommand):
    help = 'List unused template tags'

    def add_arguments(self, parser):
        parser.add_argument(
            '-a', '--app', nargs='?', type=str, action='store', required=False,
            help=_('The name of the application that needs to be scanned'))

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

        app_name = options.get('app')
        if app_name:
            app = get_app(app_name)
            AppSearch(app=app)
        else:
            ProjectSearch()
<|endoftext|>


<commit_before>'''
Helpful decorators module writing
'''

import logging
from collections import defaultdict
import inspect

class Depends(object):
    '''
    This decorator will check the module when it is loaded and check that the dependancies passed in
        are in the globals of the module. If not, it will cause the function to be unloaded (or replaced)
    '''
    # Dependancy -> list of things that depend on it
    dependancy_dict = defaultdict(set)
    def __init__(self, *dependancies, **kwargs):
        '''
        The decorator is instantiated with a list of dependancies (string of global name)

            an example use of this would be:

            @depends('modulename')
            def test():
                return 'foo'

            OR

            @depends('modulename', fallback_funcion=function)
            def test():
                return 'foo'
        '''
        logging.debug('Depends decorator instantiated with dep list of {0}'.format(dependancies))
        self.depencancies = dependancies
        self.fallback_funcion = kwargs.get('fallback_funcion')

    def __call__(self, function):
        '''
        The decorator is "__call__"d with the function, we take that function and
            determine which module and function name it is to store in the class wide
            depandancy_dict
        '''
        module = inspect.getmodule(inspect.stack()[1][0])
        for dep in self.depencancies:
            self.dependancy_dict[dep].add((module, function, self.fallback_funcion))
        return function

    @classmethod
    def enforce_dependancies(self, functions):
        '''
        This is a class global method to enforce the dependancies that you currently know about

        It will modify the "functions" dict and remove/replace modules that are missing dependancies
        '''
        for dependancy, dependant_set in self.dependancy_dict.iteritems():
            # check if dependancy is loaded
            for module, func, fallback_funcion in dependant_set:
                # check if you have the dependancy
                if dependancy in dir(module):
                    logging.debug('Dependancy ({0}) already loaded, skipping'.format(dependancy))
                    continue
                logging.debug('Unloading {0}.{1} because dependancy ({2}) is not imported'.format(module, func, dependancy))
                # if not, unload dependand_set
                mod_key = '{0}.{1}'.format(module.__name__.split('.')[-1],
                                           func.__name__)

                # if we don't have this module loaded, skip it!
                if mod_key not in functions:
                    continue

                try:
                    if fallback_funcion is not None:
                        functions[mod_key] = fallback_funcion
                    else:
                        del(functions[mod_key])
                except AttributeError:
                    # we already did???
                    logging.debug('{0} already removed, skipping'.format(mod_key))
                    continue

    @classmethod
    def old_enforce_dependancies(self):
        '''
        This is a class global method to enforce the dependancies that you currently know about
        '''
        for dependancy, dependant_set in self.dependancy_dict.iteritems():
            # check if dependancy is loaded
            for module, func, fallback_funcion in dependant_set:
                # check if you have the dependancy
                if dependancy in dir(module):
                    logging.debug('Dependancy ({0}) already loaded, skipping'.format(dependancy))
                    continue
                logging.debug('Unloading {0}.{1} because dependancy ({2}) is not imported'.format(module, func, dependancy))
                # if not, unload dependand_set

                try:
                    if fallback_funcion is not None:
                        setattr(module, func.__name__, fallback_funcion)
                    else:
                        delattr(module, func.__name__)
                except AttributeError:
                    # we already did???
                    logging.debug('{0}.{1} already removed, skipping'.format(module, func.__name__))
                    continue

class depends(Depends):
    '''
    Wrapper of Depends for capitalization
    '''
    pass

<commit_msg>Removing old code (from testing) from the Depends decorator<commit_after>'''
Helpful decorators module writing
'''

import logging
from collections import defaultdict
import inspect

class Depends(object):
    '''
    This decorator will check the module when it is loaded and check that the dependancies passed in
        are in the globals of the module. If not, it will cause the function to be unloaded (or replaced)
    '''
    # Dependancy -> list of things that depend on it
    dependancy_dict = defaultdict(set)
    def __init__(self, *dependancies, **kwargs):
        '''
        The decorator is instantiated with a list of dependancies (string of global name)

            an example use of this would be:

            @depends('modulename')
            def test():
                return 'foo'

            OR

            @depends('modulename', fallback_funcion=function)
            def test():
                return 'foo'
        '''
        logging.debug('Depends decorator instantiated with dep list of {0}'.format(dependancies))
        self.depencancies = dependancies
        self.fallback_funcion = kwargs.get('fallback_funcion')

    def __call__(self, function):
        '''
        The decorator is "__call__"d with the function, we take that function and
            determine which module and function name it is to store in the class wide
            depandancy_dict
        '''
        module = inspect.getmodule(inspect.stack()[1][0])
        for dep in self.depencancies:
            self.dependancy_dict[dep].add((module, function, self.fallback_funcion))
        return function

    @classmethod
    def enforce_dependancies(self, functions):
        '''
        This is a class global method to enforce the dependancies that you currently know about

        It will modify the "functions" dict and remove/replace modules that are missing dependancies
        '''
        for dependancy, dependant_set in self.dependancy_dict.iteritems():
            # check if dependancy is loaded
            for module, func, fallback_funcion in dependant_set:
                # check if you have the dependancy
                if dependancy in dir(module):
                    logging.debug('Dependancy ({0}) already loaded, skipping'.format(dependancy))
                    continue
                logging.debug('Unloading {0}.{1} because dependancy ({2}) is not imported'.format(module, func, dependancy))
                # if not, unload dependand_set
                mod_key = '{0}.{1}'.format(module.__name__.split('.')[-1],
                                           func.__name__)

                # if we don't have this module loaded, skip it!
                if mod_key not in functions:
                    continue

                try:
                    if fallback_funcion is not None:
                        functions[mod_key] = fallback_funcion
                    else:
                        del(functions[mod_key])
                except AttributeError:
                    # we already did???
                    logging.debug('{0} already removed, skipping'.format(mod_key))
                    continue

class depends(Depends):
    '''
    Wrapper of Depends for capitalization
    '''
    pass

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#    Copyright 2015-2016 ARM Limited
#
# 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.
#

# TRAPpy documentation build configuration file, created by
# sphinx-quickstart on Fri Sep  4 11:30:35 2015.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os
import shlex

this_dir = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(this_dir, '../..'))
import trappy

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.todo',
    'sphinx.ext.coverage',
    'sphinx.ext.mathjax',
    'sphinx.ext.ifconfig',
    'sphinx.ext.viewcode']

# Update MathJax path to use the cdnjs using HTTPS
mathjax_path = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'TRAPpy'
copyright = u'2015, ARM Ltd.'
author = u'Javi Merino, Kapileshwar Singh(KP)'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '5.1'
# The full version, including alpha/beta/rc tags.
release = '5.1.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = 'en'

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']

# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'classic'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
#html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
#html_extra_path = []

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
#   'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja'
#   'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr'
#html_search_language = 'en'

# A dictionary with options for the search language support, empty by default.
# Now only 'ja' uses this config value
#html_search_options = {'type': 'default'}

# The name of a javascript file (relative to the configuration directory) that
# implements a search results scorer. If empty, the default will be used.
#html_search_scorer = 'scorer.js'

# Output file base name for HTML help builder.
htmlhelp_basename = 'TRAPpydoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',

# Latex figure (float) alignment
#'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
  (master_doc, 'TRAPpy.tex', u'TRAPpy Documentation',
   u'Javi Merino, Kapileshwar Singh(KP)', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'trappy', u'TRAPpy Documentation',
     [author], 1)
]

# If true, show URL addresses after external links.
#man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
  (master_doc, 'TRAPpy', u'TRAPpy Documentation',
   author, 'TRAPpy', 'One line description of project.',
   'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
#texinfo_appendices = []

# If false, no module index is generated.
#texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False

# -- Options for Epub output ----------------------------------------------

# Bibliographic Dublin Core info.
epub_title = project
epub_author = author
epub_publisher = author
epub_copyright = copyright

# The basename for the epub file. It defaults to the project name.
#epub_basename = project

# The HTML theme for the epub output. Since the default themes are not optimized
# for small screen space, using the same theme for HTML and epub output is
# usually not wise. This defaults to 'epub', a theme designed to save visual
# space.
#epub_theme = 'epub'

# The language of the text. It defaults to the language option
# or 'en' if the language is not set.
#epub_language = ''

# The scheme of the identifier. Typical schemes are ISBN or URL.
#epub_scheme = ''

# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#epub_identifier = ''

# A unique identification for the text.
#epub_uid = ''

# A tuple containing the cover image and cover page html template filenames.
#epub_cover = ()

# A sequence of (type, uri, title) tuples for the guide element of content.opf.
#epub_guide = ()

# HTML files that should be inserted before the pages created by sphinx.
# The format is a list of tuples containing the path and title.
#epub_pre_files = []

# HTML files shat should be inserted after the pages created by sphinx.
# The format is a list of tuples containing the path and title.
#epub_post_files = []

# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']

# The depth of the table of contents in toc.ncx.
#epub_tocdepth = 3

# Allow duplicate toc entries.
#epub_tocdup = True

# Choose between 'default' and 'includehidden'.
#epub_tocscope = 'default'

# Fix unsupported image types using the Pillow.
#epub_fix_images = False

# Scale large images.
#epub_max_image_width = 0

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#epub_show_urls = 'inline'

# If false, no index is generated.
#epub_use_index = True
<commit_msg>doc: Update copyright to 2016<commit_after># -*- coding: utf-8 -*-
#    Copyright 2015-2016 ARM Limited
#
# 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.
#

# TRAPpy documentation build configuration file, created by
# sphinx-quickstart on Fri Sep  4 11:30:35 2015.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os
import shlex

this_dir = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(this_dir, '../..'))
import trappy

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.todo',
    'sphinx.ext.coverage',
    'sphinx.ext.mathjax',
    'sphinx.ext.ifconfig',
    'sphinx.ext.viewcode']

# Update MathJax path to use the cdnjs using HTTPS
mathjax_path = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'TRAPpy'
copyright = u'2016, ARM Ltd.'
author = u'Javi Merino, Kapileshwar Singh(KP)'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '5.1'
# The full version, including alpha/beta/rc tags.
release = '5.1.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = 'en'

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']

# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'classic'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
#html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
#html_extra_path = []

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
#   'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja'
#   'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr'
#html_search_language = 'en'

# A dictionary with options for the search language support, empty by default.
# Now only 'ja' uses this config value
#html_search_options = {'type': 'default'}

# The name of a javascript file (relative to the configuration directory) that
# implements a search results scorer. If empty, the default will be used.
#html_search_scorer = 'scorer.js'

# Output file base name for HTML help builder.
htmlhelp_basename = 'TRAPpydoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',

# Latex figure (float) alignment
#'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
  (master_doc, 'TRAPpy.tex', u'TRAPpy Documentation',
   u'Javi Merino, Kapileshwar Singh(KP)', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'trappy', u'TRAPpy Documentation',
     [author], 1)
]

# If true, show URL addresses after external links.
#man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
  (master_doc, 'TRAPpy', u'TRAPpy Documentation',
   author, 'TRAPpy', 'One line description of project.',
   'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
#texinfo_appendices = []

# If false, no module index is generated.
#texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False

# -- Options for Epub output ----------------------------------------------

# Bibliographic Dublin Core info.
epub_title = project
epub_author = author
epub_publisher = author
epub_copyright = copyright

# The basename for the epub file. It defaults to the project name.
#epub_basename = project

# The HTML theme for the epub output. Since the default themes are not optimized
# for small screen space, using the same theme for HTML and epub output is
# usually not wise. This defaults to 'epub', a theme designed to save visual
# space.
#epub_theme = 'epub'

# The language of the text. It defaults to the language option
# or 'en' if the language is not set.
#epub_language = ''

# The scheme of the identifier. Typical schemes are ISBN or URL.
#epub_scheme = ''

# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#epub_identifier = ''

# A unique identification for the text.
#epub_uid = ''

# A tuple containing the cover image and cover page html template filenames.
#epub_cover = ()

# A sequence of (type, uri, title) tuples for the guide element of content.opf.
#epub_guide = ()

# HTML files that should be inserted before the pages created by sphinx.
# The format is a list of tuples containing the path and title.
#epub_pre_files = []

# HTML files shat should be inserted after the pages created by sphinx.
# The format is a list of tuples containing the path and title.
#epub_post_files = []

# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']

# The depth of the table of contents in toc.ncx.
#epub_tocdepth = 3

# Allow duplicate toc entries.
#epub_tocdup = True

# Choose between 'default' and 'includehidden'.
#epub_tocscope = 'default'

# Fix unsupported image types using the Pillow.
#epub_fix_images = False

# Scale large images.
#epub_max_image_width = 0

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#epub_show_urls = 'inline'

# If false, no index is generated.
#epub_use_index = True
<|endoftext|>


<commit_before># Copyright (C) 2016 A10 Networks Inc. All rights reserved.

import operator

from a10_openstack_lib.resources import a10_certificate
from neutronclient.common import utils

from a10_neutronclient import client_extension

class CertificateExtension(client_extension.ClientExtension):

    resource = a10_certificate.CERTIFICATE
    resource_plural = a10_certificate.CERTIFICATES

    resource_attribute_map = a10_certificate.RESOURCE_ATTRIBUTE_MAP

    object_path = '/%s' % resource_plural
    resource_path = '/%s/%%s' % resource_plural
    versions = ['2.0']

class CertificateList(client_extension.List, CertificateExtension):
    """List A10 SSL Certificates"""

    shell_command = 'a10-certificate-list'

    list_columns = ['id', 'name', 'description']

class CertificateCreate(client_extension.Create, CertificateExtension):
    """Create A10 scaling group"""

    shell_command = 'a10-certificate-create'

    list_columns = ['id', 'name', 'description', 'cert_data', 'key_data', 'intermediate_data', 'password']

    def add_known_arguments(self, parser):
        parser.add_argument(
            '--cert-file',
            action=client_extension.ReadFileAction,
            dest='cert_data')

        parser.add_argument(
            '--key-file',
            action=client_extension.ReadFileAction,
            dest='key_data')

        parser.add_argument(
            '--intermediate-file',
            action=client_extension.ReadFileAction,
            dest='intermediate_data')
        self._add_known_arguments(parser, ['name'])

class CertificateDelete(client_extension.Delete, CertificateExtension):
    """Delete A10 SSL Certificate"""

    shell_command = 'a10-certificate-delete'

class CertificateShow(client_extension.Show, CertificateExtension):
    """Show A10 SSL Certificate"""

    shell_command = 'a10-certificate-show'

class CertificateUpdate(client_extension.Update, CertificateExtension):
    """Update A10 scaling group"""

    shell_command = 'a10-certificate-update'

    list_columns = ['id', 'name', 'cert_data', 'intermediate_data', 'key_data']

class CertificateBindingExtension(client_extension.ClientExtension):

    resource = a10_certificate.CERTIFICATE_BINDING
    resource_plural = a10_certificate.CERTIFICATE_BINDINGS

    resource_attribute_map = a10_certificate.RESOURCE_ATTRIBUTE_MAP

    object_path = '/%s' % resource_plural
    resource_path = '/%s/%%s' % resource_plural
    versions = ['2.0']

class CertificateBindingList(client_extension.List, CertificateBindingExtension):
    """List A10 A10 SSL Certificate <-> Listener Bindings"""

    shell_command = 'a10-certificatebinding-list'

    list_columns = ['certificate_id', 'listener_id']

class CertificateBindingCreate(client_extension.Create, CertificateBindingExtension):
    """Create A10 scaling group worker"""

    shell_command = 'a10-certificatebinding-create'
    list_columns = ['certificate_id', 'listener_id']

    def add_known_arguments(self, parser):
        self._add_known_arguments(parser, ['certificate_id', 'listener_id'])

class CertificateBindingDelete(client_extension.Delete, CertificateBindingExtension):
    """Delete A10 scaling group worker"""

    shell_command = 'a10-certificatebinding-delete'

class CertificateBindingShow(client_extension.Show, CertificateBindingExtension):
    """Show A10 scaling group worker"""

    shell_command = 'a10-certificatebinding-show'

<commit_msg>List ID with binding list for easy deleting<commit_after># Copyright (C) 2016 A10 Networks Inc. All rights reserved.

import operator

from a10_openstack_lib.resources import a10_certificate
from neutronclient.common import utils

from a10_neutronclient import client_extension

class CertificateExtension(client_extension.ClientExtension):

    resource = a10_certificate.CERTIFICATE
    resource_plural = a10_certificate.CERTIFICATES

    resource_attribute_map = a10_certificate.RESOURCE_ATTRIBUTE_MAP

    object_path = '/%s' % resource_plural
    resource_path = '/%s/%%s' % resource_plural
    versions = ['2.0']

class CertificateList(client_extension.List, CertificateExtension):
    """List A10 SSL Certificates"""

    shell_command = 'a10-certificate-list'

    list_columns = ['id', 'name', 'description']

class CertificateCreate(client_extension.Create, CertificateExtension):
    """Create A10 scaling group"""

    shell_command = 'a10-certificate-create'

    list_columns = ['id', 'name', 'description', 'cert_data', 'key_data', 'intermediate_data', 'password']

    def add_known_arguments(self, parser):
        parser.add_argument(
            '--cert-file',
            action=client_extension.ReadFileAction,
            dest='cert_data')

        parser.add_argument(
            '--key-file',
            action=client_extension.ReadFileAction,
            dest='key_data')

        parser.add_argument(
            '--intermediate-file',
            action=client_extension.ReadFileAction,
            dest='intermediate_data')
        self._add_known_arguments(parser, ['name'])

class CertificateDelete(client_extension.Delete, CertificateExtension):
    """Delete A10 SSL Certificate"""

    shell_command = 'a10-certificate-delete'

class CertificateShow(client_extension.Show, CertificateExtension):
    """Show A10 SSL Certificate"""

    shell_command = 'a10-certificate-show'

class CertificateUpdate(client_extension.Update, CertificateExtension):
    """Update A10 scaling group"""

    shell_command = 'a10-certificate-update'

    list_columns = ['id', 'name', 'cert_data', 'intermediate_data', 'key_data']

class CertificateBindingExtension(client_extension.ClientExtension):

    resource = a10_certificate.CERTIFICATE_BINDING
    resource_plural = a10_certificate.CERTIFICATE_BINDINGS

    resource_attribute_map = a10_certificate.RESOURCE_ATTRIBUTE_MAP

    object_path = '/%s' % resource_plural
    resource_path = '/%s/%%s' % resource_plural
    versions = ['2.0']

class CertificateBindingList(client_extension.List, CertificateBindingExtension):
    """List A10 A10 SSL Certificate <-> Listener Bindings"""

    shell_command = 'a10-certificatebinding-list'

    list_columns = ['id', 'certificate_id', 'listener_id']

class CertificateBindingCreate(client_extension.Create, CertificateBindingExtension):
    """Create A10 scaling group worker"""

    shell_command = 'a10-certificatebinding-create'
    list_columns = ['certificate_id', 'listener_id']

    def add_known_arguments(self, parser):
        self._add_known_arguments(parser, ['certificate_id', 'listener_id'])

class CertificateBindingDelete(client_extension.Delete, CertificateBindingExtension):
    """Delete A10 scaling group worker"""

    shell_command = 'a10-certificatebinding-delete'

class CertificateBindingShow(client_extension.Show, CertificateBindingExtension):
    """Show A10 scaling group worker"""

    shell_command = 'a10-certificatebinding-show'

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division

import sys
import os
import datetime

import pytest

from petl.io.gsheet import fromgsheet, togsheet, appendgsheet
from petl.test.helpers import ieq, get_env_vars_named

gspread = pytest.importorskip("gspread")
uuid = pytest.importorskip("uuid")

def _has_gshet_credentials():
    if os.getenv("PETL_GCP_JSON_PATH", None) is not None:
        return True
    json_props = get_env_vars_named("PETL_GSPREAD_")
    if json_props is not None:
        return True
    if os.path.isfile(os.path.expanduser("~/.config/gspread/service_account.json")):
        return True
    return False

if not _has_gshet_credentials():
    pytest.skip("""SKIPPED. to/from gspread needs json credentials for testing.
In order to run google spreadsheet tests, follow the steps bellow:
1. Create a json authorization file, following the steps described at
   http://gspread.readthedocs.io/en/latest/oauth2.html, and save to a local path
2. Point the envvar `PETL_GSPREAD_JSON_PATH` to the json authorization file path
2. Or fill the properties inside the json authorization file in envrionment
   variables named with prefix PETL_GSPREAD_: PETL_GSPREAD_project_id=petl
3. Or else save the file in one of the following paths:
      unix: ~/.config/gspread/service_account.json
   windows: %APPDATA%\gspread\service_account.json
""")

@pytest.fixture(autouse=True, scope="module")
def credentials():
    json_path = os.getenv("PETL_GCP_JSON_PATH", None)
    if json_path is not None:
        creds_from_file = gspread.service_account(filename=json_path)
        return creds_from_file
    json_props = get_env_vars_named("PETL_GSPREAD_")
    if json_props is not None:
        creds_from_env = gspread.service_account_from_dict(json_props)
        return creds_from_env
    default_path = os.path.expanduser("~/.config/gspread/service_account.json")
    if os.path.isfile(default_path):
        # gc = gspread.service_account()
        gc = gspread.service_account_from_dict(default_path)
        return gc
    return None

TEST1 = [
    # straight copy test
    (
        (("foo", "bar"), ("A", "1"), ("B", "2"), ("C", "2"), ("é", "1/1/2012")),
        None,
        None,
        (("foo", "bar"), ("A", "1"), ("B", "2"), ("C", "2"), ("é", "1/1/2012")),
    ),
    # Uneven row test
    (
        (("foo", "bar"), ("A", "1"), ("B", "2", "3"), ("C", "2"), ("é", "1/1/2012")),
        None,
        None,
        (
            ("foo", "bar", ""),
            ("A", "1", ""),
            ("B", "2", "3"),
            ("C", "2", ""),
            ("é", "1/1/2012", ""),
        ),
    ),
    # datetime to string representation test
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            ("é", datetime.date(2012, 1, 1)),
        ),
        "Sheet1",
        None,
        (("foo", "bar"), ("A", "1"), ("B", "2"), ("C", "2"), ("é", "2012-01-01")),
    ),
    # empty table test
    ((), None, None, ()),
    # range_string specified test
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            ("é", datetime.date(2012, 1, 1)),
        ),
        None,
        "B1:B4",
        (("bar",), ("1",), ("2",), ("2",)),
    ),
    # range_string+sheet specified test
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            ("é", datetime.date(2012, 1, 1)),
        ),
        "random_stuff-in+_名字",
        "B1:B4",
        (("bar",), ("1",), ("2",), ("2",)),
    ),
]

def test_tofromgsheet1():
    t1 = TEST1[0]
    test_tofromgsheet(t1[0], t1[2], t1[2], t1[3])

@pytest.mark.parametrize("table,worksheet,range_string,expected_result", TEST1)
def test_tofromgsheet(table, worksheet, range_string, expected_result):
    filename = "test-{}".format(str(uuid.uuid4()))
    # test to from gsheet
    togsheet(table, filename, credentials, worksheet_title=worksheet)
    result = fromgsheet(
        filename, credentials, worksheet_title=worksheet, range_string=range_string
    )
    # make sure the expected_result matches the result
    ieq(result, expected_result)

    # test open by key
    client = gspread.authorize(credentials)
    # get spreadsheet id (key) of previously created sheet
    filekey = client.open(filename).id
    key_result = fromgsheet(
        filekey, credentials, worksheet_title=worksheet, range_string=range_string
    )
    ieq(key_result, expected_result)
    # clean up created table
    client.del_spreadsheet(filekey)

TEST2 = [
    # appending to the first sheet
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            ("é", datetime.date(2012, 1, 1)),
        ),
        "Sheet1",
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            ("é", "2012-01-01"),
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
        ),
    ),
    # appending to a new sheet
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            ("é", datetime.date(2012, 1, 1)),
        ),
        "testing_time",
        (("foo", "bar"), ("A", "1"), ("B", "2"), ("C", "2")),
    ),
]

@pytest.mark.parametrize("table,append_worksheet,expected_result", TEST2)
def test_toappendfrom(table, append_worksheet, expected_result):
    filename = "test-{}".format(str(uuid.uuid4()))
    togsheet(table, filename, credentials)
    appendgsheet(table[:-1], filename, credentials, worksheet_title=append_worksheet)
    result = fromgsheet(filename, credentials, worksheet_title=append_worksheet)
    ieq(result, expected_result)
    # get client to get information
    client = gspread.authorize(credentials)
    # get spreadsheet id (key) of previously created sheet
    filekey = client.open(filename).id
    # then delete the file
    client.del_spreadsheet(filekey)
<commit_msg>pytest: gsheets test passing after refactoring<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division

import os
import json

import pytest

from petl.compat import text_type
from petl.io.gsheet import fromgsheet, togsheet, appendgsheet
from petl.test.helpers import ieq, get_env_vars_named

gspread = pytest.importorskip("gspread")
uuid = pytest.importorskip("uuid")

def _get_gspread_credentials():
    json_path = os.getenv("PETL_GCP_JSON_PATH", None)
    if json_path is not None and os.path.isfile(json_path):
        return json_path
    json_props = get_env_vars_named("PETL_GSPREAD_")
    if json_props is not None:
        return json_props
    user_path = os.path.expanduser("~/.config/gspread/service_account.json")
    if os.path.isfile(user_path):
        return user_path
    return None

if _get_gspread_credentials() is None:
    pytest.skip("""SKIPPED. to/from gspread needs json credentials for testing.
In order to run google spreadsheet tests, follow the steps bellow:
1. Create a json authorization file, following the steps described at
   http://gspread.readthedocs.io/en/latest/oauth2.html, and save to a local path
2. Point the envvar `PETL_GSPREAD_JSON_PATH` to the json authorization file path
2. Or fill the properties inside the json authorization file in envrionment
   variables named with prefix PETL_GSPREAD_: PETL_GSPREAD_project_id=petl
3. Or else save the file in one of the following paths:
      unix: ~/.config/gspread/service_account.json
   windows: %APPDATA%\\gspread\\service_account.json
""")

def _load_creds_from_file(json_path):
    with open(json_path, encoding="utf-8") as json_file:
        creds = json.load(json_file)
        return creds

def _get_env_credentials():
    creds = _get_gspread_credentials()
    if isinstance(creds, dict):
        return creds
    if isinstance(creds, text_type):
        props = _load_creds_from_file(creds)
        return props
    return None

def _get_gspread_client():
    credentials = _get_env_credentials()
    try:
        if credentials is None:
            gspread_client = gspread.service_account()
        else:
            gspread_client = gspread.service_account_from_dict(credentials)
    except gspread.exceptions.APIError as ex:
        pytest.skip("SKIPPED. to/from gspread authentication error: %s" % ex)
        return None
    return gspread_client

def _get_gspread_test_params():
    return "test-{}".format(str(uuid.uuid4()))

TEST1 = [
    # straight copy test
    (
        (("foo", "bar"), ("A", "1"), ("B", "2"), ("C", "2"), ("é", "1/1/2012")),
        None,
        None,
        (("foo", "bar"), ("A", "1"), ("B", "2"), ("C", "2"), ("é", "1/1/2012")),
    ),
    # Uneven row test
    (
        (("foo", "bar"), ("A", "1"), ("B", "2", "3"), ("C", "2"), ("é", "1/1/2012")),
        None,
        None,
        (
            ("foo", "bar", ""),
            ("A", "1", ""),
            ("B", "2", "3"),
            ("C", "2", ""),
            ("é", "1/1/2012", ""),
        ),
    ),
    # datetime to string representation test
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            # ("é", datetime.date(2012, 1, 1)),
            ("é", "2012-01-01"),
        ),
        "Sheet1",
        None,
        (("foo", "bar"), ("A", "1"), ("B", "2"), ("C", "2"), ("é", "2012-01-01")),
    ),
    # empty table test
    ((), None, None, ()),
    # cell_range specified test
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            # ("é", datetime.date(2012, 1, 1)),
            ("é", "2012-01-01"),
        ),
        None,
        "B1:B4",
        (("bar",), ("1",), ("2",), ("2",)),
    ),
    # cell_range+sheet specified test
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            # ("é", datetime.date(2012, 1, 1)),
            ("é", "2012-01-01"),
        ),
        "random_stuff-in+_名字",
        "B1:B4",
        (("bar",), ("1",), ("2",), ("2",)),
    ),
]

@pytest.mark.parametrize("table,worksheet,cell_range,expected_result", TEST1)
def test_tofromgsheet(table, worksheet, cell_range, expected_result):
    filename = _get_gspread_test_params()
    gspread_client = _get_gspread_client()
    # test to from gsheet
    spread_id = togsheet(table, gspread_client, filename, title=worksheet)
    result = fromgsheet(
        gspread_client, filename, title=worksheet, cell_range=cell_range
    )
    # make sure the expected_result matches the result
    ieq(result, expected_result)

    key_result = fromgsheet(
        gspread_client, spread_id, open_by_key=True, title=worksheet,
        cell_range=cell_range
    )
    ieq(key_result, expected_result)
    # clean up created table
    gspread_client.del_spreadsheet(spread_id)

TEST2 = [
    # Simplest test
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
        ),
        "Sheet1",
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("foo", "bar"),
            ("A", "1"),
        ),
    ),
    # appending to the first sheet
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            # ("é", datetime.date(2012, 1, 1)),
            ("é", "2012-01-01"),
        ),
        "Sheet1",
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            ("é", "2012-01-01"),
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
        ),
    ),
    # appending to a new sheet
    (
        (
            ("foo", "bar"),
            ("A", "1"),
            ("B", "2"),
            ("C", "2"),
            # ("é", datetime.date(2012, 1, 1)),
            ("é", "2012-01-01"),
        ),
        "testing_time",
        (("foo", "bar"), ("A", "1"), ("B", "2"), ("C", "2")),
    ),
]

@pytest.mark.parametrize("table,append_worksheet,expected_result", TEST2)
def test_toappendfrom(table, append_worksheet, expected_result):
    filename = _get_gspread_test_params()
    gspread_client = _get_gspread_client()
    # test to append gsheet
    spread_id = togsheet(table, gspread_client, filename)
    table2 = table[:-1]
    appendgsheet(table2, gspread_client, filename, title=append_worksheet)
    result = fromgsheet(gspread_client, filename, title=append_worksheet)
    ieq(result, expected_result)
    # then delete the file
    gspread_client.del_spreadsheet(spread_id)
<|endoftext|>


<commit_before>from uber.tests import *

def test_ints():
    assert [] == Attendee().interests_ints
    assert [c.ARCADE] == Attendee(interests=c.ARCADE).interests_ints
    assert [c.ARCADE, c.CONSOLE] == Attendee(interests='{},{}'.format(c.ARCADE, c.CONSOLE)).interests_ints

def test_label():
    assert '' == Attendee(paid=None).paid_label
    assert 'yes' == Attendee(paid=c.HAS_PAID).paid_label
    assert 'none' == Attendee(paid="none").paid_label

def test_local():
    utcnow = datetime.now(UTC)
    assert UTC == Attendee(registered=utcnow).registered.tzinfo
    assert c.EVENT_TIMEZONE.zone == Attendee(registered=utcnow).registered_local.tzinfo.zone

def test_multi_checks():
    pytest.raises(AttributeError, lambda: Attendee().CONSOLE)
    pytest.raises(AttributeError, lambda: Attendee().NOT_A_REAL_CHOICE)

    assert not AdminAccount().PEOPLE
    assert AdminAccount(access='{},{}'.format(c.PEOPLE, c.STUFF)).PEOPLE
    assert not AdminAccount(access='{},{}'.format(c.PEOPLE, c.STUFF)).ACCOUNTS
<commit_msg>fix unit test to reflect correct return value from _label()<commit_after>from uber.tests import *

def test_ints():
    assert [] == Attendee().interests_ints
    assert [c.ARCADE] == Attendee(interests=c.ARCADE).interests_ints
    assert [c.ARCADE, c.CONSOLE] == Attendee(interests='{},{}'.format(c.ARCADE, c.CONSOLE)).interests_ints

def test_label():
    assert '' == Attendee(paid=None).paid_label
    assert 'yes' == Attendee(paid=c.HAS_PAID).paid_label
    assert '' == Attendee(paid="none").paid_label

def test_local():
    utcnow = datetime.now(UTC)
    assert UTC == Attendee(registered=utcnow).registered.tzinfo
    assert c.EVENT_TIMEZONE.zone == Attendee(registered=utcnow).registered_local.tzinfo.zone

def test_multi_checks():
    pytest.raises(AttributeError, lambda: Attendee().CONSOLE)
    pytest.raises(AttributeError, lambda: Attendee().NOT_A_REAL_CHOICE)

    assert not AdminAccount().PEOPLE
    assert AdminAccount(access='{},{}'.format(c.PEOPLE, c.STUFF)).PEOPLE
    assert not AdminAccount(access='{},{}'.format(c.PEOPLE, c.STUFF)).ACCOUNTS
<|endoftext|>


<commit_before># Iris Detection 2 Example
#
# This example shows how to find the eye gaze (pupil detection) after finding
# the eyes in an image. This script uses the find_eyes function which determines
# the center point of roi that should contain a pupil. It does this by basically
# finding the center of the darkest area in the eye roi which is the pupil center.
#
# Note: This script does not detect a face first, use it with the telephoto lens.

import sensor, time, image

# Reset sensor
sensor.reset()

# Sensor settings
sensor.set_contrast(3)
sensor.set_gainceiling(16)

# Set resolution to VGA.
sensor.set_framesize(sensor.VGA)

# Bin/Crop image to 200x100, which gives more details with less data to process
sensor.set_binning((220, 200, 200, 100))

sensor.set_pixformat(sensor.GRAYSCALE)

# Load Haar Cascade
# By default this will use all stages, lower satges is faster but less accurate.
eyes_cascade = image.HaarCascade("eye", stages=24)
print(eyes_cascade)

# FPS clock
clock = time.clock()

while (True):
    clock.tick()
    # Capture snapshot
    img = sensor.snapshot()
    # Find eyes !
    # Note: Lower scale factor scales-down the image more and detects smaller objects.
    # Higher threshold results in a higher detection rate, with more false positives.
    eyes = img.find_features(eyes_cascade, threshold=0.5, scale=1.5)

    # Find iris
    for e in eyes:
        iris = img.find_eye(e)
        img.draw_rectangle(e)
        img.draw_cross(iris[0], iris[1])

    # Print FPS.
    # Note: Actual FPS is higher, streaming the FB makes it slower.
    print(clock.fps())
<commit_msg>Adjust iris window.<commit_after># Iris Detection 2 Example
#
# This example shows how to find the eye gaze (pupil detection) after finding
# the eyes in an image. This script uses the find_eyes function which determines
# the center point of roi that should contain a pupil. It does this by basically
# finding the center of the darkest area in the eye roi which is the pupil center.
#
# Note: This script does not detect a face first, use it with the telephoto lens.

import sensor, time, image

# Reset sensor
sensor.reset()

# Sensor settings
sensor.set_contrast(3)
sensor.set_gainceiling(16)

# Set resolution to VGA.
sensor.set_framesize(sensor.VGA)

# Bin/Crop image to 200x100, which gives more details with less data to process
sensor.set_binning((220, 190, 200, 100))

sensor.set_pixformat(sensor.GRAYSCALE)

# Load Haar Cascade
# By default this will use all stages, lower satges is faster but less accurate.
eyes_cascade = image.HaarCascade("eye", stages=24)
print(eyes_cascade)

# FPS clock
clock = time.clock()

while (True):
    clock.tick()
    # Capture snapshot
    img = sensor.snapshot()
    # Find eyes !
    # Note: Lower scale factor scales-down the image more and detects smaller objects.
    # Higher threshold results in a higher detection rate, with more false positives.
    eyes = img.find_features(eyes_cascade, threshold=0.5, scale=1.5)

    # Find iris
    for e in eyes:
        iris = img.find_eye(e)
        img.draw_rectangle(e)
        img.draw_cross(iris[0], iris[1])

    # Print FPS.
    # Note: Actual FPS is higher, streaming the FB makes it slower.
    print(clock.fps())
<|endoftext|>