Skip to content

Commit

Permalink
Removing unnecessary imports
Browse files Browse the repository at this point in the history
This is done to avoid dependency on third party libraries in pyxform.
'unicodecsv' and 'xlrd' is no more required
  • Loading branch information
Mr.Shiva Reddy committed Dec 17, 2019
1 parent 397881d commit 3765eec
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 114 deletions.
1 change: 1 addition & 0 deletions QRealTime_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ def prepareSendForm(self,layer):
os.chdir(os.path.expanduser('~'))
self.sendForm(layer.name(),xml)
except:
print("error in creating xform xml")
self.iface.messageBar().pushCritical(self.tag,self.tr("Survey form can't be created, check layer name"))
def sendForm(self,xForm_id,xml):
# step1 - verify if form exists:
Expand Down
16 changes: 1 addition & 15 deletions pyxform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@

__version__ = '0.11.5'

from pyxform.builder import (
SurveyElementBuilder,
create_survey, # noqa
create_survey_element_from_dict,
create_survey_from_path,
create_survey_from_xls)
from pyxform.instance import SurveyInstance # noqa
from pyxform.question import (
InputQuestion,
MultipleChoiceQuestion, # noqa
Question)
from pyxform.question_type_dictionary import QUESTION_TYPE_DICT # noqa
from pyxform.section import Section # noqa
from pyxform.survey import Survey # noqa
from pyxform.xls2json import SurveyReader as ExcelSurveyReader # noqa
from pyxform.builder import SurveyElementBuilder

# This is what gets imported when someone imports pyxform
168 changes: 84 additions & 84 deletions pyxform/builder.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import copy
import os

from pyxform import file_utils, utils
#from pyxform import file_utils, utils
from pyxform.errors import PyXFormError
from pyxform.external_instance import ExternalInstance
#from pyxform.external_instance import ExternalInstance
from pyxform.question import (InputQuestion, MultipleChoiceQuestion,
OsmUploadQuestion, Question, RangeQuestion,
TriggerQuestion, UploadQuestion)
from pyxform.question_type_dictionary import QUESTION_TYPE_DICT
from pyxform.section import GroupedSection, RepeatingSection
from pyxform.survey import Survey
from pyxform.utils import unicode
from pyxform.xls2json import SurveyReader
#from pyxform.utils import unicode
#from pyxform.xls2json import SurveyReader


def copy_json_dict(json_dict):
Expand Down Expand Up @@ -286,83 +286,83 @@ def create_survey_element_from_dict(d, sections=None):
return builder.create_survey_element_from_dict(d)


def create_survey_element_from_json(str_or_path):
d = utils.get_pyobj_from_json(str_or_path)
return create_survey_element_from_dict(d)


def create_survey_from_xls(path_or_file):
excel_reader = SurveyReader(path_or_file)
d = excel_reader.to_json_dict()
survey = create_survey_element_from_dict(d)
if not survey.id_string:
survey.id_string = excel_reader._name
return survey


def create_survey(
name_of_main_section=None, sections=None,
main_section=None,
id_string=None,
title=None,
default_language=None,
):
"""
name_of_main_section -- a string key used to find the main section in the
sections dict if it is not supplied in the
main_section arg
main_section -- a json dict that represents a survey
sections -- a dictionary of sections that can be drawn from to build the
survey
This function uses the builder class to create and return a survey.
"""
if sections is None:
sections = {}
if main_section is None:
main_section = sections[name_of_main_section]
builder = SurveyElementBuilder()
builder.set_sections(sections)

# assert name_of_main_section in sections, name_of_main_section
if u"id_string" not in main_section:
main_section[u"id_string"] = name_of_main_section \
if id_string is None else name_of_main_section
survey = builder.create_survey_element_from_dict(main_section)

# not sure where to do this without repeating ourselves,
# but it's needed to pass xls2xform tests
# TODO: I would assume that the json-dict is valid
# (i.e. that it has a id string), then throw an error here.
# We can set the id to whatever we want in xls2json.
# Although to be totally modular, maybe we do need to repeat a lot
# of the validation and setting default value stuff from xls2json
if id_string is not None:
survey.id_string = id_string

if title is not None:
survey.title = title
survey.def_lang = default_language

return survey


def create_survey_from_path(path, include_directory=False):
"""
include_directory -- Switch to indicate that all the survey forms in the
same directory as the specified file should be read
so they can be included through include types.
@see: create_survey
"""
directory, file_name = os.path.split(path)
if include_directory:
main_section_name = file_utils._section_name(file_name)
sections = file_utils.collect_compatible_files_in_directory(directory)
else:
main_section_name, section = file_utils.load_file_to_dict(path)
sections = {main_section_name: section}
pkg = {
u'name_of_main_section': main_section_name,
u'sections': sections
}

