Compare commits

...

13 Commits

Author SHA1 Message Date
2edd8a2093 Bumped package version
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-23 14:56:48 -05:00
7a400457fe Fixed double line breaks in output on Windows
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-23 14:56:29 -05:00
59cfe486aa Bumped package version
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-23 14:44:03 -05:00
266a611fea Fixed inconsistent CSV writing function
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-23 14:42:44 -05:00
cb36b8adb3 Version bump
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-21 15:56:08 -05:00
ded60aa742 Added step to test if command is runnable
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-21 15:53:07 -05:00
adf734f3c1 Added feature to add basename column to output
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-21 15:52:20 -05:00
c579c172ef Bumped version number
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-21 13:55:57 -05:00
e5bab5b12d Fixed packaging
Some checks failed
ydeng/csvbyname/pipeline/head There was a failure building this commit
2023-04-21 13:51:12 -05:00
73ae49cb89 Bumped packacge version number
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-21 11:21:03 -05:00
958e2b12e3 Added 'archive' stage to Jenkins pipeline
All checks were successful
ydeng/csvbyname/pipeline/head This commit looks good
2023-04-21 11:20:03 -05:00
425ef96e9b Updated '.gitignore' to ignore 'output.csv' 2023-04-21 11:19:46 -05:00
95b60c87a5 Bumped python version in 'environment.yml' 2023-04-21 11:19:27 -05:00
7 changed files with 128 additions and 99 deletions

1
.gitignore vendored
View File

@@ -212,3 +212,4 @@ pyrightconfig.json
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option) # Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
output.csv

5
.vscode/launch.json vendored
View File

@@ -8,19 +8,20 @@
"name": "Use Test Resources", "name": "Use Test Resources",
"type": "python", "type": "python",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/csvbyname/csvbyname.py",
"console": "integratedTerminal", "console": "integratedTerminal",
"args": [ "args": [
"${workspaceFolder}/tests/resources", "${workspaceFolder}/tests/resources",
"${workspaceFolder}/output.csv", "${workspaceFolder}/output.csv",
"-r", "-r",
"-n",
"-p", "-p",
"group_num:group(\\d)-\\w-\\d+\\.txt", "group_num:group(\\d)-\\w-\\d+\\.txt",
"group(\\d)-(?P<sect>\\w)-(?P<patid>\\d+)\\.txt", "group(\\d)-(?P<sect>\\w)-(?P<patid>\\d+)\\.txt",
"-V", "-V",
"DEBUG" "DEBUG"
], ],
"justMyCode": true "justMyCode": true,
"module": "csvbyname.cli"
} }
] ]
} }

8
Jenkinsfile vendored
View File

