Skip to content

Commit

Permalink
refactor: backup
Browse files Browse the repository at this point in the history
  • Loading branch information
badele committed Nov 21, 2023
1 parent 872ac4b commit 41c75f7
Show file tree
Hide file tree
Showing 9 changed files with 737 additions and 312 deletions.
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,52 @@ tabular data.

## Example

### Command line

```shell
deno run -A mod.ts dataset init -c /tmp/config.json

deno run -A mod.ts dataset set \
-c /tmp/config.json \
-t " ATP Tour" \
-d "$(cat << EOD
Here is a list of the best tennis players in the ATP rankings
from 2012 to 2022, as well as the list of winners of the
4 major Grand Slam tournaments.
EOD
)
" \
-m '{ "sources": [ "https://github.com/JeffSackmann/tennis_atp" ]}'

deno run -A mod.ts table add \
-c /tmp/config.json \
-f ./samples/players.csv \
-t players \
-s "The best players (number of winning matches) beetween 2012-2022" \
-l "winner_ioc,name_first,name_last,hand,height,birth,nbwins" \
-a "left,right,left,center,right,right,right" \
-m '{ "sources": [ "https://github.com/JeffSackmann/tennis_atp" ]}'

deno run -A mod.ts table add \
-c /tmp/config.json \
-f ./samples/australian_open.csv \
-t "australian_open" \
-s "Australian Open winners beetween 2012-2022" \
-d "$(cat << EOD
The Australian Open is a tennis tournament held annually at Melbourne Park
in Melbourne, Victoria, Australia. The tournament is the first of the
four Grand Slam tennis events held each year.
EOD
)
" \
-l "year,tourney_name,winner_ioc,winner_name" \
-a "left,right,left,center,right,right,right" \
-m '{ "sources": [ "https://github.com/JeffSackmann/tennis_atp" ]}'

deno run -A mod.ts dataset save -c /tmp/config.json -s samples-generated.cfwf

```

### Code

```typescript
Expand Down Expand Up @@ -56,7 +102,7 @@ samples.addArray(
"The best players (number of winning matches) beetween 2012-2022 ",
"",
players.columns,
players.values,
players.rows,
{
aligns: ["left", "right", "left", "center", "right", "right", "right"],
sources: "https://github.com/JeffSackmann/tennis_atp",
Expand All @@ -70,7 +116,7 @@ samples.addArray(
in Melbourne, Victoria, Australia. The tournament is the first of the \n\
four Grand Slam tennis events held each year.",
["year", "tourney name", "birth nat", "winner"],
australian.values,
australian.rows,
{
aligns: ["right", "left", "center", "center"],
sources: [
Expand Down Expand Up @@ -139,6 +185,11 @@ year tourney name birth nat winner
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```

## TODO

- YAML literal flow for some entries
- Keep YAML generator file .yaml.conf

## Sample dataset

The samples dataset provided by the
Expand Down
202 changes: 202 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,205 @@
export * from "./src/cfwf.ts";
export * from "./src/table.ts";
export * from "./src/types.ts";

import { CFWFDataset } from "./src/types.ts";
import { version } from "./src/version.ts";
import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.3/command/mod.ts";
import { existsSync } from "https://deno.land/std@0.205.0/fs/exists.ts";
import { parseCSV } from "./src/utils.ts";
import { CFWF } from "./src/cfwf.ts";

// deno-lint-ignore no-explicit-any
function initDatasetFile(options: any): void {
const cfwf: CFWFDataset = {
dataset: {
metadatas: {},
},
tables: {},
};

Deno.writeTextFileSync(options.configname, JSON.stringify(cfwf));
}

function readConfig(configname: string): CFWFDataset {
if (existsSync(configname) === false) {
return {};
}

return JSON.parse(Deno.readTextFileSync(configname));
}

// deno-lint-ignore no-explicit-any
async function saveCFWF(options: any): Promise<void> {
const config = readConfig(options.configname);

const cfwf = new CFWF(config);
console.log(await cfwf.toCFWF(options.saveto));
}

// deno-lint-ignore no-explicit-any
function setDatasetProperties(options: any): void {
const cfwf: CFWFDataset = readConfig(options.configname) || {};
cfwf.dataset = cfwf.dataset || {};
cfwf.dataset.metadatas = cfwf.dataset.metadatas || {};

if (options.title) {
cfwf.dataset.title = options.title;
cfwf.dataset.metadatas.font = options.font || "doom";
cfwf.dataset.metadatas.removetitlelines = options.removetitlelines || 2;
}
if (options.description) {
cfwf.dataset.description = options.description;
}
if (options.generatedtitle) {
cfwf.dataset.generatedtitle = options.generatedtitle;
}

if (options.metadatas) {
Object.assign(cfwf.dataset.metadatas, JSON.parse(options.metadatas));
}

Deno.writeTextFileSync(options.configname, JSON.stringify(cfwf));
}

// deno-lint-ignore no-explicit-any
function addTable(options: any): void {
const cfwf: CFWFDataset = readConfig(options.configname) || {};
cfwf.tables = cfwf.tables || {};

if (!options.tablename) {
throw new Error("No table name");
}

const table = cfwf.tables[options.tablename] || {
columns: [],
rows: [],
};

table.columns = table.columns || [];
table.rows = table.rows || [];
table.metadatas = table.metadatas || {};

if (options.columns) {
table.columns = options.columns;
}

if (options.filename) {
const content = Deno.readTextFileSync(options.filename);
const datas = parseCSV(content);

if (table.columns && table.columns.length === 0) {
table.columns = datas.columns;
}
table.rows = datas.rows;
}

if (options.subtitle) {
table.subtitle = options.subtitle;
}

if (options.description) {
table.description = options.description;
}

if (options.metadatas) {
Object.assign(
// deno-lint-ignore no-explicit-any
table.metadatas as Record<string, any>,
JSON.parse(options.metadatas),
);
}

if (options.aligns) {
table.metadatas.aligns = options.aligns;
}

cfwf.tables[options.tablename] = table;

Deno.writeTextFileSync(options.configname, JSON.stringify(cfwf));
}

