Created
July 11, 2017 16:38
-
-
Save gacarrillor/e0e1f890bb613f0bd745a4fb3795c6a4 to your computer and use it in GitHub Desktop.
Parse ili model in search for domain attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import glob | |
ilifile = '/docs/tr/ai/productos/curso_interlis_mayo_2017/ejercicio_3er_dia/modelos/LADM-OT-discusion.ili' #Catastro_COL_ES_V_2_0_20170331.ili' | |
domains = [ | |
'Catastro_COL_ES_V_2_0_20170331.COL_Genero', | |
'Catastro_COL_ES_V_2_0_20170331.COL_InteresadoDocumentoTipo', | |
'Catastro_COL_ES_V_2_0_20170331.COL_InteresadoDocumentoTipo', | |
'Catastro_COL_ES_V_2_0_20170331.COL_UsoTipo', | |
'documentoOTTipo', | |
'estructuraTipo', | |
'proteccionTipo', | |
'responsabilidadTipo', | |
'responsableTipo', | |
'subestructuraTipo', | |
'restriccionOTTipo', | |
'sueloTipo' | |
] | |
def parse_model(ilifile, domains): | |
re_model = re.compile('\s*MODEL\s*([\w\d_-]+)\s.*') # MODEL Catastro_COL_ES_V_2_0_20170331 (es) | |
re_topic = re.compile('\s*TOPIC\s*([\w\d_-]+)\s.*') # TOPIC Catastro_Registro [=] | |
re_class = re.compile('\s*CLASS\s*([\w\d_-]+)\s.*') # CLASS ClassName [=] | |
re_end_class = None # END ClassName; | |
re_end_topic = None # END TopicName; | |
with open(ilifile, 'r') as file: | |
current_model = '' | |
current_topic = '' | |
current_class = '' | |
attributes = dict() | |
models_info = dict() | |
for line in file: | |
if not current_model: | |
result = re_model.search(line) | |
if result: | |
current_model = result.group(1) | |
else: # The is a current_model | |
if not current_topic: | |
result = re_topic.search(line) | |
if result: | |
current_topic = result.group(1) | |
re_end_topic = re.compile('\s*END\s*{}\s*;\s.*'.format(current_topic)) # END TopicName; | |
continue | |
else: # There is a current_topic | |
if not current_class: # Go for classes | |
result = re_class.search(line) | |
if result: | |
current_class = result.group(1) | |
attributes = dict() | |
re_end_class = re.compile('\s*END\s*{}\s*;\s.*'.format(current_class)) # END ClassName; | |
continue | |
else: # There is a current_class, go for attributes | |
attribute = {res.group(1):d for d in domains for res in [re.search('\s*([\w\d_-]+).*:.*{}.*'.format(d),line)] if res} | |
if attribute: | |
attributes.update(attribute) | |
continue | |
result = re_end_class.search(line) | |
if result: | |
if attributes: | |
models_info.update({'{}.{}.{}'.format(current_model,current_topic,current_class) : attributes}) | |
current_class = '' | |
continue | |
result = re_end_topic.search(line) | |
if result: | |
current_topic = '' | |
print(models_info) | |
parse_model(ilifile, domains) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment