Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
fix some style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianriese committed Dec 10, 2017
1 parent b59ac2c commit f50ea67
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
40 changes: 19 additions & 21 deletions bin/make_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import sys
import argparse
import re
from abc import ABCMeta, abstractmethod
import os

Expand Down Expand Up @@ -36,6 +35,7 @@
help="Check for consistency and print"
"the bitmaps to stdout")


class Bitmap(object):
def __init__(self, width, height, buf=None):
self.width = width
Expand All @@ -53,10 +53,10 @@ def __str__(self):
lines = []
for i in range(self.height):
lines.append(''.join(
'o' if bit else '.'
for byte in self.buffer[i*self.pitch:(i+1)*self.pitch]
for bit in ((byte >> j) & 0x1 for j in range(8))
)[:self.width])
'o' if bit else '.'
for byte in self.buffer[i*self.pitch:(i+1)*self.pitch]
for bit in ((byte >> j) & 0x1 for j in range(8))
)[:self.width])
return '\n'.join(lines)

def wipe(self):
Expand Down Expand Up @@ -136,6 +136,7 @@ def __xor__(self, other):
cpy |= other
return cpy


class ColorHandlerMeta(ABCMeta):

def __new__(cls, name, bases, dict):
Expand All @@ -156,6 +157,7 @@ def _register_subclass(cls, sub_class, marked):
else:
cls._register_recurse(sub_class, marked)


class ColorHandler(metaclass=ColorHandlerMeta):
MODES = {}

Expand Down Expand Up @@ -205,6 +207,7 @@ def make_transparency_filter(self):
else:
return lambda x: False


class PColorHander(ColorHandler):
MODE = ['P']

Expand All @@ -215,6 +218,7 @@ def make_transparency_filter(self):
else:
return lambda x: False


class OneColorHandler(ColorHandler):
MODE = ['1']

Expand All @@ -230,7 +234,7 @@ def __init__(self, palette):
self._palette = bytearray(palette.palette)

def __getitem__(self, item):
return tuple(self._palette[i] for i in range(3*item, 3*item+3))
return tuple(self._palette[i] for i in range(3*item, 3*item + 3))


class LockMaker(object):
Expand All @@ -252,7 +256,6 @@ def __init__(self, args):
else:
self.uni_image = True


self._guess_size()
self._guess_hotspot()
self._guess_colors()
Expand Down Expand Up @@ -312,13 +315,10 @@ def _guess_hotspot(self):

def _guess_colors(self):
image_has_colors = False
bg_hist = self._histogram(self._bg_bitmap_raw)
if not self.uni_image:
fg_hist = self._histogram(self._fg_bitmap_raw)

if self.uni_image:
bg_hist = self._histogram(self._bg_bitmap_raw)
mode = self._bg_bitmap_raw.mode
info = self._bg_bitmap_raw.info

bg_color_handler = ColorHandler.make(self._bg_bitmap_raw)
tr_filter = bg_color_handler.make_transparency_filter()
Expand Down Expand Up @@ -371,7 +371,6 @@ def _guess_colors(self):
sys.exit(1)
else:
mode = self._bg_bitmap_raw.mode
info = self._bg_bitmap_raw.info

mode_fg = self._fg_bitmap_raw.mode
if mode_fg != mode:
Expand Down Expand Up @@ -402,8 +401,8 @@ def _guess_colors(self):
else:
if self.uni_image:
self.stroke_border = True
self.fg_color = (255,255,255)
self.bg_color = (0,0,0)
self.fg_color = (255, 255, 255)
self.bg_color = (0, 0, 0)

def _histogram(self, PIL_img):
hist = {}
Expand All @@ -424,13 +423,13 @@ def _stroke(self, PIL_img, bitmap, filter, wipe=True):
for i in range(self.width):
for j in range(self.height):
if filter(data[i, j]):
bitmap[i,j] = 1
bitmap[i, j] = 1

def _stroke_border(self):
def action(i, j, di, dj, in_img):
if self._bg_bitmap[i,j]:
if self._bg_bitmap[i, j]:
if not in_img:
self._fg_bitmap[i,j] = 1
self._fg_bitmap[i, j] = 1
return True
else:
if in_img:
Expand All @@ -441,13 +440,12 @@ def finish(i, j, in_img):
if in_img:
self._fg_bitmap[i, j] = 1


# stroke vertically
for i in range(self.width):
in_img = False
for j in range(self.height):
in_img = action(i, j, 0, 1, in_img)
finish(i, j, in_img)
finish(i, j, in_img)

# stroke horizontally
for j in range(self.height):
Expand Down Expand Up @@ -500,6 +498,6 @@ def bg_bitmap(self):
"y_hot": 1,
"fg_bitmap": fg_bitmap,
"bg_bitmap": bg_bitmap,
"bg_color": (0,0,0),
"fg_color": (0,0,0)
"bg_color": (0, 0, 0),
"fg_color": (0, 0, 0)
}, f)
8 changes: 5 additions & 3 deletions bin/pyxtrlock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import os
import sys
import time
import getpass
from ctypes import byref, cast, sizeof
from ctypes import byref, cast
from ctypes import POINTER, c_int, c_uint32, c_char

from xdg.BaseDirectory import load_data_paths
Expand Down Expand Up @@ -116,8 +116,10 @@ xcb.map_window(conn, window)

for i in range(100):
try:
status = xcb.grab_keyboard_sync(conn, 0, window, xcb.CURRENT_TIME,
xcb.GRAB_MODE_ASYNC, xcb.GRAB_MODE_ASYNC)
status = xcb.grab_keyboard_sync(conn, 0, window,
xcb.CURRENT_TIME,
xcb.GRAB_MODE_ASYNC,
xcb.GRAB_MODE_ASYNC)

