Skip to content

Commit

Permalink
Re-factoring redundant output path creation code (#62)
Browse files Browse the repository at this point in the history
* re-factoring

* Update dependency-review.yml
  • Loading branch information
Mark Servidio authored Nov 21, 2022
1 parent 530e271 commit 0c183ea
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Source repository: https://github.com/actions/dependency-review-action
# Public documentation: https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/about-dependency-review#dependency-review-enforcement
name: 'Dependency Review'
on: [push, pull_request]
on: [pull_request]

permissions:
contents: read
Expand Down
15 changes: 15 additions & 0 deletions transcoder/output/OutputManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#

import concurrent
import os
import sys
from concurrent import futures
from concurrent.futures import ThreadPoolExecutor

Expand Down Expand Up @@ -95,3 +97,16 @@ def wait_for_schema_creation(self):
def wait_for_completion(self):
"""Extend or override to wait until output manager has fully completed writing and other work"""
self.wait_for_schema_creation()

def create_output_path(self, output_path: str, relative_path: str):
"""Creates the output path if it doesn't exist. Output path will be created as {output_path}/{relative_path}"""
_output_path = None
if output_path is None:
main_script_dir = os.path.dirname(sys.argv[0])
_output_path = os.path.join(main_script_dir, relative_path)
else:
_output_path = output_path
exists = os.path.exists(_output_path)
if not exists:
os.makedirs(_output_path)
return _output_path
16 changes: 2 additions & 14 deletions transcoder/output/avro/BaseAvroOutputManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#

import json
import os
import sys

from transcoder.message import DatacastField, DatacastSchema
from transcoder.output import OutputManager
Expand All @@ -34,23 +32,13 @@ def __init__(self, prefix: str, output_path: str, lazy_create_resources: bool =
self.prefix = prefix
self.schemas = {}
self.writers = {}

# pylint: disable=duplicate-code
if output_path is None:
rel_path = "avroOut"
main_script_dir = os.path.dirname(sys.argv[0])
self.output_path = os.path.join(main_script_dir, rel_path)
else:
self.output_path = output_path

exists = os.path.exists(self.output_path)
if not exists:
os.makedirs(self.output_path)
self.output_path = self.create_output_path(output_path, 'avroOut')

def _create_field(self, field: DatacastField):
return field.create_avro_field()

def _add_schema(self, schema: DatacastSchema):
# pylint: disable=duplicate-code
_fields = self._get_field_list(schema.fields)
if schema.name in self.schemas:
del self.schemas[schema.name]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
# limitations under the License.
#

import os
import sys

from transcoder.output import OutputManager


Expand All @@ -36,18 +33,7 @@ def __init__(self, prefix: str, output_path: str):
self.prefix = prefix
self.schemas = {}
self.writers = {}

# pylint: disable=duplicate-code
if output_path is None:
rel_path = "tfOut"
main_script_dir = os.path.dirname(sys.argv[0])
self.output_path = os.path.join(main_script_dir, rel_path)
else:
self.output_path = output_path

exists = os.path.exists(self.output_path)
if not exists:
os.makedirs(self.output_path)
self.output_path = self.create_output_path(output_path, 'tfOut')

def _save_schema(self, name, content):
with open(self._get_file_name(name, 'tf'), mode='wt', encoding='utf-8') as file:
Expand Down
15 changes: 2 additions & 13 deletions transcoder/output/json/JsonOutputManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#
import datetime
import json
import os
import sys

from transcoder.message import DatacastSchema, DatacastField
from transcoder.output import OutputManager
Expand All @@ -33,17 +31,7 @@ def __init__(self, prefix: str, output_path: str, lazy_create_resources: bool =
self.prefix = prefix
self.schemas = {}
self.writers = {}

if output_path is None:
rel_path = "jsonOut"
main_script_dir = os.path.dirname(sys.argv[0])
self.output_path = os.path.join(main_script_dir, rel_path)
else:
self.output_path = output_path

exists = os.path.exists(self.output_path)
if not exists:
os.makedirs(self.output_path)
self.output_path = self.create_output_path(output_path, 'jsonOut')

@staticmethod
def output_type_identifier():
Expand All @@ -53,6 +41,7 @@ def _create_field(self, field: DatacastField):
return field.create_json_field(field)

def _add_schema(self, schema: DatacastSchema):
# pylint: disable=duplicate-code
if schema.name in self.schemas:
del self.schemas[schema.name]
if schema.name in self.writers:
Expand Down

0 comments on commit 0c183ea

Please sign in to comment.