Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
Some pep-8 corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
esparta committed Mar 25, 2014
1 parent 1829068 commit 08ae8d7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
5 changes: 4 additions & 1 deletion kml2json/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
""" KML2json
Module to parse & work with KML files
"""
#!/usr/bin/env python
# -*- coding: utf-8 -*-

Expand All @@ -6,4 +9,4 @@
__version__ = '0.1.0'


from .kml2json import kmlobject
from .kml2json import KMLobject
48 changes: 28 additions & 20 deletions kml2json/kml2json.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
""" KML2json
Module to parse & work with KML files
"""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lxml import etree, objectify
from lxml import objectify

class kmlobject(object):
def __init__(self, kmlfile):
self.kmlfile = kmlfile
self.root = self.__get_root()

def __get_root(self):
""" Get the root of the KML object """
with open(self.kmlfile) as kmlfile:
return self.parse(kmlfile).getroot()
class KMLobject(object):
""" KML object to perform activities on the KML files """
def __init__(self, kmlfile):
self.kmlfile = kmlfile
self.root = self.__get_root()

def parse(self,fileobject, schema=None):
"""Parses a file object
This function parses a KML file object, and optionally validates it against
a provided schema. """
if schema:
# with validation
parser = objectify.makeparser(schema = schema.schema, strip_cdata=False)
return objectify.parse(fileobject, parser=parser)
else:
# without validation
return objectify.parse(fileobject)
def __get_root(self):
""" Get the root of the KML object """
with open(self.kmlfile) as kmlfile:
return parse(kmlfile).getroot()

def parse(fileobject, schema=None):
"""Parses a file object
This function parses a KML file object, and optionally validates
it against a provided schema.
Source: http://pythonhosted.org/pykml/
"""
if schema:
# with validation
parser = objectify.makeparser(schema = schema.schema,
strip_cdata=False)
return objectify.parse(fileobject, parser=parser)
else:
# without validation
return objectify.parse(fileobject)
12 changes: 7 additions & 5 deletions tests/test_kml2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@
import unittest
from os import path, getcwd

from kml2json import kml2json, kmlobject
from kml2json import KMLobject
from lxml.etree import _ElementTree

class TestKml2json(unittest.TestCase):

class TestKmlObject(unittest.TestCase):
"""Test the KML"""
def setUp(self):
self.kmlfile = path.join(getcwd(),"tests/ags.kml")
self.kml = kmlobject(self.kmlfile)
self.kml = KMLobject(self.kmlfile)

def tearDown(self):
pass

def test_kmloader(self):
self.assertIsInstance(self.kml, kmlobject)
"""Can we create the Kmlobject"""
self.assertIsInstance(self.kml, KMLobject)

def test_kmlobject_has_root(self):
""" The KMLObject has a root """
self.assertIsNotNone(self.kml.root)

if __name__ == '__main__':
Expand Down

0 comments on commit 08ae8d7

Please sign in to comment.