Example #1
1
 def testDatabase(self):
     self.assert_(os.path.exists, self.db)
     if isinstance(self.db_connect, dict):
         db = self.db.connect(**self.db_connect)
     else:
         db = self.db.connect(*self.db_connect)
     try:
         c = db.cursor()
         # Check that data got imported
         c.execute("SELECT count(*) FROM county")
         self.assertEqual(c.fetchone(), (22,))
         c.execute("SELECT * FROM municipal WHERE municipal_id=1103")
         self.assertEqual(list(c.fetchall()), [(1103, "Stavanger", 11)])
         c.execute("SELECT * FROM postal WHERE postal_no=4042")
         # FIXME: BOOL with mysql might not equal False
         self.assertEqual(c.fetchone(), (4042, "HAFRSFJORD", 1103, False))
         # Check if wtf-8 worked fine. Note that the line
         # -*- coding: utf-8 -*- must be present at the top of
         # this file for the u"Østfold" thingie to work
         c.execute("SELECT county_name FROM county WHERE county_id=1")
         a = c.fetchone()[0]
         if not isinstance(a, unicode):
             a = a.decode("utf8")
         self.assertEqual(a, u"Østfold")
     finally:
         db.rollback()
Example #2
1
    def addUser(self, id, email=None):
        if not findUser(id):
            raise ScalakError("User with given login doesn't exists.")

        db = openDB()
        c = db.cursor()
        res = c.execute(
            "select user from user_project where user=%s and \
                project=%s limit 1",
            (id, self.id),
        )
        if res:
            raise ScalakError("User is already project member.")

        c.execute("insert into user_project values (%s, %s)", (id, self.id))

        if not email:
            res = c.execute(
                "select * from users where \
                    login=%s limit 1",
                (id,),
            )
            if res:
                id, name, last_name, email, passwd, sha_passwd, note = c.fetchone()

        db.commit()
        c.close()

        for srv in self.getService():
            srv.addUser(id, name, last_name, email, passwd, sha_passwd, note)
Example #3
1
def addUserRequest(user, project):
    """Add 'project join' requests"""

    db = openDB()
    c = db.cursor()

    c.execute("insert into project_requests values (%s, %s)", (user, project))

    db.close()
Example #4
1
    def setRequest(self, user, accept):
        db = openDB()
        c = db.cursor()
        c.execute("delete from project_requests where project = %s and user = %s", (self.id, user))
        db.commit()
        c.close()

        if accept:
            self.addUser(user)
Example #5
1
def getCapabilities(user):

    db = openDB()
    c = db.cursor()

    res = c.execute("select action, scope from capabilities where user = %s", (user,))

    db.close()

    return c.fetchall()
Example #6
1
def listUsers():
    """Return list of ids for all users"""

    db = openDB()
    c = db.cursor()

    res = c.execute("select login from users")
    db.close()

    return [x[0] for x in c.fetchall()]
Example #7
1
    def removeUser(self, id):
        if id == self.admin:
            raise ScalakError("You can't remove project administrator.")

        db = openDB()
        c = db.cursor()
        c.execute("delete from user_project where user=%s and project=%s", (id, self.id))
        db.commit()
        c.close()

        for srv in self.getService():
            srv.removeUser(id)
Example #8
1
def valid_password(env, username, password):
    """Check given auth data against DB; env may be None"""

    db = openDB()
    c = db.cursor()

    res = c.execute("select password from users where login=%s limit 1", (username,))

    if not res:
        return False

    res = c.fetchone()[0]
    db.close()

    return res == htpasswd(password, res[0] + res[1])
Example #9
1
def userProjects(id):
    """Returns all project for user with given _id_"""

    db = openDB()
    c = db.cursor()

    c.execute(
        "select project from user_project where \
            user=%s",
        (id,),
    )

    db.close()

    return c.fetchall()
Example #10
1
def getAdmin(project):
    """Returns admin for given _project_"""

    db = openDB()
    c = db.cursor()

    res = c.execute("select admin from projects where id=%s limit 1", (project,))

    if not res:
        return False

    res = c.fetchone()[0]

    db.close()

    return res
