Skip to content

Commit

Permalink
easier xmltodict
Browse files Browse the repository at this point in the history
  • Loading branch information
atait committed Jan 15, 2019
1 parent daffd5f commit 0731039
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 37 deletions.
35 changes: 3 additions & 32 deletions lygadgets/markup.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
from collections import defaultdict
from xml.etree import cElementTree as ET
import xmltodict

# XML to Dict parser, from:
# https://stackoverflow.com/questions/2148119/how-to-convert-an-xml-string-to-a-dictionary-in-python/10077069
# Used in SiEPIC-Tools (https://github.com/lukasc-ubc/SiEPIC-Tools)
def etree_to_dict(t):
d = {t.tag: {} if t.attrib else None}
children = list(t)
if children:
dd = defaultdict(list)
for dc in map(etree_to_dict, children):
for k, v in dc.items():
dd[k].append(v)
d = {t.tag: {k: v[0] if len(v) == 1 else v for k, v in dd.items()}}
if t.attrib:
d[t.tag].update(('@' + k, v) for k, v in t.attrib.items())
if t.text:
text = t.text.strip()
if children or t.attrib:
if text:
d[t.tag]['#text'] = text
else:
d[t.tag] = text
return d


def xml_to_dict(t):
try:
e = ET.XML(t)
except:
raise UserWarning("Error in the XML file.")
return etree_to_dict(e)
def xml_to_dict(xml_content):
return xmltodict.parse(xml_content, process_namespaces=True)
11 changes: 7 additions & 4 deletions lygadgets/technology.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ def _load_pya_tech(lyt_filename):

def klayout_last_open_technology():
# use this to pick out a starting "active" technology
rc_file = os.path.join(klayout_home(), 'klayoutrc')
with open(rc_file, 'r') as file:
rc_dict = xml_to_dict(file.read())
return rc_dict['config']['initial-technology']
if os.path.isdir(klayout_home()):
rc_file = os.path.join(klayout_home(), 'klayoutrc')
if os.path.isfile(rc_file):
with open(rc_file, 'r') as file:
rc_dict = xml_to_dict(file.read())
return rc_dict['config']['initial-technology']
return ''
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def readme():
author_email='[email protected]',
license='MIT',
packages=['lygadgets'],
install_requires=[],
install_requires=['xmltodict'],
package_data={'': ['*.lym']},
include_package_data=True,
entry_points={'console_scripts': ['lygadgets_link=lygadgets.command_line:cm_link_any']},
Expand Down

0 comments on commit 0731039

Please sign in to comment.