Skip to content

Commit

Permalink
lint fixes;
Browse files Browse the repository at this point in the history
  • Loading branch information
twiddli committed Oct 21, 2017
1 parent 381e79b commit 193fd3a
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 65 deletions.
21 changes: 11 additions & 10 deletions happypanda/common/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import pickle
import base64
import yaml
import gevent
Expand All @@ -12,10 +11,12 @@

log = hlogger.Logger(__name__)


class ConfigIsolation(Enum):
server = 1
client = 2


class ConfigNode:

default_namespaces = set()
Expand Down Expand Up @@ -63,6 +64,7 @@ def value(self, new_value):
def __bool__(self):
return bool(self.value)


class Config:

def __init__(self, *filepaths, user_filepath=""):
Expand All @@ -80,10 +82,10 @@ def __init__(self, *filepaths, user_filepath=""):
def create(self, ns, key, default=None, description="", type_=None):
return ConfigNode(self, ns, key, default, description, type_)


def _ordered_load(self, stream, Loader=yaml.SafeLoader, object_pairs_hook=OrderedDict):
class OrderedLoader(Loader):
pass

def construct_mapping(loader, node):
loader.flatten_mapping(node)
return object_pairs_hook(loader.construct_pairs(node))
Expand All @@ -95,14 +97,15 @@ def construct_mapping(loader, node):
def _ordered_dump(self, data, stream=None, Dumper=yaml.SafeDumper, **kwds):
class OrderedDumper(Dumper):
pass

def _dict_representer(dumper, data):
return dumper.represent_mapping(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
data.items())
OrderedDumper.add_representer(OrderedDict, _dict_representer)
return yaml.dump(data,
stream,
OrderedDumper,
OrderedDumper,
default_flow_style=False,
indent=4,
**kwds)
Expand Down Expand Up @@ -148,7 +151,7 @@ def save(self):

def save_default(self):
with open(constants.config_example_path, 'w', encoding='utf-8') as wf:
self._ordered_dump(self._default_config, wf)
self._ordered_dump(self._default_config, wf)

def apply_commandline_args(self, config_dict):
for ns in self._cfg:
Expand Down Expand Up @@ -229,8 +232,6 @@ def tmp_config(self, ns, cfg):
for n in tmp_ns:
self._cfg.pop(n)



@contextmanager
def namespace(self, ns):
assert isinstance(ns, str), "Namespace must be str"
Expand Down Expand Up @@ -343,9 +344,9 @@ def doc_render(self):
"Specify which port to start the torrent client on")

port_range = config.create(server_ns,
'port_range',
{'from':7009, 'to':7018},
"Specify a range of ports to attempt")
'port_range',
{'from': 7009, 'to': 7018},
"Specify a range of ports to attempt")

host = config.create(
server_ns,
Expand Down Expand Up @@ -432,4 +433,4 @@ def doc_render(self):
"en_us",
"The default translation locale when none is specified. See folder /translations for available locales")

config_doc = config.doc_render() # for doc
config_doc = config.doc_render() # for doc
1 change: 0 additions & 1 deletion happypanda/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,3 @@ class Priority(enum.Enum):
server_ready = True
local_ip = ""
public_ip = ""

1 change: 1 addition & 0 deletions happypanda/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ class ArchiveExtractError(ArchiveError):
"Archive extraction error"
pass


@error_code(606)
class ArchiveExistError(ArchiveError):
"Archive does not exist error"
Expand Down
5 changes: 4 additions & 1 deletion happypanda/common/hlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ def setup_logger(cls, args, logging_queue=None, main=False):
handlers=tuple(log_handlers))

if main:
Logger(__name__).i(os.path.split(constants.log_debug)[1], "created at", os.path.abspath(constants.log_debug), stdout=True)
Logger(__name__).i(
os.path.split(
constants.log_debug)[1], "created at", os.path.abspath(
constants.log_debug), stdout=True)

@staticmethod
def _listener(args, queue):
Expand Down
8 changes: 5 additions & 3 deletions happypanda/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def parse_options(args):
cfg = config.config

cmd_args = {}

if args.debug is not None:
cmd_args.setdefault(config.debug.namespace, {})[config.debug.name] = args.debug

