From b8b5c4fecf14ed934c9c1cf9fc68039d91e29ed6 Mon Sep 17 00:00:00 2001 From: Bruno Nocera Zanette <brunonzanette@gmail.com> Date: Wed, 18 Sep 2013 14:47:02 -0300 Subject: [PATCH] Modify Support functions into a Class definition This commit modifies the support functions for each version (xml and json) into a class definition. This change was made to simplify the code. For example, there is no need to pass the database and output file as arguments anymore. Also, it was implemented a function to update the indentation every time a tag is opened or closed, so that there is no need to specify the indentation every time a write tag functions is called. Signed-off-by: Bruno Nocera Zanette <brunonzanette@gmail.com> --- lib/json_support_functions.py | 97 -------------- lib/opendata_json_class.py | 124 ++++++++++++++++++ ...tion.py => opendata_queries_definition.py} | 0 ...ctions.py => opendata_string_functions.py} | 1 - lib/opendata_xml_class.py | 112 ++++++++++++++++ lib/xml_support_functions.py | 84 ------------ 6 files changed, 236 insertions(+), 182 deletions(-) delete mode 100644 lib/json_support_functions.py create mode 100644 lib/opendata_json_class.py rename lib/{queries_definition.py => opendata_queries_definition.py} (100%) rename lib/{string_functions.py => opendata_string_functions.py} (99%) create mode 100644 lib/opendata_xml_class.py delete mode 100644 lib/xml_support_functions.py diff --git a/lib/json_support_functions.py b/lib/json_support_functions.py deleted file mode 100644 index 01a2ee2..0000000 --- a/lib/json_support_functions.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre -# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR -# -# This file is part of participatorio/opendata -# -# participatorio/opendata 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. - -import MySQLdb -import codecs -import datetime -import base64 - -import string_functions as strf -import queries_definition as qry - -###################################################################### -# Support functions: - -#--------------------------------------------------------------------# -def open_json_file (json_filename): - json_file = codecs.open(json_filename,'w',encoding='utf-8') - return json_file -#--------------------------------------------------------------------# - -#--------------------------------------------------------------------# -def write_open_tag (xml, l, tag_name, sep): - if len(tag_name) > 0: - xml.write(strf.lvl(l)+"\""+tag_name+"\""+":"+sep+"\n") - else: - xml.write(strf.lvl(l)+sep+"\n") -#--------------------------------------------------------------------# - -#--------------------------------------------------------------------# -def write_close_tag (xml, l, sep, comma_flag): - if comma_flag == True: - xml.write(strf.lvl(l)+sep+","+"\n") - else: - xml.write(strf.lvl(l)+sep+"\n") -#--------------------------------------------------------------------# - -#--------------------------------------------------------------------# -def write_tag (xml, l, tag_name, info_str, comma): - name="\""+tag_name+"\"" - info="\""+strf.substbadc(info_str)+"\"" - xml.write(strf.lvl(l)+name+":"+info+comma+"\n") -#--------------------------------------------------------------------# - -#--------------------------------------------------------------------# -def write_comments (db, xml, post_guid): - post_comments = db.cursor() - post_comments.execute(qry.qry_post_comments, (post_guid,)) - - write_open_tag(xml,4,"comentarios","[") - - row=0 - for (user_id, user_name, user_username, string, time)\ - in post_comments: - - row=row+1 - - write_open_tag(xml,5,"","{") - - prefix='profile/' - user_attr=strf.urlparticipa(prefix,user_username) - - write_open_tag(xml,6,"usuario","{") - write_tag(xml,7,"uid",user_attr,",") - write_tag(xml,7,"nome",user_name,"") - write_close_tag(xml,6,"}",True) - - write_tag(xml,6,"data",strf.datestr(time),",") - write_tag(xml,6,"mensagem",string,"") - - write_close_tag(xml,5,"}",(row < post_comments.rowcount)) - - write_close_tag(xml,4,"]",False) - - post_comments.close() -#--------------------------------------------------------------------# - -###################################################################### diff --git a/lib/opendata_json_class.py b/lib/opendata_json_class.py new file mode 100644 index 0000000..94532c9 --- /dev/null +++ b/lib/opendata_json_class.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre +# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR +# +# This file is part of participatorio/opendata +# +# participatorio/opendata 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. + +import MySQLdb +import codecs +import datetime +import base64 + +import opendata_string_functions as strf +import opendata_queries_definition as qry + +class OpendataJSON: + + database = None + indentation = None + level = None + dir_results = None + filename = None + out_file = None + + #--------------------------------------------------------------------# + def __init__ (self, database, dir_results, filename): + self.database = database + + self.indentation = 0 + self.level = strf.lvl(self.indentation) + + self.dir_results = dir_results + self.filename = filename + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def update_indentation(self, increment): + self.indentation=self.indentation+(increment) + self.level = strf.lvl(self.indentation) + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def open_file (self): + self.out_file=codecs.open(self.out_filename,'w',encoding='utf-8') + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def close_file (self): + self.out_file.close() + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def write_open_tag (self, tag_name, sep): + if len(tag_name) > 0: + self.out_file.write(self.level+"\""+tag_name+"\""+":"+sep+"\n") + else: + self.out_file.write(self.level+sep+"\n") + self.update_indentation(+1) + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def write_close_tag (self, sep, comma_flag): + self.update_indentation(-1) + if comma_flag == True: + self.out_file.write(self.level+sep+","+"\n") + else: + print(self.level+sep+"\n") + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def write_tag (self, tag_name, info_str, comma): + name="\""+tag_name+"\"" + info="\""+strf.substbadc(info_str)+"\"" + print(self.level+name+":"+info+comma+"\n") + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def write_comments (self, post_guid): + post_comments = self.database.cursor() + post_comments.execute(qry.qry_post_comments, (post_guid,)) + + self.write_open_tag("comentarios","[") + + row=0 + for (user_id, user_name, user_username, string, time)\ + in post_comments: + + row=row+1 + + self.write_open_tag("","{") + + prefix='profile/' + user_attr=strf.urlparticipa(prefix,user_username) + + self.write_open_tag("usuario","{") + self.write_tag("uid",user_attr,",") + self.write_tag("nome",user_name,"") + self.write_close_tag("}",True) + + self.write_tag("data",strf.datestr(time),",") + self.write_tag("mensagem",string,"") + + self.write_close_tag("}",(row < post_comments.rowcount)) + + self.write_close_tag("]",False) + + post_comments.close() + #--------------------------------------------------------------------# diff --git a/lib/queries_definition.py b/lib/opendata_queries_definition.py similarity index 100% rename from lib/queries_definition.py rename to lib/opendata_queries_definition.py diff --git a/lib/string_functions.py b/lib/opendata_string_functions.py similarity index 99% rename from lib/string_functions.py rename to lib/opendata_string_functions.py index 0c4e292..fd58c5c 100644 --- a/lib/string_functions.py +++ b/lib/opendata_string_functions.py @@ -21,7 +21,6 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. -import codecs import datetime #--------------------------------------------------------------------# diff --git a/lib/opendata_xml_class.py b/lib/opendata_xml_class.py new file mode 100644 index 0000000..c8c9592 --- /dev/null +++ b/lib/opendata_xml_class.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre +# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR +# +# This file is part of participatorio/opendata +# +# participatorio/opendata 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. + +import MySQLdb +import codecs +import datetime + +import opendata_string_functions as strf +import opendata_queries_definition as qry + +class OpendataXML: + + database = None + indentation = None + level = None + dir_results = None + filename = None + out_file = None + + #--------------------------------------------------------------------# + def __init__ (self, database, dir_results, filename): + self.database = database + + self.indentation = 0 + self.level = strf.lvl(self.indentation) + + self.dir_results = dir_results + self.filename = filename + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def update_indentation(self, increment): + self.indentation=self.indentation+(increment) + self.level = strf.lvl(self.indentation) + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def open_file (xml_filename): + self.out_file = codecs.open(xml_filename,'w',encoding='utf-8') + self.out_file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def close_file (self): + self.out_file.close() + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def write_open_tag (self, tag_name, attr_str): + self.out_file.write(self.level+"<"+tag_name+attr_str+">"+"\n") + self.update_indentation(+1) + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def write_close_tag (self, tag_name): + self.update_indentation(-1) + self.out_file.write(self.level+"</"+tag_name+">"+"\n") + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def write_tag (self, tag_name, info_str, attr_str): + if len(info_str) > 0: + tag_begin=("<"+tag_name+attr_str+">") + tag_end=("</"+tag_name+">") + self.out_file.write(self.level+tag_begin+info_str+tag_end+"\n") + else: + self.out_file.write(self.level+"<"+tag_name+attr_str+"/>"+"\n") + #--------------------------------------------------------------------# + + #--------------------------------------------------------------------# + def write_comments (self, post_guid): + post_comments = db.cursor() + post_comments.execute(qry.qry_post_comments, (post_guid,)) + + self.write_open_tag("comentarios",'') + for (user_id, user_name, user_username, string, time)\ + in post_comments: + + self.write_open_tag("comentario",'') + + prefix='profile/' + user_attr=strf.uidstr(strf.urlparticipa(prefix,user_username)) + self.write_tag("usuario",user_name,user_attr) + self.write_tag("data",strf.datestr(time),'') + self.write_tag("mensagem",strf.cdata(string),'') + + self.write_close_tag("comentario") + + self.write_close_tag("comentarios") + + post_comments.close() + #--------------------------------------------------------------------# diff --git a/lib/xml_support_functions.py b/lib/xml_support_functions.py deleted file mode 100644 index e471e89..0000000 --- a/lib/xml_support_functions.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre -# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR -# -# This file is part of participatorio/opendata -# -# participatorio/opendata 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. - -import MySQLdb -import codecs -import datetime - -import string_functions as strf -import queries_definition as qry - -###################################################################### -# Support functions: - -#--------------------------------------------------------------------# -def open_xml_file (xml_filename): - xml_file = codecs.open(xml_filename,'w',encoding='utf-8') - xml_file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") - return xml_file -#--------------------------------------------------------------------# - -#--------------------------------------------------------------------# -def write_open_tag (xml, l, tag_name, attr_str): - xml.write(strf.lvl(l)+"<"+tag_name+attr_str+">"+"\n") -#--------------------------------------------------------------------# - -#--------------------------------------------------------------------# -def write_close_tag (xml, l, tag_name): - xml.write(strf.lvl(l)+"</"+tag_name+">"+"\n") -#--------------------------------------------------------------------# - -#--------------------------------------------------------------------# -def write_tag (xml, l, tag_name, info_str, attr_str): - if len(info_str) > 0: - tag_begin=("<"+tag_name+attr_str+">") - tag_end=("</"+tag_name+">") - xml.write(strf.lvl(l)+tag_begin+info_str+tag_end+"\n") - else: - xml.write(strf.lvl(l)+"<"+tag_name+attr_str+"/>"+"\n") -#--------------------------------------------------------------------# - -#--------------------------------------------------------------------# -def write_comments (db, xml, post_guid): - post_comments = db.cursor() - post_comments.execute(qry.qry_post_comments, (post_guid,)) - - write_open_tag(xml,4,"comentarios",'') - for (user_id, user_name, user_username, string, time) in post_comments: - - write_open_tag(xml,5,"comentario",'') - - prefix='profile/' - user_attr=strf.uidstr(strf.urlparticipa(prefix,user_username)) - write_tag(xml,6,"usuario",user_name,user_attr) - write_tag(xml,6,"data",strf.datestr(time),'') - write_tag(xml,6,"mensagem",strf.cdata(string),'') - - write_close_tag(xml,5,"comentario") - - write_close_tag(xml,4,"comentarios") - - post_comments.close() -#--------------------------------------------------------------------# - -###################################################################### -- GitLab