forked from OneBusAway/onebusaway-gtfs-modules
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from mbta/es-merge-latest-upstream
Merge latest upstream changes
- Loading branch information
Showing
23 changed files
with
440 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...gtfs-transformer/src/main/java/org/onebusaway/gtfs_transformer/impl/AddExtensionFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (C) 2023 Cambridge Systematics, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.onebusaway.gtfs_transformer.impl; | ||
|
||
import org.onebusaway.csv_entities.schema.annotations.CsvField; | ||
import org.onebusaway.gtfs.services.GtfsMutableRelationalDao; | ||
import org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy; | ||
import org.onebusaway.gtfs_transformer.services.TransformContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
/** | ||
* Insert a GTFS extension into a GTFS file via a transformation. | ||
* See the unit test for example usage. | ||
*/ | ||
|
||
public class AddExtensionFile implements GtfsTransformStrategy { | ||
private static Logger _log = LoggerFactory.getLogger(AddExtensionFile.class); | ||
|
||
|
||
@CsvField(optional = false) | ||
private String extensionFilename; | ||
@CsvField(optional = false) | ||
private String extensionName; | ||
@Override | ||
public String getName() { | ||
return this.getClass().getName(); | ||
} | ||
|
||
public void setExtensionFilename(String extensionFilename) { | ||
this.extensionFilename = extensionFilename; | ||
} | ||
public void setExtensionName(String extensionName) { | ||
this.extensionName = extensionName; | ||
} | ||
|
||
@Override | ||
public void run(TransformContext context, GtfsMutableRelationalDao dao) { | ||
// lookup the file | ||
if (extensionFilename == null) | ||
throw new IllegalStateException("missing required param extensionFilename"); | ||
if (extensionName == null) | ||
throw new IllegalStateException("missing required param extensionName"); | ||
_log.info("AddExtensionFile entered with {} to {}", extensionName, extensionFilename); | ||
File extension = new File(extensionFilename); | ||
if (!extension.exists()) { | ||
throw new IllegalStateException("attempt to add non-existant extension file:" + extension.getName()); | ||
} | ||
String content = null; | ||
try { | ||
content = Files.readString(extension.toPath()); | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
if (content == null) | ||
throw new IllegalStateException("no content for specified file " + extensionFilename); | ||
|
||
_log.info("AddExtensionFile copying {} to {}", extensionName, extensionFilename); | ||
dao.addMetadata(extensionName, content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...-transformer/src/test/java/org/onebusaway/gtfs_transformer/impl/AddExtensionFileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright (C) 2023 Cambridge Systematics, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.onebusaway.gtfs_transformer.impl; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.onebusaway.gtfs.impl.FileSupport; | ||
import org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl; | ||
import org.onebusaway.gtfs.serialization.GtfsWriter; | ||
import org.onebusaway.gtfs.services.GtfsRelationalDao; | ||
import org.onebusaway.gtfs_transformer.AbstractTestSupport; | ||
import org.onebusaway.gtfs_transformer.services.TransformContext; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* test we can insert an extension into a GTFS file via a transformation. | ||
*/ | ||
public class AddExtensionFileTest extends AbstractTestSupport { | ||
|
||
private static final String TXT_STRING = "route_id,stop_id,direction_id,name\n"; | ||
private FileSupport _support = new FileSupport(); | ||
private GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl(); | ||
private AddExtensionFile test = new AddExtensionFile(); | ||
private TransformContext context = new TransformContext(); | ||
private GtfsWriter writer = new GtfsWriter(); | ||
|
||
@Before | ||
public void setup() { | ||
_gtfs.putAgencies(1); | ||
_gtfs.putStops(1); | ||
_gtfs.putRoutes(1); | ||
_gtfs.putTrips(1, "r0", "sid0"); | ||
_gtfs.putStopTimes("t0", "s0"); | ||
|
||
} | ||
@After | ||
public void teardown() { | ||
_support.cleanup(); | ||
} | ||
|
||
@Test | ||
public void run() throws IOException { | ||
File extensionFile = File.createTempFile("subwayRouteStop-", ".csv"); | ||
if (extensionFile.exists()) | ||
extensionFile.deleteOnExit(); | ||
|
||
String extensionFilename = extensionFile.getAbsolutePath();; | ||
BufferedWriter bwriter = new BufferedWriter(new FileWriter(extensionFilename)); | ||
bwriter.write(TXT_STRING); | ||
bwriter.close(); | ||
|
||
String extensionName = "route_stop.txt"; | ||
test.setExtensionFilename(extensionFilename); | ||
test.setExtensionName(extensionName); | ||
|
||
test.run(context, dao); | ||
|
||
File tmpFileDirectory = File.createTempFile("AddExtensionFileTest-", "-tmp"); | ||
if (tmpFileDirectory.exists()) | ||
_support.deleteFileRecursively(tmpFileDirectory); | ||
tmpFileDirectory.mkdirs(); | ||
_support.markForDeletion(tmpFileDirectory); | ||
writer.setOutputLocation(tmpFileDirectory); | ||
writer.run(dao); | ||
writer.close(); | ||
|
||
String modLocation = tmpFileDirectory.getAbsolutePath() + File.separator + extensionName; | ||
File expectedFile = new File(modLocation); | ||
// verify file is there | ||
assertTrue("expected extension to be present!", expectedFile.exists()); | ||
assertTrue("expected extension to be a file!", expectedFile.isFile()); | ||
String actualText = Files.readString(Path.of(modLocation)); | ||
assertEquals(TXT_STRING, actualText); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.