Browse Source

Cleanup, whitespace, pyflakes, etc

happy-birthday-weedy
dequis 5 years ago
parent
commit
59fa65b431
8 changed files with 54 additions and 63 deletions
  1. +8
    -13
      board.py
  2. +1
    -1
      interboard.py
  3. +7
    -7
      misc.py
  4. +3
    -1
      model.py
  5. +3
    -4
      staff.py
  6. +10
    -10
      staff_interface.py
  7. +6
    -6
      template.py
  8. +16
    -21
      util.py

+ 8
- 13
board.py View File

@@ -8,7 +8,6 @@ from subprocess import Popen, PIPE

import misc
import str_format
import oekaki
import util
import model
import staff
@@ -51,7 +50,7 @@ class Board(object):
self.options.update(module.config)
else:
self.options = module.config
self.table = model.board(self.options['SQL_TABLE'])

# TODO likely will still need customization
@@ -91,7 +90,7 @@ class Board(object):
hash = '#%s' % hash
else:
hash = ''
if file:
if abbr:
file += '_abbr'
@@ -172,7 +171,7 @@ class Board(object):

def build_cache(self):
threads = self._get_all_threads()
per_page = self.options['IMAGES_PER_PAGE']
total = get_page_count(threads, per_page)

@@ -236,7 +235,7 @@ class Board(object):
thread['omitimages'] += 1

thread['posts'] = [parent] + replies
for post in thread['posts']:
abbreviation = abbreviate_html(post.comment,
self.options['MAX_LINES_SHOWN'],
@@ -247,7 +246,7 @@ class Board(object):
post.comment = abbreviation

threads.append(thread)
return threads

def get_board_page_data(self, page, total, admin_page=''):
@@ -417,7 +416,7 @@ class Board(object):
line)

res_out.write(line)
if os.path.exists(full_thread_page):
os.unlink(full_thread_page)
if os.path.exists(abbrev_thread_page):
@@ -427,7 +426,7 @@ class Board(object):
session = model.Session()
sql = select([self.table.c.num], self.table.c.parent == 0)
query = session.execute(sql)
for row in query:
self.build_thread_cache(row[0])

@@ -517,10 +516,7 @@ class Board(object):
# TODO: this process_file is just a thin wrapper around awful code
wakapost.process_file(self, editing is not None)

# TODO: here lies the ashes of oekaki

# choose whether we need an SQL UPDATE (editing) or INSERT (posting)
db_update_function = None
if editing:
db_update = self.table.update().where(
self.table.c.num == editing.num)
@@ -811,7 +807,6 @@ class Board(object):

