<filename>officinam/999999999/0/1603_3_12.py<gh_stars>0
#!/usr/bin/env python3
# ==============================================================================
#
#          FILE:  1603_3_12.py
#
#         USAGE:  ./999999999/0/1603_3_12.py
#                 ./999999999/0/1603_3_12.py --help
#                 NUMERORDINATIO_BASIM="/dir/ndata" ./999999999/0/1603_3_12.py
#
#   DESCRIPTION:  ---
#
#       OPTIONS:  ---
#
#  REQUIREMENTS:  - python3
#                   - requests[socks]
#          BUGS:  ---
#         NOTES:  ---
#       AUTHORS:  <NAME> <rocha[at]ieee.org>
# COLLABORATORS:
#                 <@TODO: put additional non-anonymous names here>
#
#       COMPANY:  EticaAI
#       LICENSE:  Public Domain dedication or Zero-Clause BSD
#                 SPDX-License-Identifier: Unlicense OR 0BSD
#       VERSION:  v0.5.0
#       CREATED:  2022-01-21 17:07 UTC created. Based on 2600.py
#      REVISION:  ---
# ==============================================================================

# pytest
#    python3 -m doctest ./999999999/0/1603_3_12.py

#    ./999999999/0/1603_3_12.py
#    NUMERORDINATIO_BASIM="/external/ndata" ./999999999/0/1603_3_12.py
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --wikidata-link
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --tsv > 999999/0/test.tsv
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --csv > 999999/0/test.csv
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --csv --hxltm

# 1603_25_1 query
# printf "Q3409626\nQ41055\nQ3321315\nQ160695\nQ9645\nQ9597\nQ713102\nQ133279\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query


# SELECT ?pic (STRAFTER(STR(?item), "entity/") AS ?item__conceptum__codicem) ?item__rem__i_lat__is_latn
# WHERE
# {
#   VALUES ?item { wd:Q3409626  wd:Q41055  wd:Q3321315  wd:Q160695  wd:Q9645  wd:Q9597  wd:Q713102  wd:Q133279  }
#   bind(xsd:integer(strafter(str(?item), 'Q')) as ?id_numeric) .
#   OPTIONAL { ?item wdt:P18 ?pic }
#   OPTIONAL { ?item rdfs:label ?item__rem__i_qcc__is_zxxx filter (lang(?item__rem__i_qcc__is_zxxx) = ""). }
#   OPTIONAL { ?item rdfs:label ?item__rem__i_lat__is_latn filter (lang(?item__rem__i_lat__is_latn) = "la"). }
# }
# ORDER BY ASC (?id_numeric)

## Example with proxy
# export HTTP_PROXY="socks5://127.0.0.1:9050"
# export HTTPS_PROXY="socks5://127.0.0.1:9050"

# TODO: https://sinaahmadi.github.io/posts/10-essential-sparql-queries-for-lexicographical-data-on-wikidata.html

import os
import sys
import argparse
# from pathlib import Path
from typing import (
    # Type,
    Union
)
from time import sleep
import math
import urllib.parse
import requests

# from itertools import permutations
from itertools import product
# valueee = list(itertools.permutations([1, 2, 3]))
import csv

NUMERORDINATIO_BASIM = os.getenv('NUMERORDINATIO_BASIM', os.getcwd())
NUMERORDINATIO_DEFALLO = int(os.getenv('NUMERORDINATIO_DEFALLO', '60'))  # �
NUMERORDINATIO_MISSING = "�"
DESCRIPTION = """
1603_3_12.py is (...)
"""

# In Python2, sys.stdin is a byte stream; in Python3, it's a text stream
STDIN = sys.stdin.buffer

# @see https://meta.wikimedia.org/wiki/User-Agent_policy
# @see https://www.mediawiki.org/wiki/API:Etiquette
USER_AGENT="EticaAI-multilingual-lexicography/2022.3.9 (https://meta.wikimedia.org/wiki/User:EmericusPetro; <EMAIL>) 1603_3_12.py/0.1"

# print('getcwd:      ', os.getcwd())
# print('oi', NUMERORDINATIO_BASIM)


# def quod_1613_2_60_datum():
#     datum = {}
#     with open(NUMERORDINATIO_BASIM + "/1613/1603.2.60.no1.tm.hxl.tsv") as file:
#         tsv_file = csv.DictReader(file, delimiter="\t")
#         return list(tsv_file)

# a b aa bb
# printf "30160\n31161\n1830260\n1891267\n" | ./999999999/0/2600.py --actionem-decifram

# a aa aaa
# printf "30160\n1830260\n109830360\n" | ./999999999/0/2600.py --actionem-decifram
# ./999999999/0/1603_3_12.py --actionem-quod-sparql


# SELECT ?item ?itemLabel
# WHERE {
#   # <NAME> or <NAME>
#   VALUES ?item { wd:Q1065 wd:Q82151 wd:Q125761 wd:Q7809}
#   # mother of
#   OPTIONAL { ?item wdt:P25 ?pseudoquery. }
#   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
# }


def hxltm_hastag_de_csvhxlated(csv_caput: list) -> list:
    """hxltm_hastag_de_csvhxlated [summary]

    Make this type of conversion:
    - 'item__conceptum__codicem' => '#item+conceptum+codicem'
    - 'item__rem__i_ara__is_arab' => '#item+rem+i_ara+is_arab'
    - '' => ''

    Args:
        csv_caput (list): Array of input items

    Returns:
        [list]:
    """
    resultatum = []
    for item in csv_caput:
        if len(item):
            resultatum.append('#' + item.replace('__', '+').replace('?', ''))
        else:
            resultatum.append('')
    return resultatum

# https://stackoverflow.com/questions/43258341/how-to-get-wikidata-labels-in-more-than-one-language


