public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Slava Bacherikov" <slava@bacherikov.org.ua>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/gentoo-packages:master commit in: gpackages/apps/packages/, gpackages/libs/
Date: Thu,  7 Jun 2012 22:48:13 +0000 (UTC)	[thread overview]
Message-ID: <1339097563.6bf1b245210d63a003efa22db88244a35cf0bfe0.bacher09@gentoo> (raw)

commit:     6bf1b245210d63a003efa22db88244a35cf0bfe0
Author:     Slava Bacherikov <slava <AT> bacher09 <DOT> org>
AuthorDate: Thu Jun  7 18:54:24 2012 +0000
Commit:     Slava Bacherikov <slava <AT> bacherikov <DOT> org <DOT> ua>
CommitDate: Thu Jun  7 19:32:43 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/gentoo-packages.git;a=commit;h=6bf1b245

Simple code

---
 gpackages/apps/packages/managers.py |   15 +++++++--------
 gpackages/apps/packages/models.py   |   33 +++++++--------------------------
 gpackages/libs/generic.py           |   23 +++++++++++++++++++++++
 3 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/gpackages/apps/packages/managers.py b/gpackages/apps/packages/managers.py
index c4ab013..ed5018c 100644
--- a/gpackages/apps/packages/managers.py
+++ b/gpackages/apps/packages/managers.py
@@ -1,6 +1,7 @@
 from django.db import models, connections, router, transaction, IntegrityError
 from porttree import Category, Package, Ebuild, Keyword
 import packages.models
+from generic import get_from_kwargs_and_del
 
 
 def _gen_query_and_manager(MixinClass, QueryClassName, ManagerClassName):
@@ -72,19 +73,17 @@ class EbuildMixin(object):#{{{
 
 class HerdsMixin(object):#{{{
     def filter(self, *args, **kwargs):
-        if 'herd__in' in kwargs:
-            herds = kwargs['herd__in']
-            del kwargs['herd__in']
-            kwargs['name__in'] = herds
+        herd__in = get_from_kwargs_and_del('herd__in',kwargs)
+        if herd__in is not None:
+            kwargs['name__in'] = herd__in
         return super(HerdsMixin, self).filter(*args, **kwargs)#}}}
 
 
 class MaintainerMixin(object):#{{{
     def filter(self, *args, **kwargs):
-        if 'maintainer__in'  in kwargs:
-            maintars = kwargs['maintainer__in']
-            del kwargs['maintainer__in']
-            kwargs['email__in'] = maintars
+        maintainer__in = get_from_kwargs_and_del('maintainer__in', kwargs)
+        if maintainer__in is not None:
+            kwargs['email__in'] = maintainer__in
         return super(MaintainerMixin, self).filter(*args, **kwargs)#}}}
 
 

diff --git a/gpackages/apps/packages/models.py b/gpackages/apps/packages/models.py
index 3b30d6f..82d3ca5 100644
--- a/gpackages/apps/packages/models.py
+++ b/gpackages/apps/packages/models.py
@@ -2,6 +2,7 @@ from django.db import models
 
 from porttree import Category, Package, Ebuild
 import managers
+from generic import get_from_kwargs_and_del
 
 class AbstractDateTimeModel(models.Model):
     created_datetime = models.DateTimeField(auto_now_add = True)
@@ -40,11 +41,7 @@ class CategoryModel(models.Model):
 class MaintainerModel(AbstractDateTimeModel):
 
     def __init__(self, *args, **kwargs):
-        #TODO: Bad code, maybe use some libraries for overload methods
-        maintainer = None
-        if 'maintainer' in kwargs:
-            maintainer = kwargs['maintainer']
-            del kwargs['maintainer']
+        maintainer = get_from_kwargs_and_del('maintainer', kwargs)
         super(MaintainerModel, self).__init__(*args, **kwargs)
         if maintainer is not None:
             self.init_by_maintainer(maintainer)
@@ -73,10 +70,7 @@ class MaintainerModel(AbstractDateTimeModel):
 class HerdsModel(AbstractDateTimeModel):
 
     def __init__(self, *args, **kwargs):
