本文整理汇总了Python中arelle.PackageManager.mappedUrl方法的典型用法代码示例。如果您正苦于以下问题:Python PackageManager.mappedUrl方法的具体用法?Python PackageManager.mappedUrl怎么用?Python PackageManager.mappedUrl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类arelle.PackageManager
的用法示例。
在下文中一共展示了PackageManager.mappedUrl方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: func_json_data
# 需要导入模块: from arelle import PackageManager [as 别名]
# 或者: from arelle.PackageManager import mappedUrl [as 别名]
def func_json_data(xule_context, *args):
"""Read a json file/url.
Arguments:
file_url (string or url)
Returns a dictionary/list of the json data.
"""
file_url = args[0]
if file_url.type not in ('string', 'uri'):
raise XuleProcessingError(_("The file url argument of the json-dta() function must be a string or uri, found '{}'.".format(file_url.value)), xule_context)
from arelle import PackageManager
mapped_file_url = PackageManager.mappedUrl(file_url.value)
# Using the FileSource object in arelle. This will open the file and handle taxonomy package mappings.
from arelle import FileSource
file_source = FileSource.openFileSource(file_url.value, xule_context.global_context.cntlr)
file = file_source.file(file_url.value, binary=True)
# file is tuple of one item as a BytesIO stream. Since this is in bytes, it needs to be converted to text via a decoder.
# Assuming the file is in utf-8.
data_source = [x.decode('utf-8') for x in file[0].readlines()]
try:
json_source = json.loads(''.join(data_source))
#except JSONDecodeError:
except ValueError:
raise XuleProcessingError(_("The file '{}' is not a valid JSON file.".format(file_url.value)), xule_context)
x = xv.system_collection_to_xule(json_source, xule_context)
return xv.system_collection_to_xule(json_source, xule_context)
示例2: openFileStream
# 需要导入模块: from arelle import PackageManager [as 别名]
# 或者: from arelle.PackageManager import mappedUrl [as 别名]
def openFileStream(cntlr, filepath, mode='r', encoding=None):
if PackageManager.isMappedUrl(filepath):
filepath = PackageManager.mappedUrl(filepath)
else:
filepath = cntlr.modelManager.disclosureSystem.mappedUrl(filepath)
if archiveFilenameParts(filepath): # file is in an archive
return openFileSource(filepath, cntlr).file(filepath, binary='b' in mode, encoding=encoding)[0]
if isHttpUrl(filepath) and cntlr:
_cacheFilepath = cntlr.webCache.getfilename(filepath)
if _cacheFilepath is None:
raise IOError(_("Unable to open file: {0}.").format(filepath))
filepath = _cacheFilepath
# file path may be server (or memcache) or local file system
if filepath.startswith(SERVER_WEB_CACHE) and cntlr:
filestream = None
cacheKey = filepath[len(SERVER_WEB_CACHE) + 1:].replace("\\","/")
if cntlr.isGAE: # check if in memcache
cachedBytes = gaeGet(cacheKey)
if cachedBytes:
filestream = io.BytesIO(cachedBytes)
if filestream is None:
filestream = io.BytesIO()
cntlr.webCache.retrieve(cntlr.webCache.cacheFilepathToUrl(filepath),
filestream=filestream)
if cntlr.isGAE:
gaeSet(cacheKey, filestream.getvalue())
if mode.endswith('t') or encoding:
contents = filestream.getvalue()
filestream.close()
filestream = FileNamedStringIO(filepath, contents.decode(encoding or 'utf-8'))
return filestream
# local file system
elif encoding is None and 'b' not in mode:
openedFileStream = io.open(filepath, mode='rb')
hdrBytes = openedFileStream.read(512)
encoding = XmlUtil.encoding(hdrBytes, default=None)
openedFileStream.close()
return io.open(filepath, mode=mode, encoding=encoding)
else:
# local file system
return io.open(filepath, mode=mode, encoding=encoding)
示例3: func_csv_data
# 需要导入模块: from arelle import PackageManager [as 别名]
# 或者: from arelle.PackageManager import mappedUrl [as 别名]
def func_csv_data(xule_context, *args):
"""Read a csv file/url.
Arguments:
file_url (string or url)
has_header (boolean) - determines if the first line of the csv file has headers
type list (list) - list of xule types in the order of the columns of the csv file. This is optional. If not provided, then all the data will be
treated as stirngs.
as_dictionary (boolean) - return the row as a dictionary instead of a list. This is optional.
"""
file_url = args[0]
has_headers = args[1]
if len(args) < 2:
raise XuleProcessingError(_("The csv-data() function requires at least 2 arguments (file url, has headers), found {} arguments.".format(len(args))), xule_context)
if len(args) > 4:
raise XuleProcessingError(_("The csv-data() function takes no more than 3 arguments (file url, has headers, column types, as dictionary), found {} arguments.".format(len(args))), xule_context)
if file_url.type not in ('string', 'uri'):
raise XuleProcessingError(_("The file url argument (1st argument) of the csv-dta() function must be a string or uri, found '{}'.".format(file_url.value)), xule_contet)
if has_headers.type != 'bool':
raise XuleProcessingError(_("The has headers argument (2nd argument) of the csv-data() function muset be a boolean, found '{}'.".format(has_headers.type)), xule_context)
if len(args) >= 3:
column_types = args[2]
if column_types.type == 'none':
ordered_cols = None
elif column_types.type == 'list':
ordered_cols = list()
for col in column_types.value:
if col.type != 'string':
raise XuleProcessingError(_("The type list argument (3rd argument) of the csv-data() function must be a list of strings, found '{}'.".format(col.type)), xule_context)
ordered_cols.append(col.value)
else:
raise XuleProcessingError(_("The type list argument (3rd argument) of the csv-data() fucntion must be list, found '{}'.".format(column_types.type)), xule_context)
else:
ordered_cols = None
if len(args) == 4:
if args[3].type != 'bool':
raise XuleProcessingError(_("The as dictionary argument (4th argument) of the csv-data() function must be a boolean, found '{}'.".format(args[3].type)), xule_context)
if args[3].value:
return_row_type = 'dictionary'
else:
return_row_type = 'list'
else:
return_row_type = 'list'
if return_row_type == 'dictionary' and not has_headers.value:
raise XuleProcessingError(_("When the csv-data() function is returning the rows as dictionaries (4th argument), the has headers argument (2nd argument) must be true."), xule_context)
result = list()
result_shadow = list()
from arelle import PackageManager
mapped_file_url = PackageManager.mappedUrl(file_url.value)
# Using the FileSource object in arelle. This will open the file and handle taxonomy package mappings.
from arelle import FileSource
file_source = FileSource.openFileSource(file_url.value, xule_context.global_context.cntlr)
file = file_source.file(file_url.value, binary=True)
# file is tuple of one item as a BytesIO stream. Since this is in bytes, it needs to be converted to text via a decoder.
# Assuming the file is in utf-8.
data_source = [x.decode('utf-8') for x in file[0].readlines()]
# if mapped_file_url.startswith('http://') or mapped_file_url.startswith('https://'):
#
# if mapped_file_url.startswith('https://') and getattr(xule_context.global_context.options, 'noCertificateCheck', False):
# try:
# import ssl
# context = ssl.create_default_context()
# context.check_hostname = False
# context.verify_mode = ssl.CERT_NONE
# except ImportError:
# context=None
# else:
# context = None
# try:
# data_source = urllib.request.urlopen(mapped_file_url, context=context).read().decode('utf-8').splitlines()
# except urllib.error.HTTPError as he:
# raise XuleProcessingError(_("Trying to open url '{}', got HTTP {} - {}, error".format(mapped_file_url, he.code, he.reason)), xule_context)
# else:
# try:
# with open(mapped_file_url, 'r', newline='') as data_file:
# data_source = data_file.readlines()
# except FileNotFoundError:
# raise XuleProcessingError(_("Trying to open file '{}', but file is not found.".format(mapped_file_url)), xule_context)
import csv
reader = csv.reader(data_source)
first_line = True
row_num = 0
for line in reader:
row_num += 1
if first_line and has_headers.value:
first_line = False
#skip the headers line
if return_row_type == 'dictionary':
#.........这里部分代码省略.........