class CS1603z3z12:
    """ [summary]

    - https://en.wikibooks.org/wiki/SPARQL

    [extended_summary]
    """

    def __init__(self):
        self.D1613_1_51 = self._init_1613_1_51_datum()

        self.linguae_limitibus = 1000
        self.linguae_paginarum_limitibus = 1
        # langpair_full = self._query_linguam()
        # self.D1613_1_51_langpair = self._query_linguam_limit(langpair_full)
        self.D1613_1_51_langpair = []
        # self.scientia_de_scriptura = {}
        # self.scientia_de_scriptura = self.D1613_2_60
        # self.cifram_signaturae = 6  # TODO: make it flexible
        # self.codex_verbum_tabulae = []
        # self.verbum_limiti = 2
        self.resultatum_separato = "\t"

        self.qid = []

    def _init_1613_1_51_datum(self):
        # archivum = NUMERORDINATIO_BASIM + "/1613/1603_2_60.no1.tm.hxl.tsv"
        # archivum = NUMERORDINATIO_BASIM + "/1603/17/2/60/1613_17_2_60.no1.tm.hxl.tsv"
        archivum = NUMERORDINATIO_BASIM + "/1603/1/51/1603_1_51.no1.tm.hxl.csv"
        datum = {}
        with open(archivum) as file:
            # tsv_file = csv.DictReader(file, delimiter="\t")
            csv_file = csv.DictReader(file)
            # return list(tsv_file)
            for conceptum in csv_file:
                # print('conceptum', conceptum)
                int_clavem = int(conceptum['#item+conceptum+codicem'])
                datum[int_clavem] = {}
                if conceptum['#item+conceptum+codicem'].startswith('0_'):
                    continue
                if not conceptum['#item+rem+i_qcc+is_zxxx+ix_wikilngm']:
                    continue

                for clavem, rem in conceptum.items():
                    if not clavem.startswith('#item+conceptum+codicem'):
                        datum[int_clavem][clavem] = rem

        return datum

    def _query_linguam(self):
        resultatum = []

        for clavem, rem in self.D1613_1_51.items():
            # for clavem, rem in enumerate(self.D1613_1_51):
            # print('clavem rem', clavem, rem)
            if '#item+rem+i_qcc+is_zxxx+ix_wikilngm' not in rem or \
                    '#item+rem+i_qcc+is_zxxx+ix_csvsffxm' not in rem:
                continue
            resultatum.append([
                rem['#item+rem+i_qcc+is_zxxx+ix_wikilngm'],
                'item__rem' + rem['#item+rem+i_qcc+is_zxxx+ix_csvsffxm'],
            ])
        # print(self.D1613_1_51)
        # print('resultatum', resultatum)
        return resultatum

    def _query_linguam_limit(self, langpair_full: list):
        # resultatum = []

        if self.lingua_divisioni < 2:
            return langpair_full

        # @see https://stackoverflow.com/questions/312443
        # /how-do-you-split-a-list-into-evenly-sized-chunks
        # def chunks(lst, n):
        #     """Yield successive n-sized chunks from lst."""
        #     for i in range(0, len(lst), n):
        #         yield lst[i:i + n]
        # if langpair_full

        # def chunks(lst, n):
        #     """Yield successive n-sized chunks from lst."""
        #     for i in range(0, len(lst), n):
        #         yield lst[i:i + n]
        # import math

        divisio_numero = math.ceil(len(langpair_full) / self.lingua_divisioni)

        def chunks(l, n):
            n = max(1, n)
            return (l[i:i+n] for i in range(0, len(l), n))

        # limited = list(chunks(langpair_full, self.lingua_divisioni))
        limited = list(chunks(langpair_full, divisio_numero))

        limited_group = limited[self.lingua_paginae - 1]
        # limited = chunks(langpair_full, self.linguae_limitibus)
        # raise ValueError(limited_group)
        # raise ValueError([limited_group, limited])
        return limited_group

        # # print('resultatum', resultatum)
        # return resultatum

    def est_resultatum_separato(self, resultatum_separato: str):
        self.resultatum_separato = resultatum_separato
        return self

    def est_lingua_divisioni(
            self, lingua_divisioni: Union[str, int]):
        self.lingua_divisioni = int(lingua_divisioni)
        return self

    def est_lingua_paginae(
            self, lingua_paginae: Union[str, int]):
        self.lingua_paginae = int(lingua_paginae)
        return self

    def est_wikidata_q(self, wikidata_codicem: str):
        if wikidata_codicem not in self.qid:
            self.qid.append(wikidata_codicem)

        return self

#     def query(self):
#         term = """# https://en.wikiversity.org/wiki/Research_in_programming_Wikidata/Countries#List_of_countries
# # https://w.wiki/4ij4
# SELECT ?item ?item__eng_latn ?item__rus_cyrl
# WHERE
# {
#   ?item wdt:P31 wd:Q6256. # instance country
#   OPTIONAL {
#     ?item rdfs:label ?item__eng_latn filter (lang(?item__eng_latn) = "en").
#     ?item rdfs:label ?item__rus_cyrl filter (lang(?item__rus_cyrl) = "ru").
#   }
# }
#         """
#         return term


# SELECT ?item ?item_rem__eng_latn ?item_rem__rus_cyrl
# WHERE
# {
#   VALUES ?item { wd:Q1065 wd:Q82151 wd:Q125761 wd:Q7809 }
#   OPTIONAL {
#     ?item rdfs:label ?item_rem__eng_latn filter (lang(?item_rem__eng_latn) = "en").
#     ?item rdfs:label ?item_rem__rus_cyrl filter (lang(?item_rem__rus_cyrl) = "ru").
#   }
# }

    def query(self):
        langpair_full = self._query_linguam()
        self.D1613_1_51_langpair = self._query_linguam_limit(langpair_full)

        qid = ['wd:' + x for x in self.qid if isinstance(x, str)]
        # select = '?item ' + " ".join(self._query_linguam())

        # select = ['(?item AS ?item__conceptum__codicem)']
        select = [
            '(STRAFTER(STR(?item), "entity/") AS ?item__conceptum__codicem)']
        # select = [
        #     '(STRAFTER(STR(?item), "entity/") AS ?item__conceptum__codicem)',
        #     '(STRAFTER(STR(?item), "entity/") AS ?item__rem__i_qcc__is_zxxx__ix_wikiq)'
        # ]
        filter_otional = []
        for pair in self.D1613_1_51_langpair:
            select.append('?' + pair[1])
            # filter_otional.append(
            #     '?item rdfs:label ?' +
            #     pair[1] + ' filter (lang(?' + pair[1] +
            #     ') = "' + pair[0] + '").'
            # )
            filter_otional.append(
                'OPTIONAL { ?item rdfs:label ?' +
                pair[1] + ' filter (lang(?' + pair[1] +
                ') = "' + pair[0] + '"). }'
            )
        filter_otional_done = ['  ' + x for x in filter_otional]
        # print('select', self.D1613_1_51_langpair)
        # print('select', select)
        # print('filter_otional', filter_otional)
        term = """
SELECT {select}
WHERE
{{
  VALUES ?item {{ {qitems} }}
  bind(xsd:integer(strafter(str(?item), 'Q')) as ?id_numeric) .
{langfilter}
}}
ORDER BY ASC (?id_numeric)
        """.format(
            qitems=" ".join(qid),
            select=" ".join(select),
            langfilter="\n".join(filter_otional_done),
        )
        # """.format(qitems = " ".join(self.qid))

        # [TRY IT ↗]()
        return term

    def exportatum_sparql(self):
        resultatum = []
        # resultatum.append('#TODO')
        # resultatum.append(str(self.D1613_1_51))
        resultatum.append(self.query())
        return resultatum


