Skip to content

Commit

Permalink
file accessors, work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
martinroob committed Jan 27, 2019
1 parent 3ca20c0 commit c375d17
Show file tree
Hide file tree
Showing 44 changed files with 1,047 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ test/work
# Secrets
githubtoken.secret
googleTranslateApiKey.txt
githubApiKey.txt
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ test/work

# Secrets
googleTranslateApiKey.txt
githubApiKey.txt
3 changes: 3 additions & 0 deletions projects/tiny-translator/src/app/app-material.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {NgModule} from '@angular/core';
import {
MatAutocompleteModule,
MatButtonModule, MatCardModule, MatCheckboxModule, MatDialogModule, MatIconModule, MatInputModule, MatListModule,
MatMenuModule, MatOptionModule,
MatProgressSpinnerModule,
Expand All @@ -14,6 +15,7 @@ import {FlexLayoutModule} from '@angular/flex-layout';
@NgModule({
declarations: [],
imports: [
MatAutocompleteModule,
MatButtonModule,
MatCardModule,
MatCheckboxModule,
Expand All @@ -34,6 +36,7 @@ import {FlexLayoutModule} from '@angular/flex-layout';
FlexLayoutModule
],
exports: [
MatAutocompleteModule,
MatButtonModule,
MatCardModule,
MatCheckboxModule,
Expand Down
18 changes: 15 additions & 3 deletions projects/tiny-translator/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
<mat-icon>save</mat-icon>
<span i18n>Save..</span>
</button>
<button mat-menu-item (click)="configureAutoTranslate()"
matTooltip="configure Google Translate API Key" i18n-matTooltip>
<button mat-menu-item [matMenuTriggerFor]="configMenu">
<mat-icon>settings</mat-icon>
<span i18n>Google Translate settings..</span>
<span i18n>Settings..</span>
</button>
<button mat-menu-item (click)="autoTranslate()"
matTooltip="Translate all untranslated units with Google Translate" i18n-matTooltip>
Expand All @@ -47,6 +46,19 @@
<span i18n>Last results..</span>
</button>
</mat-menu>
<!-- config menu -->
<mat-menu #configMenu="matMenu">
<button mat-menu-item (click)="configureAutoTranslate()"
matTooltip="configure Google Translate API Key" i18n-matTooltip>
<mat-icon>g_translate</mat-icon>
<span i18n>Google Translate settings..</span>
</button>
<button mat-menu-item (click)="configureGithub()"
matTooltip="configure Github API access" i18n-matTooltip>
<img class="mat-icon" src="../assets/Octocat.jpg" id="githublogo">
<span i18n>Github access settings..</span>
</button>
</mat-menu>
</mat-toolbar-row>
</mat-toolbar>
<div id="main" fxFlex="1 1 auto" fxLayout="column" fxLayoutAlign="start start">
Expand Down
4 changes: 4 additions & 0 deletions projects/tiny-translator/src/app/app.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ mat-toolbar .mat-toolbar-layout mat-toolbar-row {
background-color: mat-color($dit-app-primary);
height: 20px;
}

#githublogo {
width: 30px;
}
4 changes: 4 additions & 0 deletions projects/tiny-translator/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ export class AppComponent implements OnInit {
this.router.navigateByUrl('autotranslatesummary');
}

configureGithub() {
this.router.navigateByUrl('configuregithub');
}

}
11 changes: 10 additions & 1 deletion projects/tiny-translator/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export class AppConfig {
// if set to true, all autotranslations containing placeholder or tags will FAIL BY DESIGN
// Used to allow testing of report page and filters for failed translations.
GOOGLETRANSLATE_PROVOKE_FAILURES?: boolean;
// URL to Github API (optional)
GITHUB_API_ROOT_URL?: string;
// Your API Key, should not be set here, because it is secret
// can be typed in in the application, but test config needs it
GITHUB_API_KEY?: string;
GITHUB_PROVOKE_FAILURES?: boolean;
BUILDVERSION: string;
BUILDTIME: string;
}
Expand All @@ -25,7 +31,10 @@ export const APP_CONFIG_VALUE: AppConfig = {
'BUILDTIME': '2018-12-02', // TODO should be dynamic
GOOGLETRANSLATE_API_ROOT_URL: 'https://translation.googleapis.com/',
GOOGLETRANSLATE_API_KEY: environment.googletranslate_api_key,
GOOGLETRANSLATE_PROVOKE_FAILURES: environment.googletranslate_provoke_failures
GOOGLETRANSLATE_PROVOKE_FAILURES: environment.googletranslate_provoke_failures,
GITHUB_API_ROOT_URL: 'https://api.github.com/',
GITHUB_API_KEY: environment.github_api_key,
GITHUB_PROVOKE_FAILURES: environment.github_provoke_failures,
};

export let APP_CONFIG = new InjectionToken<AppConfig>('app.config');
6 changes: 6 additions & 0 deletions projects/tiny-translator/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ import { FilterPageComponent } from './filter-page/filter-page.component';
import { UpdateAvailableComponent } from './update-available/update-available.component';
import {TranslationFileUploadComponent} from './file-accessors/download-upload/translation-file-upload/translation-file-upload.component';
import {DownloadUploadService} from './file-accessors/download-upload/download-upload.service';
import {TranslationFileFromGithubComponent} from './file-accessors/github/translation-file-from-github/translation-file-from-github.component';
import {GithubAccessorService} from './file-accessors/github/github-accessor.service';
import {ConfigureGithubPageComponent} from './file-accessors/github/configure-github-page/configure-github-page.component';

@NgModule({
declarations: [
Expand All @@ -56,6 +59,8 @@ import {DownloadUploadService} from './file-accessors/download-upload/download-u
TranslateUnitComponent,
TranslateUnitListComponent,
TranslationFileUploadComponent,
TranslationFileFromGithubComponent,
ConfigureGithubPageComponent,
LanguageComponent,
AbbreviatePipe,
ProjectComponent,
Expand Down Expand Up @@ -89,6 +94,7 @@ import {DownloadUploadService} from './file-accessors/download-upload/download-u
AsynchronousFileReaderService,
DownloaderService,
DownloadUploadService,
GithubAccessorService,
ActiveProjectGuard,
{provide: BackendServiceAPI, useClass: BackendLocalStorageService},
{provide: AutoTranslateServiceAPI, useClass: AutoTranslateGoogleService},
Expand Down
2 changes: 2 additions & 0 deletions projects/tiny-translator/src/app/app.routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {CreateProjectPageComponent} from './create-project-page/create-project-p
import {EditProjectPageComponent} from './edit-project-page/edit-project-page.component';
import {AutoTranslateSummaryPageComponent} from './auto-translate-summary-page/auto-translate-summary-page.component';
import {ConfigureAutoTranslatePageComponent} from './configure-auto-translate-page/configure-auto-translate-page.component';
import {ConfigureGithubPageComponent} from './file-accessors/github/configure-github-page/configure-github-page.component';

/**
* Created by martin on 23.03.2017.
Expand All @@ -22,6 +23,7 @@ const appRoutes: Routes = [
{ path: 'selectfilter', component: FilterPageComponent, canActivate: [ActiveProjectGuard] },
{ path: 'autotranslatesummary', component: AutoTranslateSummaryPageComponent},
{ path: 'configureautotranslate', component: ConfigureAutoTranslatePageComponent},
{ path: 'configuregithub', component: ConfigureGithubPageComponent},
{ path: '', redirectTo: '/translate', pathMatch: 'full' },
];

Expand Down
8 changes: 7 additions & 1 deletion projects/tiny-translator/src/app/file-accessors/Readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# FileAccessors
An abstract mechanism to load and save files from different sources.

Every file accessor has to provide a component to allow input of a file (and optionally a master file for xmb)
and a service to load and save.

TODO configuration component

## Available file accessors
### DownloadUpload
The simplest file accessor.

It can load files via the browser file upload mechanism and store them via browsers file download.


### Github
It can get translation files from a branch in a github repo and commit changes to it.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {Injectable} from '@angular/core';
import {DownloadUploadService} from '../download-upload/download-upload.service';
import {IFileAccessService} from './i-file-access-service';
import {FileAccessorType} from './file-accessor-type';
import {GithubAccessorService} from '../github/github-accessor.service';
import {IFileAccessConfiguration} from './i-file-access-configuration';
import {BackendLocalStorageService} from '../../model/backend-local-storage.service';

/**
* This service returns a suitable service used to load and save a translation file.
*/
@Injectable({
providedIn: 'root'
})
export class FileAccessServiceFactoryService {

constructor(
private downloadUploadService: DownloadUploadService,
private githubAccessorService: GithubAccessorService
) { }

getFileAccessService(filetype: FileAccessorType): IFileAccessService {
switch (filetype) {
case FileAccessorType.DOWNLOAD_UPLOAD:
return this.downloadUploadService;
case FileAccessorType.GITHUB:
return this.githubAccessorService;
default:
throw new Error('Unknown file type ' + filetype);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export enum FileAccessorType {
DOWNLOAD_UPLOAD
DOWNLOAD_UPLOAD = 'Browser Upload and Download',
GITHUB = 'Github'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* A generic configuration of a file access service.
* It contains informations like access tokens, directory names, etc.
* The details depend on the concrete file accessor.
*/
import {FileAccessorType} from './file-accessor-type';

export interface IFileAccessConfiguration {
type: FileAccessorType;
id?: string; // id is auto set when stored
label: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import {Observable} from 'rxjs';
import {IFile} from './i-file';
import {IFileDescription} from './i-file-description';
import {IFileAccessConfiguration} from './i-file-access-configuration';

export interface IFileAccessService {

/**
* Load a file from the backend system.
* @param desription backend dependent description of the file to load.
* @param description backend dependent description of the file to load.
* @return Observable of the loaded file.
*/
load(description: IFileDescription): Observable<IFile>;
Expand All @@ -21,4 +22,17 @@ export interface IFileAccessService {
*/
save(file: IFile): Observable<any>;

/**
* Serialize a configuration to store it.
* @param configuration configuration to be serialized.
* @return serialized form that can be deserialized.
*/
serialize(configuration: IFileAccessConfiguration): string;

/**
* Create a configuration from a stored string (generated by serialize()).
* @param serialzedConfiguration stored configuration string
* @return deserialized version.
*/
deserialize(serialzedConfiguration: string): IFileAccessConfiguration;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* It contains enough information to load and save it.
*/
import {FileAccessorType} from './file-accessor-type';
import {IFileAccessConfiguration} from './i-file-access-configuration';

export interface IFileDescription {
type: FileAccessorType;
configuration: IFileAccessConfiguration;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {IFileAccessConfiguration} from '../common/i-file-access-configuration';
import {FileAccessorType} from '../common/file-accessor-type';

export class DownloadUploadConfiguration implements IFileAccessConfiguration {

static _instance = new DownloadUploadConfiguration();

readonly type = FileAccessorType.DOWNLOAD_UPLOAD;

readonly label = '';

readonly id = '0';

public static singleInstance() {
return this._instance;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {DownloadedFile} from './downloaded-file';
import {AsynchronousFileReaderService} from './asynchronous-file-reader.service';
import {DownloaderService} from './downloader.service';
import {map} from 'rxjs/operators';
import {IFileAccessConfiguration} from '../common/i-file-access-configuration';
import {FileAccessorType} from '../common/file-accessor-type';

@Injectable()
export class DownloadUploadService implements IFileAccessService {
Expand Down Expand Up @@ -33,4 +35,12 @@ export class DownloadUploadService implements IFileAccessService {
// TODO
return of('ok');
}

serialize(configuration: IFileAccessConfiguration): string {
return 'DOWNLOAD_UPLOAD';
}

deserialize(serialzedConfiguration: string): IFileAccessConfiguration {
return {type: FileAccessorType.DOWNLOAD_UPLOAD, label: ''};
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {IFileDescription} from '../common/i-file-description';
import {FileAccessorType} from '../common/file-accessor-type';
import {DownloadUploadConfiguration} from './download-upload-configuration';

export class DownloadedFile implements IFileDescription {

constructor(private _file: File) {}
readonly type = FileAccessorType.DOWNLOAD_UPLOAD;
readonly configuration = DownloadUploadConfiguration.singleInstance();

get type(): FileAccessorType {
return FileAccessorType.DOWNLOAD_UPLOAD;
}
constructor(private _file: File) {}

get browserFile(): File {
return this._file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {TranslationProject} from '../../../model/translation-project';
import {FILETYPE_XTB} from '@ngx-i18nsupport/ngx-i18nsupport-lib';
import {IFileDescription} from '../../common/i-file-description';
import {DownloadedFile} from '../downloaded-file';
import {GithubConfiguration} from '../../github/github-configuration';
import {DownloadUploadConfiguration} from '../download-upload-configuration';

@Component({
selector: 'app-translation-file-upload',
Expand All @@ -13,6 +15,7 @@ import {DownloadedFile} from '../downloaded-file';
export class TranslationFileUploadComponent implements OnInit {

@Input() createdProject?: TranslationProject;
@Input() configuration: DownloadUploadConfiguration;
@Output() fileSelected: EventEmitter<IFileDescription> = new EventEmitter();
@Output() masterXmlFileSelected: EventEmitter<IFileDescription> = new EventEmitter();

Expand Down
Loading

0 comments on commit c375d17

Please sign in to comment.