def delete_file(self, relative_file_path, relative_thumb_path,
archiving=False):
# pch = oekaki.find_pch(row.image)
full_file_path = os.path.join(self.path, relative_file_path)
full_thumb_path = os.path.join(self.path, relative_thumb_path)
archive_base = os.path.join(self.path,
@@ -1267,7 +1262,7 @@ class Board(object):
# cut off any directory in the original filename
newfilename = self.make_path(filestorage.filename.split("/")[-1],
dirc='IMG_DIR', ext=None)

# verify no name clash
if not os.path.exists(newfilename):


+ 1
- 1
interboard.py View File

@@ -321,7 +321,7 @@ def ban_check(numip, name, subject, comment):
if ip_row:
raise WakaError('Address %s banned. Reason: %s' % \
(misc.dec_to_dot(numip), ip_row.comment))
# To determine possible string bans, first normalize input to lowercase.
comment = comment.lower()
subject = subject.lower()


+ 7
- 7
misc.py View File

@@ -125,8 +125,8 @@ def spam_engine(trap_fields, spam_files):
spam_screen()

spam_checker = compile_spam_checker(spam_files)
fields = request.values.keys()
fields = request.values.keys()
fulltext = '\n'.join([str_format.decode_string(request.values[x])
for x in fields])

@@ -180,7 +180,7 @@ def make_date(timestamp, style='futaba'):
time_str = str(timestamp)
elif style.lower() == '2ch-sep93': # Damn AOLers! Get offa mah lawn!
# September 1, 1993 as a timestamp.
SEP_1_1993 = 746884800L
SEP_1_1993 = 746884800L
seconds_past = long(timestamp) - SEP_1_1993
days_past = seconds_past / 86400L
time_str = '1993-09-%u' % (days_past + 1)
@@ -190,7 +190,7 @@ def make_date(timestamp, style='futaba'):
time_str = time.strftime(style, timestamp)

return time_str
def make_cookies(**kwargs):
expires = kwargs.pop('expires', time.time() + 14 * 24 * 3600)
path = kwargs.pop('path', '/')
@@ -277,7 +277,7 @@ def analyze_jpeg(file):
# MS GDI+ JPEG exploit uses short chunks
raise util.WakaError("Possible virus in image")

if mark >= 0xc0 and mark <= 0xc2: # SOF0..SOF2 - what the hell are the rest?
if mark >= 0xc0 and mark <= 0xc2: # SOF0..SOF2 - what the hell are the rest?
bits, height, width = struct.unpack(">BHH", file.read(5))
return (width, height)

@@ -361,11 +361,11 @@ def kwargs_from_params(request, params_arg=None, **params):
'''Associate function to convert CGI request data with dictionary
of parameter keys to a dictionary of keyword arguments for passing
to an application function.
Dictonary format: {'cookies': ['cookie_keys'],
'form': ['html_form_input_names'],
'file': ['file_keys']}
Not all keys are necessary. Invalid keys are ignored.'''

if params_arg:


+ 3
- 1
model.py View File

@@ -2,9 +2,11 @@ import config, config_defaults
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, Text, String, MetaData
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.exc import OperationalError
from sqlalchemy.sql import func

# needed by other modules
from sqlalchemy.exc import OperationalError

pool_opts = {}

if config.SQL_POOLING:


+ 3
- 4
staff.py View File

@@ -46,9 +46,9 @@ class LoginData(object):
class StaffMember(object):
'''A staff object for acquiring and updating personnel account
information. Use the class factory method to initialize:
>>> StaffMember.get('SirDerpDeeDoo')
To create new staff accounts, use the add_staff() function instead.'''

def __init__(self, username):
@@ -81,7 +81,6 @@ class StaffMember(object):

@password.setter
def password(self, new):
table = self._table
if len(new) < 8:
raise WakaError('Passwords should be at least eight characters!')

@@ -232,7 +231,7 @@ def edit_staff(username, clear_pass=None, new_class=None, reign=None,
disable=None):

staff_obj = StaffMember.get(username)
if clear_pass:
staff_obj.password = clear_pass



+ 10
- 10
staff_interface.py View File

@@ -227,7 +227,7 @@ class StaffInterface(Template):

table = model.activity
account_table = model.account
dual_table_select = [account_table.c.username,
account_table.c.account,
account_table.c.disabled,
@@ -481,7 +481,7 @@ class StaffInterface(Template):
for board in boards:
if board in edited_user.reign:
board['underpower'] = True
Template.__init__(self, 'staff_edit_template',
user_to_edit=username,
boards=boards)
@@ -587,17 +587,17 @@ class StaffInterface(Template):
item['standalone'] = 1

for post in thread:
image_dir \
= os.path.join(board.path, board.options['IMG_DIR'])
#image_dir \
# = os.path.join(board.path, board.options['IMG_DIR'])

thumb_dir \
= os.path.join(board.path, board.options['THUMB_DIR'])
#thumb_dir \
# = os.path.join(board.path, board.options['THUMB_DIR'])

base_thumb = os.path.basename(post['thumbnail'] or '')
base_image = os.path.basename(post['image'] or '')

base_filename = (post['image'] or '')\
.replace(image_dir, '').lstrip('/')
#base_filename = (post['image'] or '')\
# .replace(image_dir, '').lstrip('/')

backup_dir = os.path.join(board.url,
board.options['ARCHIVE_DIR'],
@@ -611,7 +611,7 @@ class StaffInterface(Template):
post['thumbnail'] or ''):
post['thumbnail'] \
= os.path.join(backup_dir, base_thumb)
item['omit'] = postcount - max_res if postcount > max_res\
else 0

@@ -762,7 +762,7 @@ class StaffInterface(Template):
= config.PASSPROMPT_EXPIRE_TO_FAILURE - now \
+ row['timestamp']
entries.append(row)
Template.__init__(self, 'script_security_panel', entries=entries)

@interface_for(DELETE_ALL_CONFIRM)


+ 6
- 6
template.py View File

@@ -36,10 +36,10 @@ class Template(object):
loader=jinja2.FileSystemLoader(TEMPLATES_DIR),
bytecode_cache=jinja2.FileSystemBytecodeCache(CACHE_DIR)
)
for filter in _filters:
self.env.filters[filter] = getattr(self, filter)
for function in _functions:
self.env.globals[function] = getattr(self, function)

@@ -79,7 +79,7 @@ class Template(object):
rc.write(contents)

os.chmod(filename, 0644)
def update_parameters(self, **kwargs):
self.vars.update(kwargs)

@@ -119,7 +119,7 @@ class Template(object):
@function
def get_secure_script_name(self):
return misc.get_secure_script_name()
@filter
def get_reply_link(self, reply, parent, abbreviated=False,
force_http=False):
@@ -177,9 +177,9 @@ class Template(object):
if pictype not in filetypes:
ret_list.append(pictype.upper())
ret_list.extend(filetypes)
return ', '.join(ret_list)
def get_stylesheets(self, board=None):
files = glob.glob(os.path.abspath\
(os.path.join(self.environ['DOCUMENT_ROOT'],


+ 16
- 21
util.py View File

@@ -37,7 +37,7 @@ class SpamError(WakaError):
def wrap_static(application, *app_paths, **kwds):
'''Application used in the development server to serve static files
(i.e. everything except the CGI filename). DO NOT USE IN PRODUCTION'''
@functools.wraps(application)
def wrapper(environ, start_response):
filename = environ['PATH_INFO'].strip('/')
@@ -63,7 +63,7 @@ def wrap_static(application, *app_paths, **kwds):
return handler(environ, start_response)
start_response('404 Not found', [('Content-Type', 'text/plain')])
return ['404 Not found: %s' % filename]
return wrapper

def module_default(modulename, defaults):
@@ -114,7 +114,7 @@ def headers(f):
headerlist.append(tuple(cookie.output().split(": ", 1)))

start_response(status, headerlist)
appiter = f(environ, new_start_response)
new_start_response(environ['waka.status'], environ['waka.headers'])
return appiter
@@ -155,16 +155,16 @@ def make_http_forward(location, alternate_method=False):
# http://www.evanfosmark.com/2009/01/
# cross-platform-file-locking-support-in-python/
# I can't really improve on this to my knowledge.
class FileLockException(Exception):
pass

class FileLock(object):
""" A file locking mechanism that has context-manager support so
""" A file locking mechanism that has context-manager support so
you can use it in a with statement. This should be relatively cross
compatible as it doesn't rely on msvcrt or fcntl for the locking.
"""
def __init__(self, file_name, timeout=10, delay=.05):
""" Prepare the file locker. Specify the file to lock and optionally
the maximum timeout and the delay between each attempt to lock.
@@ -174,12 +174,11 @@ class FileLock(object):
self.file_name = file_name
self.timeout = timeout
self.delay = delay

def acquire(self):
""" Acquire the lock, if possible. If the lock is in use, it check again
every `wait` seconds. It does this until it either gets the lock or
exceeds `timeout` number of seconds, in which case it throws
exceeds `timeout` number of seconds, in which case it throws
an exception.
"""
start_time = time.time()
@@ -189,41 +188,37 @@ class FileLock(object):
break;
except OSError as e:
if e.errno != errno.EEXIST:
raise
raise
if (time.time() - start_time) >= self.timeout:
raise FileLockException("Timeout occured.")
time.sleep(self.delay)
self.is_locked = True

def release(self):
""" Get rid of the lock by deleting the lockfile.
When working in a `with` statement, this gets automatically
""" Get rid of the lock by deleting the lockfile.
When working in a `with` statement, this gets automatically
called at the end.
"""
if self.is_locked:
os.close(self.fd)
os.unlink(self.lockfile)
self.is_locked = False

def __enter__(self):
""" Activated when used in the with statement.
""" Activated when used in the with statement.
Should automatically acquire a lock to be used in the with block.
"""
if not self.is_locked:
self.acquire()
return self

def __exit__(self, type, value, traceback):
""" Activated at the end of the with statement.
It automatically releases the lock if it isn't locked.
"""
if self.is_locked:
self.release()

def __del__(self):
""" Make sure that the FileLock instance doesn't leave a lockfile
lying around.


Loading…
Cancel
Save