Skip to content

Commit

Permalink
Merge branch 'master' of git.sv.gnu.org:/srv/git/gnowsys
Browse files Browse the repository at this point in the history
  • Loading branch information
gnowgi committed Aug 12, 2012
2 parents 968ac9c + 0065973 commit 3180e36
Show file tree
Hide file tree
Showing 5 changed files with 500 additions and 18 deletions.
112 changes: 112 additions & 0 deletions gstudio/management/commands/sync-instances.py
@@ -0,0 +1,112 @@
# Copyright (c) 2012 Free Software Foundation

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from django.core.management.base import BaseCommand
from optparse import make_option

import re
import sys
from datetime import datetime

from objectapp.models import Gbobject
from objectapp.models import Objecttype
from objectapp.models import Attributetype
from objectapp.models import Relationtype
from objectapp.models import ObjectDoesNotExist

# from gstudio.xmlrpc.metaweblog import class_checker
from gstudio import models as gstmodels
from objectapp import models as objmodels
import inspect

from xmlrpclib import DateTime
from xmlrpclib import ServerProxy

from django.db.utils import IntegrityError

class Command(BaseCommand):
"""Gets all Gbobjects from a specified server"""
option_list = BaseCommand.option_list + (
make_option("--server", action="store", type="string",
dest="server", help="Specify an ip"),)

def handle(self, *args, **options):
def class_checker(m):
"""Returns a dict which contains all classes of the m module"""
res = {}
for name, obj in inspect.getmembers(m):
if inspect.isclass(obj) and obj.__module__ == m.__name__:
res[name] = obj
return res

def parse(module=None, instance=None, id=None):
"""Parses and saves instances"""
try:
instances = srv.metaWeblog.show_instance(module, instance, id)

if module == "objectapp.models":
module = objmodels

if module == "gstudio.models":
module = gstmodels

for i in instances:
pattern = "^(\d{4})(\d{2})(\d{2}).(\d{2}).(\d{2}).(\d{2})$"

if "_tags_cache" in i:
del i["_tags_cache"]

if "_state" in i:
del i["_state"]

if "_altnames_cache" in i:
del i["_altnames_cache"]

if "_mptt_cached_fields" in i:
del i["_mptt_cached_fields"]

def group(value):
return value.group(1, 2, 3, 4, 5, 6)

def str_to_int(string):
return [int(x) for x in string]

for key in i.keys():
# Weird check for DateTime objects
if "make_comparable" in dir(i[key]):
dt = DateTime().make_comparable(i[key])[1]
dt = str_to_int(group(re.search(pattern, dt)))

i[key] = datetime(*dt)

class_checker(module)[instance](**i).save()

except (ObjectDoesNotExist, IntegrityError):
sys.stderr.write("Object matching query does not exist\n")

except ValueError:
sys.stderr.write("Object already exists\n")

server = options["server"]
srv = ServerProxy(server, allow_none=True)

objcc = class_checker(objmodels)
gstcc = class_checker(gstmodels)

for i in objcc.keys():
parse("objectapp.models", i)

for i in gstcc.keys():
parse("gstudio.models", i)

0 comments on commit 3180e36

Please sign in to comment.