class CLI_2600:
    def __init__(self):
        """
        Constructs all the necessary attributes for the Cli object.
        """
        self.pyargs = None
        # self.args = self.make_args()
        # Posix exit codes
        self.EXIT_OK = 0
        self.EXIT_ERROR = 1
        self.EXIT_SYNTAX = 2

    def make_args(self, hxl_output=True):
        parser = argparse.ArgumentParser(description=DESCRIPTION)

        # https://en.wikipedia.org/wiki/Code_word
        # https://en.wikipedia.org/wiki/Coded_set

        # cōdex verbum tabulae
        # parser.add_argument(
        #     '--actionem',
        #     help='Action to execute. Defaults to codex.',
        #     # choices=['rock', 'paper', 'scissors'],
        #     choices=[
        #         'codex',
        #         'fontem-verbum-tabulae',
        #         'neo-scripturam',
        #     ],
        #     dest='actionem',
        #     required=True,
        #     default='codex',
        #     const='codex',
        #     type=str,
        #     nargs='?'
        # )

        parser.add_argument(
            '--punctum-separato-de-resultatum',
            help='Character(s) used as separator for generate output.' +
            'Defaults to tab "\t"',
            dest='resultatum_separato',
            default="\t",
            nargs='?'
        )

        neo_codex = parser.add_argument_group(
            "sparql",
            "(DEFAULT USE) SPARQL query")

        neo_codex.add_argument(
            '--actionem-sparql',
            help='Define mode to operate with generation of SPARQL ' +
            'queries',
            metavar='',
            dest='actionem_sparql',
            const=True,
            nargs='?'
        )

        neo_codex.add_argument(
            '--query',
            help='Generate SPARQL query',
            metavar='',
            dest='query',
            const=True,
            nargs='?'
        )
        neo_codex.add_argument(
            '--wikidata-link',
            help='Generate query.wikidata.org link (from piped in query)',
            metavar='',
            dest='wikidata_link',
            const=True,
            nargs='?'
        )
        neo_codex.add_argument(
            '--csv',
            help='Generate TSV output (from piped in query)',
            metavar='',
            dest='csv',
            const=True,
            nargs='?'
        )

        neo_codex.add_argument(
            '--tsv',
            help='Generate TSV output (from piped in query)',
            metavar='',
            dest='tsv',
            const=True,
            nargs='?'
        )

        neo_codex.add_argument(
            '--hxltm',
            help='Generate HXL-tagged output (from piped in query). ' +
            'Concepts use #item+conceptum+codicem instead ' +
            'of #item+code+v_wiki_q',
            metavar='',
            dest='hxltm',
            const=True,
            nargs='?'
        )

        # linguae, f, pl, (Nominative) https://en.wiktionary.org/wiki/lingua
        # pāginārum, f, pl, (Gengitive) https://en.wiktionary.org/wiki/pagina
        # dīvīsiōnibus, f, pl, (Dative) https://en.wiktionary.org/wiki/divisio
        # līmitibus, m, pl, (Dative) https://en.wiktionary.org/wiki/limes#Latin
        # //linguae pāginārum līmitibus//

        # lingua, f, s, (Nominative) https://en.wiktionary.org/wiki/lingua#Latin
        # pāginae, f, s, (Dative) https://en.wiktionary.org/wiki/lingua#Latin
        # dīvīsiōnī, f, s, (Dative) https://en.wiktionary.org/wiki/lingua#Latin
        neo_codex.add_argument(
            '--lingua-divisioni',
            help='For the languages on [1603:1:51], how many divisions ' +
            '(or number of chunks) should be done. 1 means no division.' +
            'If using more than 1, use --lingua-paginae do paginate the ' +
            'Options. Default: 1',
            dest='lingua_divisioni',
            metavar='',
            default="1",
            nargs='?'
        )

        neo_codex.add_argument(
            '--lingua-paginae',
            help='If --lingua-divisioni different from 1, defines which page '
            'of languages to return. Default 1.',
            dest='lingua_paginae',
            metavar='',
            default="1",
            nargs='?'
        )

        parser.add_argument(
            '--verbose',
            help='Verbose output',
            metavar='verbose',
            nargs='?'
        )
        if hxl_output:
            parser.add_argument(
                'outfile',
                help='File to write (if omitted, use standard output).',
                nargs='?'
            )

        # print('oioioi', parser)
        return parser.parse_args()

    # def execute_cli(self, args, stdin=STDIN, stdout=sys.stdout,
    #                 stderr=sys.stderr):
    def execute_cli(self, pyargs, stdin=STDIN, stdout=sys.stdout,
                    stderr=sys.stderr):
        # print('TODO')

        self.pyargs = pyargs

        # cs1603_3_12 = cs1603_3_12()
        cs1603_3_12 = CS1603z3z12()

        # cs1603_3_12 = cs1603_3_12()

        # print('self.pyargs', self.pyargs)

        # cs1603_3_12.est_verbum_limiti(args.verbum_limiti)
        cs1603_3_12.est_resultatum_separato(args.resultatum_separato)
        cs1603_3_12.est_lingua_divisioni(args.lingua_divisioni)
        cs1603_3_12.est_lingua_paginae(
            args.lingua_paginae)

        if self.pyargs.actionem_sparql:
            # print('oi')

            if self.pyargs.query:
                if stdin.isatty():
                    print("ERROR. Please pipe data in. \nExample:\n"
                          "  cat data.txt | {0} --actionem-quod-sparql\n"
                          "  printf \"Q1065\\nQ82151\\n\" | {0} --actionem-quod-sparql"
                          "".format(__file__))
                    return self.EXIT_ERROR

                for line in sys.stdin:
                    codicem = line.replace('\n', ' ').replace('\r', '')
                    # TODO: deal with cases were have more than Qcode
                    cs1603_3_12.est_wikidata_q(codicem)

                quod_query = cs1603_3_12.exportatum_sparql()
                # tabulam_numerae = ['TODO']
                # return self.output(tabulam_numerae)
                return self.output(quod_query)

            if self.pyargs.wikidata_link:
                if stdin.isatty():
                    print("ERROR. Please pipe data in. \nExample:\n"
                          "  cat data.txt | {0} --actionem-sparql --query | {0} --actionem-sparql --wikidata-link\n"
                          "  printf \"Q1065\\nQ82151\\n\" | {0} --actionem-sparql --query | {0} --actionem-sparql --wikidata-link"
                          "".format(__file__))
                    return self.EXIT_ERROR

                full_query = []
                for line in sys.stdin:
                    full_query.append(line)

                wikidata_backend = "https://query.wikidata.org/#"
                quod_query = wikidata_backend + \
                    urllib.parse.quote("".join(full_query).encode('utf8'))

                print(quod_query)
                return self.EXIT_OK

            if self.pyargs.tsv or self.pyargs.csv:
                if stdin.isatty():
                    print("ERROR. Please pipe data in. \nExample:\n"
                          "  cat data.txt | {0} --actionem-sparql --query | {0} --actionem-sparql --tsv\n"
                          "  printf \"Q1065\\nQ82151\\n\" | {0} --actionem-sparql --query | {0} --actionem-sparql --tsv"
                          "".format(__file__))
                    return self.EXIT_ERROR

                full_query = []
                for line in sys.stdin:
                    full_query.append(line)

                sparql_backend = "https://query.wikidata.org/sparql"
                # sparql_backend = "http://localhost:1234/"
                # @see https://stackoverflow.com/questions/10588644

                # https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/en#Supported_formats

                if self.pyargs.tsv:
                    separator = "\t"
                    headers = {'Accept': 'text/tab-separated-values'}
                if self.pyargs.csv:
                    separator = ","
                    headers = {'Accept': 'text/csv'}
                if self.pyargs.hxltm:
                    # headers = {'Accept': 'text/tab-separated-values'}
                    headers = {'Accept': 'text/csv'}

                # TODO: make it configurable via command line
                headers['User-Agent'] = USER_AGENT
                headers['Api-User-Agent'] = USER_AGENT

                payload_query = "".join(full_query)
                # Lets put an sleep, 3 seconds, just in case
                sleep(3)
                r = requests.post(sparql_backend, headers=headers, data={
                    'query': payload_query
                })

                # @TODO: --tsv --hxltm is know to be bugged (not sure if
                #        Wikidata result already skip values)

                if self.pyargs.hxltm:
                    result_string = r.text.strip()

                    # @TODO: this likely to break with fields with newlines.
                    #        however no testing sample exists at the moment.
                    #        Eventually needs be checked.
                    lines = result_string.splitlines()
                    # caput = hxltm_hastag_de_csvhxlated(next(iter(lines)).split(","))
                    caput_crudum = lines.pop(0)
                    # print('caput_crudum', caput_crudum)
                    caput = hxltm_hastag_de_csvhxlated(caput_crudum.split(','))
                    print(separator.join(caput))
                    print("\n".join(lines))

                    # reader = csv.reader(lines, delimiter="\t")
                    # caput = hxltm_hastag_de_csvhxlated(next(reader))
                    # print(separator.join(caput))
                    # for row in reader:
                    #     print(separator.join(row))
                else:
                    print(r.text.strip())

                # TODO: generate explicit error messages and return code
                # print(r.content)
                return self.EXIT_OK

        # if self.pyargs.verbum_simplex:
        #     tabulam_multiplicatio = cs1603_3_12.quod_tabulam_multiplicatio()
        #     return self.output(tabulam_multiplicatio)

        # if self.pyargs.codex_completum:
        #     tabulam_multiplicatio = cs1603_3_12.quod_codex()
        #     return self.output(tabulam_multiplicatio)

        # if self.pyargs.neo_scripturam:
        #     scientia = cs1603_3_12.exportatum_scientia_de_scriptura(
        #         args.neo_scripturam_hxl_selectum)
        #     return self.output(scientia)

        # Let's default to full table
        # tabulam_multiplicatio = cs1603_3_12.quod_codex()
        # return self.output(tabulam_multiplicatio)
        print('unknow option.')
        return self.EXIT_ERROR

    def output(self, output_collectiom):
        for item in output_collectiom:
            # TODO: check if result is a file instead of print

            # print(type(item))
            if isinstance(item, int) or isinstance(item, str):
                print(item)
            else:
                print(self.pyargs.resultatum_separato.join(item))

        return self.EXIT_OK

