summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2015-09-23 23:27:27 -0700
committeryo mama <pepper@scannerjammer.com>2015-09-23 23:27:27 -0700
commit5c982ed500b51f5b8d911e5a5adec10ac2d31d2e (patch)
treeefca7a0f0209d25972df00173e4c92a814956a7a /lib
parent2755b029d873f93d91086b3d69a4c57d05c40fb5 (diff)
ok good
Diffstat (limited to 'lib')
-rw-r--r--lib/Db.py (renamed from lib/Db/__init__.py)0
-rw-r--r--lib/Db/sqlalchemy_example_code.py105
-rw-r--r--lib/Param/Bool.py (renamed from lib/Param/Bool/__init__.py)0
-rw-r--r--lib/Param/Color.py (renamed from lib/Param/Color/__init__.py)0
-rw-r--r--lib/Param/Enum.py (renamed from lib/Param/Enum/__init__.py)0
-rw-r--r--lib/Param/Float.py (renamed from lib/Param/Float/__init__.py)0
-rw-r--r--lib/Param/Img_url.py (renamed from lib/Param/Img_url/__init__.py)0
-rw-r--r--lib/Param/Int.py (renamed from lib/Param/Int/__init__.py)0
-rw-r--r--lib/Param/Json.py (renamed from lib/Param/Json/__init__.py)0
-rw-r--r--lib/Param/Raw.py (renamed from lib/Param/Raw/__init__.py)0
-rw-r--r--lib/Param/String.py (renamed from lib/Param/String/__init__.py)0
-rw-r--r--lib/Params.py (renamed from lib/Params/__init__.py)0
-rwxr-xr-xlib/Pb/Break.py (renamed from lib/Pb/Break/__init__.py)0
-rwxr-xr-xlib/Pb/Break/__init__.py~205
-rwxr-xr-xlib/Pb/Generate.py (renamed from lib/Pb/Generate/__init__.py)0
-rwxr-xr-xlib/Pb/Gradient.py (renamed from lib/Pb/Gradient/__init__.py)0
-rwxr-xr-xlib/Pb/Grid.py (renamed from lib/Pb/Grid/__init__.py)0
-rwxr-xr-xlib/Pb/Landscape/landscape163
-rwxr-xr-xlib/Pb/Pattern.py (renamed from lib/Pb/Pattern/__init__.py)0
-rw-r--r--lib/S3Cli.py (renamed from lib/S3Cli/__init__.py)0
-rw-r--r--lib/Server.py (renamed from lib/Server/__init__.py)16
-rw-r--r--lib/Server/InvalidUsage/__init__.py16
22 files changed, 15 insertions, 490 deletions
diff --git a/lib/Db/__init__.py b/lib/Db.py
index c6286ad..c6286ad 100644
--- a/lib/Db/__init__.py
+++ b/lib/Db.py
diff --git a/lib/Db/sqlalchemy_example_code.py b/lib/Db/sqlalchemy_example_code.py
deleted file mode 100644
index fc38b38..0000000
--- a/lib/Db/sqlalchemy_example_code.py
+++ /dev/null
@@ -1,105 +0,0 @@
-# coding: utf-8
-import MySQLdb
-import time, sys
-USER = "asdfus"
-PASSWORD = "gTYgT&M6q"
-DATABASE = "asdfus"
-
-
-from sqlalchemy import Column, Integer, LargeBinary, String, create_engine
-from sqlalchemy.ext.declarative import declarative_base
-from sqlalchemy.orm import sessionmaker
-engine = create_engine('mysql://{}:{}@localhost/{}'.format(USER, PASSWORD, DATABASE))
-Session = sessionmaker(bind=engine)
-session = Session()
-connection = engine.connect()
-#result = connection.execute("select username from users")
-#for row in result:
-# print "username:", row['username']
-#connection.close()
-
-
-Base = declarative_base()
-metadata = Base.metadata
-
-
-class ImCmd(Base):
- __tablename__ = 'im_cmd'
- id = Column(Integer, primary_key=True)
- date = Column(Integer)
- remote_addr = Column(String(16))
- name = Column(String(16))
- url = Column(String(256))
- dir = Column(String(2))
- oldfile = Column(String(256))
- newfile = Column(String(256))
- cmd = Column(LargeBinary)
- dataobj = Column(LargeBinary)
- tag = Column(String(50))
-#def __repr__(self):
-#... return "<User(name='%s', fullname='%s', password='%s')>" % (
-#... self.name, self.fullname, self.password)
-
-
-for instance in session.query(ImCmd).order_by(ImCmd.id):
- print instance.name, instance.date
-
-
-ed_user = User(name='ed', fullname='Ed Jones', password='edspassword')
-session.add(ed_user)
-#session.add_all([
-#... User(name='wendy', fullname='Wendy Williams', password='foobar'),
-#... User(name='mary', fullname='Mary Contrary', password='xxg527'),
-#... User(name='fred', fullname='Fred Flinstone', password='blah')])
-
-#session.commit()
-
-class Db(object):
- def __init__ (self):
- self.conn = None
- self.cursor = None
- self.connect()
-
- def connect (self):
- self.conn = MySQLdb.connect (host = "lalalizard.com",
- 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/lib/Param/Bool/__init__.py b/lib/Param/Bool.py
index 7b779cb..7b779cb 100644
--- a/lib/Param/Bool/__init__.py
+++ b/lib/Param/Bool.py
diff --git a/lib/Param/Color/__init__.py b/lib/Param/Color.py
index 9163338..9163338 100644
--- a/lib/Param/Color/__init__.py
+++ b/lib/Param/Color.py
diff --git a/lib/Param/Enum/__init__.py b/lib/Param/Enum.py
index 3923a64..3923a64 100644
--- a/lib/Param/Enum/__init__.py
+++ b/lib/Param/Enum.py
diff --git a/lib/Param/Float/__init__.py b/lib/Param/Float.py
index 0581815..0581815 100644
--- a/lib/Param/Float/__init__.py
+++ b/lib/Param/Float.py
diff --git a/lib/Param/Img_url/__init__.py b/lib/Param/Img_url.py
index 55bf353..55bf353 100644
--- a/lib/Param/Img_url/__init__.py
+++ b/lib/Param/Img_url.py
diff --git a/lib/Param/Int/__init__.py b/lib/Param/Int.py
index 0a73cc1..0a73cc1 100644
--- a/lib/Param/Int/__init__.py
+++ b/lib/Param/Int.py
diff --git a/lib/Param/Json/__init__.py b/lib/Param/Json.py
index 28a7126..28a7126 100644
--- a/lib/Param/Json/__init__.py
+++ b/lib/Param/Json.py
diff --git a/lib/Param/Raw/__init__.py b/lib/Param/Raw.py
index f8adaab..f8adaab 100644
--- a/lib/Param/Raw/__init__.py
+++ b/lib/Param/Raw.py
diff --git a/lib/Param/String/__init__.py b/lib/Param/String.py
index 8f08e49..8f08e49 100644
--- a/lib/Param/String/__init__.py
+++ b/lib/Param/String.py
diff --git a/lib/Params/__init__.py b/lib/Params.py
index e3ba10b..e3ba10b 100644
--- a/lib/Params/__init__.py
+++ b/lib/Params.py
diff --git a/lib/Pb/Break/__init__.py b/lib/Pb/Break.py
index b7edfb1..b7edfb1 100755
--- a/lib/Pb/Break/__init__.py
+++ b/lib/Pb/Break.py
diff --git a/lib/Pb/Break/__init__.py~ b/lib/Pb/Break/__init__.py~
deleted file mode 100755
index 64d9cc6..0000000
--- a/lib/Pb/Break/__init__.py~
+++ /dev/null
@@ -1,205 +0,0 @@
-#!/usr/bin/python2.7
-import os
-import sys
-import random
-import re
-import urllib
-import inspect
-from config import *
-from Pb import Pb
-from Pb.Params import Params
-
-DEFAULT_FINALFORMAT = "png";
-SUBTLE_BREAK_MARK = 'pron'
-EXTREME_BREAK_MARK = 'sugar'
-
-HEADER_OFFSET = 2000
-
-class Breaker(Pb):
- def __init__(self, url=None, breaktype=None, finalformat=DEFAULT_FINALFORMAT,
- breakmode=None, breakangle=None, username=None, expanded=None, firsttime=None):
- super(Breaker,self).__init__();
-
- self.params.breaktype = self._get_breaktype(breaktype);
- self.params.url = url
-
- _frame = inspect.currentframe();
- _args_vals = inspect.getargvalues(_frame);
- for arg in _args_vals.args:
- if arg == "self":
- continue
- sys.stderr.write(str(arg) + "\n")
- try:
- if arg not in ['breaktype', 'url']:
- sys.stderr.write("Yoooo");
- sys.stderr.write(arg)
- strarg = str(_args_vals.locals.get(arg))
- django self.bool_correct(strarg)
- self.params.__setattr__("dingo", django)
-# self.params.__setattr__(arg, self.bool_correct(_args_vals.locals.get(arg)))
- except Exception as e:
- sys.stderr.write("\n"+str(e)+"\n")
- sys.exit(1);
- self.params = Params(**self.params);
-
- self.tag = "imBreak"
- self.commands = [];
- self._now = self.now()
- self.files_created = []
-
- self.basename, self._first_format = self._get_filename_and_type_from_url();
- self._downloaded_file = self.tempname_create(basename=self.basename, fmt=self._first_format)
-
- try:
- self.download(self.params.url, self._downloaded_file)
- self.files_created.append(self._downloaded_file)
- except Exception as e:
- self.err_warn(str(e))
-
- self._gif_frames = self.gif_frames(self._downloaded_file)
- self._gif_frames = self._gif_frames if len(self._gif_frames) > 1 else False
- self.width, self.height = self.dimensions(self._downloaded_file) # same here
-
- if not self.params.finalformat and self._gif_frames:
- self.params.finalformat = 'gif'
- if self.params.breaktype == 'miff':
- self.params.finalformat = 'jpg'
- self.params.breakmode = 'subtle'
- #final filepath is stored in self.filepath
- self.filename = "{}.{}".format(self.basename, self.params.finalformat)
- self.filepath = os.path.join(self._working_dir, self.filename)
- self._conversion_file = self.tempname_create(basename=self.basename, fmt=self.params.breaktype);
-
- def _call_cmd(self, cmd):
- super(Breaker,self)._call_cmd(cmd, error)
- self.commands.append(" ".join(cmd));
-
- def _get_breaktype(self, key):
- #{{{ conversion table
- breaktypeTranslate = {
- 'CLASSIC':'jpg',
- 'REDUX':'pcds',
- 'BLURRY_BREAK':'viff',
- 'BLURRY_BREAK_2':'mat',
- 'SWIPE':'miff',
- 'RGB_WASH':'psd',
- 'RGB_WASH_2':'psb',
- 'NOISY_BREAK':'palm',
- 'NOISY_BREAK_2':'fig',
- 'BROKEN_VIGNETTE':'pbm',
- 'FAX_MACHINE':'cals',
- 'STRIPES':'exr',
- 'PHOTOCOPY':'art',
- }
- #}}}
- return breaktypeTranslate[key]
-
-#{{{#########rotatefunctions#######################################
- def _rotate(self):
- cmd = [BIN_CONVERT,self._downloaded_file,"-rotate",self.params.breakangle,"+repage",self._downloaded_file]
- self._call_cmd(cmd)
-
- def _rotate_back(self):
- angle = str(360-int(self.params.breakangle))
- cmd = [BIN_CONVERT,self.filepath,"-rotate",angle,"+repage",self.filepath]
- self._call_cmd(cmd)
- if not self.params.expanded:
- cmd = [BIN_CONVERT,self.filepath,"-gravity","Center","-crop","{}x{}+0+0".format(
- self.width, self.height),"+repage",self.filepath]
- self._call_cmd(cmd)
-#}}}
- def _subtle_break(self):
- #assume the header is no longer than HEADER_OFFSET bytes
- breakpoint = random.randint(HEADER_OFFSET, len(self.file_data))
- newfile = self.file_data[0:breakpoint] \
- + SUBTLE_BREAK_MARK \
- + self.file_data[breakpoint:]
- self.file_data = newfile[0:len(self.file_data)]
-
- def _extreme_break(self):
- increment = len(self.file_data)/10;
- i = 0
- newfile = "";
- for b in self.file_data:
- if i > HEADER_OFFSET and not (i % increment):
- b += EXTREME_BREAK_MARK
- newfile += b
- i += 1
- self.file_data = newfile[0:len(self.file_data)]
-
- def _choose_frame(self):
- frame = random.choice(self._gif_frames)
- self._call_cmd([BIN_CONVERT, frame, self._downloaded_file])
-
- def _enforce_jpg(self):
- if self.params.breaktype in [ "exr", "bmp", "miff" ] and not re.match(r'jpe?g$', self._first_format, re.IGNORECASE):
- jpg_file = self.tempname_create(basename=self.basename, fmt="jpg")
- self._call_cmd([BIN_CONVERT,self._downloaded_file,jpg_file])
- self._call_cmd(["rm",self._downloaded_file])
- self._downloaded_file = jpg_file
-
- def _first_conversion(self):
- if self._first_format == self.params.breaktype:
- self._downloaded_file = self._conversion_file
- return
- self._call_cmd([BIN_CONVERT, self._downloaded_file, self._conversion_file])
- self.files_created.append(self._conversion_file)
-
- def _prepare_filedata(self):
- if self._gif_frames:
- self._choose_frame()
- if self.params.breakangle:
- self._rotate()
- self._enforce_jpg();
- self._first_conversion();
- self.file_data = self._file_read(self._conversion_file)
- if not self.file_data:
- self.err_warn("Unable to get file data");
-
- def _add_false_data(self, breakmode):
- if breakmode == "subtle":
- self._subtle_break()
- elif breakmode == "extreme":
- self._extreme_break()
- f = open(self._conversion_file, 'w')
- f.write(self.file_data)
- f.close();
-
- def _final_conversion(self):
- self._call_cmd( [BIN_CONVERT, self._conversion_file, self.filepath])
- def psd_psbfilepath(num):
- return os.path.join(self._working_dir, "{}-{}.{}".format(self.basename, num, self.params.finalformat))
- if self.params.breaktype == 'psd':
- self._call_cmd(['mv', psd_psbfilepath(1), self.filepath])
- self.files_created.append(psd_psbfilepath(0))
- if self.params.breaktype == 'psb':
- self._call_cmd(['mv', psd_psbfilepath(0), self.filepath])
- self.files_created.append(psd_psbfilepath(1))
- if self.params.breakangle:
- self._rotate_back()
-
- def _cleanup(self):
- cmd = ["rm"]+self.files_created
- self._call_cmd(cmd)
-
- def create(self, breakmode=""):
- if not breakmode: breakmode = self.params.breakmode
- self._prepare_filedata();
- self._add_false_data(breakmode);
- self._final_conversion()
- self._cleanup()
-
- @classmethod
- def test(cls):
- TEST_PARAMS = {
- "url" : "http://i.asdf.us/im/27/1424816234661dumpfmpfifferkinggr_1424816412_pfifferking.gif" ,
- "breaktype" : "RGB_WASH",
- "finalformat" : "png",
- "breakmode" : "extreme",
- "breakangle" : "10",
- "username" : "donkey",
- "expanded" : "false"
- }
- b = cls(**TEST_PARAMS)
- b.create();
- print b.filepath
diff --git a/lib/Pb/Generate/__init__.py b/lib/Pb/Generate.py
index e631002..e631002 100755
--- a/lib/Pb/Generate/__init__.py
+++ b/lib/Pb/Generate.py
diff --git a/lib/Pb/Gradient/__init__.py b/lib/Pb/Gradient.py
index 340ce62..340ce62 100755
--- a/lib/Pb/Gradient/__init__.py
+++ b/lib/Pb/Gradient.py
diff --git a/lib/Pb/Grid/__init__.py b/lib/Pb/Grid.py
index b0d731f..b0d731f 100755
--- a/lib/Pb/Grid/__init__.py
+++ b/lib/Pb/Grid.py
diff --git a/lib/Pb/Landscape/landscape b/lib/Pb/Landscape/landscape
deleted file mode 100755
index 10e8ede..0000000
--- a/lib/Pb/Landscape/landscape
+++ /dev/null
@@ -1,163 +0,0 @@
-#!/usr/bin/python2.7
-import cgi
-import sys
-import os
-import re
-import time
-import string
-import urllib
-from subprocess import Popen, PIPE
-import sha
-import simplejson as json
-
-import mimetypes
-import s3
-
-import db
-DB = db.db ()
-
-import base64
-import urlparse
-
-
-AWS_ACCESS_KEY_ID = 'AKIAIR53VPBXKJMXZIBA'
-AWS_SECRET_ACCESS_KEY = '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'
-BUCKET_NAME = 'i.asdf.us'
-BASE_PATH = "/var/www/asdf.us/httpdocs/imlandscape"
-BASE_URL = "http://i.asdf.us/"
-PARAM_LIST = "heightmap texture name imgdata filename"
-BIN_IDENTIFY = "/usr/bin/identify"
-
-print "Content-type: text/plain"
-print ""
-def insert_cmd (dir, newfile, name, texture, dataobj):
- if texture == "":
- texture = "NULL"
- try:
- sql = "INSERT INTO im_cmd (date,remote_addr,name,url,dir,oldfile,newfile,cmd, dataobj, tag) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s, %s)"
-
- args = (now(), "NULL", name, texture, dir, "NULL", newfile, "NULL", dataobj, "imlandscape")
- DB.execute(sql, args)
- except ():
- return
-
-def hash_dir (s):
- return sha.new(s).hexdigest()[:2]
-
-def bin_identify (filename):
- ident = Popen([BIN_IDENTIFY, filename], stdout=PIPE).communicate()[0]
- partz = ident.split(" ")
- width,height = partz[2].split("x")
- return width, height
-
-def get_params (paramlist):
- paramkeys = paramlist.split()
- form = cgi.FieldStorage()
- params = {}
- for key in paramkeys:
- if key in form:
- if key == 'heightmap':
- params[key] = form[key].value
- elif key == 'imgdata':
- params[key] = form[key].value
- elif key == 'texture':
- params[key] = form[key].value
- else:
- params[key] = sanitize(form[key].value)
- else:
- params[key] = None
- return params
-
-def error (e):
- print "#@imlandscape"
- print "ERROR\t"+e
- sys.exit()
-
-def now ():
- return int(time.mktime(time.localtime()))
-
-def sanitize (str):
- return re.sub(r'\W+', '', str)
-
-def filename_from_url (url, name=""):
- if "?" in url:
- url = url.split("?")[0]
- if "/" in url:
- url = urllib.unquote(url).replace(" ","")
- filename = url.split("/")[-1]
- filetype = "png"
- filename = sanitize(filename[:-4])
- else:
- filename = ""
- if name != "":
- name = name+"_"
- return "{}_{}{}_{}.{}".format("imlandscape", name, filename,now(), "png")
-
-def saveImgData(url, filename):
- try:
- up = urlparse.urlparse(url)
- head, data = up.path.split(',', 1)
- bits = head.split(';')
- mime_type = bits[0] if bits[0] else 'text/plain'
- charset, b64 = 'ASCII', False
- for bit in bits[1]:
- if bit.startswith('charset='):
- charset = bit[8:]
- elif bit == 'base64':
- b64 = True
-
- # Do something smart with charset and b64 instead of assuming
- plaindata = base64.b64decode(data)
-
- with open(filename, 'wb') as f:
- f.write(plaindata)
- except Exception as e:
- error(str(e));
-
-def file_size (file):
- return os.stat(file)[6]
-
-def moveToS3(filename,objectname):
- conn = s3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
- sys.stderr.write( "Uploading %s" % filename)
- filedata = open(filename, 'rb').read()
- content_type = mimetypes.guess_type(filename)[0]
- if not content_type:
- content_type = 'text/plain'
- conn.put(BUCKET_NAME, objectname, s3.S3Object(filedata),
- {'x-amz-acl': 'public-read', 'Content-Type': content_type, 'x-amz-storage-class': 'REDUCED_REDUNDANCY'})
-
-param = get_params(PARAM_LIST)
-if param['imgdata'] is None:
- error("no imgdata")
-url = param['imgdata']
-if param['texture'] is None:
- param['texture'] = "";
-if param['heightmap'] is None:
- param['heightmap'] = "";
-if param['name'] is None:
- param['name'] = "";
-
-dataobj = json.dumps({
- 'texture' : param['texture'],
- 'heightmap' : param['heightmap'],
- 'name' : param['name']
-})
-
-dir = hash_dir(param['imgdata']);
-
-filename = filename_from_url(param['texture'], param['name']);
-
-tag = "imlandscape"
-objectname = "im/"+dir+"/"+filename
-
-saveImgData(param['imgdata'], filename);
-
-print "#@imlandscape"
-#print ", ".join([k+"="+str(v) for k,v in param.iteritems()])
-print file_size (filename)
-print bin_identify (filename)
-print BASE_URL+objectname
-insert_cmd(dir, filename, param['name'], param['texture'], dataobj);
-moveToS3(filename, objectname);
-os.remove(filename);
diff --git a/lib/Pb/Pattern/__init__.py b/lib/Pb/Pattern.py
index 6ae6eb7..6ae6eb7 100755
--- a/lib/Pb/Pattern/__init__.py
+++ b/lib/Pb/Pattern.py
diff --git a/lib/S3Cli/__init__.py b/lib/S3Cli.py
index 70ba24c..70ba24c 100644
--- a/lib/S3Cli/__init__.py
+++ b/lib/S3Cli.py
diff --git a/lib/Server/__init__.py b/lib/Server.py
index 91bc880..00bae9f 100644
--- a/lib/Server/__init__.py
+++ b/lib/Server.py
@@ -3,7 +3,21 @@ from flask import abort, redirect, url_for, request, jsonify
import sys, os
sys.path.append("./lib")
from Pb import *
-from Server.InvalidUsage import InvalidUsage
+
+class InvalidUsage(Exception):
+ status_code = 400
+
+ def __init__(self, message, status_code=None, payload=None):
+ Exception.__init__(self)
+ self.message = message
+ if status_code is not None:
+ self.status_code = status_code
+ self.payload = payload
+
+ def to_dict(self):
+ rv = dict(self.payload or ())
+ rv['message'] = self.message
+ return rv
class Server(object):
def __init__(self):
diff --git a/lib/Server/InvalidUsage/__init__.py b/lib/Server/InvalidUsage/__init__.py
deleted file mode 100644
index e5616d4..0000000
--- a/lib/Server/InvalidUsage/__init__.py
+++ /dev/null
@@ -1,16 +0,0 @@
-from flask import jsonify
-
-class InvalidUsage(Exception):
- status_code = 400
-
- def __init__(self, message, status_code=None, payload=None):
- Exception.__init__(self)
- self.message = message
- if status_code is not None:
- self.status_code = status_code
- self.payload = payload
-
- def to_dict(self):
- rv = dict(self.payload or ())
- rv['message'] = self.message
- return rv