-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add upgrate step to migrate terms from VCGE 1 to 2 #56
Open
hvelarde
wants to merge
1
commit into
master
Choose a base branch
from
hvelarde-migration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,65 @@ | ||
# -*- 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.""" | ||
msg = 'Some terms of VCGE 1 will be ignored. For more information see: ' | ||
logger.warning(msg + 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case all terms would be migrated? or are there any example of term that would remain as it is from old to new version?
for example, we have 3 old terms ['term1', 'term2', 'term3'];
'term2' should be replaced by 'term4' and 'term3' by 'term5';
with the code written now, the result would be ['term4', 'term5'] because 'term1' is not into
equivalences
dict.Is it fine like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, check the tests.