if status == xcb.GrabSuccess:
break
Expand Down
1 change: 0 additions & 1 deletion pyxtrlock/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import sys
import os


def panic(*message_parts, exit_code=1):
Expand Down
3 changes: 2 additions & 1 deletion pyxtrlock/cursor_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

MAX_CURSOR_SIZE = 512


def _check_size(w, h, data):
pitch = (w + 7) // 8
size = pitch * h
Expand Down Expand Up @@ -45,7 +46,7 @@ def load_cursor(f):
res = {}

for attr in INTEGER_ATTRIBUTES:
if not (isinstance(cursor[attr], int) and
if not (isinstance(cursor[attr], int) and
0 <= cursor[attr] <= MAX_CURSOR_SIZE):
raise ValueError("{} must be integer".format(attr))
res[attr] = cursor[attr]
Expand Down
1 change: 1 addition & 0 deletions pyxtrlock/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ctypes import cdll
from ctypes.util import find_library


def check_and_load_library(libname):
handle = find_library(libname)
if handle is None:
Expand Down
20 changes: 19 additions & 1 deletion pyxtrlock/xcb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ctypes import *
from pyxtrlock.utils import check_and_load_library


class XCBError(Exception):
"""Raised on XCBErrors.
Expand Down Expand Up @@ -86,6 +87,7 @@ class Cookie(Structure):
("sequence", c_uint)
]


VoidCookie = Cookie
AllocNamedColorCookie = Cookie
AllocColorCookie = Cookie
Expand Down Expand Up @@ -117,6 +119,7 @@ class AllocNamedColorReply(Structure):
("visual_blue", c_uint16)
]


class AllocColorReply(Structure):
_fields_ = [
("response_type", c_uint8),
Expand Down Expand Up @@ -167,6 +170,7 @@ def __str__(self):
','.join(str(getattr(self, field))
for field, _ in self._fields_))


class GenericEvent(Structure):
_fields_ = [
("response_type", c_uint8),
Expand All @@ -176,9 +180,11 @@ class GenericEvent(Structure):
("full_sequence", c_uint32)
]


Keycode = c_uint8
Timestamp = c_uint32


class KeyPressEvent(Structure):
_fields_ = [
("response_type", c_uint8),
Expand All @@ -198,7 +204,6 @@ class KeyPressEvent(Structure):
]



GrabKeyboardReply = GrabReply
GrabPointerReply = GrabReply

Expand Down Expand Up @@ -321,6 +326,7 @@ def alloc_named_color_sync(conn, colormap, color_string):
free(res)
return ret


def alloc_color_sync(conn, colormap, r, g, b):
"""Synchronously allocate a color
Expand Down Expand Up @@ -349,6 +355,7 @@ def alloc_color_sync(conn, colormap, r, g, b):
free(res)
return ret


request_check = libxcb.xcb_request_check
request_check.argtypes = [POINTER(Connection), VoidCookie]
request_check.restype = POINTER(GenericError)
Expand Down Expand Up @@ -387,14 +394,17 @@ def create_cursor_sync(conn, source, mask, fg, bg, x, y):

return cursor


map_window = libxcb.xcb_map_window
map_window.argtypes = [POINTER(Connection), Window]
map_window.restype = VoidCookie


flush = libxcb.xcb_flush
flush.argtypes = [POINTER(Connection)]
flush.restype = c_int


grab_keyboard = libxcb.xcb_grab_keyboard
grab_keyboard.argtypes = [
POINTER(Connection), # connection
Expand All @@ -406,6 +416,7 @@ def create_cursor_sync(conn, source, mask, fg, bg, x, y):
]
grab_keyboard.restype = GrabKeyboardCookie


grab_keyboard_reply = libxcb.xcb_grab_keyboard_reply
grab_keyboard_reply.argtypes = [
POINTER(Connection), # connection
Expand Down Expand Up @@ -459,13 +470,15 @@ def grab_keyboard_sync(conn, owner_events, grab_window, time, ptr_mode,
]
grab_pointer_reply.restype = POINTER(GrabPointerReply)


# constants to interpret grab results
GrabSuccess = 0
AlreadyGrabbed = 1
GrabInvalidTime = 2
GrabNotViewable = 3
GrabFrozen = 4


def grab_pointer_sync(conn, owner_events, window, event_mask, ptr_mode,
kbd_mode, confine_to, cursor, timestamp):
"""
Expand All @@ -486,14 +499,17 @@ def grab_pointer_sync(conn, owner_events, window, event_mask, ptr_mode,
free(ptr_grab)
return status


wait_for_event_ = libxcb.xcb_wait_for_event
wait_for_event_.argtypes = [POINTER(Connection)]
wait_for_event_.restype = POINTER(GenericEvent)


free = libc.free
free.argtypes = [c_void_p]
free.restype = None


class FreeWrapper(object):

def __init__(self, pointer):
Expand All @@ -509,12 +525,14 @@ def __exit__(self, etype, evalue, traceback):
def wait_for_event(conn):
return FreeWrapper(wait_for_event_(conn))


connection_has_error = libxcb.xcb_connection_has_error
connection_has_error.restype = c_int
connection_has_error.argtypes = [
POINTER(Connection),
]


# xcb_image
image_create_pixmap_from_bitmap_data = \
libxcb_image.xcb_create_pixmap_from_bitmap_data
Expand Down

0 comments on commit f50ea67

Please sign in to comment.