Example #11
1
    def isMemeber(self, id):
        """Checking whether user with given id is project member"""

        db = openDB()
        c = db.cursor()
        c.execute(
            "select login from user_project where user=%s and \
                project=%s limit 1",
            (id, self.id),
        )
        db.commit()
        c.close()

        if c.fetchone():
            return True
        return False
Example #12
1
 def add_user(db):
     c = db.cursor()
     c.execute("delete from session_attribute where sid=%s", (login,))
     c.execute("delete from permission where username=%s", (login,))
     c.execute(
         "insert into session_attribute (sid, authenticated, \
             name, value) values (%s, 1, 'email', %s)",
         (login, email),
     )
     c.execute(
         "insert into session_attribute (sid, authenticated, \
             name, value) values (%s, 1, 'name', %s)",
         (login, "{0} {1}".format(name, last_name)),
     )
     c.execute('insert into permission values (%s, "BROWSER_VIEW")', (login,))
     db.commit()
     c.close()
Example #13
1
    def prepareTables(self):
        # First connect raw
        if isinstance(self.db_connect, dict):
            db = self.db.connect(**self.db_connect)
        else:
            db = self.db.connect(*self.db_connect)
        if self.db_mod == "postgresql":
            db.autocommit(1)
        try:
            c = db.cursor()
            if "sqlite" in str(db).lower():
                # Ignore sync-checks for faster import
                c.execute("pragma synchronous = off")
            # Find our root by inspecting our own module
            import testforgetsql2

            root = os.path.dirname(testforgetsql2.__file__)
            file = os.path.join(root, "test-data.sql")
            sql = codecs.open(file, encoding="utf8").read()

            # DROP TABLE
            if self.db_mod == "mysql":
                for table in ("county", "municipal", "postal", "insertion", "shop", "changed"):
                    c.execute("DROP TABLE IF EXISTS %s" % table)
            elif self.db_mod == "postgresql":
                c.execute(
                    """SELECT tablename FROM pg_catalog.pg_tables 
                                 WHERE schemaname=pg_catalog.current_schema()"""
                )
                existing = c.fetchall()
                for table in ("county", "municipal", "postal", "insertion", "shop", "changed"):
                    if (table,) in existing:
                        c.execute("DROP TABLE %s" % table)
            elif self.db_mod == "sqlite":
                # No need to drop tables in sqlite, we blank out the db each
                # time
                pass
            else:
                raise "Unknown db", self.db_mod

            # CREATE TABLE // EXECUTE
            if self.db_mod == "sqlite":
                # We have to fake since sqlite does not support the
                # fancy "bool" type.
                sql = sql.replace("FALSE", "0")
                sql = sql.replace("TRUE", "1")
                c.executescript(sql)
            elif self.db_mod in ("mysql", "postgresql"):
                for statement in sql.split(";"):
                    if not statement.strip():
                        continue  # Skip empty lines
                    c.execute(statement.encode("utf8"))

            # Create database specific table "insertion"
            if self.db_mod == "sqlite":
                # This one is seperate because of "AUTOINCREMENT" vs "AUTO_INCREMENT"
                c.execute(
                    """
                    CREATE TABLE insertion (
                      insertion_id INTEGER PRIMARY KEY AUTOINCREMENT,
                      value VARCHAR(15)
                    )"""
                )
            elif self.db_mod == "mysql":
                c.execute(
                    """
                    CREATE TABLE insertion (
                      insertion_id INTEGER PRIMARY KEY AUTO_INCREMENT,
                      value VARCHAR(15)
                    )"""
                )
            elif self.db_mod == "postgresql":
                c.execute(
                    """
                    CREATE TABLE insertion (
                      insertion_id SERIAL PRIMARY KEY,
                      value VARCHAR(15)
                    )"""
                )
            else:
                raise "Unknown db", self.db_mod
            db.commit()
        finally:
            db.rollback()
Example #14
0
    def getRequests(self):
        """User may create request to be accepted as a project member"""

        db = openDB()
        c = db.cursor()
        res = c.execute(
            "select user, email from project_requests, users \
                where project=%s and user = login",
            (self.id,),
        )
        res = c.fetchall()
        c.close()

        return res