# if not sys.stdin.isatty():
#     print ("not sys.stdin.isatty")
# else:
#     print ("is  sys.stdin.isatty")

# import fcntl
# import os
# import sys

# # make stdin a non-blocking file
# fd = sys.stdin.fileno()
# fl = fcntl.fcntl(fd, fcntl.F_GETFL)
# fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

# try:
#     print(sys.stdin.read())
# except:
#     print('No input')

# from sys import stdin
# from os import isatty

# is_pipe = not isatty(stdin.fileno())

# print('is_pipe', is_pipe)


if __name__ == "__main__":

    cli_2600 = CLI_2600()
    args = cli_2600.make_args()
    # pyargs.print_help()

    # args.execute_cli(args)
    cli_2600.execute_cli(args)


# import itertools
# valueee = list(permutations([1, 2, 3]))
# valueee = list(permutations([1, 2, 3]))

# print(valueee)

# cs1603_3_12 = cs1603_3_12()

# # print(quod_1613_2_60_datum())
# # print(cs1603_3_12)

# print('0')
# print(cs1603_3_12.quod_numerordinatio_digitalem('0', True))
# print('05')
# print(cs1603_3_12.quod_numerordinatio_digitalem('05', True))
# print('zz')
# print(cs1603_3_12.quod_numerordinatio_digitalem('zz', True))

<filename>officinam/999999999/0/1603_3_12.py<gh_stars>0
#!/usr/bin/env python3
# ==============================================================================
#
#          FILE:  1603_3_12.py
#
#         USAGE:  ./999999999/0/1603_3_12.py
#                 ./999999999/0/1603_3_12.py --help
#                 NUMERORDINATIO_BASIM="/dir/ndata" ./999999999/0/1603_3_12.py
#
#   DESCRIPTION:  ---
#
#       OPTIONS:  ---
#
#  REQUIREMENTS:  - python3
#                   - requests[socks]
#          BUGS:  ---
#         NOTES:  ---
#       AUTHORS:  <NAME> <rocha[at]ieee.org>
# COLLABORATORS:
#                 <@TODO: put additional non-anonymous names here>
#
#       COMPANY:  EticaAI
#       LICENSE:  Public Domain dedication or Zero-Clause BSD
#                 SPDX-License-Identifier: Unlicense OR 0BSD
#       VERSION:  v0.5.0
#       CREATED:  2022-01-21 17:07 UTC created. Based on 2600.py
#      REVISION:  ---
# ==============================================================================

# pytest
#    python3 -m doctest ./999999999/0/1603_3_12.py

#    ./999999999/0/1603_3_12.py
#    NUMERORDINATIO_BASIM="/external/ndata" ./999999999/0/1603_3_12.py
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --wikidata-link
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --tsv > 999999/0/test.tsv
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --csv > 999999/0/test.csv
#    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --csv --hxltm

# 1603_25_1 query
# printf "Q3409626\nQ41055\nQ3321315\nQ160695\nQ9645\nQ9597\nQ713102\nQ133279\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query


# SELECT ?pic (STRAFTER(STR(?item), "entity/") AS ?item__conceptum__codicem) ?item__rem__i_lat__is_latn
# WHERE
# {
#   VALUES ?item { wd:Q3409626  wd:Q41055  wd:Q3321315  wd:Q160695  wd:Q9645  wd:Q9597  wd:Q713102  wd:Q133279  }
#   bind(xsd:integer(strafter(str(?item), 'Q')) as ?id_numeric) .
#   OPTIONAL { ?item wdt:P18 ?pic }
#   OPTIONAL { ?item rdfs:label ?item__rem__i_qcc__is_zxxx filter (lang(?item__rem__i_qcc__is_zxxx) = ""). }
#   OPTIONAL { ?item rdfs:label ?item__rem__i_lat__is_latn filter (lang(?item__rem__i_lat__is_latn) = "la"). }
# }
# ORDER BY ASC (?id_numeric)

## Example with proxy
# export HTTP_PROXY="socks5://127.0.0.1:9050"
# export HTTPS_PROXY="socks5://127.0.0.1:9050"

# TODO: https://sinaahmadi.github.io/posts/10-essential-sparql-queries-for-lexicographical-data-on-wikidata.html

import os
import sys
import argparse
# from pathlib import Path
from typing import (
    # Type,
    Union
)
from time import sleep
import math
import urllib.parse
import requests

# from itertools import permutations
from itertools import product
# valueee = list(itertools.permutations([1, 2, 3]))
import csv

NUMERORDINATIO_BASIM = os.getenv('NUMERORDINATIO_BASIM', os.getcwd())
NUMERORDINATIO_DEFALLO = int(os.getenv('NUMERORDINATIO_DEFALLO', '60'))  # �
NUMERORDINATIO_MISSING = "�"
DESCRIPTION = """
1603_3_12.py is (...)
"""

# In Python2, sys.stdin is a byte stream; in Python3, it's a text stream
STDIN = sys.stdin.buffer

# @see https://meta.wikimedia.org/wiki/User-Agent_policy
# @see https://www.mediawiki.org/wiki/API:Etiquette
USER_AGENT="EticaAI-multilingual-lexicography/2022.3.9 (https://meta.wikimedia.org/wiki/User:EmericusPetro; <EMAIL>) 1603_3_12.py/0.1"

# print('getcwd:      ', os.getcwd())
# print('oi', NUMERORDINATIO_BASIM)


# def quod_1613_2_60_datum():
#     datum = {}
#     with open(NUMERORDINATIO_BASIM + "/1613/1603.2.60.no1.tm.hxl.tsv") as file:
#         tsv_file = csv.DictReader(file, delimiter="\t")
#         return list(tsv_file)

# a b aa bb
# printf "30160\n31161\n1830260\n1891267\n" | ./999999999/0/2600.py --actionem-decifram

# a aa aaa
# printf "30160\n1830260\n109830360\n" | ./999999999/0/2600.py --actionem-decifram
# ./999999999/0/1603_3_12.py --actionem-quod-sparql


# SELECT ?item ?itemLabel
# WHERE {
#   # <NAME> or <NAME>
#   VALUES ?item { wd:Q1065 wd:Q82151 wd:Q125761 wd:Q7809}
#   # mother of
#   OPTIONAL { ?item wdt:P25 ?pseudoquery. }
#   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
# }


def hxltm_hastag_de_csvhxlated(csv_caput: list) -> list:
    """hxltm_hastag_de_csvhxlated [summary]

    Make this type of conversion:
    - 'item__conceptum__codicem' => '#item+conceptum+codicem'
    - 'item__rem__i_ara__is_arab' => '#item+rem+i_ara+is_arab'
    - '' => ''

    Args:
        csv_caput (list): Array of input items

    Returns:
        [list]:
    """
    resultatum = []
    for item in csv_caput:
        if len(item):
            resultatum.append('#' + item.replace('__', '+').replace('?', ''))
        else:
            resultatum.append('')
    return resultatum

# https://stackoverflow.com/questions/43258341/how-to-get-wikidata-labels-in-more-than-one-language