return create_survey(**pkg)
##def create_survey_element_from_json(str_or_path):
## d = utils.get_pyobj_from_json(str_or_path)
## return create_survey_element_from_dict(d)
##
##
##def create_survey_from_xls(path_or_file):
## excel_reader = SurveyReader(path_or_file)
## d = excel_reader.to_json_dict()
## survey = create_survey_element_from_dict(d)
## if not survey.id_string:
## survey.id_string = excel_reader._name
## return survey
##
##
##def create_survey(
## name_of_main_section=None, sections=None,
## main_section=None,
## id_string=None,
## title=None,
## default_language=None,
## ):
## """
## name_of_main_section -- a string key used to find the main section in the
## sections dict if it is not supplied in the
## main_section arg
## main_section -- a json dict that represents a survey
## sections -- a dictionary of sections that can be drawn from to build the
## survey
## This function uses the builder class to create and return a survey.
## """
## if sections is None:
## sections = {}
## if main_section is None:
## main_section = sections[name_of_main_section]
## builder = SurveyElementBuilder()
## builder.set_sections(sections)
##
## # assert name_of_main_section in sections, name_of_main_section
## if u"id_string" not in main_section:
## main_section[u"id_string"] = name_of_main_section \
## if id_string is None else name_of_main_section
## survey = builder.create_survey_element_from_dict(main_section)
##
## # not sure where to do this without repeating ourselves,
## # but it's needed to pass xls2xform tests
## # TODO: I would assume that the json-dict is valid
## # (i.e. that it has a id string), then throw an error here.
## # We can set the id to whatever we want in xls2json.
## # Although to be totally modular, maybe we do need to repeat a lot
## # of the validation and setting default value stuff from xls2json
## if id_string is not None:
## survey.id_string = id_string
##
## if title is not None:
## survey.title = title
## survey.def_lang = default_language
##
## return survey
##
##
##def create_survey_from_path(path, include_directory=False):
## """
## include_directory -- Switch to indicate that all the survey forms in the
## same directory as the specified file should be read
## so they can be included through include types.
## @see: create_survey
## """
## directory, file_name = os.path.split(path)
## if include_directory:
## main_section_name = file_utils._section_name(file_name)
## sections = file_utils.collect_compatible_files_in_directory(directory)
## else:
## main_section_name, section = file_utils.load_file_to_dict(path)
## sections = {main_section_name: section}
## pkg = {
## u'name_of_main_section': main_section_name,
## u'sections': sections
## }
##
## return create_survey(**pkg)
24 changes: 12 additions & 12 deletions pyxform/question_type_dictionary.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from pyxform.xls2json import QuestionTypesReader, print_pyobj_to_json

def generate_new_dict():
"""
This is just here incase there is ever any need to generate the question
type dictionary from all.xls again.
It shouldn't be called as part of any application.
"""
path_to_question_types = "/home/nathan/aptana-workspace/pyxform"\
"/pyxform/question_types/all.xls"
json_dict = QuestionTypesReader(path_to_question_types).to_json_dict()
print_pyobj_to_json(json_dict, 'new_quesiton_type_dict.json')
##from pyxform.xls2json import QuestionTypesReader, print_pyobj_to_json
##
##def generate_new_dict():
## """
## This is just here incase there is ever any need to generate the question
## type dictionary from all.xls again.
## It shouldn't be called as part of any application.
## """
## path_to_question_types = "/home/nathan/aptana-workspace/pyxform"\
## "/pyxform/question_types/all.xls"
## json_dict = QuestionTypesReader(path_to_question_types).to_json_dict()
## print_pyobj_to_json(json_dict, 'new_quesiton_type_dict.json')


QUESTION_TYPE_DICT = \
Expand Down
2 changes: 1 addition & 1 deletion pyxform/survey_element.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from pyxform import constants
from pyxform.utils import is_valid_xml_tag, node, unicode
from pyxform.xls2json import print_pyobj_to_json
#from pyxform.xls2json import print_pyobj_to_json
from pyxform.question_type_dictionary import QUESTION_TYPE_DICT
from pyxform.errors import PyXFormError

Expand Down
4 changes: 2 additions & 2 deletions pyxform/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import codecs
import json
import copy
import unicodecsv as csv
import xlrd
#import unicodecsv as csv
#import xlrd
import os

try:
Expand Down

0 comments on commit 3765eec

Please sign in to comment.