Skip to content

Commit 9b5a18d

Browse files
committed
Some fixes.
1. Tests fix for non-Windows OSes 2. removed obsolete stuff from setup.py 3. metadata retrieval fixes. 4. Replaced os.path with pathlib 5. Dropped python2
1 parent 1d4197f commit 9b5a18d

File tree

13 files changed

+90
-76
lines changed

13 files changed

+90
-76
lines changed

Coco/CodeGenerator.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
import os.path
2+
from pathlib import Path
33

44
class CodeGenerator( object ):
55
CR = '\r'
@@ -23,12 +23,12 @@ def openFiles( self, frameFileName, sourceFileName, outputFileName, backup=False
2323

2424
self._frameFile = None
2525
for frameName in frameFileName:
26-
fr = os.path.join( self.__class__.sourceDir, frameName )
27-
if not os.path.exists( fr ):
26+
fr = self.__class__.sourceDir / frameName
27+
if not fr.is_file():
2828
if self.__class__.frameDir is not None:
29-
fr = os.path.join( self.__class__.frameDir, frameName )
29+
fr = self.__class__.frameDir / frameName
3030
try:
31-
self._frameFile = open( fr, 'rt', encoding="utf-8")
31+
self._frameFile = fr.open('rt', encoding="utf-8")
3232
break
3333
except IOError:
3434
pass
@@ -37,15 +37,15 @@ def openFiles( self, frameFileName, sourceFileName, outputFileName, backup=False
3737
raise RuntimeError( '-- Compiler Error: Cannot open ' + frameFileName[0] )
3838

3939
try:
40-
fn = os.path.join( self.__class__.outputDir, outputFileName )
41-
fn = str(fn)
42-
if backup and os.path.exists( fn ):
43-
if os.path.exists( fn + '.old' ):
44-
os.remove( fn + '.old' )
45-
os.rename( fn, fn + '.old' )
46-
self._outputFile = open( fn, 'wt', encoding="utf-8")
40+
fn = self.__class__.outputDir / outputFileName
41+
if backup and fn.is_file():
42+
backup = fn.parent / (fn.name + ".old")
43+
if backup.is_file():
44+
os.remove(str(backup))
45+
os.rename(fn, str(backup))
46+
self._outputFile = fn.open('wt', encoding="utf-8")
4747
except:
48-
raise RuntimeError( '-- Compiler Error: Cannot create ' + outputFileName[0] + '.py' )
48+
raise RuntimeError( '-- Compiler Error: Cannot create ' + str(outputFileName[0]) + '.py' )
4949

5050
def close( self ):
5151
self._frameFile.close( )

Coco/Core.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#If not otherwise stated, any source coe generated by Coco/R (other than
2626
#Coco/R itself) does not fall under the GNU General Public License.
2727
#-------------------------------------------------------------------------*/
28-
29-
import os.path
28+
import os
29+
from pathlib import Path
3030
import sys
3131
import copy
3232
from optparse import OptionParser
@@ -2326,24 +2326,25 @@ def FillStartTab(startTab):
23262326
def OpenGen(backUp):
23272327
assert isinstance(backUp,bool)
23282328
try:
2329-
fn = os.path.join(__class__.outDir, "Scanner.py") # String
2330-
if backUp and os.path.exists(fn):
2331-
if os.path.exists(fn + '.old'):
2332-
os.remove( fn + '.old' )
2333-
os.rename( fn, fn + '.old' )
2334-
__class__.gen = open( fn, 'wt', encoding="utf-8")
2329+
fn = __class__.outDir / "Scanner.py" # String
2330+
if backUp and fn.is_file():
2331+
backUpFile = fn.parent / (fn.name + '.old')
2332+
if backUpFile.is_file():
2333+
os.remove(str(backUpFile))
2334+
os.rename( str(fn), str(backUpFile))
2335+
__class__.gen = fn.open('wt', encoding="utf-8")
23352336
except:
23362337
raise RuntimeError("-- Compiler Error: Cannot generate scanner file.")
23372338

23382339
@staticmethod
23392340
def WriteScanner( withNames):
23402341
assert isinstance(withNames,bool)
23412342
startTab = [ 0 for i in range(CharClass.charSetSize) ]
2342-
fr = os.path.join(__class__.srcDir, "Scanner.frame") # String
2343-
if not os.path.exists( fr ):
2343+
fr = __class__.srcDir / "Scanner.frame" # String
2344+
if not fr.is_file():
23442345
if Tab.frameDir is not None:
2345-
fr = os.path.join( Tab.frameDir.strip(), "Scanner.frame" )
2346-
if not os.path.exists(fr):
2346+
fr = Tab.frameDir / "Scanner.frame"
2347+
if not fr.is_file():
23472348
raise RuntimeError("-- Compiler Error: Cannot find Scanner.frame")
23482349
try:
23492350
__class__.fram = open( fr, 'rt', encoding="utf-8")
@@ -2440,10 +2441,10 @@ def WriteScanner( withNames):
24402441
__class__.gen.close()
24412442

24422443
@staticmethod
2443-
def Init ( file:str, srcDir:str, outDir:str):
2444-
assert isinstance(file, str)
2445-
assert isinstance(srcDir, str)
2446-
assert isinstance(outDir, str)
2444+
def Init ( file:str, srcDir:Path, outDir:Path):
2445+
assert isinstance(file, str), repr(str)
2446+
assert isinstance(srcDir, Path), repr(srcDir)
2447+
assert isinstance(outDir, Path), repr(outDir)
24472448
__class__.srcName = file
24482449
__class__.srcDir = srcDir
24492450
__class__.outDir = outDir

Coco/DriverGen.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#Coco/R itself) does not fall under the GNU General Public License.
2727
#-------------------------------------------------------------------------*/
2828
import os
29-
import os.path
29+
from pathlib import Path
3030
from .Errors import Errors
3131
from .Core import Tab
3232
from .CodeGenerator import CodeGenerator
@@ -53,12 +53,13 @@ def WriteDriver( ):
5353
#DriverGen.codeGen.write( Tab.gramSy.name )
5454
DriverGen.codeGen.CopyFramePart( '$$$' )
5555
DriverGen.codeGen.close( )
56-
os.chmod(os.path.join( __class__.outDir, fn ), 0o755)
56+
os.chmod(__class__.outDir / fn, 0o755)
5757

5858
@staticmethod
59-
def Init( f, dir, outDir):
59+
def Init( f: str, dir: Path, outDir: Path):
6060
assert isinstance( f, str )
61-
assert isinstance( dir, str )
61+
assert isinstance( dir, Path )
62+
assert isinstance( outDir, Path )
6263
DriverGen.srcName = f
6364
DriverGen.srcDir = dir
6465
DriverGen.outDir = outDir

Coco/Errors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ def Init( fn, dir, merge, getParsingPos, errorMessages ):
5858
Errors.getParsingPos = getParsingPos
5959
Errors.errorMessages = errorMessages
6060
Errors.fileName = fn
61-
listName = os.path.join(dir, 'listing.txt')
61+
listName = dir / 'listing.txt'
6262
Errors.mergeErrors = merge
6363
if Errors.mergeErrors:
6464
try:
65-
Errors.mergedList = open( listName, 'wt', encoding="utf-8")
65+
Errors.mergedList = listName.open('wt', encoding="utf-8")
6666
except IOError:
67-
raise RuntimeError( '-- Compiler Error: could not open ' + listName )
67+
raise RuntimeError( '-- Compiler Error: could not open ' + str(listName) )
6868

6969
@staticmethod
7070
def storeError( line, col, s ):

Coco/ParserGen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#-------------------------------------------------------------------------*/
2828
import copy
2929
import os
30-
import os.path
30+
from pathlib import Path
3131
import io
3232

3333
from .Errors import Errors
@@ -301,9 +301,9 @@ def InitSets( ):
301301
__class__.codeGen.write( 'x],\n' )
302302

303303
@staticmethod
304-
def Init( fn, dir ):
304+
def Init( fn, dir: Path ):
305305
assert isinstance( fn, str )
306-
assert isinstance( dir, str )
306+
assert isinstance( dir, Path )
307307
__class__.srcName = fn
308308
__class__.srcDir = dir
309309
__class__.errorNr = -1

Coco/Trace.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@
2626
#Coco/R itself) does not fall under the GNU General Public License.
2727
#-------------------------------------------------------------------------*/
2828

29-
import os.path
29+
import os
30+
from pathlib import Path
3031

3132
class Trace( object ):
3233
fileName = ''
3334
trace = None
3435

3536
@staticmethod
3637
def Init( dir ):
37-
assert isinstance( dir, str )
38-
Trace.fileName = os.path.join( dir, 'trace.txt' )
38+
assert isinstance( dir, Path )
39+
Trace.fileName = dir / 'trace.txt'
3940
try:
40-
Trace.trace = open(Trace.fileName, 'wt', encoding="utf-8")
41+
Trace.trace = Trace.fileName.open('wt', encoding="utf-8")
4142
except IOError:
4243
raise RuntimeError( '-- Compiler Error: could not open ' + Trace.fileName )
4344

@@ -75,9 +76,9 @@ def WriteLine( s:str=None, w:int=None ):
7576
@staticmethod
7677
def Close( ):
7778
Trace.trace.close( )
78-
stat = os.stat( Trace.fileName )
79+
stat = os.stat( str(Trace.fileName) )
7980
if stat.st_size == 0:
8081
os.remove( Trace.fileName )
8182
else:
8283
print()
83-
print('trace output is in', os.path.basename(Trace.fileName))
84+
print('trace output is in', Trace.fileName.parent)

Coco/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Coco/__main__.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
# -------------------------------------------------------------------------*/
4848
import sys
4949
import os
50-
import os.path
50+
from pathlib import Path
5151

5252
from .Scanner import Scanner
5353
from .Errors import Errors
@@ -62,16 +62,21 @@
6262
from .setupInfo import MetaData
6363

6464

65-
ROOT_DIR = os.path.dirname( __file__ )
65+
ROOT_DIR = Path( __file__ ).absolute().parent
6666

6767
from .CLI import CocoArgs
6868
import plumbum.cli
6969

7070
class CocoCli(CocoArgs):
71-
DESCRIPTION = 'Coco/R v%s for Python (May 16, 2007) - Translated by %s (%s)\n' % ( MetaData[ 'version' ], MetaData[ 'author' ], MetaData[ 'author_email' ] )
71+
if 'version' in MetaData and 'author' in MetaData and 'author_email' in MetaData:
72+
DESCRIPTION = 'Coco/R v%s for Python (May 16, 2007) - Translated by %s (%s)\n' % ( MetaData[ 'version' ], MetaData[ 'author' ], MetaData[ 'author_email' ] )
73+
else:
74+
DESCRIPTION = 'Coco/R v??? for Python (May 16, 2007) - Translated by ??? (??)\nWE CANNOT RETRIEVE THE METADATA correctly, SOMETHING GONE WRONG, FIX IT: ' + repr(MetaData)
7275
def main(self, ATGName:plumbum.cli.ExistingFile):
7376
Tab.SetDDT( self )
74-
dirName, fileName = os.path.split(ATGName)
77+
ATGName = Path(ATGName)
78+
dirName = ATGName.parent
79+
fileName = ATGName.name
7580

7681
if not self.outputDir:
7782
self.outputDir=dirName
@@ -81,19 +86,19 @@ def main(self, ATGName:plumbum.cli.ExistingFile):
8186
if self.frameFileDir:
8287
framesDir = self.frameFileDir
8388
else:
84-
framesDir = os.path.join( ROOT_DIR, 'frames' )
89+
framesDir = ROOT_DIR / 'frames'
8590
CodeGenerator.frameDir = framesDir
8691
Tab.frameDir = framesDir
8792
except:
8893
pass
8994

9095
# Initialize the Scanner
9196
try:
92-
with open(ATGName, 'rt', encoding="utf-8") as s:
97+
with ATGName.open('rt', encoding="utf-8") as s:
9398
try:
9499
strVal = s.read( )
95100
except IOError:
96-
sys.stdout.write( '-- Compiler Error: Failed to read from source file "%s"\n' % ATGName )
101+
raise RuntimeError( '-- Compiler Error: Failed to read from source file "%s"\n' % ATGName )
97102
except IOError:
98103
raise RuntimeError( '-- Compiler Error: Cannot open file "%s"' % ATGName )
99104

Coco/frames/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Coco/setupInfo.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
from setuptools.config import read_configuration
22
import os
3+
from pathlib import Path
34

4-
curDir=os.path.dirname(__file__)
5-
setupCfgPath=os.path.join(curDir, "..", "setup.cfg")
6-
if os.path.isfile(setupCfgPath):
5+
curDir = Path(__file__).absolute().parent
6+
setupCfgPath = curDir / ".." / "setup.cfg"
7+
if setupCfgPath.is_file():
78
MetaData = read_configuration(setupCfgPath)["metadata"]
89
else:
9-
from pkg_resources import get_distribution
10-
MetaData = get_distribution('Coco').__dict__
10+
try:
11+
from importlib import metadata
12+
MetaData = metadata.metadata('CocoPy')
13+
except ImportError:
14+
from pkg_resources import get_distribution, DistributionNotFound
15+
try:
16+
d = get_distribution('CocoPy')
17+
except DistributionNotFound:
18+
d = get_distribution('Coco')
19+
MetaData = d.__dict__
1120

1221
VersionInfo = {
1322
'1.1.0rc': {

0 commit comments

Comments
 (0)