class CS1603z3z12:
    """ [summary]

    - https://en.wikibooks.org/wiki/SPARQL

    [extended_summary]
    """

    def __init__(self):
        self.D1613_1_51 = self._init_1613_1_51_datum()

        self.linguae_limitibus = 1000
        self.linguae_paginarum_limitibus = 1
        # langpair_full = self._query_linguam()
        # self.D1613_1_51_langpair = self._query_linguam_limit(langpair_full)
        self.D1613_1_51_langpair = []
        # self.scientia_de_scriptura = {}
        # self.scientia_de_scriptura = self.D1613_2_60
        # self.cifram_signaturae = 6  # TODO: make it flexible
        # self.codex_verbum_tabulae = []
        # self.verbum_limiti = 2
        self.resultatum_separato = "\t"

        self.qid = []

    def _init_1613_1_51_datum(self):
        # archivum = NUMERORDINATIO_BASIM + "/1613/1603_2_60.no1.tm.hxl.tsv"
        # archivum = NUMERORDINATIO_BASIM + "/1603/17/2/60/1613_17_2_60.no1.tm.hxl.tsv"
        archivum = NUMERORDINATIO_BASIM + "/1603/1/51/1603_1_51.no1.tm.hxl.csv"
        datum = {}
        with open(archivum) as file:
            # tsv_file = csv.DictReader(file, delimiter="\t")
            csv_file = csv.DictReader(file)
            # return list(tsv_file)
            for conceptum in csv_file:
                # print('conceptum', conceptum)
                int_clavem = int(conceptum['#item+conceptum+codicem'])
                datum[int_clavem] = {}
                if conceptum['#item+conceptum+codicem'].startswith('0_'):
                    continue
                if not conceptum['#item+rem+i_qcc+is_zxxx+ix_wikilngm']:
                    continue

                for clavem, rem in conceptum.items():
                    if not clavem.startswith('#item+conceptum+codicem'):
                        datum[int_clavem][clavem] = rem

        return datum

    def _query_linguam(self):
        resultatum = []

        for clavem, rem in self.D1613_1_51.items():
            # for clavem, rem in enumerate(self.D1613_1_51):
            # print('clavem rem', clavem, rem)
            if '#item+rem+i_qcc+is_zxxx+ix_wikilngm' not in rem or \
                    '#item+rem+i_qcc+is_zxxx+ix_csvsffxm' not in rem:
                continue
            resultatum.append([
                rem['#item+rem+i_qcc+is_zxxx+ix_wikilngm'],
                'item__rem' + rem['#item+rem+i_qcc+is_zxxx+ix_csvsffxm'],
            ])
        # print(self.D1613_1_51)
        # print('resultatum', resultatum)
        return resultatum

    def _query_linguam_limit(self, langpair_full: list):
        # resultatum = []

        if self.lingua_divisioni < 2:
            return langpair_full

        # @see https://stackoverflow.com/questions/312443
        # /how-do-you-split-a-list-into-evenly-sized-chunks
        # def chunks(lst, n):
        #     """Yield successive n-sized chunks from lst."""
        #     for i in range(0, len(lst), n):
        #         yield lst[i:i + n]
        # if langpair_full

        # def chunks(lst, n):
        #     """Yield successive n-sized chunks from lst."""
        #     for i in range(0, len(lst), n):
        #         yield lst[i:i + n]
        # import math

        divisio_numero = math.ceil(len(langpair_full) / self.lingua_divisioni)

        def chunks(l, n):
            n = max(1, n)
            return (l[i:i+n] for i in range(0, len(l), n))

        # limited = list(chunks(langpair_full, self.lingua_divisioni))
        limited = list(chunks(langpair_full, divisio_numero))

        limited_group = limited[self.lingua_paginae - 1]
        # limited = chunks(langpair_full, self.linguae_limitibus)
        # raise ValueError(limited_group)
        # raise ValueError([limited_group, limited])
        return limited_group

        # # print('resultatum', resultatum)
        # return resultatum

    def est_resultatum_separato(self, resultatum_separato: str):
        self.resultatum_separato = resultatum_separato
        return self

    def est_lingua_divisioni(
            self, lingua_divisioni: Union[str, int]):
        self.lingua_divisioni = int(lingua_divisioni)
        return self

    def est_lingua_paginae(
            self, lingua_paginae: Union[str, int]):
        self.lingua_paginae = int(lingua_paginae)
        return self

    def est_wikidata_q(self, wikidata_codicem: str):
        if wikidata_codicem not in self.qid:
            self.qid.append(wikidata_codicem)

        return self

#     def query(self):
#         term = """# https://en.wikiversity.org/wiki/Research_in_programming_Wikidata/Countries#List_of_countries
# # https://w.wiki/4ij4
# SELECT ?item ?item__eng_latn ?item__rus_cyrl
# WHERE
# {
#   ?item wdt:P31 wd:Q6256. # instance country
#   OPTIONAL {
#     ?item rdfs:label ?item__eng_latn filter (lang(?item__eng_latn) = "en").
#     ?item rdfs:label ?item__rus_cyrl filter (lang(?item__rus_cyrl) = "ru").
#   }
# }
#         """
#         return term


# SELECT ?item ?item_rem__eng_latn ?item_rem__rus_cyrl
# WHERE
# {
#   VALUES ?item { wd:Q1065 wd:Q82151 wd:Q125761 wd:Q7809 }
#   OPTIONAL {
#     ?item rdfs:label ?item_rem__eng_latn filter (lang(?item_rem__eng_latn) = "en").
#     ?item rdfs:label ?item_rem__rus_cyrl filter (lang(?item_rem__rus_cyrl) = "ru").
#   }
# }

    def query(self):
        langpair_full = self._query_linguam()
        self.D1613_1_51_langpair = self._query_linguam_limit(langpair_full)

        qid = ['wd:' + x for x in self.qid if isinstance(x, str)]
        # select = '?item ' + " ".join(self._query_linguam())

        # select = ['(?item AS ?item__conceptum__codicem)']
        select = [
            '(STRAFTER(STR(?item), "entity/") AS ?item__conceptum__codicem)']
        # select = [
        #     '(STRAFTER(STR(?item), "entity/") AS ?item__conceptum__codicem)',
        #     '(STRAFTER(STR(?item), "entity/") AS ?item__rem__i_qcc__is_zxxx__ix_wikiq)'
        # ]
        filter_otional = []
        for pair in self.D1613_1_51_langpair:
            select.append('?' + pair[1])
            # filter_otional.append(
            #     '?item rdfs:label ?' +
            #     pair[1] + ' filter (lang(?' + pair[1] +
            #     ') = "' + pair[0] + '").'
            # )
            filter_otional.append(
                'OPTIONAL { ?item rdfs:label ?' +
                pair[1] + ' filter (lang(?' + pair[1] +
                ') = "' + pair[0] + '"). }'
            )
        filter_otional_done = ['  ' + x for x in filter_otional]
        # print('select', self.D1613_1_51_langpair)
        # print('select', select)
        # print('filter_otional', filter_otional)
        term = """
SELECT {select}
WHERE
{{
  VALUES ?item {{ {qitems} }}
  bind(xsd:integer(strafter(str(?item), 'Q')) as ?id_numeric) .
{langfilter}
}}
ORDER BY ASC (?id_numeric)
        """.format(
            qitems=" ".join(qid),
            select=" ".join(select),
            langfilter="\n".join(filter_otional_done),
        )
        # """.format(qitems = " ".join(self.qid))

        # [TRY IT ↗]()
        return term

    def exportatum_sparql(self):
        resultatum = []
        # resultatum.append('#TODO')
        # resultatum.append(str(self.D1613_1_51))
        resultatum.append(self.query())
        return resultatum


