class Translation(object): ''' Translation class. ''' def __init__(self, locale_file): ''' Constructor. Arguments: locale_file: The localization file ''' if not isinstance(locale_file, File): raise UnsupportedLocaleError('Unsupported localization') self._locale = str(locale_file.parent) self._domain = locale_file.basename self._translations = Map(yaml.load(unicode(locale_file.read()))) @property def locale(self): ''' Returns the translation localization. Returns: str: The translation localization name ''' return self._locale @property def domain(self): ''' Returns the translation domain. Returns: str: The translation domain name ''' return self._domain @cachedmethod def translate(self, message, **placeholders): ''' Translates the given message with their placeholders. Arguments: message (str): The message to be translated placeholders (dict): Any translation placeholder Returns: str: The translated message ''' if message in self._translations: return self._translations.get(message).format(placeholders) return message
class Database(object): ''' Database class. ''' def __init__(self, **info): ''' Constructor. Arguments: info (dict): The database info ''' self._info = Map(info) self._data = None @property def year(self): ''' The database year. ''' return str(self._info.year) @property def archive(self): ''' The database archive, if any. ''' return self._info.get('archive') @property def file(self): ''' The database file. ''' return self._info.file @property def format(self): ''' The database format. ''' return self._info.format @property def sheet(self): ''' The database sheet, if any. ''' return self._info.get('sheet') @property def entities(self): ''' The database entities. ''' return self._info.get('entities', Entities) @property def cacheFile(self): ''' The cached database file. ''' return File(SRC_DIR / '.cache' / Path(self.file).name) def download(self): ''' Downloads the given database. ''' ftp = ftplib.FTP('geoftp.ibge.gov.br') archive_data = io.BytesIO() base_data = io.BytesIO() logger.debug('Connecting to FTP server...') ftp.connect() logger.debug('Logging into the FTP server...') ftp.login() ftp.cwd(str(Path('organizacao_do_territorio', 'estrutura_territorial', 'divisao_territorial', self.year))) logger.info('Retrieving database...') if self.archive: ftp.retrbinary('RETR {}'.format(self.archive), archive_data.write) with zipfile.ZipFile(archive_data, 'r') as archive_file: logger.info('Reading database...') with archive_file.open(self.file, 'r') as base_file: base_data.write(base_file.read()) else: ftp.retrbinary('RETR {}'.format(self.file), base_data.write) try: Directory(self.cacheFile.parent).create() except OSError: pass with self.cacheFile.open('wb') as cache_file: cache_file.write(base_data.getvalue()) return self def read(self): ''' Reads the given database. Returns: places.core.types.Bytes: The database raw binary data ''' if not self.cacheFile.exists(): self.download() base_data = File(self.cacheFile).readBytes() return Bytes(base_data) def parse(self, **options): ''' Parses the given database. Arguments: options (dict): The parsing options Raises: ParseError: When database fails to parse ''' parser = ParserFactory.fromFormat(self.format) self._data = parser(self).parse(**options) return self def export(self, _format, filename, **options): ''' Exports the given database. Arguments: _format: The file format to export the database filename (str): The filename to write options (dict): The exporting options Raises: ExportError: When database fails to export ''' exporter = ExporterFactory.fromFormat(_format, self._data) return exporter.exportToFile(filename, **options)
class Database(object): ''' Database class. ''' def __init__(self, **info): ''' Constructor. Arguments: info (dict): The database info ''' self._info = Map(info) self._data = None @property def year(self): ''' The database year. ''' return str(self._info.year) @property def archive(self): ''' The database archive, if any. ''' return self._info.get('archive') @property def file(self): ''' The database file. ''' return self._info.file @property def format(self): ''' The database format. ''' return self._info.format @property def sheet(self): ''' The database sheet, if any. ''' return self._info.get('sheet') @property def entities(self): ''' The database entities. ''' return self._info.get('entities', Entities) @property def cacheFile(self): ''' The cached database file. ''' return File(SRC_DIR / '.cache' / Path(self.file).name) def download(self): ''' Downloads the given database. ''' ftp = ftplib.FTP('geoftp.ibge.gov.br') archive_data = io.BytesIO() base_data = io.BytesIO() logger.debug('Connecting to FTP server...') ftp.connect() logger.debug('Logging into the FTP server...') ftp.login() ftp.cwd( str( Path('organizacao_do_territorio', 'estrutura_territorial', 'divisao_territorial', self.year))) logger.info('Retrieving database...') if self.archive: ftp.retrbinary('RETR {}'.format(self.archive), archive_data.write) with zipfile.ZipFile(archive_data, 'r') as archive_file: logger.info('Reading database...') with archive_file.open(self.file, 'r') as base_file: base_data.write(base_file.read()) else: ftp.retrbinary('RETR {}'.format(self.file), base_data.write) try: Directory(self.cacheFile.parent).create() except OSError: pass with self.cacheFile.open('wb') as cache_file: cache_file.write(base_data.getvalue()) return self def read(self): ''' Reads the given database. Returns: places.core.types.Bytes: The database raw binary data ''' if not self.cacheFile.exists(): self.download() base_data = File(self.cacheFile).readBytes() return Bytes(base_data) def parse(self, **options): ''' Parses the given database. Arguments: options (dict): The parsing options Raises: ParseError: When database fails to parse ''' parser = ParserFactory.fromFormat(self.format) self._data = parser(self).parse(**options) return self def export(self, _format, filename, **options): ''' Exports the given database. Arguments: _format: The file format to export the database filename (str): The filename to write options (dict): The exporting options Raises: ExportError: When database fails to export ''' exporter = ExporterFactory.fromFormat(_format, self._data) return exporter.exportToFile(filename, **options)