Package: task3
Parsing xSV with Regex in Python.Module 2: Python. Task 3A+B.PrefaceThis project contains a solution to one of the tasks of the EPAM DevOps Initial Internal Training Course #7 in 2023.
Detailed information about the course, as well as reports on each of the completed tasks (including this one) can be foundhere.As mentioned above, the project contains a solution to task #3A+B as part of module #2 of learning the Python programming language.
Below you will find a detailed description of the task, as well as a brief description of the implementation.Table of ContentsPart A conditionPart B conditionImplementationStructureInstallationUsageLibraryCLIPipes and filesxSV example file generatorShowcaseGeneral ProvisionsPart A conditionsWork with Regex in Python and make a bunch of regex:IPv4 address (special address: private network, CIDR notation);IPv6 address (special address: private network, CIDR notation);IP mask (any length, given length);MAC address (format: general, Linux, Windows, Cisco);domain address (only TLD, first DL, second);email (get login, get domain);URI;URL;SSH Key (private, public);card number;UUID.Part B conditionsThe xSV parsing CLI is supposed to provide the following features:It should work with the canonic file formats CSV, TSV, DSV.Implements import/export data from those file formats:has or doesn't have a header row;filtering:in all rows;in a specific row field by regular expression;in certain rows by particular regular expression;Output file fragment:by row number or row number rangeby column index or column index rangehas different string value format:single stringmulti-stringprocessing files in case of breach of the canonical format (try to get the content as completely as possible)Implements all of the above:by pure python string, module re (regular expression);by using the Python module CSV.Convert CSV to JSON with Python by pure Python module CSV and JSON.The CSV file must contain the following columns:MAC addressHostnameIPv4IPv6Netmask (xxx.xxx.xxx.xxx or xx (CIDR))User login (in epam format)Full usernameEmailSSH private keySSH public keyHost descriptionInstalled app listUUIDImplementationParse_xSVis a Python package that could be added to your global or virtual environment by preferable package manager pip, pipenv, poetry, etc.
The project itself was managed and built using thePoetry library,
so if you intend to clone this repo and make some changes for your own purposes, please installPoetryor migrate to your preferred package management library.Based on the need to build and the possibility of using both the library and the CLI, the code was split into a library for importing and a script for execution
via the command line. Additionally, the package contains a showcase that demonstrates all use cases when run through the command line.To enhance the command line's functionality and expand showcase capabilities, theQuestionarylibrary is used and will be installed through a dependency link upon package installation.In order to help users generate sample xSV files, theFakerlibrary is used and will be installed through a dependency link upon package installation.Structuretask3/
├── README.md (You are here now)
├── pyproject.toml # Poetry package management file
└── parse_xsv/ 
    ├──__init__.py # library entry point
    ├──__main__.py # CLI entry point
    ├──__version__.py 
    ├── parse_xsv.py # library implementation
    ├── cli/
    │   ├──__init__.py
    │   ├──__main__.py
    │   └── cli.py # CLI code implementation
    ├── sample_gen/
    │   ├──__init__.py
    │   ├──__main__.py
    │   └── sample_gen.py # CLI for generation of sample xSV files
    └── showcase/
            ├──__init__.py 
            ├──__main__.py # showcase entry point when using python -m parse_xsv.showcase
            ├── sample.csv # a sample CSV file
            ├── sample.tsv # a sample TSV file
            ├── corrupted_sample.csv # an ill-formed format CSV file
            └── showcase.py # showcase implementationInstallationUse your preferred installation method via different package installation managers to install Parse_xSV.PipTo install Parse_xSV packet to your environment using pip manager invokepip install task3.$pipinstalltask3