class CLI_2600:
    def __init__(self):
        """
        Constructs all the necessary attributes for the Cli object.
        """
        self.pyargs = None
        # self.args = self.make_args()
        # Posix exit codes
        self.EXIT_OK = 0
        self.EXIT_ERROR = 1
        self.EXIT_SYNTAX = 2

    def make_args(self, hxl_output=True):
        parser = argparse.ArgumentParser(description=DESCRIPTION)

        # https://en.wikipedia.org/wiki/Code_word
        # https://en.wikipedia.org/wiki/Coded_set

        # cōdex verbum tabulae
        # parser.add_argument(
        #     '--actionem',
        #     help='Action to execute. Defaults to codex.',
        #     # choices=['rock', 'paper', 'scissors'],
        #     choices=[
        #         'codex',
        #         'fontem-verbum-tabulae',
        #         'neo-scripturam',
        #     ],
        #     dest='actionem',
        #     required=True,
        #     default='codex',
        #     const='codex',
        #     type=str,
        #     nargs='?'
        # )

        parser.add_argument(
            '--punctum-separato-de-resultatum',
            help='Character(s) used as separator for generate output.' +
            'Defaults to tab "\t"',
            dest='resultatum_separato',
            default="\t",
            nargs='?'
        )

        neo_codex = parser.add_argument_group(
            "sparql",
            "(DEFAULT USE) SPARQL query")

        neo_codex.add_argument(
            '--actionem-sparql',
            help='Define mode to operate with generation of SPARQL ' +
            'queries',
            metavar='',
            dest='actionem_sparql',
            const=True,
            nargs='?'
        )

        neo_codex.add_argument(
            '--query',
            help='Generate SPARQL query',
            metavar='',
            dest='query',
            const=True,
            nargs='?'
        )
        neo_codex.add_argument(
            '--wikidata-link',
            help='Generate query.wikidata.org link (from piped in query)',
            metavar='',
            dest='wikidata_link',
            const=True,
            nargs='?'
        )
        neo_codex.add_argument(
            '--csv',
            help='Generate TSV output (from piped in query)',
            metavar='',
            dest='csv',
            const=True,
            nargs='?'
        )

        neo_codex.add_argument(
            '--tsv',
            help='Generate TSV output (from piped in query)',
            metavar='',
            dest='tsv',
            const=True,
            nargs='?'
        )

        neo_codex.add_argument(
            '--hxltm',
            help='Generate HXL-tagged output (from piped in query). ' +
            'Concepts use #item+conceptum+codicem instead ' +
            'of #item+code+v_wiki_q',
            metavar='',
            dest='hxltm',
            const=True,
            nargs='?'
        )

        # linguae, f, pl, (Nominative) https://en.wiktionary.org/wiki/lingua
        # pāginārum, f, pl, (Gengitive) https://en.wiktionary.org/wiki/pagina
        # dīvīsiōnibus, f, pl, (Dative) https://en.wiktionary.org/wiki/divisio
        # līmitibus, m, pl, (Dative) https://en.wiktionary.org/wiki/limes#Latin
        # //linguae pāginārum līmitibus//

        # lingua, f, s, (Nominative) https://en.wiktionary.org/wiki/lingua#Latin
        # pāginae, f, s, (Dative) https://en.wiktionary.org/wiki/lingua#Latin
        # dīvīsiōnī, f, s, (Dative) https://en.wiktionary.org/wiki/lingua#Latin
        neo_codex.add_argument(
            '--lingua-divisioni',
            help='For the languages on [1603:1:51], how many divisions ' +
            '(or number of chunks) should be done. 1 means no division.' +
            'If using more than 1, use --lingua-paginae do paginate the ' +
            'Options. Default: 1',
            dest='lingua_divisioni',
            metavar='',
            default="1",
            nargs='?'
        )

        neo_codex.add_argument(
            '--lingua-paginae',
            help='If --lingua-divisioni different from 1, defines which page '
            'of languages to return. Default 1.',
            dest='lingua_paginae',
            metavar='',
            default="1",
            nargs='?'
        )

        parser.add_argument(
            '--verbose',
            help='Verbose output',
            metavar='verbose',
            nargs='?'
        )
        if hxl_output:
            parser.add_argument(
                'outfile',
                help='File to write (if omitted, use standard output).',
                nargs='?'
            )

        # print('oioioi', parser)
        return parser.parse_args()

    # def execute_cli(self, args, stdin=STDIN, stdout=sys.stdout,
    #                 stderr=sys.stderr):
    def execute_cli(self, pyargs, stdin=STDIN, stdout=sys.stdout,
                    stderr=sys.stderr):
        # print('TODO')

        self.pyargs = pyargs

        # cs1603_3_12 = cs1603_3_12()
        cs1603_3_12 = CS1603z3z12()

        # cs1603_3_12 = cs1603_3_12()

        # print('self.pyargs', self.pyargs)

        # cs1603_3_12.est_verbum_limiti(args.verbum_limiti)
        cs1603_3_12.est_resultatum_separato(args.resultatum_separato)
        cs1603_3_12.est_lingua_divisioni(args.lingua_divisioni)
        cs1603_3_12.est_lingua_paginae(
            args.lingua_paginae)

        if self.pyargs.actionem_sparql:
            # print('oi')

            if self.pyargs.query:
                if stdin.isatty():
                    print("ERROR. Please pipe data in. \nExample:\n"
                          "  cat data.txt | {0} --actionem-quod-sparql\n"
                          "  printf \"Q1065\\nQ82151\\n\" | {0} --actionem-quod-sparql"
                          "".format(__file__))
                    return self.EXIT_ERROR

                for line in sys.stdin:
                    codicem = line.replace('\n', ' ').replace('\r', '')
                    # TODO: deal with cases were have more than Qcode
                    cs1603_3_12.est_wikidata_q(codicem)

                quod_query = cs1603_3_12.exportatum_sparql()
                # tabulam_numerae = ['TODO']
                # return self.output(tabulam_numerae)
                return self.output(quod_query)

            if self.pyargs.wikidata_link:
                if stdin.isatty():
                    print("ERROR. Please pipe data in. \nExample:\n"
                          "  cat data.txt | {0} --actionem-sparql --query | {0} --actionem-sparql --wikidata-link\n"
                          "  printf \"Q1065\\nQ82151\\n\" | {0} --actionem-sparql --query | {0} --actionem-sparql --wikidata-link"
                          "".format(__file__))
                    return self.EXIT_ERROR

                full_query = []
                for line in sys.stdin:
                    full_query.append(line)

                wikidata_backend = "https://query.wikidata.org/#"
                quod_query = wikidata_backend + \
                    urllib.parse.quote("".join(full_query).encode('utf8'))

                print(quod_query)
                return self.EXIT_OK

            if self.pyargs.tsv or self.pyargs.csv:
                if stdin.isatty():
                    print("ERROR. Please pipe data in. \nExample:\n"
                          "  cat data.txt | {0} --actionem-sparql --query | {0} --actionem-sparql --tsv\n"
                          "  printf \"Q1065\\nQ82151\\n\" | {0} --actionem-sparql --query | {0} --actionem-sparql --tsv"
                          "".format(__file__))
                    return self.EXIT_ERROR

                full_query = []
                for line in sys.stdin:
                    full_query.append(line)

                sparql_backend = "https://query.wikidata.org/sparql"
                # sparql_backend = "http://localhost:1234/"
                # @see https://stackoverflow.com/questions/10588644

                # https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/en#Supported_formats

                if self.pyargs.tsv:
                    separator = "\t"
                    headers = {'Accept': 'text/tab-separated-values'}
                if self.pyargs.csv:
                    separator = ","
                    headers = {'Accept': 'text/csv'}
                if self.pyargs.hxltm:
                    # headers = {'Accept': 'text/tab-separated-values'}
                    headers = {'Accept': 'text/csv'}

                # TODO: make it configurable via command line
                headers['User-Agent'] = USER_AGENT
                headers['Api-User-Agent'] = USER_AGENT

                payload_query = "".join(full_query)
                # Lets put an sleep, 3 seconds, just in case
                sleep(3)
                r = requests.post(sparql_backend, headers=headers, data={
                    'query': payload_query
                })

                # @TODO: --tsv --hxltm is know to be bugged (not sure if
                #        Wikidata result already skip values)

                if self.pyargs.hxltm:
                    result_string = r.text.strip()

                    # @TODO: this likely to break with fields with newlines.
                    #        however no testing sample exists at the moment.
                    #        Eventually needs be checked.
                    lines = result_string.splitlines()
                    # caput = hxltm_hastag_de_csvhxlated(next(iter(lines)).split(","))
                    caput_crudum = lines.pop(0)
                    # print('caput_crudum', caput_crudum)
                    caput = hxltm_hastag_de_csvhxlated(caput_crudum.split(','))
                    print(separator.join(caput))
                    print("\n".join(lines))

                    # reader = csv.reader(lines, delimiter="\t")
                    # caput = hxltm_hastag_de_csvhxlated(next(reader))
                    # print(separator.join(caput))
                    # for row in reader:
                    #     print(separator.join(row))
                else:
                    print(r.text.strip())

                # TODO: generate explicit error messages and return code
                # print(r.content)
                return self.EXIT_OK

        # if self.pyargs.verbum_simplex:
        #     tabulam_multiplicatio = cs1603_3_12.quod_tabulam_multiplicatio()
        #     return self.output(tabulam_multiplicatio)

        # if self.pyargs.codex_completum:
        #     tabulam_multiplicatio = cs1603_3_12.quod_codex()
        #     return self.output(tabulam_multiplicatio)

        # if self.pyargs.neo_scripturam:
        #     scientia = cs1603_3_12.exportatum_scientia_de_scriptura(
        #         args.neo_scripturam_hxl_selectum)
        #     return self.output(scientia)

        # Let's default to full table
        # tabulam_multiplicatio = cs1603_3_12.quod_codex()
        # return self.output(tabulam_multiplicatio)
        print('unknow option.')
        return self.EXIT_ERROR

    def output(self, output_collectiom):
        for item in output_collectiom:
            # TODO: check if result is a file instead of print

            # print(type(item))
            if isinstance(item, int) or isinstance(item, str):
                print(item)
            else:
                print(self.pyargs.resultatum_separato.join(item))

        return self.EXIT_OK

