From e7e1ba84f31759cec59460be93bcbd01f1add62a Mon Sep 17 00:00:00 2001 From: Dieter Verfaillie Date: Thu, 25 Apr 2013 17:28:55 +0200 Subject: giscanner: move odict.OrderedDict into a collections package because we'll add more related code to collections later on https://bugzilla.gnome.org/show_bug.cgi?id=699536 --- Makefile-giscanner.am | 10 ++- giscanner/annotationparser.py | 2 +- giscanner/ast.py | 2 +- giscanner/collections/__init__.py | 22 +++++++ giscanner/collections/ordereddict.py | 120 +++++++++++++++++++++++++++++++++++ giscanner/odict.py | 117 ---------------------------------- 6 files changed, 151 insertions(+), 122 deletions(-) create mode 100644 giscanner/collections/__init__.py create mode 100644 giscanner/collections/ordereddict.py delete mode 100644 giscanner/odict.py diff --git a/Makefile-giscanner.am b/Makefile-giscanner.am index e8def0ee..47750864 100644 --- a/Makefile-giscanner.am +++ b/Makefile-giscanner.am @@ -26,14 +26,14 @@ libgiscanner_la_CFLAGS = $(GOBJECT_CFLAGS) $(GIO_CFLAGS) # Python module pkgpyexecdir = $(pkglibdir)/giscanner pkgpyexec_LTLIBRARIES = _giscanner.la -pkgpyexec_PYTHON = \ +pkgpyexec_PYTHON = \ giscanner/__init__.py \ giscanner/annotationmain.py \ giscanner/annotationparser.py \ giscanner/ast.py \ giscanner/cachestore.py \ giscanner/codegen.py \ - giscanner/docmain.py \ + giscanner/docmain.py \ giscanner/docwriter.py \ giscanner/dumper.py \ giscanner/introspectablepass.py \ @@ -41,7 +41,6 @@ pkgpyexec_PYTHON = \ giscanner/girwriter.py \ giscanner/gdumpparser.py \ giscanner/libtoolimporter.py \ - giscanner/odict.py \ giscanner/maintransformer.py \ giscanner/message.py \ giscanner/shlibs.py \ @@ -53,6 +52,11 @@ pkgpyexec_PYTHON = \ giscanner/utils.py \ giscanner/xmlwriter.py +collectionsdir = $(pkgpyexecdir)/collections +collections_PYTHON = \ + giscanner/collections/__init__.py \ + giscanner/collections/ordereddict.py + templatedir = $(pkglibdir) nobase_dist_template_DATA = \ giscanner/doctemplates/base.tmpl \ diff --git a/giscanner/annotationparser.py b/giscanner/annotationparser.py index f722346c..ef9cd31d 100644 --- a/giscanner/annotationparser.py +++ b/giscanner/annotationparser.py @@ -26,7 +26,7 @@ import re from . import message -from .odict import OrderedDict +from .collections import OrderedDict # GTK-Doc comment block parts diff --git a/giscanner/ast.py b/giscanner/ast.py index d307b553..25184886 100644 --- a/giscanner/ast.py +++ b/giscanner/ast.py @@ -24,8 +24,8 @@ from itertools import chain from . import message +from .collections import OrderedDict from .message import Position -from .odict import OrderedDict from .utils import to_underscores class Type(object): diff --git a/giscanner/collections/__init__.py b/giscanner/collections/__init__.py new file mode 100644 index 00000000..29987a10 --- /dev/null +++ b/giscanner/collections/__init__.py @@ -0,0 +1,22 @@ +# -*- Mode: Python -*- +# GObject-Introspection - a framework for introspecting GObject libraries +# Copyright (C) 2013 Dieter Verfaillie +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. +# + + +from .ordereddict import OrderedDict diff --git a/giscanner/collections/ordereddict.py b/giscanner/collections/ordereddict.py new file mode 100644 index 00000000..0cb4b956 --- /dev/null +++ b/giscanner/collections/ordereddict.py @@ -0,0 +1,120 @@ +# -*- Mode: Python -*- +# GObject-Introspection - a framework for introspecting GObject libraries +# Copyright (C) 2008 Johan Dahlin +# Copyright (C) 2013 Dieter Verfaillie +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + + +# Borrowed from: +# http://hg.sqlalchemy.org/sqlalchemy/raw-file/77e2264283d4/lib/sqlalchemy/util/_collections.py +# http://hg.sqlalchemy.org/sqlalchemy/raw-file/77e2264283d4/AUTHORS +# +# util/_collections.py +# Copyright (C) 2005-2012 the SQLAlchemy authors and contributors +# +# This module is part of SQLAlchemy and is released under +# the MIT License: http://www.opensource.org/licenses/mit-license.php + + +class OrderedDict(dict): + """A dict that returns keys/values/items in the order they were added.""" + + def __init__(self, ____sequence=None, **kwargs): + self._list = [] + if ____sequence is None: + if kwargs: + self.update(**kwargs) + else: + self.update(____sequence, **kwargs) + + def clear(self): + self._list = [] + dict.clear(self) + + def copy(self): + return self.__copy__() + + def __copy__(self): + return OrderedDict(self) + + def sort(self, *arg, **kw): + self._list.sort(*arg, **kw) + + def update(self, ____sequence=None, **kwargs): + if ____sequence is not None: + if hasattr(____sequence, 'keys'): + for key in ____sequence.keys(): + self.__setitem__(key, ____sequence[key]) + else: + for key, value in ____sequence: + self[key] = value + if kwargs: + self.update(kwargs) + + def setdefault(self, key, value): + if key not in self: + self.__setitem__(key, value) + return value + else: + return self.__getitem__(key) + + def __iter__(self): + return iter(self._list) + + def values(self): + return [self[key] for key in self._list] + + def itervalues(self): + return iter([self[key] for key in self._list]) + + def keys(self): + return list(self._list) + + def iterkeys(self): + return iter(self.keys()) + + def items(self): + return [(key, self[key]) for key in self.keys()] + + def iteritems(self): + return iter(self.items()) + + def __setitem__(self, key, obj): + if key not in self: + try: + self._list.append(key) + except AttributeError: + # work around Python pickle loads() with + # dict subclass (seems to ignore __setstate__?) + self._list = [key] + dict.__setitem__(self, key, obj) + + def __delitem__(self, key): + dict.__delitem__(self, key) + self._list.remove(key) + + def pop(self, key, *default): + present = key in self + value = dict.pop(self, key, *default) + if present: + self._list.remove(key) + return value + + def popitem(self): + item = dict.popitem(self) + self._list.remove(item[0]) + return item diff --git a/giscanner/odict.py b/giscanner/odict.py deleted file mode 100644 index f9558954..00000000 --- a/giscanner/odict.py +++ /dev/null @@ -1,117 +0,0 @@ -# -*- Mode: Python -*- -# GObject-Introspection - a framework for introspecting GObject libraries -# Copyright (C) 2008 Johan Dahlin -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the -# Free Software Foundation, Inc., 59 Temple Place - Suite 330, -# Boston, MA 02111-1307, USA. - - -# Borrowed from: -# http://hg.sqlalchemy.org/sqlalchemy/raw-file/77e2264283d4/lib/sqlalchemy/util/_collections.py -# http://hg.sqlalchemy.org/sqlalchemy/raw-file/77e2264283d4/AUTHORS -# -# util/_collections.py -# Copyright (C) 2005-2012 the SQLAlchemy authors and contributors -# -# This module is part of SQLAlchemy and is released under -# the MIT License: http://www.opensource.org/licenses/mit-license.php -class OrderedDict(dict): - """A dict that returns keys/values/items in the order they were added.""" - - def __init__(self, ____sequence=None, **kwargs): - self._list = [] - if ____sequence is None: - if kwargs: - self.update(**kwargs) - else: - self.update(____sequence, **kwargs) - - def clear(self): - self._list = [] - dict.clear(self) - - def copy(self): - return self.__copy__() - - def __copy__(self): - return OrderedDict(self) - - def sort(self, *arg, **kw): - self._list.sort(*arg, **kw) - - def update(self, ____sequence=None, **kwargs): - if ____sequence is not None: - if hasattr(____sequence, 'keys'): - for key in ____sequence.keys(): - self.__setitem__(key, ____sequence[key]) - else: - for key, value in ____sequence: - self[key] = value - if kwargs: - self.update(kwargs) - - def setdefault(self, key, value): - if key not in self: - self.__setitem__(key, value) - return value - else: - return self.__getitem__(key) - - def __iter__(self): - return iter(self._list) - - def values(self): - return [self[key] for key in self._list] - - def itervalues(self): - return iter([self[key] for key in self._list]) - - def keys(self): - return list(self._list) - - def iterkeys(self): - return iter(self.keys()) - - def items(self): - return [(key, self[key]) for key in self.keys()] - - def iteritems(self): - return iter(self.items()) - - def __setitem__(self, key, obj): - if key not in self: - try: - self._list.append(key) - except AttributeError: - # work around Python pickle loads() with - # dict subclass (seems to ignore __setstate__?) - self._list = [key] - dict.__setitem__(self, key, obj) - - def __delitem__(self, key): - dict.__delitem__(self, key) - self._list.remove(key) - - def pop(self, key, *default): - present = key in self - value = dict.pop(self, key, *default) - if present: - self._list.remove(key) - return value - - def popitem(self): - item = dict.popitem(self) - self._list.remove(item[0]) - return item -- cgit v1.2.1