From 0a90d5fb89bb5baa0bc34fb0dcea134d6bc8e220 Mon Sep 17 00:00:00 2001 From: yo mama Date: Wed, 12 Aug 2015 16:08:28 -0700 Subject: cleanup successful, still testing --- pb/lib/Db/__init__.py | 55 +++++++++++++++++++++ pb/lib/Utils/__init__.py | 123 +++++++++++++++++++++++++++++++++++++++++++++++ pb/lib/db.py | 54 --------------------- pb/lib/utils.py | 123 ----------------------------------------------- 4 files changed, 178 insertions(+), 177 deletions(-) create mode 100644 pb/lib/Db/__init__.py create mode 100644 pb/lib/Utils/__init__.py delete mode 100755 pb/lib/db.py delete mode 100644 pb/lib/utils.py (limited to 'pb/lib') diff --git a/pb/lib/Db/__init__.py b/pb/lib/Db/__init__.py new file mode 100644 index 0000000..91b0fcf --- /dev/null +++ b/pb/lib/Db/__init__.py @@ -0,0 +1,55 @@ +import MySQLdb +import time +USER = "asdfus" +PASSWORD = "gTYgT&M6q" +DATABASE = "asdfus" + +class Db(object): + def __init__ (self): + self.conn = None + self.cursor = None + self.connect() + + def connect (self): + self.conn = MySQLdb.connect (host = "localhost", + user = USER, + passwd = PASSWORD, + db = DATABASE + ) + self.cursor = self.conn.cursor () + + def execute (self,sql,args=()): + try: + self.cursor.execute(sql,args) + except MySQLdb.Error, e: + print "Error %d: %s" % (e.args[0], e.args[1]) + # sys.exit (1) + self.connect() + self.cursor.execute(sql,args) + + def lastinsertid (self): + return self.conn.insert_id() + + def insert_cmd ( + self, + date=time.time(), + remote_addr="NULL", + username="NULL", + url="NULL", + directory="NULL", + oldfile="NULL", + newfile="NULL", + cmd="NULL", + dataobj="NULL", + tag="NULL"): + try: + sql = "INSERT INTO im_cmd " + sql += "(date, remote_addr, name, url, dir, oldfile, newfile, cmd, dataobj, tag) " + sql += "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" + #or "NULL" + args = (date, remote_addr, username, url, directory, oldfile, newfile, cmd, dataobj, tag) + #args = (now(), os.environ['REMOTE_ADDR'], name, url, dir, oldfile, newfile, " ".join(cmd),dataobj) + self.execute(sql, args) + except Exception as e: + sys.stderr.write(str(e)) + return diff --git a/pb/lib/Utils/__init__.py b/pb/lib/Utils/__init__.py new file mode 100644 index 0000000..1d5a708 --- /dev/null +++ b/pb/lib/Utils/__init__.py @@ -0,0 +1,123 @@ +import re +from pb.Config import * +import time +import urllib +import urllib2 +import sys +import os +from subprocess import Popen,PIPE,call +Request = urllib2.Request +urlencode = urllib.urlencode +urlopen = urllib2.urlopen + +def call_cmd(cmd, error=""): + try: + call(cmd) + except Exception as e: + raise (str(e)) + +def is_color(s): + if s == "": + return "transparent" + if re.match('(rgba?\([0-9]+,[0-9]+,[0-9]+\))|([a-zA-Z]+)|(\#[A-Ha-h0-9]+)', s): + return s.replace(' ', ''); + else: + sys.stderr.write("Not a color: {}\n".format(s)) + raise ValueError + +def dimensions (filepath): + #works in lieu of a mimetype check (it reads the header as well) + ident = (Popen([BIN_IDENTIFY, filepath], stdout=PIPE).communicate()[0]).split(" ") + return ident[2].split("x") + +def is_number(s): + try: + return int(s) + except (ValueError, TypeError): + return False + +def bool_correct(s): + if re.match(r'^false$', s, re.IGNORECASE): + return False + elif re.match(r'^true$', s, re.IGNORECASE): + return True + else: + return s + +class dotdict(dict): + """dot.notation access to dictionary attributes""" + def __getattr__(self, attr): + return self.get(attr) + __setattr__= dict.__setitem__ + __delattr__= dict.__delitem__ + +def get_mimetype(f): + try: + mimetype = Popen( + [BIN_IDENTIFY, f], stdout=PIPE + ).communicate()[0].split(" ")[1].lower() + return mimetype + except Exception as e: + sys.stderr.write("couldn't determine mimetype") + sys.stderr.write(str(e)) + raise; + +def sanitize (str): + return re.sub(r'\W+', '', str) + +def now(): + return int(time.time()) + +def browser_request (url, data=None): + headers = { + 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', + 'Accept': '*/*', + } + try: + req = Request(url, data, headers) + response = urlopen(req) + except IOError, e: + if hasattr(e, 'code'): + sys.stderr.write( '%s - ERROR %s' % (url, e.code) ) + raise; + return None + else: + return response + +def download(url, destination, max_size=MAX_SIZE): + response = browser_request(url, None) + rawimg = response.read() + if len(rawimg) == 0: + sys.stderr.write("got zero-length file") + raise; + if len(rawimg) > max_size: + sys.stderr.write("file too big: max size {} KB / {} is {} KB".format( + str(MAX_SIZE/1024), + destination, + str(len(rawimg)/1024) + )) + raise; + f = open(destination, "w") + f.write(rawimg) + f.close() + +def file_size (filepath): + try: + return os.stat(filepath)[6] + except Exception as e: + sys.stderr.write("Couldn't determine filesize\n") + sys.stderr.write(str(e)+"\n") + raise; + +def gif_frames(filepath): + try: + info = Popen([BIN_IDENTIFY,filepath], stdout=PIPE).communicate()[0] + frames = filter((lambda x: x), map( + (lambda x: x.split(" ")[0]), + (info).split('\n') + )) + return frames + except Exception as e: + sys.stderr.write("IMGRID couldn't get gif frames") + sys.stderr.write(str(e)) + raise; diff --git a/pb/lib/db.py b/pb/lib/db.py deleted file mode 100755 index 66db35d..0000000 --- a/pb/lib/db.py +++ /dev/null @@ -1,54 +0,0 @@ -import MySQLdb -USER = "asdfus" -PASSWORD = "gTYgT&M6q" -DATABASE = "asdfus" - -class Db(object): - def __init__ (self): - self.conn = None - self.cursor = None - self.connect() - - def connect (self): - self.conn = MySQLdb.connect (host = "localhost", - user = USER, - passwd = PASSWORD, - db = DATABASE - ) - self.cursor = self.conn.cursor () - - def execute (self,sql,args=()): - try: - self.cursor.execute(sql,args) - except MySQLdb.Error, e: - print "Error %d: %s" % (e.args[0], e.args[1]) - # sys.exit (1) - self.connect() - self.cursor.execute(sql,args) - - def lastinsertid (self): - return self.conn.insert_id() - - def insert_cmd ( - self, - date=time.time(), - remote_addr="NULL", - username="NULL", - url="NULL", - directory="NULL", - oldfile="NULL", - newfile="NULL", - cmd="NULL", - dataobj="NULL", - tag="NULL"): - try: - sql = "INSERT INTO im_cmd " - sql += "(date, remote_addr, name, url, dir, oldfile, newfile, cmd, dataobj, tag) " - sql += "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" - #or "NULL" - args = (date, remote_addr, username, url, directory, oldfile, newfile, cmd, dataobj, tag) - #args = (now(), os.environ['REMOTE_ADDR'], name, url, dir, oldfile, newfile, " ".join(cmd),dataobj) - self.execute(sql, args) - except Exception as e: - sys.stderr.write(str(e)) - return diff --git a/pb/lib/utils.py b/pb/lib/utils.py deleted file mode 100644 index 5ca5b3e..0000000 --- a/pb/lib/utils.py +++ /dev/null @@ -1,123 +0,0 @@ -import re -from pb.config import * -import time -import urllib -import urllib2 -import sys -import os -from subprocess import Popen,PIPE,call -Request = urllib2.Request -urlencode = urllib.urlencode -urlopen = urllib2.urlopen - -def call_cmd(cmd, error=""): - try: - call(cmd) - except Exception as e: - raise (str(e)) - -def is_color(s): - if s == "": - return "transparent" - if re.match('(rgba?\([0-9]+,[0-9]+,[0-9]+\))|([a-zA-Z]+)|(\#[A-Ha-h0-9]+)', s): - return s.replace(' ', ''); - else: - sys.stderr.write("Not a color: {}\n".format(s)) - raise ValueError - -def dimensions (filepath): - #works in lieu of a mimetype check (it reads the header as well) - ident = (Popen([BIN_IDENTIFY, filepath], stdout=PIPE).communicate()[0]).split(" ") - return ident[2].split("x") - -def is_number(s): - try: - return int(s) - except (ValueError, TypeError): - return False - -def bool_correct(s): - if re.match(r'^false$', s, re.IGNORECASE): - return False - elif re.match(r'^true$', s, re.IGNORECASE): - return True - else: - return s - -class dotdict(dict): - """dot.notation access to dictionary attributes""" - def __getattr__(self, attr): - return self.get(attr) - __setattr__= dict.__setitem__ - __delattr__= dict.__delitem__ - -def get_mimetype(f): - try: - mimetype = Popen( - [BIN_IDENTIFY, f], stdout=PIPE - ).communicate()[0].split(" ")[1].lower() - return mimetype - except Exception as e: - sys.stderr.write("couldn't determine mimetype") - sys.stderr.write(str(e)) - raise; - -def sanitize (str): - return re.sub(r'\W+', '', str) - -def now(): - return int(time.time()) - -def browser_request (url, data=None): - headers = { - 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', - 'Accept': '*/*', - } - try: - req = Request(url, data, headers) - response = urlopen(req) - except IOError, e: - if hasattr(e, 'code'): - sys.stderr.write( '%s - ERROR %s' % (url, e.code) ) - raise; - return None - else: - return response - -def download(url, destination, max_size=MAX_SIZE): - response = browser_request(url, None) - rawimg = response.read() - if len(rawimg) == 0: - sys.stderr.write("got zero-length file") - raise; - if len(rawimg) > max_size: - sys.stderr.write("file too big: max size {} KB / {} is {} KB".format( - str(MAX_SIZE/1024), - destination, - str(len(rawimg)/1024) - )) - raise; - f = open(destination, "w") - f.write(rawimg) - f.close() - -def file_size (filepath): - try: - return os.stat(filepath)[6] - except Exception as e: - sys.stderr.write("Couldn't determine filesize\n") - sys.stderr.write(str(e)+"\n") - raise; - -def gif_frames(filepath): - try: - info = Popen([BIN_IDENTIFY,filepath], stdout=PIPE).communicate()[0] - frames = filter((lambda x: x), map( - (lambda x: x.split(" ")[0]), - (info).split('\n') - )) - return frames - except Exception as e: - sys.stderr.write("IMGRID couldn't get gif frames") - sys.stderr.write(str(e)) - raise; -- cgit v1.2.3-70-g09d2