# if not sys.stdin.isatty():
#     print ("not sys.stdin.isatty")
# else:
#     print ("is  sys.stdin.isatty")

# import fcntl
# import os
# import sys

# # make stdin a non-blocking file
# fd = sys.stdin.fileno()
# fl = fcntl.fcntl(fd, fcntl.F_GETFL)
# fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

# try:
#     print(sys.stdin.read())
# except:
#     print('No input')

# from sys import stdin
# from os import isatty

# is_pipe = not isatty(stdin.fileno())

# print('is_pipe', is_pipe)


if __name__ == "__main__":

    cli_2600 = CLI_2600()
    args = cli_2600.make_args()
    # pyargs.print_help()

    # args.execute_cli(args)
    cli_2600.execute_cli(args)


# import itertools
# valueee = list(permutations([1, 2, 3]))
# valueee = list(permutations([1, 2, 3]))

# print(valueee)

# cs1603_3_12 = cs1603_3_12()

# # print(quod_1613_2_60_datum())
# # print(cs1603_3_12)

# print('0')
# print(cs1603_3_12.quod_numerordinatio_digitalem('0', True))
# print('05')
# print(cs1603_3_12.quod_numerordinatio_digitalem('05', True))
# print('zz')
# print(cs1603_3_12.quod_numerordinatio_digitalem('zz', True))