-        herd = None
-        if 'herd' in kwargs:
-            herd = kwargs['herd']
-            del kwargs['herd']
+        herd = get_from_kwargs_and_del('herd', kwargs)
         super(HerdsModel, self).__init__(*args, **kwargs)
         if herd is not None:
             self.init_by_herd(herd)
@@ -106,20 +100,11 @@ class HerdsModel(AbstractDateTimeModel):
 
 class PackageModel(AbstractDateTimeModel):
     def __init__(self, *args, **kwargs):
-        # TODO: Bad code, maybe use some library to overload method
-        package_object = None
-        if len(args)>=1:
-            package_object = args[0] 
+        package_object = get_from_kwargs_and_del('package', kwargs)
         
-        if 'package' in kwargs:
-            package_object = kwargs['package']
-            del kwargs['package']
-
+        super(PackageModel, self).__init__(*args, **kwargs)
         if isinstance(package_object, Package):
-            super(PackageModel, self).__init__(*args, **kwargs)
             self.init_by_package(package_object, category = kwargs.get('category'))
-        else:
-            super(PackageModel, self).__init__(*args, **kwargs)
             
         
 
@@ -221,7 +206,6 @@ class EbuildModel(AbstractDateTimeModel):
     is_deleted = models.BooleanField(default = False)
     is_masked = models.BooleanField(default = False)
 
-    #homepage = models.URLField(blank = True, null = True, max_length=255)
     homepages = models.ManyToManyField(HomepageModel, blank = True)
     description = models.TextField(blank = True, null = True)
 
@@ -233,12 +217,9 @@ class EbuildModel(AbstractDateTimeModel):
     objects = managers.EbuildManager()
 
     def __init__(self, *args, **kwargs ):
-        ebuild = None
-        if 'ebuild' in kwargs:
-            ebuild = kwargs['ebuild']
-            del kwargs['ebuild']
+        ebuild = get_from_kwargs_and_del('ebuild', kwargs)
         super(EbuildModel, self).__init__(*args, **kwargs)
-        if ebuild is not None and isinstance(ebuild, Ebuild):
+        if isinstance(ebuild, Ebuild):
             self.init_by_ebuild(ebuild)
     
     def __unicode__(self):

diff --git a/gpackages/libs/generic.py b/gpackages/libs/generic.py
index bc1cd55..0629ed6 100644
--- a/gpackages/libs/generic.py
+++ b/gpackages/libs/generic.py
@@ -1,10 +1,33 @@
 import os.path
 import hashlib
+import types
 from datetime import datetime
 
 __all__ = ('StrThatIgnoreCase', 'ToStrMixin', 'file_get_content', 'file_sha1', \
            'file_mtime', 'cached_property' )
 
+def del_from_dict(what_list, dict_todel):
+    for item in what_list:
+        if item in dict_todel:
+            del dict_todel[item]
+    #dict_todel already modified
+    return dict_todel
+
+def get_from_kwargs_and_del(list_what, kwargs):
+    ret_list = []
+    if isinstance(list_what, types.StringTypes):
+        list_what = (list_what, )
+    for item in list_what:
+        if item in kwargs:
+            ret_list.append(v)
+        else:
+            ret_list.append(None)
+    del_from_dict(ret_list, kwargs)
+    if len(ret_list)==1:
+        return ret_list[0]
+    else:
+        return ret_list
+            
 class StrThatIgnoreCase(unicode):
     __slots__ = ('_forcmp',)
 



             reply	other threads:[~2012-06-07 22:48 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-07 22:48 Slava Bacherikov [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-06-18 21:26 [gentoo-commits] proj/gentoo-packages:master commit in: gpackages/apps/packages/, gpackages/libs/ Slava Bacherikov
2012-06-12 18:34 Slava Bacherikov
2012-06-12 18:34 Slava Bacherikov
2012-06-12 18:34 Slava Bacherikov
2012-06-06 22:48 Slava Bacherikov
2012-06-05 20:48 Slava Bacherikov
2012-06-05 15:49 Slava Bacherikov
2012-06-03 19:34 Slava Bacherikov
2012-05-29 21:34 Slava Bacherikov
2012-05-28 20:32 Slava Bacherikov
2012-05-28 17:00 Slava Bacherikov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1339097563.6bf1b245210d63a003efa22db88244a35cf0bfe0.bacher09@gentoo \
    --to=slava@bacherikov.org.ua \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox