Skip to content

Commit

Permalink
feat: Added english Uni2Work rating files support
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkoelle committed Oct 26, 2021
1 parent 2e95137 commit 79b0971
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 22 deletions.
15 changes: 10 additions & 5 deletions app/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'electron';
import { normalize } from 'normalizr';
import Correction from './model/Correction';
import Parser, { ParserType } from './parser/Parser';
import Parser from './parser/Parser';
import instanciateParser from './parser/ParserUtil';
import {
IMPORT_CONFLICTS,
Expand All @@ -27,6 +27,7 @@ import {
getAllFilesInDirectory,
} from './utils/FileAccess';
import { CorrectionSchema } from './model/NormalizationSchema';
import ParserType from './parser/ParserType';

export interface ImportProgress {
name: string;
Expand Down Expand Up @@ -162,7 +163,8 @@ export default class Importer {
const content = fs.readFileSync(importPath, Importer.ENCODING);
const correction: Correction = parser.deserialize(
content,
Path.basename(Path.dirname(importPath))
Path.basename(Path.dirname(importPath)),
Path.basename(importPath)
);

// Were the submissions already imported?
Expand Down Expand Up @@ -224,7 +226,8 @@ export default class Importer {
const content = zipEntry.getData().toString(Importer.ENCODING);
const correction: Correction = parser.deserialize(
content,
Path.basename(Path.dirname(zipEntry.entryName))
Path.basename(Path.dirname(zipEntry.entryName)),
Path.basename(zipEntry.entryName)
);

// Were the submissions already imported?
Expand Down Expand Up @@ -381,7 +384,8 @@ export default class Importer {
const parser: Parser = instanciateParser(c.parser);
const correction: Correction = parser.deserialize(
content,
Path.basename(Path.dirname(c.path))
Path.basename(Path.dirname(c.path)),
Path.basename(c.path)
);
corrections.push(
this.ingestCorrectionFromZip(
Expand All @@ -407,7 +411,8 @@ export default class Importer {
const parser: Parser = instanciateParser(c.parser);
const correction: Correction = parser.deserialize(
content,
Path.basename(Path.dirname(c.path))
Path.basename(Path.dirname(c.path)),
Path.basename(c.path)
);
corrections.push(
this.ingestCorrectionFromFolder(
Expand Down
2 changes: 1 addition & 1 deletion app/modals/ImportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import {
import { CorrectionsSchema } from '../model/NormalizationSchema';
import { loadCorrections } from '../model/CorrectionsSlice';
import CircularProgressWithLabel from '../components/CircularProgressWithLabel';
import { ParserType } from '../parser/Parser';
// eslint-disable-next-line import/no-cycle
import OverwriteDuplicateSubmissionsDialog from '../dialogs/OverwriteDuplicateSubmissionsDialog';
import ConfirmationDialog from '../dialogs/ConfirmationDialog';
import { save } from '../utils/FileAccess';
import { selectSettingsGeneral } from '../model/SettingsSlice';
import ParserType from '../parser/ParserType';

type ImportModalProps = ModalProps & {
path?: string;
Expand Down
2 changes: 2 additions & 0 deletions app/model/Correction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Note from './Note';
import Annotation from './Annotation';
import Submission from './Submission';
import Rating from './Rating';
import ParserOptions from '../parser/ParserOptions';

type Correction = {
id: string;
Expand All @@ -18,6 +19,7 @@ type Correction = {
annotation?: Annotation;
timeElapsed?: number;
autoCorrectionAttempted?: boolean;
parserOptions?: ParserOptions;
};

export default Correction;
7 changes: 2 additions & 5 deletions app/parser/Parser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import Correction from '../model/Correction';

export enum ParserType {
Uni2Work = 'UNI2WORK',
}
import ParserType from './ParserType';

export default interface Parser {
configFilePattern: RegExp;
deserialize(text: string, dirName: string): Correction;
deserialize(text: string, dirName: string, fileName: string): Correction;
serialize(correction: Correction, tasksAndComments?: string): string;
getConfigFileName(correction: Correction): string;
getType(): ParserType;
Expand Down
9 changes: 9 additions & 0 deletions app/parser/ParserOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ParserType from './ParserType';

type ParserOptions = {
type: ParserType;
language: string;
fileName: string;
};

export default ParserOptions;
4 changes: 4 additions & 0 deletions app/parser/ParserType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum ParserType {
Uni2Work = 'UNI2WORK',
}
export default ParserType;
10 changes: 7 additions & 3 deletions app/parser/Uni2WorkParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,13 @@ test('deserializeSchool Institut für Informatik', () => {
// Correction

test('deserialize Correction u2wTestData1', () => {
expect(parser.deserialize(u2wTestString1, 'uwazxvya2akrnnc2')).toMatchObject(
correctionTestData1
);
expect(
parser.deserialize(
u2wTestString1,
'uwazxvya2akrnnc2',
'bewertung_uwazxvya2akrnnc2.txt'
)
).toMatchObject(correctionTestData1);
});

test('serialize correctionTestData2', () => {
Expand Down
41 changes: 33 additions & 8 deletions app/parser/Uni2WorkParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable class-methods-use-this */
import * as YAML from 'yaml';
import Parser, { ParserType } from './Parser';
import Parser from './Parser';
import UUID from '../utils/UUID';
import Term from '../model/Term';
import Correction from '../model/Correction';
Expand All @@ -10,6 +10,8 @@ import Location from '../model/Location';
import Course from '../model/Course';
import School from '../model/School';
import Rating from '../model/Rating';
import ParserOptions from './ParserOptions';
import ParserType from './ParserType';

export type Uni2WorkDataStructure = {
term: string;
Expand Down Expand Up @@ -39,9 +41,13 @@ export type Uni2WorkDataStructure = {
};

export default class Uni2WorkParser implements Parser {
public configFilePattern = /bewertung_([a-z0-9]{16})\.txt/g;
public configFilePattern = /(bewertung|rating)_([a-z0-9]{16})\.txt/g;

public deserialize(text: string, dirName: string): Correction {
public deserialize(
text: string,
dirName: string,
fileName: string
): Correction {
const [configText, ...rest] = text.split('...');
const u2wDoc = YAML.parseDocument(configText);

Expand Down Expand Up @@ -86,6 +92,11 @@ export default class Uni2WorkParser implements Parser {
corrector: Uni2WorkParser.deserializeCorrector(u2wData.rated_by),
status: Uni2WorkParser.deserializeStatus(u2wData.rating_done),
location: Uni2WorkParser.deserializeLocation(u2wData.rated_at),
parserOptions: {
type: ParserType.Uni2Work,
language: fileName.includes('rating') ? 'en' : 'de',
fileName,
},
};

// Exam Sheet
Expand All @@ -105,7 +116,7 @@ export default class Uni2WorkParser implements Parser {
public static deserializeTerm(term: string): Term {
// More detailed: 1. WiSe|SoSe 2. Century e.g. 20 3. Year start 4. Year end (only WiSe)
// const termPattern = /(wise|sose)\s*(\d{2})(\d{2})(?:\/(\d{2}))?/gi;
const termPattern = /(wise|sose)\s*(\d{4})/i;
const termPattern = /(wise|sose|Winter|Summer)\s*(\d{4})/i;
const summertermPattern = /sose/gi;
const termGroups = term.match(termPattern);
if (termGroups === null) {
Expand All @@ -121,8 +132,16 @@ export default class Uni2WorkParser implements Parser {
};
}

public static serializeTerm(term: Term): string {
return `${term.summerterm ? 'SoSe' : 'WiSe'} ${term.year}${
public static serializeTerm(term: Term, options?: ParserOptions): string {
let summerterm = 'SoSe';
let winterterm = 'WiSe';

if (options?.language === 'en') {
summerterm = 'Summer';
winterterm = 'Winter';
}

return `${term.summerterm ? summerterm : winterterm} ${term.year}${
term.summerterm ? '' : `/${String(term.year + 1).slice(-2)}`
}`;
}
Expand Down Expand Up @@ -165,7 +184,10 @@ export default class Uni2WorkParser implements Parser {

public serialize(correction: Correction, tasksAndComments = ''): string {
const u2wData: Uni2WorkDataStructure = {
term: Uni2WorkParser.serializeTerm(correction.submission.sheet.term),
term: Uni2WorkParser.serializeTerm(
correction.submission.sheet.term,
correction.parserOptions
),
school: correction.submission.sheet.school.name,
course: correction.submission.sheet.course.name,
sheet: {
Expand Down Expand Up @@ -199,7 +221,10 @@ export default class Uni2WorkParser implements Parser {
}

getConfigFileName(correction: Correction): string {
return `bewertung_${correction.submission.name}.txt`;
return (
correction.parserOptions?.fileName ||
`bewertung_${correction.submission.name}.txt`
);
}

getType(): ParserType {
Expand Down

0 comments on commit 79b0971

Please sign in to comment.