#!/usr/bin/env python3 # ============================================================================== # #          FILE:  1603_3_12.py # #         USAGE:  ./999999999/0/1603_3_12.py #                 ./999999999/0/1603_3_12.py --help #                 NUMERORDINATIO_BASIM="/dir/ndata" ./999999999/0/1603_3_12.py # #   DESCRIPTION:  --- # #       OPTIONS:  --- # #  REQUIREMENTS:  - python3 #                   - requests[socks] #          BUGS:  --- #         NOTES:  --- #       AUTHORS:  <NAME> <rocha[at]ieee.org> # COLLABORATORS: #                 <@TODO: put additional non-anonymous names here> # #       COMPANY:  EticaAI #       LICENSE:  Public Domain dedication or Zero-Clause BSD #                 SPDX-License-Identifier: Unlicense OR 0BSD #       VERSION:  v0.5.0 #       CREATED:  2022-01-21 17:07 UTC created. Based on 2600.py #      REVISION:  --- # ============================================================================== # pytest #    python3 -m doctest ./999999999/0/1603_3_12.py #    ./999999999/0/1603_3_12.py #    NUMERORDINATIO_BASIM="/external/ndata" ./999999999/0/1603_3_12.py #    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query #    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --wikidata-link #    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --tsv > 999999/0/test.tsv #    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --csv > 999999/0/test.csv #    printf "Q1065\nQ82151\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query | ./999999999/0/1603_3_12.py --actionem-sparql --csv --hxltm # 1603_25_1 query # printf "Q3409626\nQ41055\nQ3321315\nQ160695\nQ9645\nQ9597\nQ713102\nQ133279\n" | ./999999999/0/1603_3_12.py --actionem-sparql --query # SELECT ?pic (STRAFTER(STR(?item), "entity/") AS ?item__conceptum__codicem) ?item__rem__i_lat__is_latn # WHERE # { #   VALUES ?item { wd:Q3409626  wd:Q41055  wd:Q3321315  wd:Q160695  wd:Q9645  wd:Q9597  wd:Q713102  wd:Q133279  } #   bind(xsd:integer(strafter(str(?item), 'Q')) as ?id_numeric) . #   OPTIONAL { ?item wdt:P18 ?pic } #   OPTIONAL { ?item rdfs:label ?item__rem__i_qcc__is_zxxx filter (lang(?item__rem__i_qcc__is_zxxx) = ""). } #   OPTIONAL { ?item rdfs:label ?item__rem__i_lat__is_latn filter (lang(?item__rem__i_lat__is_latn) = "la"). } # } # ORDER BY ASC (?id_numeric) ## Example with proxy # export HTTP_PROXY="socks5://127.0.0.1:9050" # export HTTPS_PROXY="socks5://127.0.0.1:9050" # TODO: https://sinaahmadi.github.io/posts/10-essential-sparql-queries-for-lexicographical-data-on-wikidata.html # from pathlib import Path # Type, # from itertools import permutations # valueee = list(itertools.permutations([1, 2, 3])) # � 1603_3_12.py is (...) # In Python2, sys.stdin is a byte stream; in Python3, it's a text stream # @see https://meta.wikimedia.org/wiki/User-Agent_policy # @see https://www.mediawiki.org/wiki/API:Etiquette # print('getcwd:      ', os.getcwd()) # print('oi', NUMERORDINATIO_BASIM) # def quod_1613_2_60_datum(): #     datum = {} #     with open(NUMERORDINATIO_BASIM + "/1613/1603.2.60.no1.tm.hxl.tsv") as file: #         tsv_file = csv.DictReader(file, delimiter="\t") #         return list(tsv_file) # a b aa bb # printf "30160\n31161\n1830260\n1891267\n" | ./999999999/0/2600.py --actionem-decifram # a aa aaa # printf "30160\n1830260\n109830360\n" | ./999999999/0/2600.py --actionem-decifram # ./999999999/0/1603_3_12.py --actionem-quod-sparql # SELECT ?item ?itemLabel # WHERE { #   # <NAME> or <NAME> #   VALUES ?item { wd:Q1065 wd:Q82151 wd:Q125761 wd:Q7809} #   # mother of #   OPTIONAL { ?item wdt:P25 ?pseudoquery. } #   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # } hxltm_hastag_de_csvhxlated [summary]      Make this type of conversion:     - 'item__conceptum__codicem' => '#item+conceptum+codicem'     - 'item__rem__i_ara__is_arab' => '#item+rem+i_ara+is_arab'     - '' => ''      Args:         csv_caput (list): Array of input items      Returns:         [list]: # https://stackoverflow.com/questions/43258341/how-to-get-wikidata-labels-in-more-than-one-language [summary]      - https://en.wikibooks.org/wiki/SPARQL      [extended_summary] # langpair_full = self._query_linguam() # self.D1613_1_51_langpair = self._query_linguam_limit(langpair_full) # self.scientia_de_scriptura = {} # self.scientia_de_scriptura = self.D1613_2_60 # self.cifram_signaturae = 6  # TODO: make it flexible # self.codex_verbum_tabulae = [] # self.verbum_limiti = 2 # archivum = NUMERORDINATIO_BASIM + "/1613/1603_2_60.no1.tm.hxl.tsv" # archivum = NUMERORDINATIO_BASIM + "/1603/17/2/60/1613_17_2_60.no1.tm.hxl.tsv" # tsv_file = csv.DictReader(file, delimiter="\t") # return list(tsv_file) # print('conceptum', conceptum) # for clavem, rem in enumerate(self.D1613_1_51): # print('clavem rem', clavem, rem) # print(self.D1613_1_51) # print('resultatum', resultatum) # resultatum = [] # @see https://stackoverflow.com/questions/312443 # /how-do-you-split-a-list-into-evenly-sized-chunks # def chunks(lst, n): #     """Yield successive n-sized chunks from lst.""" #     for i in range(0, len(lst), n): #         yield lst[i:i + n] # if langpair_full # def chunks(lst, n): #     """Yield successive n-sized chunks from lst.""" #     for i in range(0, len(lst), n): #         yield lst[i:i + n] # import math # limited = list(chunks(langpair_full, self.lingua_divisioni)) # limited = chunks(langpair_full, self.linguae_limitibus) # raise ValueError(limited_group) # raise ValueError([limited_group, limited]) # # print('resultatum', resultatum) # return resultatum #     def query(self): #         term = """# https://en.wikiversity.org/wiki/Research_in_programming_Wikidata/Countries#List_of_countries # # https://w.wiki/4ij4 # SELECT ?item ?item__eng_latn ?item__rus_cyrl # WHERE # { #   ?item wdt:P31 wd:Q6256. # instance country #   OPTIONAL { #     ?item rdfs:label ?item__eng_latn filter (lang(?item__eng_latn) = "en"). #     ?item rdfs:label ?item__rus_cyrl filter (lang(?item__rus_cyrl) = "ru"). #   } # } #         """ #         return term # SELECT ?item ?item_rem__eng_latn ?item_rem__rus_cyrl # WHERE # { #   VALUES ?item { wd:Q1065 wd:Q82151 wd:Q125761 wd:Q7809 } #   OPTIONAL { #     ?item rdfs:label ?item_rem__eng_latn filter (lang(?item_rem__eng_latn) = "en"). #     ?item rdfs:label ?item_rem__rus_cyrl filter (lang(?item_rem__rus_cyrl) = "ru"). #   } # } # select = '?item ' + " ".join(self._query_linguam()) # select = ['(?item AS ?item__conceptum__codicem)'] # select = [ #     '(STRAFTER(STR(?item), "entity/") AS ?item__conceptum__codicem)', #     '(STRAFTER(STR(?item), "entity/") AS ?item__rem__i_qcc__is_zxxx__ix_wikiq)' # ] # filter_otional.append( #     '?item rdfs:label ?' + #     pair[1] + ' filter (lang(?' + pair[1] + #     ') = "' + pair[0] + '").' # ) # print('select', self.D1613_1_51_langpair) # print('select', select) # print('filter_otional', filter_otional) SELECT {select} WHERE {{   VALUES ?item {{ {qitems} }}   bind(xsd:integer(strafter(str(?item), 'Q')) as ?id_numeric) . {langfilter} }} ORDER BY ASC (?id_numeric) # """.format(qitems = " ".join(self.qid)) # [TRY IT ↗]() # resultatum.append('#TODO') # resultatum.append(str(self.D1613_1_51)) Constructs all the necessary attributes for the Cli object. # self.args = self.make_args() # Posix exit codes # https://en.wikipedia.org/wiki/Code_word # https://en.wikipedia.org/wiki/Coded_set # cōdex verbum tabulae # parser.add_argument( #     '--actionem', #     help='Action to execute. Defaults to codex.', #     # choices=['rock', 'paper', 'scissors'], #     choices=[ #         'codex', #         'fontem-verbum-tabulae', #         'neo-scripturam', #     ], #     dest='actionem', #     required=True, #     default='codex', #     const='codex', #     type=str, #     nargs='?' # ) #item+conceptum+codicem instead ' + #item+code+v_wiki_q', # linguae, f, pl, (Nominative) https://en.wiktionary.org/wiki/lingua # pāginārum, f, pl, (Gengitive) https://en.wiktionary.org/wiki/pagina # dīvīsiōnibus, f, pl, (Dative) https://en.wiktionary.org/wiki/divisio # līmitibus, m, pl, (Dative) https://en.wiktionary.org/wiki/limes#Latin # //linguae pāginārum līmitibus// # lingua, f, s, (Nominative) https://en.wiktionary.org/wiki/lingua#Latin # pāginae, f, s, (Dative) https://en.wiktionary.org/wiki/lingua#Latin # dīvīsiōnī, f, s, (Dative) https://en.wiktionary.org/wiki/lingua#Latin # print('oioioi', parser) # def execute_cli(self, args, stdin=STDIN, stdout=sys.stdout, #                 stderr=sys.stderr): # print('TODO') # cs1603_3_12 = cs1603_3_12() # cs1603_3_12 = cs1603_3_12() # print('self.pyargs', self.pyargs) # cs1603_3_12.est_verbum_limiti(args.verbum_limiti) # print('oi') # TODO: deal with cases were have more than Qcode # tabulam_numerae = ['TODO'] # return self.output(tabulam_numerae) #" # sparql_backend = "http://localhost:1234/" # @see https://stackoverflow.com/questions/10588644 # https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/en#Supported_formats # headers = {'Accept': 'text/tab-separated-values'} # TODO: make it configurable via command line # Lets put an sleep, 3 seconds, just in case # @TODO: --tsv --hxltm is know to be bugged (not sure if #        Wikidata result already skip values) # @TODO: this likely to break with fields with newlines. #        however no testing sample exists at the moment. #        Eventually needs be checked. # caput = hxltm_hastag_de_csvhxlated(next(iter(lines)).split(",")) # print('caput_crudum', caput_crudum) # reader = csv.reader(lines, delimiter="\t") # caput = hxltm_hastag_de_csvhxlated(next(reader)) # print(separator.join(caput)) # for row in reader: #     print(separator.join(row)) # TODO: generate explicit error messages and return code # print(r.content) # if self.pyargs.verbum_simplex: #     tabulam_multiplicatio = cs1603_3_12.quod_tabulam_multiplicatio() #     return self.output(tabulam_multiplicatio) # if self.pyargs.codex_completum: #     tabulam_multiplicatio = cs1603_3_12.quod_codex() #     return self.output(tabulam_multiplicatio) # if self.pyargs.neo_scripturam: #     scientia = cs1603_3_12.exportatum_scientia_de_scriptura( #         args.neo_scripturam_hxl_selectum) #     return self.output(scientia) # Let's default to full table # tabulam_multiplicatio = cs1603_3_12.quod_codex() # return self.output(tabulam_multiplicatio) # TODO: check if result is a file instead of print # print(type(item)) # if not sys.stdin.isatty(): #     print ("not sys.stdin.isatty") # else: #     print ("is  sys.stdin.isatty") # import fcntl # import os # import sys # # make stdin a non-blocking file # fd = sys.stdin.fileno() # fl = fcntl.fcntl(fd, fcntl.F_GETFL) # fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) # try: #     print(sys.stdin.read()) # except: #     print('No input') # from sys import stdin # from os import isatty # is_pipe = not isatty(stdin.fileno()) # print('is_pipe', is_pipe) # pyargs.print_help() # args.execute_cli(args) # import itertools # valueee = list(permutations([1, 2, 3])) # valueee = list(permutations([1, 2, 3])) # print(valueee) # cs1603_3_12 = cs1603_3_12() # # print(quod_1613_2_60_datum()) # # print(cs1603_3_12) # print('0') # print(cs1603_3_12.quod_numerordinatio_digitalem('0', True)) # print('05') # print(cs1603_3_12.quod_numerordinatio_digitalem('05', True)) # print('zz') # print(cs1603_3_12.quod_numerordinatio_digitalem('zz', True))