@@ -20,9 +20,15 @@ pipeline {
stage("test") { stage("test") {
steps { steps {
sh "pip install dist/*.whl" sh "pip install dist/*.whl"
sh "csvbyname -h"
} }
} }
stage("publish") { stage("archive") {
steps {
archiveArtifacts artifacts: 'dist/*.tar.gz, dist/*.whl'
}
}
stage("publish package") {
when { when {
branch '**/main' branch '**/main'
} }

95
csvbyname/cli.py Normal file
View File

@@ -0,0 +1,95 @@
import argparse
import logging
from csvbyname.generate import collect_files, write_collected_to_csv
def run(args):
logger.info('Collecting files from "%s"', args.directory)
collected, pkeys = collect_files(
args.directory,
args.include_folders,
args.entire_path,
args.recursive,
args.add_re_property,
)
write_collected_to_csv(args.output, collected, pkeys, args.output_basename)
def main():
argparser = argparse.ArgumentParser(
"csvbyname",
description="Catalogue a directory of files by patterns in their names into a "
"CSV.",
)
argparser.add_argument(
"directory",
type=str,
help="The directory containing the files to obtain catalogue names of",
metavar="i",
)
argparser.add_argument(
"output", type=str, help="The path to the catalogued CSVs.", metavar="o"
)
argparser.add_argument(
"-l",
"--include-folders",
help="Include folders in the cataloguing process",
action="store_true",
required=False,
default=False,
)
argparser.add_argument(
"-e",
"--entire-path",
help="Include the full path when applying the groupings to find properties",
action="store_true",
required=False,
default=False,
)
argparser.add_argument(
"-r",
"--recursive",
help="Catalogue recursively",
action="store_true",
required=False,
default=False,
)
argparser.add_argument(
"-p",
"--add-re-property",
help="Add a property in the resulting CSV obtained from the first capture "
"group of the given REGEX in the following format:\n property-name:regex.\n"
"Alternatively, use named REGEX groups.",
nargs="+",
type=str,
required=True
)
argparser.add_argument(
"-n",
"--output-basename",
help='Adds a column called "basename" to the resulting CSV where it is just '
"The base name of the path instead of the entire path. This is not guaranteed "
"to be unique.",
default=False,
required=False,
action="store_true",
)
argparser.add_argument(
"-V",
"--verbosity",
help="Set the verbosity of the logging",
type=str,
required=False,
default="INFO",
)
args = argparser.parse_args()
logging.basicConfig(level=args.verbosity.upper())
global logger
logger = logging.getLogger(__name__)
run(args)
if __name__ == "__main__":
main()

View File

@@ -1,11 +1,12 @@
import argparse
import csv import csv
import os import os
import re import re
from typing import Iterable from typing import Iterable
import exceptions from csvbyname import exceptions
import logging import logging
logger = logging.getLogger(__name__)
def matcher(full_path: str, use_full_path: bool, regex_groups: list[str]): def matcher(full_path: str, use_full_path: bool, regex_groups: list[str]):
captured_properties = {} captured_properties = {}
@@ -71,100 +72,24 @@ def collect_files(
def write_collected_to_csv( def write_collected_to_csv(
output_path: str, collected: dict[str, dict[str, str]], property_keys: Iterable[str] output_path: str,
collected: dict[str, dict[str, str]],
property_keys: Iterable[str],
output_basename: bool,
): ):
with open(output_path, "w") as output_fd: with open(output_path, "w", newline="", encoding="utf-8") as output_fd:
s_property_keys = sorted(property_keys) s_property_keys = sorted(property_keys)
header = ["path", *s_property_keys] header = ["path"]
if output_basename:
header.append("basename")
header.extend(s_property_keys)
writer = csv.writer(output_fd) writer = csv.writer(output_fd)
writer.writerow(header) writer.writerow(header)
for full_path, properties in collected.items(): for full_path, properties in collected.items():
writer.writerow( row = [full_path]
[ if output_basename:
full_path, row.append(os.path.basename(full_path))
*( row.extend(
properties[k] if k in properties else "N/A" (properties[k] if k in properties else "N/A" for k in s_property_keys)
for k in s_property_keys
),
]
) )
writer.writerow(row)
def run(args):
logger.info('Collecting files from "%s"', args.directory)
collected, pkeys = collect_files(
args.directory,
args.include_folders,
args.entire_path,
args.recursive,
args.add_re_property,
)
write_collected_to_csv(args.output, collected, pkeys)
def main():
argparser = argparse.ArgumentParser(
"csvbyname",
description="Catalogue a directory of files by patterns in their names into a "
"CSV.",
)
argparser.add_argument(
"directory",
type=str,
help="The directory containing the files to obtain catalogue names of",
metavar="i",
)
argparser.add_argument(
"output", type=str, help="The path to the catalogued CSVs.", metavar="o"
)
argparser.add_argument(
"-l",
"--include-folders",
help="Include folders in the cataloguing process",
action="store_true",
required=False,
default=False,
)
argparser.add_argument(
"-e",
"--entire-path",
help="Include the full path when applying the groupings to find properties",
action="store_true",
required=False,
default=False,
)
argparser.add_argument(
"-r",
"--recursive",
help="Catalogue recursively",
action="store_true",
required=False,
default=False,
)
argparser.add_argument(
"-p",
"--add-re-property",
help="Add a property in the resulting CSV obtained from the first capture "
"group of the given REGEX in the following format:\n property-name:regex.\n"
"Alternatively, use named REGEX groups.",
nargs="+",
type=str,
)
argparser.add_argument(
"-V",
"--verbosity",
help="Set the verbosity of the logging",
type=str,
required=False,
default="INFO",
)
args = argparser.parse_args()
logging.basicConfig(level=args.verbosity.upper())
global logger
logger = logging.getLogger(__name__)
run(args)
if __name__ == "__main__":
main()

View File

@@ -5,4 +5,4 @@ dependencies:
- build=0.7.0 - build=0.7.0
- pytest=7.2.2 - pytest=7.2.2
- twine=4.0.2 - twine=4.0.2
- python=3.9 - python=3.11

View File

@@ -1,10 +1,11 @@
[metadata] [metadata]
name = csvbyname name = csvbyname
version = 0.0.1 version = 0.0.6
author = Harrison
[options] [options]
packages = csvbyname packages = csvbyname
[options.entry_points] [options.entry_points]
console_scripts = console_scripts =
csvbyname = csvbyname.csvbyname:main csvbyname = csvbyname.cli:main