Expand All @@ -122,7 +122,6 @@ def parse_options(args):
if cmd_args:
cfg.apply_commandline_args(cmd_args)


if constants.dev:
sys.displayhook == pprint.pprint

Expand Down Expand Up @@ -224,6 +223,7 @@ def random_name():
r = base64.urlsafe_b64encode(uuid.uuid4().bytes).decode('utf-8').replace('=', '').replace('_', '-')
return r


def this_function():
"Return name of current function"
return getframeinfo(currentframe()).function
Expand Down Expand Up @@ -273,13 +273,15 @@ def switch(priority=constants.Priority.Normal):
assert isinstance(priority, constants.Priority)
gevent.idle(priority.value)


def get_context(key="ctx"):
"Get a dict local to the spawn tree of current greenlet"
l = getattr(gevent.getcurrent(), 'locals', None)
l = getattr(gevent.getcurrent(), 'locals', None)
if key is not None:
return l[key]
return l


class AttributeList(UserList):
"""
l = AttributeList("one", "two")
Expand Down
2 changes: 2 additions & 0 deletions happypanda/core/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import gevent
import weakref


class Greenlet(gevent.Greenlet):
'''
A subclass of gevent.Greenlet which adds additional members:
Expand All @@ -12,6 +13,7 @@ class Greenlet(gevent.Greenlet):
- stacks: a record of the stack at which the greenlet was
spawned, and ancestors
'''

def __init__(self, f, *a, **kw):
super(Greenlet, self).__init__(f, *a, **kw)
spawner = self.spawn_parent = weakref.proxy(gevent.getcurrent())
Expand Down
4 changes: 2 additions & 2 deletions happypanda/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def connect(self, user=None, password=None):
"Connect to the server"
if not self._tries:
raise exceptions.ServerDisconnectError(
self.name, "Failed to establish server connection after {} tries".format(self._tries_count))
self._tries -= 1 # TODO: increasing timer
self.name, "Failed to establish server connection after {} tries".format(self._tries_count))
self._tries -= 1 # TODO: increasing timer
if not self._alive:
self._last_user = user
self._last_pass = password
Expand Down
1 change: 1 addition & 0 deletions happypanda/core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def _main_wrap(self, *args, **kwargs):
def main(self, *args, **kwargs):
pass


class UndoCommand(Command):
"Command capable of undoing itself"

Expand Down
5 changes: 5 additions & 0 deletions happypanda/core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

log = hlogger.Logger(__name__)


def list_api():
"Returns {name : object}"
_special_functions = tuple() # functions we don't consider as part of the api
Expand All @@ -30,6 +31,7 @@ def list_api():
return {x[0]: x[1] for x in all_functions if not x[0]
in _special_functions and not x[0].startswith('_')}


class Session:
"Connection longevity"

Expand Down Expand Up @@ -404,6 +406,7 @@ def _server_command(self, data):
pass
return None


class Greenlet(gevent.Greenlet):
'''
A subclass of gevent.Greenlet which adds additional members:
Expand All @@ -414,6 +417,7 @@ class Greenlet(gevent.Greenlet):
- stacks: a record of the stack at which the greenlet was
spawned, and ancestors
'''

def __init__(self, f, *a, **kw):
super(Greenlet, self).__init__(f, *a, **kw)
spawner = self.spawn_parent = weakref.proxy(gevent.getcurrent())
Expand All @@ -427,6 +431,7 @@ def __init__(self, f, *a, **kw):
cur = cur.f_back
self.stacks = (tuple(stack),) + getattr(spawner, 'stacks', ())[:10]


class HPServer:
"Happypanda Server"

Expand Down
2 changes: 1 addition & 1 deletion happypanda/core/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _callback_wrapper(self, command_id, command_obj, callback, greenlet):
command_obj.state = command.CommandState.failed
command_obj.exception = greenlet.exception
if constants.dev:
raise # doesnt work
raise # doesnt work

if isinstance(greenlet.value, gevent.GreenletExit):
command_obj.state = command.CommandState.stopped
Expand Down
6 changes: 3 additions & 3 deletions happypanda/interface/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_image(item_type: enums.ItemType=enums.ItemType.Gallery,
Returns:
.. code-block:: guess
{
{
item_id : async_command_id
}
"""
Expand Down Expand Up @@ -169,7 +169,7 @@ def get_count(item_type: enums.ItemType=enums.ItemType.Gallery):
Returns:
.. code-block:: guess
{
{
'count' : int
}
"""
Expand Down Expand Up @@ -197,7 +197,7 @@ def get_related_count(item_type: enums.ItemType=enums.ItemType.Gallery,
Returns:
.. code-block:: guess
{
{
'id' : int
'count' : int
}
Expand Down
1 change: 0 additions & 1 deletion happypanda/interface/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class ViewType(_APIEnum):
Inbox = 3



class ItemType(_APIEnum):
#: Gallery
Gallery = 1
Expand Down
30 changes: 14 additions & 16 deletions happypanda/interface/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from happypanda.interface import enums


#def add_gallery(galleries: list=[], paths: list=[]):
# def add_gallery(galleries: list=[], paths: list=[]):
# """
# Add galleries to the database.

Expand All @@ -25,7 +25,7 @@
# return message.Message("works")


#def scan_gallery(paths: list=[], add_after: bool=False,
# def scan_gallery(paths: list=[], add_after: bool=False,
# ignore_exist: bool=True):
# """
# Scan folders for galleries
Expand Down Expand Up @@ -54,7 +54,7 @@ def source_exists(item_type: enums.ItemType=enums.ItemType.Gallery,
Returns:
.. code-block:: guess
{
{
'exists' : bool
'missing' : [
{'id': int, 'item_type': item_type},
Expand Down Expand Up @@ -87,13 +87,13 @@ def source_exists(item_type: enums.ItemType=enums.ItemType.Gallery,
elif item_type == enums.ItemType.Gallery:
s = constants.db_session()
if item and not check_all:
p = s.query(db.Page.path).filter(db.Gallery.id==item_id).first()
p = s.query(db.Page.path).filter(db.Gallery.id == item_id).first()
if p:
paths[item_id] = (os.path.split(p[0])[0], item_type.value)
else:
not_empty = True
else:
ps = s.query(db.Page.id, db.Page.path).filter(db.Page.gallery_id==item_id).all()
ps = s.query(db.Page.id, db.Page.path).filter(db.Page.gallery_id == item_id).all()
for p in ps:
paths[p[0]] = (p[1], enums.ItemType.Page.value)
not_empty = bool(ps)
Expand All @@ -106,11 +106,9 @@ def source_exists(item_type: enums.ItemType=enums.ItemType.Gallery,
except exceptions.ArchiveExistError:
e = False
if not e:
missing.append({'id':t_id, 'item_type':t_type})

return message.Identity("exists", {'exists':not missing and not_empty, 'missing':missing})

missing.append({'id': t_id, 'item_type': t_type})

return message.Identity("exists", {'exists': not missing and not_empty, 'missing': missing})


def get_page(page_id: int=None, gallery_id: int=None, number: int=None, prev: bool=False):
Expand All @@ -128,8 +126,8 @@ def get_page(page_id: int=None, gallery_id: int=None, number: int=None, prev: bo
"""
if not (gallery_id or page_id):
raise exceptions.APIError(
utils.this_function(),
"Either a gallery id or page id is required")
utils.this_function(),
"Either a gallery id or page id is required")

if number is None:
number = 0
Expand All @@ -146,12 +144,12 @@ def get_page(page_id: int=None, gallery_id: int=None, number: int=None, prev: bo

if not item:
f = db.Page.number < number if prev else db.Page.number > number
f = db.and_op(f, db.Page.gallery_id==gallery_id)
f = db.and_op(f, db.Page.gallery_id == gallery_id)
item = database_cmd.GetModelItemByID().run(db.Page,
order_by=db.Page.number.desc() if prev else db.Page.number,
filter=f,
limit=1)
order_by=db.Page.number.desc() if prev else db.Page.number,
filter=f,
limit=1)
if item:
item = item[0]

return message.Page(item) if item else None
return message.Page(item) if item else None

0 comments on commit 193fd3a

Please sign in to comment.