-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add upgrate step to migrate terms from VCGE 1 to 2
- Loading branch information
Showing
9 changed files
with
1,638 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# -*- coding:utf-8 -*- | ||
from brasil.gov.vcge.config import PROJECTNAME | ||
|
||
import logging | ||
|
||
|
||
logger = logging.getLogger(PROJECTNAME) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<?xml version="1.0"?> | ||
<metadata> | ||
<version>2000</version> | ||
<version>2001</version> | ||
<dependencies> | ||
<dependency>profile-raptus.autocompletewidget:default</dependency> | ||
<dependency>profile-raptus.autocompletewidget:default</dependency> | ||
</dependencies> | ||
</metadata> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
xmlns:genericsetup="http://namespaces.zope.org/genericsetup" | ||
i18n_domain="brasil.barra.gov"> | ||
|
||
<!-- Passos de atualizacao --> | ||
<configure xmlns="http://namespaces.zope.org/zope"> | ||
<include package=".v1000" /> | ||
<include package=".v2000" /> | ||
|
||
<include package=".v2001" /> | ||
</configure> |
1,532 changes: 1,532 additions & 0 deletions
1,532
src/brasil/gov/vcge/upgrades/v2001/VCGE_DEPARA_01.csv
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# -*- coding:utf-8 -*- | ||
from brasil.gov.vcge.dx.interfaces import IVCGEDx | ||
from brasil.gov.vcge.logger import logger | ||
from plone import api | ||
|
||
import csv | ||
import os | ||
|
||
|
||
INFO_URL = 'https://www.governoeletronico.gov.br/documentos-e-arquivos/VCGE_DEPARA_01.pdf' | ||
|
||
|
||
class Migrator(): | ||
"""Context manager to handle term migration.""" | ||
|
||
def __init__(self): | ||
path = os.path.dirname(__file__) | ||
self.filename = os.path.join(path, 'VCGE_DEPARA_01.csv') | ||
|
||
def __enter__(self): | ||
"""Read equivalences file and create dictionary.""" | ||
self.equivalences = {} | ||
logger.debug('Reading equivalence file') | ||
with open(self.filename) as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row in reader: | ||
new = row['termo_vcge_2B'] | ||
if new: | ||
old = row['termo_vcge_1'] | ||
self.equivalences[old] = new | ||
|
||
def __exit__(self, *args): | ||
pass # update the catalog? | ||
|
||
def migrate(self, obj): | ||
"""Migrate existing terms on objects.""" | ||
if obj is None: | ||
return # invalid object | ||
|
||
if not obj.skos: | ||
return # nothing to do (empty or None) | ||
|
||
new = () | ||
# ignore terms with no equivalence | ||
for term in obj.skos: | ||
if term in self.equivalences: | ||
new += self.equivalences[term] | ||
obj.skos = new | ||
|
||
|
||
def term_migration(context): | ||
"""Migrate terms from VCGE 1 to 2B.""" | ||
logger.warning( | ||
'Some terms of VCGE 1 will be ignored. ' | ||
'For more information see: ' + INFO_URL | ||
) | ||
with Migrator() as migrator: | ||
results = api.content.find(object_provides=IVCGEDx.__identifier__) | ||
logger.info('{0} objects will be processed'.format(len(results))) | ||
for brain in results: | ||
try: | ||
obj = brain.getObject() | ||
except (AttributeError, KeyError): | ||
continue # the object referenced is invalid | ||
migrator.migrate(obj) | ||
|
||
logger.info('VCGE terms migrated from 1 to 2B') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
xmlns:genericsetup="http://namespaces.zope.org/genericsetup"> | ||
|
||
<genericsetup:upgradeSteps | ||
source="2000" | ||
destination="2001" | ||
profile="brasil.gov.vcge:default"> | ||
|
||
<genericsetup:upgradeStep | ||
title="Migrate terms from VCGE 1 to 2B" | ||
description="" | ||
handler=".term_migration" | ||
/> | ||
|
||
</genericsetup:upgradeSteps> | ||
|
||
</configure> |