Commit 10b03f63 authored by Stefan Behnel's avatar Stefan Behnel

remove an unnecessary dependency on "glob" from Code.py, minor cleanups

parent bdcf0f26
...@@ -11,7 +11,6 @@ cython.declare(re=object, Naming=object, Options=object, StringEncoding=object, ...@@ -11,7 +11,6 @@ cython.declare(re=object, Naming=object, Options=object, StringEncoding=object,
import os import os
import re import re
import glob
import Naming import Naming
import Options import Options
import StringEncoding import StringEncoding
...@@ -52,7 +51,7 @@ class UtilityCodeBase(object): ...@@ -52,7 +51,7 @@ class UtilityCodeBase(object):
_utility_cache = {} _utility_cache = {}
# @classmethod @classmethod
def _add_utility(cls, utility, type, lines, begin_lineno): def _add_utility(cls, utility, type, lines, begin_lineno):
if utility: if utility:
# Remember line numbers as least until after templating # Remember line numbers as least until after templating
...@@ -63,9 +62,7 @@ class UtilityCodeBase(object): ...@@ -63,9 +62,7 @@ class UtilityCodeBase(object):
else: else:
utility[1] = code utility[1] = code
_add_utility = classmethod(_add_utility) @classmethod
# @classmethod
def load_utilities_from_file(cls, path): def load_utilities_from_file(cls, path):
utilities = cls._utility_cache.get(path) utilities = cls._utility_cache.get(path)
if utilities: if utilities:
...@@ -123,9 +120,7 @@ class UtilityCodeBase(object): ...@@ -123,9 +120,7 @@ class UtilityCodeBase(object):
cls._utility_cache[path] = utilities cls._utility_cache[path] = utilities
return utilities return utilities
load_utilities_from_file = classmethod(load_utilities_from_file) @classmethod
# @classmethod
def load(cls, util_code_name, from_file=None, context=None, **kwargs): def load(cls, util_code_name, from_file=None, context=None, **kwargs):
""" """
Load a utility code from a file specified by from_file (relative to Load a utility code from a file specified by from_file (relative to
...@@ -165,20 +160,22 @@ class UtilityCodeBase(object): ...@@ -165,20 +160,22 @@ class UtilityCodeBase(object):
return cls(**kwargs) return cls(**kwargs)
load = classmethod(load) @classmethod
# @classmethod
def load_as_string(cls, util_code_name, from_file=None, context=None): def load_as_string(cls, util_code_name, from_file=None, context=None):
""" """
Load a utility code as a string. Returns (proto, implementation) Load a utility code as a string. Returns (proto, implementation)
""" """
if from_file is None: if from_file is None:
files = glob.glob(os.path.join(get_utility_dir(), utility_dir = get_utility_dir()
util_code_name + '.*')) prefix = util_code_name + '.'
if len(files) != 1: files = [ os.path.join(utility_dir, filename)
raise ValueError("Need exactly one utility file") for filename in os.listdir(utility_dir)
if filename.startswith(prefix) ]
from_file, = files if not files:
raise ValueError("No match found for utility code " + util_code_name)
if len(files) > 1:
raise ValueError("More than one filename match found for utility code " + util_code_name)
from_file = files[0]
utilities = cls.load_utilities_from_file(from_file) utilities = cls.load_utilities_from_file(from_file)
...@@ -193,8 +190,6 @@ class UtilityCodeBase(object): ...@@ -193,8 +190,6 @@ class UtilityCodeBase(object):
return proto and proto.lstrip(), impl and impl.lstrip() return proto and proto.lstrip(), impl and impl.lstrip()
load_as_string = classmethod(load_as_string)
def none_or_sub(self, s, context, tempita): def none_or_sub(self, s, context, tempita):
""" """
Format a string in this utility code with context. If None, do nothing. Format a string in this utility code with context. If None, do nothing.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment