Skip to content

Commit

Permalink
Merge pull request #5 from jamilatta/urllegendarium
Browse files Browse the repository at this point in the history
Adicionado URL Legendarium para formar as URL do OPAC
  • Loading branch information
jfunez authored Sep 8, 2016
2 parents e69c368 + 41bc0b1 commit 9d3f63a
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 1 deletion.
153 changes: 153 additions & 0 deletions legendarium/urlegendarium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# coding: utf-8

import re

class URLegendarium(object):

def __init__(self, acron='', year_pub='', volume='', number='',
fpage='', lpage='', article_id=''):

self.acron = acron
self.year_pub = str(year_pub)
self.volume = volume
self.number = number
self.fpage = fpage
self.lpage = lpage
self.article_id = article_id

def __unicode__(self):
return self.url_article

def __str__(self):
return self.url_article

def __repr__(self):
return self.url_article

def _get_numbers(self, text):
"""
Get just numbers in a text.
"""
if text:
return re.sub("[^0-9]", "", text)
else:
return ''

def _clean_year_pub(self):
"""
Clean the year removing all caracter and keep just 4/1 numbers.
"""
if self.year_pub:
year = self._get_numbers(self.year_pub)
if len(year) == 4:
return '{0}'.format(year[:4])
else:
raise ValueError(u'Probably not a valid year')
else:
return u''

def _clean_volume(self):
"""
Clean the volume removing all caracter and keep just numbers.
"""
return self._get_numbers(self.volume)

def _clean_number(self):
"""
Clean the number stripped the beginning and the end of the string.
"""
if self.number:
return self.number.strip()
else:
return ''

def _clean_acron(self):
"""
Clean the title stripped the beginning and the end of the string.
"""
return self.acron.strip()

def get_journal_seg(self):
"""
Method to build the journal URL.
"""
if self.acron:
title = self._clean_acron()
else:
raise ValueError(u'The journal is mandatory to mount the URL')

return u'{0}'.format(title)

def get_issue_seg(self):
"""
Method to build the issue.
"""
year = self._clean_year_pub()
volume = self._clean_volume()
number = self._clean_number()

if number:
number = u'n{0}'.format(number)

if volume:
volume = u'v{0}'.format(volume)

if year:
year = u'{0}'.format(year)

if year or volume or number:
return u'{0}.{1}{2}'.format(year, volume, number)
else:
raise ValueError(u'Year or Volume or Year must exists to form URL Issue Segment')

def get_article_seg(self):
"""
Method to build the article.
"""
article = u''

if self.article_id:
article = self.article_id
elif self.fpage and self.lpage:
article = u'{0}-{1}'.format(self.fpage, self.lpage)
elif self.fpage:
article = self.fpage
elif self.lpage:
article = self.lpage
else:
return article

return u'{0}'.format(article)

@property
def url_journal(self):
"""
Print the url follow the basic definition:
REVISTA/
"""
args = [self.url_journal_seg()]

return u'{0}/'.format(*args)

@property
def url_issue(self):
"""
Print the url follow the basic definition:
REVISTA/nVOLUMEvNUMBER
"""
args = [self.url_journal_seg(), self.url_issue_seg()]

return u'{0}/{1}'.format(*args)

@property
def url_article(self):
"""
Print the url follow the basic definition:
REVISTA/nVOLUMEvNUMBER/e1928639
"""
args = [self.url_journal_seg(), self.url_issue_seg(), self.url_article_seg()]

return u'{0}/{1}/{2}'.format(*args)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name='legendarium',
version='0.1.2',
version='0.1.3',
description="Python library to handle SciELO`s bibliographic legend",
long_description=readme + '\n\n' + history,
author="Jamil Atta Junior",
Expand Down

0 comments on commit 9d3f63a

Please sign in to comment.