134 lines
5.0 KiB
Python
134 lines
5.0 KiB
Python
import sys
|
|
import os
|
|
import json
|
|
import csv
|
|
import xmltodict
|
|
import jmespath
|
|
import requests
|
|
import pprint
|
|
|
|
pdc_years = ['2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023']
|
|
pdc_codes = {
|
|
'M14': ['M14_CCAS_INF3500', 'M14_CCAS_SUP3500', 'M14_CE', 'M14_COM_500_3500', 'M14_COM_INF500', 'M14_COM_SUP3500'],
|
|
'M4': ['M4', 'M41', 'M43', 'M49_A', 'M49_D'],
|
|
'M52': ['M52'],
|
|
'M57': ['M57', 'M57_A', 'M57_D', 'M57_P'],
|
|
'M61': ['M61'],
|
|
'M71': ['M71'],
|
|
}
|
|
|
|
pdc_root_url = "http://odm-budgetaire.org/composants/normes"
|
|
|
|
|
|
def get_pdc_urls(pdc):
|
|
print('Get URL of plan de compte {}.'.format(pdc))
|
|
pdc_urls = []
|
|
for pdc_year in pdc_years:
|
|
for pdc_code in pdc_codes[pdc]:
|
|
pdc_urls.append(pdc_root_url + '/{year}/{c1}/{c2}/planDeCompte.xml'.format(year=pdc_year, c1=pdc, c2=pdc_code))
|
|
return pdc_urls
|
|
|
|
|
|
def get_children(parents, children_name, children_path, results):
|
|
"""Get all children from a dictonary and return a list"""
|
|
|
|
for parent in parents:
|
|
children = jmespath.search(children_path, parent)
|
|
if children is not None:
|
|
get_children(children, children_name, children_path, results)
|
|
del parent[children_name]
|
|
results.append(parent)
|
|
else:
|
|
results.append(parent)
|
|
return results
|
|
|
|
|
|
def get_nature_chapitres(pdc_dict):
|
|
path_nomenclature_nature_chapitres = 'Nomenclature[0].Nature[0].Chapitres[0].Chapitre[*]'
|
|
nature_chapitres = jmespath.search(path_nomenclature_nature_chapitres, pdc_dict) or []
|
|
return nature_chapitres
|
|
|
|
|
|
def get_nature_comptes(pdc_dict):
|
|
path_nomenclature_nature_comptes = 'Nomenclature[0].Nature[0].Comptes[0].Compte[*]'
|
|
path_comptes = 'Compte[*]'
|
|
nature_comptes = jmespath.search(path_nomenclature_nature_comptes, pdc_dict) or []
|
|
nature_comptes = get_children(nature_comptes, 'Compte', path_comptes, [])
|
|
return nature_comptes
|
|
|
|
|
|
def get_fonction_chapitres(pdc_dict):
|
|
path_nomenclature_fonction_chapitres = 'Nomenclature[0].Fonction[0].Chapitres[0].Chapitre[*]'
|
|
fonction_chapitres = jmespath.search(path_nomenclature_fonction_chapitres, pdc_dict) or []
|
|
return fonction_chapitres
|
|
|
|
|
|
def get_fonction_comptes(pdc_dict):
|
|
path_nomenclature_fonction_comptes = 'Nomenclature[0].Fonction[0].Comptes[0].Compte[*]'
|
|
path_comptes = 'Compte[*]'
|
|
fonction_comptes = jmespath.search(path_nomenclature_fonction_comptes, pdc_dict) or []
|
|
fonction_comptes = get_children(fonction_comptes, 'Compte', path_comptes, [])
|
|
return fonction_comptes
|
|
|
|
|
|
def get_fonction_references(pdc_dict):
|
|
path_nomenclature_fonction_references = 'Nomenclature[0].Fonction[0].RefFonctionnelles[0].RefFonc[*]'
|
|
path_references = 'RefFonc[*]'
|
|
fonction_references = jmespath.search(path_nomenclature_fonction_references, pdc_dict) or []
|
|
fonction_references = get_children(fonction_references, 'RefFonc', path_references, [])
|
|
return fonction_references
|
|
|
|
|
|
def clean_dict(pdc_dict):
|
|
codes = []
|
|
result = []
|
|
for d in pdc_dict:
|
|
if d['Code'] not in codes:
|
|
result.append(d)
|
|
codes.append(d['Code'])
|
|
return result
|
|
|
|
|
|
def save_pdc(filename, pdc_dict):
|
|
pdc_dict = clean_dict(pdc_dict)
|
|
with open(filename, "w") as file:
|
|
json.dump(pdc_dict, file, indent=4)
|
|
print('Plan de compte {} saved.'.format(filename))
|
|
|
|
|
|
def main():
|
|
|
|
for pdc in pdc_codes.keys():
|
|
pdc_urls = get_pdc_urls(pdc)
|
|
|
|
pdc_dict_nature_chapitres = []
|
|
pdc_dict_nature_comptes = []
|
|
pdc_dict_fonction_chapitres = []
|
|
pdc_dict_fonction_comptes = []
|
|
pdc_dict_fonction_references = []
|
|
|
|
print('Get plan de compte {}'.format(pdc))
|
|
for pdc_url in pdc_urls:
|
|
pdc_response = requests.get(pdc_url)
|
|
if pdc_response.status_code == 200:
|
|
# print("Plan de compte: {pdc_url} SUCCESS".format(pdc_url=pdc_url))
|
|
pdc_dict = xmltodict.parse(pdc_response.content, attr_prefix='', force_list=True)
|
|
pdc_dict_nature_chapitres = pdc_dict_nature_chapitres + get_nature_chapitres(pdc_dict)
|
|
pdc_dict_nature_comptes = pdc_dict_nature_comptes + get_nature_comptes(pdc_dict)
|
|
pdc_dict_fonction_chapitres = pdc_dict_fonction_chapitres + get_fonction_chapitres(pdc_dict)
|
|
pdc_dict_fonction_comptes = pdc_dict_fonction_comptes + get_fonction_comptes(pdc_dict)
|
|
pdc_dict_fonction_references = pdc_dict_fonction_references + get_fonction_references(pdc_dict)
|
|
else:
|
|
# print("Plan de compte: {pdc_url} ERROR".format(pdc_url=pdc_url))
|
|
pass
|
|
|
|
save_pdc('./pdc/' + pdc + '_nature_chapitres.json', pdc_dict_nature_chapitres)
|
|
save_pdc('./pdc/' + pdc + '_nature_comptes.json', pdc_dict_nature_comptes)
|
|
save_pdc('./pdc/' + pdc + '_fonction_chapitres.json', pdc_dict_fonction_chapitres)
|
|
save_pdc('./pdc/' + pdc + '_fonction_comptes.json', pdc_dict_fonction_comptes)
|
|
save_pdc('./pdc/' + pdc + '_fonction_references.json', pdc_dict_fonction_references)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|