Example #15
0
def connect(db=None):
    try:
        sqlconf = __salt__["soy_mysql.setup"](db)
        conn = sql.connect(**sqlconf)
        curs = sql.cursor()
        return conn, curs
    except sql.Error as e:
        warn("mysql error: %s" % e.message, RuntimeWarning)
        return False
    except:
        warn("error while establishing connection", RuntimeWarning)
        return False
    finally:
        if conn:
            conn.close()
Example #16
0
def getUserData(id):
    """Get all public user data (without pass hashs)"""

    db = openDB()
    c = db.cursor()

    if not findUser(id):
        raise ScalakError("User doesn't exists")

    c.execute(
        "select login, name, last_name, email, note from users where \
            login=%s limit 1",
        (id,),
    )

    db.close()

    return c.fetchone()
Example #17
0
def findUser(id, project=None):
    """Check if user with given _id_ exists globally or in _project_ if given"""

    db = openDB()
    c = db.cursor()

    if not project:
        res = c.execute("select login from users where login=%s limit 1", (id,))

    else:
        res = c.execute(
            "select login from users, user_project where login=%s \
                and login=user and project=%s limit 1",
            (id, project),
        )

    db.close()

    if not res:
        return False

    return True
Example #18
0
def getUserRequests(user=None, project=None):
    """Returns 'project join' requests for given data or all requests"""

    db = openDB()
    cur = db.cursor()

    if project and user:
        res = cur.execute(
            "select * from project_requests where user=%s and \
                project=%s limit 1",
            (user, project),
        )

    elif not project and not user:
        res = cur.execute(
            "select * from project_requests where user=%s \
                limit 1",
            (id,),
        )

    elif project and not user:
        res = cur.execute(
            "select * from project_requests where \
                project=%s limit 1",
            (project,),
        )

    elif not project and user:
        res = cur.execute(
            "select * from project_requests where user=%s and \
                limit 1",
            (user,),
        )

    db.close()

    return cur.fetchall()
# 124 sample
classifier.setdb("test_100.db")

# List operations: for i in length of features classify
##classifier.classify(tweet_features[i])

# single test
location_text = "Beverly Hills"
classified_zipcode = classifier.classify("Beverly Hills")

zipcode = str(classified_zipcode[0])
confidence = str(classfied_zipcode[1])

# query MYSQL database and append to python list
db = MySQLdb("")
cur = db.cursor()
cur.execute("SELECT latitude,longitude FROM sqlbook.zipcounty WHERE zipcode = %s", zipcode)
rows = cur.fetchall()
latitude = rows[0][0]
longitude = rows[0][1]

likely_zipcode = {"location_text": location_text, "zipcode": zipcode, "latitude": latitude, "longitude": longitude}
weather = get__weather_data.getWeather(likely_zipcode["zipcode"])

likely_zipcode.update(weather)
print likely_zipcode

# if not close enough
# select 10 close locations to the one classified.

cur.execute(
            dias = map(int, track.split())

            if isTrueTrack(diasToSsmIds, dias):
                for dia in dias:
                    attributedDias.add(dia)

            track = inf.readline()
            if count % 10000 == 0:
                sys.stderr.write("so far, read %d lines...\n" % count)

    return attributedDias


if __name__ == "__main__":
    db = db.connect(user=DB_USER, passwd=DB_PASS, db=DB_DB)
    curs = db.cursor()

    inFiles = []
    # read each argument. It may be a glob.
    for arg in sys.argv[1:]:
        newInFiles = glob.glob(arg)
        # open each file to make sure it exists.
        for inf in newInFiles:
            sys.stderr.write("Opening %r as input file\n" % inf)
            inFiles.append(file(inf, "r"))
            sys.stderr.write("Opened it \n")

    # make a diaSource -> ssmId map
    sys.stderr.write("Building map of diaSources -> ssmIds...\n")
    diasToSsmIds = makeDiaToSsmIdMap(curs)
Example #21
0
 def remove_user(db):
     c = db.cursor()
     c.execute("delete from session_attribute where sid=%s", (id,))
     c.execute("delete from permission where username=%s", (id,))