pickle and cStringIO
You just cannot (I guess because of lacking proper pickling C API support) pickle cStringIO.StringIO instances. Use StringIO.StringIO instead.
from pyparsing import * nginx_conf_expr = OneOrMore(Suppress(SkipTo('server' + White())) + originalTextFor(Word('server ') + nestedExpr('{', '}'))).parseWithTabs() server_name_expr = (Suppress(SkipTo('server_name' + White()) + Word('server_name') + White()) + CharsNotIn(' ;')).parseWithTabs() nginx_conf = open('nginx.conf').read() new_nginx_conf = str(nginx_conf) for server in nginx_conf_expr.parseString(nginx_conf): try: name = server_name_expr.parseString(server)[0] f = open('nginxsite_%s.conf' % name, 'w+') f.write(str(server)) f.close() # update nginx config new_nginx_conf = new_nginx_conf.replace(str(server), '') except: print 'Entry failed:\n', server open('new_nginx.conf', 'w').write(new_nginx_conf)
This script basically extracts all server {} entries and writes them to separate files named "nginxsite_sitename.conf" (all in current working directory). It also produces new_nginx.conf that does not include extracted entries. All you need to do is to add include directive to new_nginx.conf like this:
# path is relative to this global nginx.conf include sites/*.conf;
temp $ ls migrate_nginx_config.py nginx.conf temp $ python migrate_nginx_config.py migrate_nginx_config.py nginx.conf new_nginx.conf nginxsite_www.fubar.si.conf nginxsite www.tvnext.si.conf ...
python setup.py egg_info -RDb "" bdist_wininst --install-script postinstall.py
... setup(name='package', ... scripts=['postinstall.py'], ... )
#! python # -*- coding: utf-8 -*- import os import sys import shutil import my_package DESKTOP_FOLDER = get_special_folder_path("CSIDL_DESKTOPDIRECTORY") NAME = 'program.lnk' if sys.argv[1] == '-install': create_shortcut( os.path.join(sys.prefix, 'pythonw.exe'), # program 'Description of the shortcut', # description NAME, # filename mypackage.__file__, # parameters '', # workdir os.path.join(os.path.dirname(my_package.__file__), 'favicon.ico'), # iconpath ) # move shortcut from current directory to DESKTOP_FOLDER shutil.move(os.path.join(os.getcwd(), NAME), os.path.join(DESKTOP_FOLDER, NAME)) # tell windows installer that we created another # file which should be deleted on uninstallation file_created(os.path.join(DESKTOP_FOLDER, NAME)) if sys.argv[1] == '-remove': pass # This will be run on uninstallation. Nothing to do.
from subprocess import Popen, PIPE p = Popen(['command doing lots of I/O and takes long to complete'], shell=True, stdout=PIPE) while p.poll() == None: print p.stdout.read()
import os import fcntl import gobject from subprocess import Popen, PIPE p = Popen(['command doing lots of I/O and takes long to complete'], shell=True, stdout=PIPE) fd = p.stdout.fileno() #file_flags = fcntl.fcntl(fd, fcntl.F_GETFL) #fcntl.fcntl(fd, fcntl.F_SETFL, file_flags | os.O_NDELAY) def test_io_watch(f, cond): out = f.read() if out == '': return False print out return True gobject.io_add_watch(p.stdout, gobject.IO_IN | gobject.IO_HUP, test_io_watch) gobject.MainLoop().run()
The O_NDELAY flag causes read() or write() to return zero instead of blocking.This changes my guessing of how I/O blocking is handled in Python; it behaves by C implementation based on flags used by filedescriptors. Thus, Python does not (literally) wait for EOF (as it is stated in documentation) but it is just behavior of flags passed when opening a file.
class ApplyForm(formencode.Schema): allow_extra_fields = True filter_extra_fields = True billIsNaturalPerson = validators.StringBool(not_empty=True) billEmail = validators.Email(not_empty=True) billAddress = validators.UnicodeString(not_empty=True) billPost = validators.UnicodeString(not_empty=True) billPhone = InternationalPhoneNumber(default_cc=386, not_empty=True) billFirm = validators.UnicodeString(if_missing=None) billTaxNumber = SlovenianTaxNumber(if_missing=None) billIDNumber = validators.Int(if_missing=None) billContactPerson = validators.UnicodeString(if_missing=None) billIsDDV = validators.Bool(not_empty=True, if_missing=None) billIsProformaInvoice = validators.Bool(not_empty=True) billIsSIR = validators.Bool(not_empty=True) billIsDinner = validators.Bool(not_empty=True) relParticipants = formencode.ForEach(ParticipantForm) relDaysAttending = validators.DictConverter({ '1': [model.meta.Session()\ .query(model.PortorozDay)\ .filter(model.PortorozDay.plPDayDate == date(2009, 11, 18)).one()], # wed '2': [model.meta.Session()\ .query(model.PortorozDay)\ .filter(model.PortorozDay.plPDayDate == date(2009, 11, 19)).one()], # thu '3': model.meta.Session()\ .query(model.PortorozDay).all() , # both }, not_empty=True, hideDict=True) pre_validators = [NestedVariables()] chained_validators = [ validators.RequireIfPresent('billFirm', missing='billIsNaturalPerson'), validators.RequireIfPresent('billTaxNumber', missing='billIsNaturalPerson'), validators.RequireIfPresent('billIDNumber', missing='billIsNaturalPerson'), validators.RequireIfPresent('billContactPerson', missing='billIsNaturalPerson'), ] class ParticipantForm(formencode.Schema): allow_extra_fields = True filter_extra_fields = False parName = validators.UnicodeString(if_missing=None) parSurname = validators.UnicodeString(if_missing=None) chained_validators = [ validators.RequireIfPresent('parName', present='parSurname'), validators.RequireIfPresent('parSurname', present='parName'), MapToModel(model.PortorozParticipant), ]
<div> <input type="text" name="relParticipants-1.parName" value="John" /> <input type="text" name="relParticipants-1.parSurname" value="Smih" /> </div> <div> <input type="text" name="relParticipants-2.parName" value="..." /> ...
{'relParticipants': [ {'parName': 'John', 'parSurname': 'Smith'}, ... ]}
class MapToModel(validators.FormValidator): __unpackargs__ = ('model',) model = None def _to_python(self, value_dict, state): return self.model(**value_dict)
18:24:07,349 DEBUG [project.controllers.portoroz] Form input:
{'billAddress': u'Foo street 12',
'billContactPerson': u'John Smith',
'billEmail': u'john.smith@gmail.com',
'billFirm': u'Firm',
'billIDNumber': 1337,
'billIsDDV': None,
'billIsDinner': False,
'billIsNaturalPerson': False,
'billIsProformaInvoice': True,
'billIsSIR': False,
'billPhone': '+386-11-444489',
'billPost': u'1000 Ljubljana',
'billTaxNumber': 40952649,
'relDaysAttending': [<PortorozDay 2009-11-18>, <PortorozDay 2009-11-19>],
'relParticipants': [<PortorozParticipant(None) parName=u'foo', parSurname=u'bar'>,
<PortorozParticipant(None) parName=u'hello', parSurname=u'kitty'>,
]
}
try: c.form_result = ApplyForm().to_python(request.POST) except formencode.validators.Invalid, e: return formencode.htmlfill.render( self.apply_action(), defaults=request.POST, errors=e.unpack_errors(formencode.variabledecode.variable_encode))
Configuring reverse proxy headers was on bottom of my TODO list because I never felt it was important for my applications. Today I tried to fuddle with nginx to pass those headers but it gave me a headache.
For sake of reference and because I'm sure many others tripped on this one, here is what I figured out from mailing lists and source code (please inform me of a better, straight forward way if there is one):
Add to your deployment.ini file:[app:main] # ... filter-with = proxy-headers [filter:proxy-headers] use = egg:PasteDeploy#prefix
location / { proxy_pass http://localhost:5000; proxy_set_header X_FORWARDED_SERVER $server_name; proxy_set_header X_FORWARDED_FOR $proxy_add_x_forwarded_for; proxy_set_header X_FORWARDED_HOST $proxy_host; }
>>> print ' '.join(['The', 'fox', 'jumped', 'over', 'the', 'dog.']) The fox jumped over the dog.
>>> print ', '.join(['apples', 'oranges', '', 'cocos']) apples, oranges, , cocos
>>> print ', '.join(filter(None, ['apples', 'oranges', '', 'cocos'])) apples, oranges, cocos
class StringBuffer: def __init__(self, sep=''): self.sep = sep self.output = list() def write(self, content): self.output.extend(content) def getvalue(self): return self.sep.join(filter(None, self.output)) >>> sb = StringBuffer(', ') >>> sb.write(['apples']) >>> sb.write(['', None]) >>> sb.write(['oranges', 'cocos']) >>> print sb.getvalue() apples, oranges, cocos