Collectingtask3Usingcachedtask3-0.1.N-py3-none-any.whl(62kB)Collectingfaker<20.0.0,>=19.2.0(fromtask3)DownloadingFaker-19.2.0-py3-none-any.whl(1.7MB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━1.7/1.7MBN.NMB/seta0:00:00
Collectingquestionary<2.0.0,>=1.10.0(fromtask3)Downloadingquestionary-1.10.0-py3-none-any.whl(31kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━31.1/31.1kBN.NMB/seta0:00:00
Collectingpython-dateutil>=2.4(fromfaker<20.0.0,>=19.2.0->task3)Downloadingpython_dateutil-2.8.2-py2.py3-none-any.whl(247kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━247.2/247.2kBN.NMB/seta0:00:00Collectingprompt_toolkit<4.0,>=2.0(fromquestionary<2.0.0,>=1.10.0->task3)Downloadingprompt_toolkit-3.0.39-py3-none-any.whl(385kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━385.2/385.2kBN.NMB/seta0:00:00
Collectingwcwidth(fromprompt_toolkit<4.0,>=2.0->questionary<2.0.0,>=1.10.0->task3)Downloadingwcwidth-0.2.6-py2.py3-none-any.whl(29kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━29.2/29.2kBN.NMB/seta0:00:00
Collectingsix>=1.5(frompython-dateutil>=2.4->faker<20.0.0,>=19.2.0->task3)Downloadingsix-1.16.0-py2.py3-none-any.whl(11kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━11.8/11.8kBN.NMB/seta0:00:00Installingcollectedpackages:wcwidth,six,prompt_toolkit,questionary,python-dateutil,faker,task3
Successfullyinstalledfaker-19.2.0prompt_toolkit-3.0.39python-dateutil-2.8.2questionary-1.10.0six-1.16.0task3-0.1.4wcwidth-0.2.6To uninstall Pwgen from your environment invokepip uninstall task3.
It's important to note that the pip manager does not uninstall dependent packages. Therefore, if you wish to remove them, you'll need to take
the initiative and perform the task yourself. You can do this by using the commandspip uninstall questionary prompt-toolkit faker python-dateutil wcwidth six.PoetryTo install Pwgen packet to your environment using poetry manager invokepoetry add task3.$poetryaddtask3
Usingversion^0.1.Nfortask3

Updatingdependencies
Resolvingdependencies...

Packageoperations:6installs,0updates,0removals•Installingsix(1.16.0)•Installingwcwidth(0.2.6)•Installingprompt-toolkit(3.0.39)•Installingpython-dateutil(2.8.2)•Installingfaker(19.2.0)•Installingquestionary(1.10.0)WritinglockfileBy taking this action, a new dependency line will be added to your pyproject.toml file.[tool.poetry.dependencies]task3="^0.1.N"To uninstall Pwgen from your environment invokepoetry remove task3.
One of the benefits of utilizing Poetry is that it allows for the removal of all dependent packages with a single command.UsageThere are various ways to use this library, as mentioned earlier.Utilize it like a library you can just import it into your .py file and use ReaderXSV class within your code.Utilize CLI via the command shell, either as a Python module or as a standalone command.Utilize CLI command in a pipe by passing stdout of other commands to the stdin of the parse_xsv command, writing stdout and stderr to files, or passing them to following commands.The library contains CLI for generation of sample xSV files that allows to make sample files for a test purposes.The library also contains rich showcase command that allows you to test all the use cases and even perform them in batches.LibraryBelow is a code snippet that demonstrates how to be able to use the pwgen library in your code.importloggingfromtask3.parse_xsvimportReaderXSVwithopen('sample.csv','r')asf:output=list(ReaderXSV(file_name,no_header=self.arguments.no_header,search_string=self.arguments.search,search_regex=s_regex,column_regex=c_regex,rows=self.arguments.rows,columns=self.arguments.columns,restkey='rest',delimiter=self.arguments.delimiter,dialect=self.arguments.dialect,quoting=QuotingTypes[self.arguments.quoting].value,json=self.arguments.json,strict=notself.arguments.force))output_file=[','.join([quoting_value(el)forelinln])forlninoutput]output_file='\n'.join(output_file)print(output_file)CLIThe CLI interface has a single command called "parse_xsv". It can be invoked using two methods:python3 -m parse_xsvor simplyparse_xsv.
Parse_xsv accepts various arguments, which are described below.parse_xsv [-h] [--version] [-d] [-D] [-j] [-p] [-q] [--no-header] [--force] [-v]
          [-c [INDEXES ...]] [-r [INDEXES ...]]
          [-C [REGEX ...] | -s SEARCH | -S REGEX] [file_name]

positional arguments:
file_name             An input filename

optional arguments:
-h, --help            Show this help message and exit
--version             Show program's version number and exit
-d , --delimiter      Specifies values separator (default: ",")
-D , --dialect        The xsv dialect type. Possible values: excel,excel-tab,unix
-j, --json            Show output in JSON format. If filename is passed
-q , --quoting        How values are quoted in the CSV file (default: QUOTE_MINIMAL).
                      Possible values could be: QUOTE_NONE, QUOTE_NONNUMERIC,
                      QUOTE_MINIMAL, QUOTE_ALL
--no-header           Whether parsing CSV file(s) contains a header or not
--force               Forcibly process an ill-formed format input file
-v                    Increase verbosity level (add more v)
-c [INDEXES ...], --columns [INDEXES ...]
                      The column range from the xCV file to be output. 
                      You can pass values in the following formats:
                      particular indexes: index1 index2 ...
                      indexN range of indexes: index1-index2
                      from the beginning up to index: -index
                      from index to the end: index-                      
-r [INDEXES ...], --rows [INDEXES ...]
                      The row range from the xCV file to be output. 
                      You can pass values in the following formats: 
                      particular indexes: index1 index2 ...
                      indexN range of indexes: index1-index2 
                      from the beginning up to index: -index
                      from index to the end: index-                      
-C [REGEX ...], --col-regex [REGEX ...]
                      Find rows based on a given regex string in specific columns.
                      You can pass values in the following formats:
                      particular indexes: index1,regex1 index2,regex2 ... indexN,
                      regexN range of indexes: index1-index2,regex 
                      from the beginning up to index: -index,regex
                      from index to the end: index-,regex
                      Possible values could be: IP4, IP4_PRIVATE, IP4_CIDR, IP6,
                      IP6_PRIVATE, IP6_CIDR, MASK, MAC, MAC_LINUX, MAC_WINDOWS,
                      MAC_CISCO, DOMAIN, TLD, DOMAIN_SECOND, EMAIL, URI, URL, SSH_KEY,
                      SSH_PRIVATE, SSH_PUBLIC, CARD, UUID                      
-s, --search          Find rows based on a given search string
-S REGEX, --search-regex REGEX
                      Find rows based on a given regex. Possible values could be:
                      IP4, IP4_PRIVATE, IP4_CIDR, IP6, IP6_PRIVATE, IP6_CIDR, MASK,
                      MAC, MAC_LINUX, MAC_WINDOWS, MAC_CISCO, DOMAIN, TLD, DOMAIN_SECOND,
                      EMAIL, URI, URL, SSH_KEY, SSH_PRIVATE, SSH_PUBLIC, CARD, UUIDThere are some rules for argument handling:if you pass argument -j, the output will be in JSON format. This will prevent further use of this command in a pipe
with the following parse_xsv commands. Without this argument the output will be in the xSV format.the -C, -s, -S arguments are mutual. These arguments cannot be used together, they are mutually exclusive.To handle the arguments, theargparsemodule is used. If you are already acquainted with it, you will have no difficulty in passing the arguments along with their values and comprehending their behavior.The examples in a more convenient form you could find in theshowcase.Pipes and filesThe parse_xsv command could be used inside the pipe of the BASH commands. It can be used in various ways within a pipeline:receiving inputdirect output to a filedirect logging also to a filedirect output to the next command in the pipelineusing a sequence of the parse_xsv command mainly to put on different filters in a row on the same filexSV example file generatorIn order to create sample xSV files for a test purposes of parse_xsv CLI the xsv_sample_gen CLI was created.
The CLI interface has a single command called "xsv_sample_gen". It can be invoked using two methods:python3 -m xsv_sample_genor simplyxsv_sample_gen.
Parse_xsv accepts various arguments, which are described below. The common way of using this command is to pass the list
(separated by space) of column types with a header which will be used to create the xSV file structure.
To fill this file with mock values the externalFakerlibrary is used.sample_gen [-h] [--version] [-d] [-D] [-q] [--no-header] [-i] [-v] [-r] structure [structure ...]

positional arguments:
  structure          The list of column names and column types to be used as output file structure.
                     You can pass values in the following formats: 
                     just a type (column name will be index number): type1 type2 ... typeN
                     column name + type: column_name1,typeN column_name2,typeN ...
                     column_nameN,typeN.
                     Possible type values could be: address, administrative_unit, bothify,
                     building_number, city, city_prefix, city_suffix, country, country_code,
                     current_country, current_country_code, hexify, language_code, lexify,
                     locale, military_apo, military_dpo, military_ship, military_state,
                     numerify, postalcode, postalcode_in_state, postalcode_plus4, postcode,
                     postcode_in_state, random_choices, random_digit, random_digit_above_two,
                     random_digit_not_null, random_digit_not_null_or_empty, random_digit_or_empty,
                     random_element, random_elements, random_int, random_letter, random_letters,
                     random_lowercase_letter, random_number, random_sample,
                     random_uppercase_letter, randomize_nb_elements, secondary_address, state,
                     state_abbr, street_address, street_name, street_suffix, zipcode,
                     zipcode_in_state, zipcode_plus4, license_plate, vin, aba, bank_country,
                     bban, iban, swift, swift11, swift8, ean, ean13, ean8, localized_ean,
                     localized_ean13, localized_ean8, upc_a, upc_e, color, color_name, hex_color,
                     rgb_color, rgb_css_color, safe_color_name, safe_hex_color, bs, catch_phrase,
                     company, company_suffix, credit_card_expire, credit_card_full, credit_card_number,
                     credit_card_provider, credit_card_security_code, cryptocurrency,
                     cryptocurrency_code, cryptocurrency_name, currency, currency_code,        
                     currency_name, currency_symbol, pricetag, am_pm, century, date, date_between,
                     date_between_dates, date_object, date_of_birth, date_this_century, date_this_decade,
                     date_this_month, date_this_year, date_time, date_time_ad, date_time_between,
                     date_time_between_dates, date_time_this_century, date_time_this_decade,
                     date_time_this_month, date_time_this_year, day_of_month, day_of_week, future_date,
                     future_datetime, iso8601, month, month_name, past_date, past_datetime, pytimezone,
                     time, time_delta, time_object, time_series, timezone, unix_time, year, emoji,
                     file_extension, file_name, file_path, mime_type, unix_device, unix_partition,  
                     coordinate, latitude, latlng, local_latlng, location_on_land, longitude,
                     ascii_company_email, ascii_email, ascii_free_email, ascii_safe_email, company_email,
                     dga, domain_name, domain_word, email, free_email, free_email_domain, hostname,
                     http_method, iana_id, image_url, ipv4, ipv4_network_class, ipv4_private, ipv4_public,
                     ipv6, mac_address, nic_handle, nic_handles, port_number, ripe_id,
                     safe_domain_name, safe_email, slug, tld, uri, uri_extension, uri_page, uri_path, url, user_name, isbn10, isbn13, job, paragraph,
                     paragraphs, sentence, sentences, text, texts, word, words, binary, boolean, csv, dsv, fixed_width, json, json_bytes, md5, null_boolean,    
                     password, psv, sha1, sha256, tar, tsv, uuid4, zip, passport_dates, passport_dob, passport_full, passport_gender, passport_number,
                     passport_owner, first_name, first_name_female, first_name_male, first_name_nonbinary, language_name, last_name, last_name_female,
                     last_name_male, last_name_nonbinary, name, name_female, name_male, name_nonbinary, prefix, prefix_female, prefix_male, prefix_nonbinary,   
                     suffix, suffix_female, suffix_male, suffix_nonbinary, basic_phone_number, country_calling_code, msisdn, phone_number, profile,
                     simple_profile, enum, pybool, pydecimal, pydict, pyfloat, pyint, pyiterable, pylist, pyobject, pyset, pystr, pystr_format, pystruct,       
                     pytuple, sbn9, ein, invalid_ssn, itin, ssn, android_platform_token, chrome, firefox, internet_explorer, ios_platform_token,
                     linux_platform_token, linux_processor, mac_platform_token, mac_processor, opera, safari, user_agent, windows_platform_token

optional arguments:
  -h, --help         show this help message and exit
  --version          show program's version number and exit
  -d , --delimiter   Specifies values separator (default: ",")
  -D , --dialect     The xsv dialect type. Possible values: excel,excel-tab,unix,faker-csv
  -q , --quoting     How values are quoted in the CSV file (default: QUOTE_MINIMAL). Possible values could be: QUOTE_NONE, QUOTE_NONNUMERIC, QUOTE_MINIMAL,     
                     QUOTE_ALL
  --no-header        Whether generated CSV file contains a header or not
  -i, --ill-formed   Forcibly generate an ill-formed format file
  -v                 Increase verbosity level (add more v)
  -r , --rows        Number of rows to be generated

This generator generates an xSV file an print it to stdout.
Mainly it will be displayed on your monitor screen.
If you want ot save generated data to file, please use > operator or pipe |.ShowcaseTo showcase the behavior of the parse_xsv library, an interactive command called "parse_xsv_showcase" has been created.
This command utilizes both the parse_xsv CLI and the parse_xsv library. It's an interactive command you can invoke viaparse_xsv_showcaseorpython3 -m parse_xsv.showcase.
It has an optional flag that allows you to view all use cases at once without any interaction.
You can use the commandparse_xsv_showcase --allto activate this feature.
There are also ready-made xSV files present -sample.csv,sample.tsvandcorrupted_sample.csv,
but you can use theхsv_sample_gencommand for this purpose.General provisionsAll materials provided and/or made available contain EPAM’s proprietary and confidential information and must not to be copied,
reproduced or disclosed to any third party or to any other person, other than those persons who have a bona fide need to review it
for the purpose of participation in the online courses being provided by EPAM.
The intellectual property rights in all materials (including any trademarks) are owned by EPAM Systems Inc or its associated companies,
and a limited license, terminable at the discretion of EPAM without notice, is hereby granted to you solely for the purpose of participating
in the online courses being provided by EPAM. Neither you nor any other party shall acquire any intellectual property rights of any kind
in such materials.