if (import.meta.main) {
const cmdtable = new Command()
.description("Configure table")
// Add table
.command("add", "Add table configuration")
.option("-c, --configname <configname:file>", "Config filenme", {
required: true,
})
.option("-f, --filename <filename:file>", "File to be load", {
required: true,
})
.option("-t, --tablename <tablename:string>", "Define table name", {
required: true,
})
.option("-s, --subtitle <subtitle:string>", "Define table subtitle", {})
.option("-l, --columns <column:string[]>", "Columns name", { default: [] })
.option("-a, --aligns <column:string[]>", "Aligns for each columns", {
required: true,
})
.option(
"-d, --description <description:string>",
"Define table description",
{},
)
.option(
"-m, --metadatas <metadatas:string>",
"Define dataset metadatas (JSON format)",
{},
)
.action((options) => addTable(options));

const cmddataset = new Command()
.description("Configure dataset")
.command("set", "Set dataset properties")
.option("-c, --configname <configname:file>", "Config filenme", {
required: true,
})
.option("-f, --font <title:string>", "Define title font", {
default: "doom",
})
.option("-t, --title <title:string>", "Define dataset title", {})
.option(
"-d, --description <title:string>",
"Define dataset description",
)
.option(
"-g, --generatedtitle <generated:string>",
"Generated title",
{ conflicts: ["title", "font"] },
)
.option(
"-m, --metadatas <metadatas:string>",
"Define dataset metadatas (JSON format)",
{},
)
.action((options) => setDatasetProperties(options))
// Init
.command("init", "Init dataset file")
.option("-c, --configname <configname:file>", "Config filenme", {
required: true,
})
.action((options) => initDatasetFile(options))
// Save
.command("save", "Save dataset to cfwf format file")
.option("-c, --configname <configname:file>", "Config filenme", {})
.option("-s, --saveto <saveto:file>", "Save to filenme", {
required: true,
})
.action((options) => saveCFWF(options));

await new Command()
.name("cfwf")
.version(version)
.description("CFWF Command line")
.command(
"dataset",
cmddataset,
)
.command(
"table",
cmdtable,
)
.parse(Deno.args);
}
78 changes: 56 additions & 22 deletions samples.cfwf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from 2012 to 2022, as well as the list of winners of the
┈┈┈

players
The best players (number of winning matches) beetween 2012-2022
The best players (number of winning matches) beetween 2012-2022

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
winner_ioc name_first name_last hand height birth nbwins
Expand Down Expand Up @@ -106,7 +106,7 @@ in the world and is regarded by many as the most prestigious.
It has been held at the All England Lawn Tennis and Croquet Club in Wimbledon, London, since 1877

wimbledon
wimbledon winners beetween 2012-2022
Wimbledon winners beetween 2012-2022

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
year tourney name birth nat winner
Expand All @@ -123,24 +123,58 @@ year tourney name birth nat winner
2012 Wimbledon SUI Roger Federer
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

_infos_:
font: doom
generated_with: 'Generated with https://github.com/badele/cfwf@0.0.1'
removetitlelines: 2
sources: ['https://github.com/JeffSackmann/tennis_atp']
dataset:
title: ' ATP Tour'
australian_open:
aligns: [right, left, center, center]
sources: ['https://fr.wikipedia.org/wiki/Open_d%27Australie', 'https://github.com/JeffSackmann/tennis_atp']
players:
aligns: [left, right, left, center, right, right, right]
sources: 'https://github.com/JeffSackmann/tennis_atp'
roland_garros:
aligns: [right, left, center, center]
sources: ['https://fr.wikipedia.org/wiki/Internationaux_de_France_de_tennis', 'https://github.com/JeffSackmann/tennis_atp']
us_open:
aligns: [right, left, center, center]
sources: ['https://fr.wikipedia.org/wiki/US_Open_de_tennis', 'https://github.com/JeffSackmann/tennis_atp']
wimbledon:
aligns: [right, left, center, center]
sources: ['https://fr.wikipedia.org/wiki/Tournoi_de_Wimbledon', 'https://github.com/JeffSackmann/tennis_atp']
metadatas:
sources:
- 'https://github.com/JeffSackmann/tennis_atp'
generated_with: 'cfwf@0.0.1 - https://github.com/badele/cfwf'
tables:
players:
metadatas:
aligns:
- left
- right
- left
- center
- right
- right
- right
sources:
- 'https://github.com/JeffSackmann/tennis_atp'
australian_open:
metadatas:
aligns:
- right
- left
- center
- center
sources:
- 'https://fr.wikipedia.org/wiki/Open_d%27Australie'
roland_garros:
metadatas:
aligns:
- right
- left
- center
- center
sources:
- 'https://fr.wikipedia.org/wiki/Internationaux_de_France_de_tennis'
us_open:
metadatas:
aligns:
- right
- left
- center
- center
sources:
- 'https://fr.wikipedia.org/wiki/US_Open_de_tennis'
wimbledon:
metadatas:
aligns:
- right
- left
- center
- center
sources:
- 'https://fr.wikipedia.org/wiki/Tournoi_de_Wimbledon'
Loading

0 comments on commit 41c75f7

Please sign in to comment.