Search This Blog

Loading...

Tuesday, February 12, 2013

Shared libraries in JBoss 7 using eclipse

Posting this because it took a bunch of trail and error to figure out.

If you need to add non-maven external libraries to a an EAR project created using the JBoss tools you probably want to put them in ear-project\src\main\application\lib

Yes you can probably use maven and have a local repository as well, this has the advantage or reducing the number of moving parts and working more easily with a CI setup that assumes ant

Friday, January 6, 2012

Eclipse related fail

I recently wasted a bunch of time on this error so am putting it here to try and prevent it happening to anyone else.

When Launching Tomcat from eclipse I kept getting:
java.lang.NoClassDefFoundError: org/apache/log4j/Logger


Turns out Eclipse decided to only half-deploy the servlet. The easy solution was to go to workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps and delete the folder.

After than removing the servlet, restarting, then adding it again fixed the issue.

Monday, November 29, 2010

Screenscrape a website using cookies

Snippet of code to script a site that uses login cookies. Contains a couple of common helpers




import cookielib
import urllib2
from lxml.html import parse
from urllib import urlencode
import cookielib
import urllib2
import urlparse

txheaders = {'User-agent': 'Pony automation bot'}
Request = urllib2.Request

def login_required(func):
"""Gives a sensible error message if func is called without logging in"""
def wrapped_func(*args, **kwargs):
if args[0].logged_in or args[0].__logged_in:
return func(*args, **kwargs)
else:
return (False, "Not logged in")
return wrapped_func


class Site(object):
def __init__(self, base):
self.base = base
self.__logged_in = False
self.cj = cookielib.LWPCookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)

def __request_body(self, theurl, txdata):
req = Request(theurl, txdata, txheaders)
return parse(self.opener.open(req)).getroot().body

Wednesday, October 13, 2010

Generate a list of files under a directory with full paths

Handy more often than one might think.



from itertools import chain
import os

list(chain(*[map(lambda y: os.path.join(x[0], y), x[2]) for x in os.walk("tmp")])))

Tuesday, June 1, 2010

Boilerplate code for a python-tk GUI

This is the most common boilerplate for python-tk guis that I wind up writing. Posted here for ease of copy-paste because typing this out again was irritating me




from Tkinter import *
import threading, Queue, sys, os
from optparse import OptionParser
from ConfigParser import SafeConfigParser

__version__ = "0.0.1"

class GUI(object):
def __init__(self, root, queue, shutdown, config):
self.root = root
self.config = config
# Widget creation goes here

def process_events(self):
pass

class ThreadedClient(object):
def __init__(self, root, conf):
self.root = root
self.config = SafeConfigParser()
self.config.read(conf)
self.config_file = os.path.abspath(conf)
self.queue = Queue.Queue()
self.gui = GUI(self.root, self.queue, self.shutdown, self.config)
self.running = True
self.work_thread = threading.Thread(target=self.secure_thread)
self.work_thread.setDaemon(True)
self.work_thread.start()
self.periodic_call()

def periodic_call(self):
self.root.after(200, self.periodic_call)
self.gui.process_events()
if not self.running:
sys.exit(0)

def secure_thread(self):
while self.running:
try:
msg = self.queue.get(0)
except Queue.Empty:
pass
else:
# App work goes here to kepe the gui responsive
pass
sys.exit(0)

def shutdown(self):
self.running = False

if __name__ == "__main__":
parser = OptionParser(usage="%prog [-c]", version=__version__)
parser.add_option("-c", "--config", metavar="FILE",
help="Read config from FILE", default="")
(options, args) = parser.parse_args()
root = Tk()
if args:
app = ThreadedClient(root, options.config, args[0])
else:
app = ThreadedClient(root, options.config)
root.mainloop()

Thursday, November 12, 2009

Lotus Notes database from python

Inspired by this.




NOTES_SERVER = "Your/Notes/Server"
NOTES_PASSWORD = "Your Password"
NOTES_DB = "path\\to\\db\\on\\server.nsf"

# To find these out open lotus notes, right click on the database and go to database->properties
# NOTES_SERVER is the Server field exactly as it appears there
# NOTES_DB is the Filename exactly as it appears there

from win32com.client import Dispatch
session = Dispatch('Lotus.NotesSession')
session.Initialize(NOTES_PASSWORD)

def iterateDocuments(docs):
doc = docs.getFirstDocument()
while doc:
yield doc
doc = docs.getNextDocument(doc)

def get(doc, attr):
return doc.getItemValue(attr)

db = session.getDatabase(NOTES_SERVER, NOTES_DB)
if False == db.isOpen:
print "Failed to open db"
else:
query = db.getView("All Documents")
for d in iterateDocuments(query):
do_stuff_with(d)